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:
macro_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!
(Current directory: /rust-analyzer, current file: none) bash-$
Assistant
Let me explore the repository structure to understand what we're working with.
Assistant
This is rust-analyzer. The issue is about macro path completion inside macros. When you have
foo!(bar$0)wherefoo!expands to$i!(), the completion at the cursor position should suggestbaras a macro path completion.Let me search for the completion code related to macros.
Assistant
Let me look at the directory structure.
Assistant
Let me understand the issue better. The problem is that when you're inside a macro call
foo!(bar$0)wherefoo!expands to$i!(), the completion should suggestbaras a macro. But the cursor is in a position where the$iis being used as a macro path ($i!()).Let me look at how macro completion paths work. Let me look at the context analysis to understand how the path context is determined.
Assistant
Let me look at the key part of the expansion logic - around line 379-420 where fn-like macro calls are expanded.
Assistant
Let me look at the full expand function to understand how it picks the best expansion.
Assistant
Let me look at the analyze function more carefully, specifically how it handles the macro call path context.
Assistant
Let me look at existing completion tests for macros.
Assistant
Let me look at the
completes_in_macro_bodytest which seems more relevant.Assistant
Let me create a test to reproduce the issue. Let me write a quick test using the test infrastructure.
Assistant
Let me create a temporary test to reproduce the issue.
Assistant
I need to set up a toolchain. Let me try installing one.
Assistant
The repo needs nightly. Let me check what's needed. The rust-version is 1.95. Let me try building with stable.
Assistant
The test passes with the empty expect, which confirms the issue: completion returns nothing. Now let me understand the issue deeper. Let me check what the COMPLETION_MARKER is and look at the expansion logic.
Assistant
So the fake ident
raCompletionMarkeris inserted at the cursor position. In our case,foo!(bar$0)becomesfoo!(barraCompletionMarker). Then the macrofoois called with$i:ident. The$iwould bebarraCompletionMarkeras a single ident. Then the expansion would bebarraCompletionMarker!().So the speculative expansion would map the
barraCompletionMarkertoken to thebarraCompletionMarkertoken in the expansionbarraCompletionMarker!(). The fake_mapped_token text containsCOMPLETION_MARKER.Then
expand_maybe_stopis called recursively. In the recursion, the offset would be atbarraCompletionMarkerin the expansion. The expansionbarraCompletionMarker!()is a macro call. The cursor is on the pathbarraCompletionMarker. Now we try to expand further - isbarraCompletionMarkera macro call? No,barraCompletionMarker!()is a macro call but the macrobarraCompletionMarkerdoesn't exist (it's notbar). Sosema.expand_macro_callwould fail. So the recursive expand returns None.Then we check the base case - is the token inside a macro call? The token
barraCompletionMarkerin the expansion IS inside a macro call (thebarraCompletionMarker!()macro call itself - well, the path is the macro being called, but the token is the path of the macro call). Hmm.Actually,
is_inside_macro_call- let me check what that does. The token is part of the macro callbarraCompletionMarker!(). Is it "inside" the macro call? The path of the macro call might not be considered "inside" the macro call. Let me check.Assistant
So
is_inside_macro_callchecks if any parent ancestor is aMacroCall. In our case, the tokenbarraCompletionMarkeris the path of the macro callbarraCompletionMarker!(). The path's parent is theMacroCallnode. Sois_inside_macro_callreturnstrue.So in the base case check, the original token (in the actual expansion
bar!()) is the pathbarof the macro callbar!().is_inside_macro_callreturns true because the parent is a MacroCall. So the base case fails, andexpand_maybe_stopreturnsNone.So the whole expansion returns None, and we fall back to the non-expanded context. Let me look at the part after expansion that determines what to do.
Assistant
So the issue is clear. The expansion of
foo!(bar)->bar!()succeeds, but then when we try to recurse/stop, the base case check fails becausebar(in the expansionbar!()) is considered "inside a macro call" (because it's the path of thebar!()macro call).The problem is the
is_inside_macro_callcheck. When the cursor is on the path of a macro call (i.e., we're completing the macro name itself), we shouldn't consider that as "inside a macro call" - we should stop and provide completions.Let me check the usages of
is_inside_macro_call.Assistant
Let me look at the search usage to see if it would be affected.
Assistant
In search,
is_inside_macro_callis used to determine whether to descend into macros. Let me understand the structure of MacroCall better.Assistant
Now I understand the structure. The
MacroCallhas apath()(the macro name) andtoken_tree()(the input). The path is the macro being invoked, NOT the input. Theis_inside_macro_callsemantics should be: a token is inside a macro call if it's part of the input (token tree) that gets expanded, NOT if it's the path (macro name).Let me look at the
is_inside_macro_callimplementation more carefully and modify it.Assistant
The doc comment explicitly says: "we strictly check if it lies inside the input of a macro call." The path (macro name) is NOT the input. So the current implementation has a bug - it returns true for the path of a macro call, contradicting the documented intent. Let me fix
is_inside_macro_call.Assistant
Now let me make the fix to
is_inside_macro_call:Assistant
Now let me build and test the reproduction.
Assistant
The fix works - now
baris completed. Let me see the full output.Assistant
The fix works -
ma bar macro_rules! barnow appears. Let me remove the temporary test and run the relevant test suites to check for regressions.Assistant
All ide-completion tests pass. Now let me run the search and references tests since
is_inside_macro_callis also used there.Assistant
All ide tests pass. Let me also run the hir tests to be safe.
Assistant
Now let me add a proper regression test. Let me look at the existing completion test for macros to add one in the right place.
Assistant
Let me add a regression test after
completes_in_simple_macro_call.Assistant
The test passes. Let me verify a few edge cases with temporary tests.
Assistant
Both passed but with empty expect - let me check if they actually produce completions. The qualified case
foo!(self::bar$0)won't expand since$i:identonly matches a single identifier. Let me investigate the nested case more.Assistant
Let me investigate the nested case more - I expected it to produce completions. Let me check the actual output.
Assistant
Let me change the expect to a non-empty value to see the actual output for edge2.
Assistant
Wait, it passed even with
expect![[r#"X"#]]? That means the actual was also "X"? No, that doesn't make sense. Oh wait, maybe the sed didn't replace correctly. Let me check.Assistant
The sed produced a duplicate. Let me clean this up - let me remove all the temp tests.
Assistant
I reverted everything. Let me investigate the nested case first with a clean temporary test before adding the final regression test.
Assistant
Let me test the single-level
outer!(bar$0)case (outer expands$i!()=bar!()) which is a cleaner test.Assistant
outer!(bar$0)works. Now let me re-test the nested case cleanly.Assistant
Let me check if the nested case was already returning empty before my fix (i.e., it's a pre-existing limitation, not a regression).
Assistant
The stash removed the test too. Let me do this differently - keep the test but revert only the semantics.rs change.
Assistant
The nested case returns empty even WITHOUT my fix - it's a pre-existing limitation, not a regression. Let me confirm
outer!(bar$0)was also empty before my fix (to confirm my fix enables it).Assistant
outer!(bar$0)was empty before my fix, returns completions after. My fix enables both. The nested case is a pre-existing separate limitation. Let me clean up and add the proper regression test.Assistant
The regression test passes. Now let me run the full ide-completion, ide-db, and ide test suites once more to confirm no regressions.
Assistant
All tests pass. Let me run a few more relevant crates that involve macro semantics to be thorough.
Assistant
All pass. Let me review the final diff.
Assistant
Let me remove the specific issue number reference since I don't actually know it.
Assistant
Let me remove the unnecessary
.clone()sincecastconsumes the owned node and we return immediately.Assistant
The clone is necessary because
ancestoris used later whencastreturns None. Let me restore it.Assistant
The clippy error is in the
syntaxcrate, unrelated to my change. Let me verify it's pre-existing.Assistant
The clippy error is in
crates/syntax/src/fuzz.rs- a pre-existing issue with the newer clippy version (1.97.0), completely unrelated to my change. My change is fine.Let me do a final review of the complete diff.
Assistant
The fix is complete. Let me verify there are no leftover temp files and run one final confirmation of the relevant test suites.
Assistant
Let me run the final confirmation of the full test suites.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →