ISSUE DESCRIPTION Cannot complete macro path in macro <!-- Troubleshooting guide:…
- User
ISSUE DESCRIPTION
Cannot complete macro path in macro
rust-analyzer version: rust-analyzer 0.4.2897-standalone
rustc version: rustc 1.97.0-nightly (36ba2c771 2026-04-23)
editor or extension: Vim
code snippet to reproduce:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /rust-analyzer, current file: none) bash-$
- Assistant
I'll start by exploring the repository to understand the structure and locate relevant files for this issue. Let me first get an overview.
- Assistant
Let me explore the repository structure more systematically.
- Assistant
Let me understand the issue. The problem is that rust-analyzer cannot complete macro paths when inside a macro invocation. Specifically, when you have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0afterbar, rust-analyzer should provide completion suggestions forbaras a macro path, but it doesn't.Let me explore the repository to understand how completions work, particularly for macro paths.
- Assistant
Let me look at the issue more carefully. The issue is about completing macro paths inside a macro call. When the cursor is at
foo!(bar$0), the user expects to get completion forbar- i.e., completion should suggestbaras a macro name.Let me explore the completion crate to understand how completions work.
- Assistant
Let me explore the completions directory to understand the structure.
- Assistant
Let me first understand the issue better by looking at the context and completions related to macro paths.
- Assistant
Let me understand the issue better. The problem is about completing macro paths inside macro calls. When we have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0, the user expects to get completion suggestions forbaras a macro name. But rust-analyzer can't complete the macro path inside a macro call.This seems to be a completion issue where the completion context doesn't recognize that we're inside a macro call and need to suggest macro names.
Let me look at the completion context analysis to understand how completions work in macro contexts.
- Assistant
Let me explore the completion context analysis to understand how macro completions are handled. I need to understand how the completion system handles macro calls and what context it's in when the cursor is inside a macro call.
Let me look at the context analysis module.
- Assistant
Let me search for the completion tests to understand how macro completion is tested.
- Assistant
Let me understand how completion works in macro call arguments. Let me look at the path completion logic.
- Assistant
Let me look at how completion works in macro call arguments. Let me look at the path completion logic.
- Assistant
Let me look at the specific test for completing inside macro calls.
- Assistant
Let me search for relevant completion code related to path completion.
- Assistant
Let me look at the context/analysis.rs more carefully to understand how macro expansion works in the completion context.
- Assistant
Let me first create a test to reproduce the issue.
- Assistant
Let me look at how the completion context handles macro calls, specifically the part that deals with speculative expansion.
- Assistant
Let me look at the section around line 380 to understand how macro calls are handled.
- Assistant
Let me understand the completion flow better. Let me look at how the expansion is handled and what
expand_maybe_stopdoes. - Assistant
Let me look at the
expand_maybe_stopfunction to understand how it decides when to stop expanding. - Assistant
Let me look at the
expandfunction to understand the flow better. - Assistant
Let me first try to reproduce the issue by writing a test. Let me look at the existing test patterns.
- Assistant
Let me understand the issue better. The problem is that when you have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0afterbar, the user expects to get completion suggestions forbaras a macro name. But rust-analyzer can't complete the macro path inside a macro call.This seems to be about completing identifiers inside macro arguments where the fragment specifier is
$i:ident. When you typebarinsidefoo!(bar$0), the macro expects an identifier, andbaris a valid identifier that's also a macro.Let me look at how completions work in macro arguments. The issue might be that the completion system doesn't recognize that we're inside a macro call's token tree, or that it doesn't know that
baris a valid identifier in this context.Let me search for how completions handle macro call arguments.
- Assistant
Let me look at the comment in the analysis file about completing in macros.
- Assistant
Let me trace through the logic. When we're completing in
foo!(bar$0), the expansion process needs to:- Insert the completion marker after
barmaking itbar raCompletionMarker - Find the macro call
foo! - Speculatively expand it
- Look for the completion marker in the expansion
- Figure out what context we're in at that point
The issue might be that
foo!($i:ident)requires an identifier, and when we append the completion marker, the identifier becomesbar raCompletionMarkerwhich doesn't match$i:ident, causing the speculative expansion to fail.Let me look at the code more carefully.
- Insert the completion marker after
- Assistant
Let me look at how the speculative file is created - how the completion marker is inserted.
- Assistant
Let me look at the lib.rs to see how completion is invoked and how the speculative file is created.
- Assistant
Let me look at how completion works at the high level to understand where the speculative file is created.
- Assistant
Let me look at the lib.rs file to understand how completions work.
- Assistant
Let me look at the lib.rs file to understand the completion flow.
- Assistant
Let me understand the issue better. When the cursor is at
foo!(bar$0), we're inside a macro call. The macrofootakes$i:ident. The issue is that completion should suggestbaras a macro name.Let me first write a test to reproduce the issue, then look at how the expansion handles this.
- Assistant
Let me check if there's a specific cargo or toolchain configuration.
- Assistant
Let me check if there's a rust-toolchain file or if cargo is available somewhere.
- Assistant
Let me check if there's a way to run cargo.
- Assistant
Good, I can run tests. Now let me write a test to reproduce the exact issue. Let me first check the test patterns.
- Assistant
Now let me create a temporary test script to reproduce the issue:
- Assistant
Let me look at the test helper functions to understand how to write a proper test.
- Assistant
Let me look at the check function to understand how it works.
- Assistant
Let me look at the existing test for completing inside a macro call more carefully, particularly the
completes_in_simple_macro_calltest: - Assistant
Let me look at the
analyzefunction to understand how the completion context is determined. - Assistant
Let me look at the expand function more carefully:
- Assistant
Let me look at the
ancestors_at_offsetfunction: - Assistant
Let me find the
ancestors_at_offsetfunction: - Assistant
Let me look at the syntax algo function.
- Assistant
Let me trace through the whole expansion flow. Let me write a small reproduction script to understand the issue:
- Assistant
Let me understand the issue by looking at
is_inside_macro_call: - Assistant
Let me look at that function.
- Assistant
Let me now understand the full flow better. Let me look at the expand function more carefully to see how it handles macro expansion step by step:
- Assistant
Now let me trace through what happens when completing in
foo!(bar$0). Let me look at the full expansion logic: - Assistant
Let me look at
is_inside_macro_callmore carefully: - Assistant
Let me look at how
speculative_expand_macro_callworks: - Assistant
Let me look at that function.
- Assistant
Now I understand the issue. Let me trace through the completion flow:
- When completing
foo!(bar$0), the code inserts a completion marker, making itfoo!(barCompletionMarker) - The macro
foois expanded speculatively withbarCompletionMarkeras the$i:identparameter - This produces
barCompletionMarker!()in the expansion - Then the code tries to further expand
barCompletionMarker!() - The macro call paths are compared:
bar(from actualbar!()) vsbarCompletionMarker(from speculative) - They don't match, so the expansion fails
- Since we're "inside a macro call",
expand_maybe_stopreturnsNone - The
unwrap_orfallback provides the base result, but thenanalyzecan't find aNameLikenode in the token tree
Let me verify this by looking at how the non-expansion path works:
- When completing
- Assistant
Now I understand the issue. Let me trace through what happens:
foo!(bar$0)becomesfoo!(barCompletionMarker)with the fake identfoo!($i:ident) => { $i!() }expands speculatively tobarCompletionMarker!()- Now the system tries to further expand
barCompletionMarker!() - The actual
bar!()and speculativebarCompletionMarker!()have different paths, soexpandreturnsNone - In
expand_maybe_stop,expandreturnsNone, andis_inside_macro_callfor the original token istrue(sincebaris insidebar!()which is itself insidefoo!()), so it returnsNone - The same happens recursively - ultimately the unwrap_or fallback kicks in
- But the fallback analyzes the original non-expanded file, where
baris inside a TOKEN_TREE with no NameLike node, soanalyzereturnsNone
The key issue is that after expanding
foo!(barCompletionMarker)to , the checks whether the original token is inside a macro call. The original token (from ) IS inside a macro call ( itself), so it returns , preventing us from using this as a valid completion point. - Assistant
Let me look at the full
is_inside_macro_callimplementation: - Assistant
Now I need to understand the full picture. Let me write a test to reproduce the issue first:
- Assistant
Let me understand the issue better by looking at what the expanded file looks like. The key question is what happens when
foo!(barCompletionMarker)expands tobarCompletionMarker!(). - Assistant
Let me now understand the exact failure path. When
foo!(barCompletionMarker)expands tobarCompletionMarker!(), then the code tries to further expand the result. Let me trace through the code: - Assistant
Now I understand the core issue. Let me trace through the problem step by step:
foo!(bar$0)→ fake ident inserted →foo!(barCompletionMarker)- Macro
foo!($i:ident) => { $i!() }expands speculatively tobarCompletionMarker!() - The
barCompletionMarkertoken is now the path of a new macro call expand_maybe_stoprecursively tries to expandbarCompletionMarker!(), butexpand()returnsNonebecause the token is a macro path (not inside a token tree)- Then
is_inside_macro_callreturnstruebecausebaris the path ofbar!(), which is aMacroCallnode - So
expand_maybe_stopreturnsNoneinstead of using the current result as the base case
The issue is: when the token ends up as the PATH of a macro call after expansion (not inside its arguments),
expand_maybe_stopconsiders it "inside a macro call" and returnsNone. But completing a macro path IS a valid completion position.Let me look at how the expansion result propagates:
- Assistant
Now I understand the problem. Let me trace through the full flow:
foo!(bar$0)→ fake ident →foo!(barCompletionMarker)foo!($i:ident) => { $i!() }expands speculatively tobarCompletionMarker!()- Now
barCompletionMarkeris the PATH of a macro callbarCompletionMarker!() expand_maybe_stoprecursively tries to expandbarCompletionMarker!()- In
expand(), the token is the path of the macro call (not inside a token tree), soorig_ttreturnsNone(no TokenTree/Meta ancestors found) expand()returnsNone- In
expand_maybe_stop,is_inside_macro_callreturnstruebecausebaris inside theMacroCallnodebar!() - So
expand_maybe_stopreturnsNone - The same happens at every level, and ultimately
expand_and_analyzeuses the non-expanded fallback - In the non-expanded file,
baris inside a of , where no node exists
- Assistant
Let me look at the full context again, particularly how the code detects we're at a macro path position:
- Assistant
Let me trace through more carefully to understand the exact flow. Let me look at the
expandfunction when it encounters the expanded macro: - Assistant
Now I understand the full picture. The issue is:
foo!(bar)expands tobar!()(actual)foo!(barCompletionMarker)expands tobarCompletionMarker!()(speculative)- Then
expand_maybe_stoprecursively tries to expandbarCompletionMarker!() - In
expand, it finds the macro call pathsbarandbarCompletionMarkerwhich don't match → returnsNone - Back in
expand_maybe_stop,is_inside_macro_callreturnstrue→ returnsNone - This cascades back and eventually falls through to the non-expanded file
- In the non-expanded file, the token is inside a TOKEN_TREE with no NameLike node → no completions
The fix: when the speculative expansion of a macro call changes the path (because the completion marker is part of the identifier), the
expand_maybe_stopshould still return the expansion result, treating it as a valid completion point.Let me look at the exact location where
expandreturnsNonedue to path mismatch: - Assistant
Now I understand the problem completely. Let me trace through the full chain:
foo!(bar$0)→ fake ident →foo!(barCompletionMarker)expand()findsfoo!macro, expands:- Actual:
foo!(bar)→bar!() - Speculative:
foo!(barCompletionMarker)→barCompletionMarker!()
- Actual:
expand_maybe_stop()recursively called withbar!()(actual) andbarCompletionMarker!()(speculative)- In recursive
expand(), it tries to find TokenTree ancestors at the offset. Butbaris in the path ofbar!(), not inside a TokenTree. Soorig_ttisNone, andexpand()returnsNone. expand_maybe_stop()then checksis_inside_macro_call(). The tokenbaris inside theMacroCallnode ofbar!(), so it returnstrue. Result:None.- This cascades up, and eventually the fallback is used with the non-expanded files.
- Assistant
Now I have a clear understanding. Let me implement the fix. The issue is in
expand_maybe_stop- when the completion marker ends up as the path of a macro call after expansion, we should still return the expansion result rather thanNone. - Assistant
Now let me implement the fix. The key change should be in
expand_maybe_stop: - Assistant
Let me understand the exact flow more clearly. The issue is that when
foo!(barCompletionMarker)expands tobarCompletionMarker!(), the completion marker becomes the path of a macro call. In the recursiveexpand_maybe_stop, whenexpandreturnsNoneand the token IS inside a macro call (as the path), it should still return the result.Let me implement the fix:
- Assistant
Now let me implement the fix. The key insight is that in
expand_maybe_stop, when the token is the PATH of a macro call (likebarinbar!()) rather than being inside its arguments, we should still allow completion. - Assistant
Let me look at the
is_inside_macro_callto understand its semantics better, and then figure out the right fix: - Assistant
Now I understand the issue clearly. The
is_inside_macro_callfunction returnstruefor tokens that are the PATH of a macro call (likebarinbar!()), not just for tokens inside the macro's arguments. This is overly conservative for completion purposes.The fix should be in
expand_maybe_stop: whenexpandreturnsNoneand the token is at the PATH of a macro call (not inside its token tree/arguments), we should still return the result as a valid completion point.Let me implement the fix:
- Assistant
Now I understand the full picture. Let me implement the fix. The issue is that when a macro expansion produces a new macro call with the completion marker as the path (e.g.,
barCompletionMarker!()), the recursiveexpand_maybe_stopreturnsNonebecause:expand()can't find TokenTree/Meta at the macro path positionis_inside_macro_callreturnstruefor the path ofbar!()
The fix: when the speculative file has the fake ident token as the PATH of a macro call (not inside its arguments), we should treat this as a valid completion point.
- Assistant
Let me think about this more carefully. The fix needs to handle the case where the speculative file has the completion marker at the PATH of a macro call. Let me check what the AST looks like for
barCompletionMarker!(): - Assistant
Let me look at how
fake_ident_tokenrelates to the macro call's path vs its token tree: - Assistant
Let me look at the MacroCall AST structure to understand the path vs token tree distinction.
- Assistant
Let me look at the MacroCall structure to understand how path and token_tree are laid out:
- Assistant
Now I understand the full picture. Let me implement the fix. The key insight is:
When
expand()returnsNoneand the original token IS inside a macro call, we need to check if the token is the path of the macro call (likebarinbar!()) vs being inside the arguments (token tree). If it's the path, that's a valid completion point. - Assistant
Let me implement the fix now. The key insight is: when the speculative file has the
fake_ident_tokenat the PATH of a macro call (not inside its token tree arguments), this is a valid completion point for macro path completion. - Assistant
Now I need to add the
is_at_macro_call_pathhelper function. Let me add it: - Assistant
Now I need to add the
is_at_macro_call_pathfunction. Let me add it right beforeexpand_maybe_stop: - Assistant
Let me use a different approach:
- Assistant
Now let me verify the changes look correct:
- Assistant
Now let me check that the fix compiles. Let me also verify the logic is correct by checking what
classify_name_refdoes with a NameRef that's the path of a macro call: - Assistant
Let me first check if the code compiles:
- Assistant
Good, the code compiles. Now let me run the existing tests to make sure the change doesn't break anything:
- Assistant
All 732 tests pass. Now let me write a test to verify the fix works for the issue:
- Assistant
Let me add a test for this fix. First let me find where to add it:
- Assistant
Now let me write a temporary test script to verify the fix:
- Assistant
Let me add the test to the test file to verify it works:
- Assistant
Let me add the test to the special.rs file temporarily and run it:
- Assistant
Now let me run the test:
- Assistant
The test shows progress! We're getting completions now, but the output format is different from what I expected. Let me update the expected output:
- Assistant
Let me first check what completions are actually produced:
- Assistant
The diff output is hard to parse. Let me use
check_no_kwand see what the actual output is: - Assistant
The test shows we're getting completions now! The key part is
ma bar macro_rules! barandma foo macro_rules! foo. Let me run the test withcheck_no_kwto get the exact output: - Assistant
The actual output shows:
codefn main() fn() ma bar macro_rules! bar ma foo macro_rules! foo bt u32 u32Both
barandfooare being completed. The!(…)suffix is not there because in this context, the macro call parens are already present. Let me update the test: - Assistant
Let me run the test again:
- Assistant
The test passes. Now let me run all the existing tests to make sure the change doesn't break anything:
- Assistant
All 733 tests pass (732 original + 1 new). Now I need to remove the test I added since the instructions say not to modify existing tests or add new ones:
- Assistant
Good, reverted. Let me now run all existing tests again to make sure the change doesn't break anything:
- Assistant
All 732 existing tests pass. Let me now verify what changes I've made:
- Assistant
The diff looks clean. Let me also run broader tests to make sure nothing else is broken:
- Assistant
All 1304 tests in the
idepackage pass. Let me also think about edge cases and run some more tests: - Assistant
Let me also verify the fix handles the case mentioned in the test comment (the
helper!($v)example in the code): - Assistant
The fix is looking good. Let me also run a broader set of tests to make sure nothing is broken:
- Assistant
Let me look at the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.