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); }Interface
No new interfaces are introduced.
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!
Assistant
Let me explore the repository structure to understand what we're working with.
Assistant
Let me understand the issue. When you have a macro call like
foo!(bar$0), wherefoo!is a macro that expands to$i!(), the completion forbar$0(wherebaris a macro name) doesn't work. The cursor is atbar$0inside the macro invocation, and the completion should complete the macro pathbar.Let me look at the completion code to understand how macro paths are completed inside macro calls.
Assistant
Let me look at the context analysis for completion - how it determines whether we're in a macro path completion context.
Assistant
Let me look at the analysis.rs file around line 1484 to understand how macro call completion works.
Assistant
Let me look at how the path is determined in the analysis, especially the part where the identifier is found.
Assistant
Let me understand the flow better. When the cursor is at
bar$0insidefoo!(bar$0), thebaris inside a token tree (the{ bar }part of the macro call). Let me look at how the completion decides what path context we're in.Let me look at the top of the analysis.rs file and the
analyzefunction.Assistant
Let me understand the bigger picture. The issue is "Cannot complete macro path in macro". When the cursor is at
bar$0insidefoo!(bar$0), thebaris inside a token tree of thefoo!macro call.When inside a macro's token tree, the completion needs to figure out what the expanded context would be. Since
foo!($i:ident) => { $i!() }, thebaridentifier would expand tobar!()— i.e., it becomes a macro call path.The completion analysis likely expands the macro speculatively and then figures out the context in the expanded code. Let me look at the expand_and_analyze function.
Assistant
Now I understand the macro expansion flow. Let me write a test to reproduce the issue. Let me look at the test infrastructure for completion.
Assistant
I see. So when we have
m!(x$0)withm!($e:expr) => { $e }, the macro expands and thexbecomes an expr. The completion offersmas a macro. So the completion of macros inside macros does work in the expr fragment case.The issue is specifically about
$i:identfragment where the macro expands to$i!(). Let me create a reproduction test.Assistant
There's a test for
$i:ident => { $i::bar }which expands to a path and works. The issue is about$i:ident => { $i!() }which expands to a macro call. Let me write a temporary test to reproduce.Assistant
No toolchain installed. Let me check if there's a way to use the system cargo.
Assistant
There's no rust toolchain installed. Let me try to install one.
Assistant
Now I have cargo. Let me check if there's a rust-toolchain file and try building.
Assistant
The rust-version is a commit hash, not a version number. Interesting. Let me try to build the ide-completion test.
Assistant
I've reproduced the issue. The expected output is
ma bar!(…) macro_rules! bar, but the actual output is empty. Let me understand the completion marker and how the cursor is handled.Assistant
So COMPLETION_MARKER is "raCompletionMarker". When cursor is at
bar$0, the input becomesfoo!(barraCompletionMarker). Let me look at the rest of theexpandfunction (the macro_call token tree handling).Assistant
So when cursor is at
bar$0insidefoo!(bar$0), the expansion producesbarraCompletionMarker!(). The tokenbarraCompletionMarkeris the path of a macro call (not inside a token tree). Let me checkis_inside_macro_call.Assistant
I've reproduced the issue. Now let me understand the types involved in the recursive expansion.
Assistant
Let me see the rest of the
expandfunction (the fn-like macro section).Assistant
Now I understand the full picture. Let me verify the
parent_ancestorsAPI on tokens.Assistant
token.parent_ancestors()is used already. Now let me implement the fix. Let me look at the exact lines to edit.Assistant
Now let me implement the fix. I'll modify the base case condition and add a helper function.
Assistant
Now let me edit the base case condition and add a helper function.
Assistant
Now let me run my reproduction test.
Assistant
The fix works -
baris now offered as a macro completion. The actual output includes the full expr-position completions plusma barandma foo, which is correct behavior for an expression context. Let me remove my temporary test and run the full completion test suite to check for regressions.Assistant
Good, the temp test is removed and only my source change remains. Now let me run the full completion test suite.
Assistant
All 732 completion tests pass. Now let me test some edge cases to make sure my fix is robust. Let me create temporary tests.
Assistant
Let me test the nested macro case and a qualified path case.
Assistant
The nested macro args case (passthrough!(bare:expr => $e). Let me see the actual output - it differs from expected. Let me run with UPDATE_EXPECT or just see the diff.
Assistant
So for the passthrough (expr fragment) case, macros are rendered WITH
!(…)becausehas_macro_bang = false(expansion is plain exprbar, notbar!()). In the issue's case ($i!() => bar!()),barbecomes the path of a macro call sohas_macro_bang = trueand render does NOT add!(). Both behaviors are correct - my expected was wrong. The fix works correctly for both. Let me remove this temp test and verify the exact issue case with correct expectations.Assistant
The exact issue case now works. Let me also verify
check_editworks (applying the completion produces the correct result).Assistant
The edit applies correctly:
foo!(bar$0)->foo!(bar). Now let me clean up the temp test and run the full test suites.Assistant
All 732 completion tests pass. Let me also run the broader
idecrate tests to ensure nothing downstream breaks.Assistant
All 1304 ide tests pass as well. Let me review my final change.
Assistant
The change is clean and minimal. Let me verify there are no leftover temp test remnants and that the build is clean.
Assistant
No temp test remnants. Only my source change remains. Let me run a final clippy check on the modified crate to ensure code quality.
Assistant
The clippy error is a pre-existing issue in the
syntaxcrate dependency (aredundant reference in format!warning from the newer nightly clippy), not in my modified code — my code compiles cleanly as confirmed by all passing tests.Let me do a final review of the complete change before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.