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 start by exploring the repository structure to understand what we're working with.
Assistant
Now, let me reproduce the issue. Let me first understand the test framework:
Assistant
Let me look at the existing tests in the pattern.rs file for the in_simple_macro_call:
Assistant
Now let me reproduce the issue. Let me write a test for this issue:
Assistant
Let me first write a script to reproduce the issue:
Assistant
Now let me create a test script to reproduce the issue:
Assistant
Let me look at how the existing test framework works, especially to check the completion tests:
Assistant
Let me create a temporary test file to verify the issue. First, let me look at how to run a single test:
Assistant
Let me write a temporary test and run it to confirm the bug:
Assistant
Let me look at how other tests check what's happening. Let me explore the structure of the analysis file more thoroughly:
Assistant
Now I need to understand the issue. Let me first try to reproduce the bug by adding a test:
Assistant
Let me run the existing test first:
Assistant
Now let me create a temporary test to verify the issue:
Assistant
Let me add a temporary test in the appropriate place:
Assistant
Let me add a test in special.rs:
Assistant
I've confirmed the bug. Now let me investigate the root cause. The issue is that no completion is offered for
bar(a macro name) when the cursor is inside a macro call argument.Let me trace what happens in the analysis:
Assistant
Let me trace through the problem. Looking at
analyze:- The cursor is at
barinsidefoo!(bar) foo!is a macro call - so we expand it:foo!($i:ident)->$i!()->bar!()- Now in the expansion we have
bar!()which is itself a macro call - The cursor is on the
baridentifier, which is the path of this macro call
Wait. Let me read again more carefully. The cursor at
bar$0infoo!(bar$0). The text after expansion offoo!isbar!(). The cursor would be atbar$0!().So the cursor is on a name reference that's a path inside a MacroCall. Let me see what should happen here.
Let me look at how the issue is reproduced to understand better:
- The cursor is at
Assistant
Let me dig into how
kind_macro_callworks and what happens. The flow:- Cursor at
bar$0infoo!(bar) - This expands
foo!(sincebaris an ident argument), expandingbar!() - Now
bar$0!()is parsed (expanded from original code) - The cursor is at
bar$0!()- on the namebar - The parent of
barisMacroCall, so we go throughkind_macro_call
But wait, this is happening inside an expanded macro. Let me look at what happens here:
- Cursor at
Assistant
Now let me look at how the
kind_macro_callreturns its kind:Assistant
Now let me see the
kind_macro_callmore carefully:Assistant
Assistant
OK, so MacroStmts exists. Let me check where this is generated:
Assistant
So MacroStmts is a top-level entry point. Let me see what the parser uses to parse a macro used as a statement:
Assistant
OK, so
MacroStmtsis the wrapping node. Let me look at the structure more carefully. When a macro is used as a statement (likebar!();), how is it parsed in the source code?Assistant
Let me look at the parser for macro call expressions:
Assistant
I see, so when the parser sees a path followed by
!, it wraps it inMACRO_CALLthenMACRO_EXPR. TheMACRO_EXPRis the wrapper for an expression-level macro call.So when we have
bar!()inside a function body, the AST is:- ExprStmt
- MacroExpr
- MacroCall (with path =
bar)- Path
- PathSegment (with name_ref =
bar)
- PathSegment (with name_ref =
- Path
- MacroCall (with path =
- MacroExpr
So
kind_macro_calldoes matchast::MacroExpr(expr) => make_path_kind_expr(expr.into())correctly.Now back to the issue:
macro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }After expansion of
foo!, the code becomes:fn main() { bar!(); }But wait, this is the code, not original. The cursor position is preserved. So in the expanded code, we have .
- ExprStmt
Assistant
Good. Now let me see what gets generated in the expanded code. In the buggy case:
- The
foo!(bar)macro call is insidefn main() { ... } foo!expands to$i!()(which after substitution isbar!())foo!is in statement position- So when parsing the expanded code, we use
TopEntryPoint::MacroStmts - The expanded code is
bar!() - This is parsed with
macro_stmtswhich callsexpressions::stmtwhich callsexpr_stmtetc.
Looking at the parser code, in
macro_stmtsit parses expressions as statements. Let me trace what happens:When parsing
bar!()with MacroStmts entry point, it callsexpressions::stmt(p, Semicolon::Optional). Thestmtfunction parses expressions, andbar!()would be parsed aspath_exprwhich would invoke theT![!]branch and wrap inMACRO_EXPR.So the structure should be:
- MacroStmts
- Stmt
- MacroExpr
- Stmt
- The
Assistant
So in
path_expr, when we encounter!, we wrap inMACRO_CALLand thenMACRO_EXPR. So the AST is always:- MacroExpr
- MacroCall
Hmm but wait, I need to verify this. Let me look at how stmt parses:
- MacroExpr
Assistant
OK so
stmtparses the expression. So when we parsebar!()as a statement, it should be wrapped inMacroExpr.Hmm but the test fails. Let me actually check. Let me write a small test to understand the AST:
Assistant
Let me dig deeper by adding logging to the analysis:
Assistant
Let me look at the analyze function and find_node_at_offset:
Assistant
Let me first take out my added test and try to understand by inspecting the AST. Let me modify my test to print the AST:
Assistant
Let me modify the temporary test to print debugging info:
Assistant
Let me revert the special.rs file and add my temporary test elsewhere:
Assistant
Now let me write a proper debug test to inspect the AST:
Assistant
Let me look at the analysis and understand the path more carefully:
Assistant
Now let me write a temporary test that prints the expansion state:
Assistant
Let me modify the test to use parse_or_expand through attach_db pattern:
Assistant
So
hir::attach_dbshould work. Let me try a different approach and use parse directly:Assistant
Let me update my test to use
parse_guess_edition:Assistant
Let me try a different approach. Let me just add the
use syntax::AstNodeimport:Assistant
Wait, this is showing the original file's AST, not the expanded one. Let me look at the expansion process more carefully:
OK so this confirms the original code's AST: the
foo!(bar)is aMACRO_CALLinsideMACRO_EXPRinsideEXPR_STMT.Now the question is: in the expanded file (after
foo!expands tobar!()), what does the AST look like?Let me see how the completion system handles this. Looking at the test
completes_in_simple_macro_call:macro_rules! m { ($e:expr) => { $e } } fn main() { m!(self::f$0); } fn foo() {}After expansion:
self::f$0(which is a path expression at statement level).Assistant
Let me modify my debug test to print the expanded file:
Assistant
The sed pattern didn't work. Let me check the current state:
Assistant
Let me look at the entire test:
Assistant
The sed deleted too much. Let me restore from git:
Assistant
Oh no, the sed broke everything! Let me restore:
Assistant
Now I see the expanded file:
MACRO_STMTS@0..6 MACRO_EXPR@0..6 MACRO_CALL@0..6 PATH@0..3 PATH_SEGMENT@0..3 NAME_REF@0..3 IDENT@0..3 "bar" BANG@3..4 "!" TOKEN_TREE@4..6So when expanded,
bar!()is properly parsed asMacroExpr > MacroCall. So thekind_macro_callshould work.Now the issue might be in the speculative expansion. Let me think about this more carefully. The completion is checking the speculative file (which is the original file with
COMPLETION_MARKERinserted into the macro arg).Let me re-read the
analyzefunction:Assistant
Let me now look at how the speculative expansion produces the file and what
fake_ident_tokenis:Assistant
OK, so
expand_maybe_stopis called recursively. Let me understand the flow:- The original file is
foo!(bar)(or whatever the user wrote). The cursor is atbar$0. - A fake ident (COMPLETION_MARKER) is inserted into the macro arg, making it
foo!(COMPLETION_MARKER). - Then
expandis called, which:- Finds the token tree in the original file (containing
bar) - Finds the token tree in the speculative file (containing COMPLETION_MARKER)
- Finds the token tree in the original file (containing
- The original file is
Assistant
So in this loop, for each token in fake_expansion that contains COMPLETION_MARKER, we recurse
expand_maybe_stop. In our case:- Actual expansion:
bar!() - Fake (speculative) expansion:
COMPLETION_MARKER!()(well, the macrofoo!takes$i:identand expands to$i!(), so after replacing$iwith COMPLETION_MARKER, we getCOMPLETION_MARKER!())
Wait, COMPLETION_MARKER is a fixed string "raCompletionMarker". Let me check what gets passed in.
- Actual expansion:
Assistant
Assistant
OK, so the speculative expansion uses which is determined from the macro call's parent in the original file.
Assistant
OK so
kind_itemchecks if the parent isItemList,AssocItemList,ExternItemList, orSourceFile. The parent ofraCompletionMarker!()would beMacroExpr, sokind_itemreturns None.Then we go through
match_ast!. The parent isMacroExpr, so we callmake_path_kind_expr(expr.into()).But wait, this is the speculative file's
MacroExpr. What about the actual expansion? The actual expansion is the result of the actualexpand_macro_call. Let me check.In the second level recursive call:
original_file: actual expansion =bar!()wrapped inMACRO_STMTS > MACRO_EXPR > MACRO_CALLspeculative_file: fake expansion =raCompletionMarker!()wrapped in similar
find_node_at_offset(&speculative_file, speculative_offset)finds (NameRef). Its parent is , parent is , parent is .Assistant
Now let me debug by adding eprintln statements to the analysis code:
Assistant
Let me add eprintln to track what's happening:
Assistant
Let me add an eprintln to trace what's happening:
Assistant
Now let me also add a test to run this. Let me restore my temporary test:
Assistant
see! The fake ident token is
barraCompletionMarker(wherebarwas already there andraCompletionMarkerwas inserted). Let me add more eprintln to track the flow:Assistant
The eprintlns for
classify_name_refandkind_macro_callweren't printed. This means the analyze function isn't reaching those functions. The expansion didn't happen! Let me check why:Assistant
Let me look at where
analyzeis called from:Assistant
eprintlns for
classify_name_refandkind_macro_callweren't printed, suggesting they're not being called. Let me check if it's a compile issue or if the analyze function returns earlier:Assistant
Let me check line 561 which is the call to
classify_name_ref:Assistant
The
analyze::NameRef brancheprintln was never printed, which meansfind_node_at_offsetreturned None. Let me look at that function:Assistant
Now I understand. The cursor is on an IDENT inside a TOKEN_TREE (not a NameRef). Let me look at the else branch in analyze:
Assistant
Let me look at the expand function to understand the recursion:
Assistant
Let me look at
is_inside_macro_call:Assistant
Now I understand the issue! Let me look at the
is_inside_macro_callcheck more carefully:Assistant
Now let me clean up the debug code and make the actual fix. The issue is in
expand_maybe_stopwhere it checksis_inside_macro_callwhich is too strict - it should be checking if the cursor is inside the args (token tree) of a macro call, not just anywhere in a macro call.Assistant
Now I understand the issue. Let me make the actual fix. The fix is to change the fallback in
expand_maybe_stopto be more specific - only return None when the cursor is inside a token tree (the args of a macro call), not just anywhere in a macro call:Assistant
Now let me verify the change:
Assistant
Now let me add the test for the issue and verify the fix works:
Assistant
The fix works. The completion now returns
ma bar,ma foo,fn main, andbt u32. Let me update the test to match:Assistant
The fix works. Now let me check if it breaks any existing tests. Let me run all the completion tests:
Assistant
All 733 tests pass! Now let me think about edge cases.
Assistant
So with
foo!(sel$0), the completion givesmain,bar,foo,u32. This is the same as before, which is good - it suggests everything that's in scope (includingself::).Actually wait,
selis a partial prefix ofself. The completion system is filtering by prefix. Let me update the test to check more carefully:Assistant
nested test case was incorrect (the expansion fails because
bar(baz)isn't an ident). Let me remove those and keep only the basic test:Assistant
The fix works and gives the expected result (all items in scope). Let me update the test to match:
Assistant
Let me run all the completion tests to verify nothing is broken:
Assistant
All 733 tests pass! Let me also think about the issue and look at the existing test that this case would have similar behavior to. Let me also check tests in the wider codebase:
Assistant
All tests pass! Now let me think about more edge cases. Let me look at what
is_inside_macro_call_token_treedoes for various cases:Assistant
Now let me think about edge cases more carefully. Let me also check that the docstring location is appropriate (the helper function shouldn't be placed in the middle of a docstring):
Assistant
Let me fix the placement of the helper function to be cleaner:
Assistant
Now let me think about edge cases more carefully. Let me look at the issue again:
Assistant
Let me look at other related tests to understand the patterns:
Assistant
Now let me add a test that exactly matches the issue description. Let me look at the test format used in the codebase:
Assistant
Now let me also remove my temporary test from
special.rsand verify the final state of the test file:Assistant
Now let me run the full test suite once more to make sure everything still passes:
Assistant
All 733 tests pass. Now let me look at my final fix:
Assistant
me run a wider set of tests to be sure nothing is broken:
Assistant
No tests failed. Let me also think about a few more edge cases. Let me trace through some scenarios manually to make sure:
Assistant
me debug this edge case more carefully:
Assistant
I need to be more careful. The issue is that when the cursor is in the middle of an identifier like
a$0b, the fix still triggers completion. Let me add a more precise check:Assistant
Let me also check the original behavior for
a$0b:Assistant
Wait, the stash also restored my fix. Let me re-verify.
Actually looking at the git status output: my changes to
crates/ide-completion/src/context/analysis.rswere not stashed (they're in the modified state but not shown). Let me check the actual state.Assistant
The git stash discarded my fix to analysis.rs! That's not what I wanted. Let me re-apply the fix.
Wait, actually, I stashed and then unstashed. The stash might have included the analysis.rs changes too. Let me check by looking at the file:
Assistant
fix to analysis.rs was lost. Let me re-apply it with the additional check for the cursor position:
Assistant
The new check is too strict. Let me look at the failing test:
Assistant
me investigate why this fails:
Assistant
see - the STRING token includes the quotes in its text. Let me only apply the suffix check for IDENT tokens:
Assistant
All 734 tests pass. Let me also verify my edge case test:
Assistant
Both tests pass. Let me also clean up the code and run the full workspace test:
Assistant
All 76 test suites pass. Now let me also think about more edge cases. Let me look at the test file for the existing test to see if there are similar cases:
Assistant
Let me also verify the actual fix works for the original issue case:
Assistant
Both tests pass. Now let me also remove the
is_potentially_split_identhelper since it's overly specific. Actually wait, let me think about this. The current logic is:!is_inside_macro_call_token_tree(&original_token) && (!is_potentially_split_ident(&fake_ident_token) || fake_ident_token.text().ends_with(COMPLETION_MARKER))So we check:
- The original token is not inside a token tree (not inside macro args)
- The fake_ident_token is NOT an IDENT OR it ends with COMPLETION_MARKER
This is OK but it's a bit confusing. Let me think if I can simplify it.
Actually, looking at this more, the check is essentially:
- If the fake_ident_token is an IDENT, then it should end with COMPLETION_MARKER (cursor at end of identifier)
- If the fake_ident_token is not an IDENT (e.g., STRING), don't apply this check
Let me simplify it by inlining:
Assistant
All 734 tests pass. Now let me look at the final state of my changes:
Assistant
Let me run the workspace tests one more time to make sure everything is fine:
Assistant
All tests pass! Let me also verify the changes I made by removing the temporary edge case test and keeping only the main fix test:
Assistant
Wait, the special.rs no longer has my test! Let me check:
Assistant
The tests are in place. Let me run them to make sure they pass:
Assistant
Both my tests pass. Now let me also look at the entire diff one more time to make sure my changes are clean:
Assistant
Let me check if my fix has any unintended consequences by running more comprehensive tests:
Assistant
All 734 tests pass. Now let me also think about whether my edge case test (for
a$0b) is actually necessary or if it's testing the wrong thing.Assistant
Both tests are valuable. Let me also look at the final state of my changes:
Assistant
me also do a final check to make sure my fix is well-formed:
Assistant
No clippy warnings on the analysis code. Let me also run a final round of tests to verify everything is fine:
Assistant
All 734 tests pass. Let me also remove the edge case test since it's not the primary fix being tested:
Assistant
No failures. Let me also make a final summary of my changes:
Assistant
Let me run all the tests one final time to make sure everything is fine:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.