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
I'll start by exploring the repository structure to understand the codebase. Let's first see what's in the current directory.
- Assistant
Let me explore the repository more thoroughly.
- Assistant
This is the rust-analyzer repository. The issue is about macro path completion inside a macro call. Let me understand the issue:
- We have
barmacro defined:macro_rules! bar { () => (); } - We have
foomacro defined:macro_rules! foo { ($i:ident) => { $i!() }; } - In
main, we callfoo!(bar$0)where$0is the cursor positionThe issue is that completion doesn't work for the macro pathbarinside the macrofoo. This means when the user typesfoo!(barand tries to complete, they don't getbaras a completion suggestion.
Let me explore the repository structure to understand how completion works, especially in the context of macros.
- We have
- Assistant
The issue is about completing macro paths inside macros. Let me understand the problem better.
When we have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The cursor is after
barinsidefoo!(bar$0). The user expects to get completion forbaras a macro path. Let me look at the completion code to understand how macro path completions work. - Assistant
Let me look at the completion context to understand how macro completions work.
- Assistant
Let me look at the completion context and how it handles macro arguments.
- Assistant
Let me understand the issue better. The problem is about completing macro paths when inside a macro call. Specifically, when you have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The user is inside
foo!(...)and wants to typebar. The issue is that completion doesn't work forbaras a macro path in this context. Let me look at how completion handles tokens inside macro calls. - Assistant
Let me look at the test files to understand how macro completions are tested.
- Assistant
Let me understand the issue better. The problem is about completing a macro path inside a macro call. Let me look at how completion works for macro arguments, specifically for
identfragments. - Assistant
Let me look at the completion context analysis to understand how it handles macro calls. The issue is about completing macro paths inside macro calls, which likely involves how the speculative expansion and analysis works.
- Assistant
Let me look at the
expand_maybe_stopfunction to understand how macro expansion works during completion. - Assistant
Let me look at the
expand_maybe_stopfunction: - Assistant
Let me understand the issue better. The problem is about completing a macro path inside a macro call. The completion of
barinsidefoo!(bar$0)should work. Let me look at how the completion context is resolved for positions inside macro calls.Let me look for tests related to this kind of scenario:
- Assistant
Let me look at the specific completion logic more carefully. Let me look at how
expand_maybe_stopworks and what happens when we're inside a macro call that contains$i:ident: - Assistant
Let me read through the expand function more carefully to understand the issue. The key part is where we expand fn-like macro calls. The problem seems to be that when we have
foo!(bar$0), the fake identifier would make itfoo!(bar$COMPLETION_MARKER), but this might not be a valid$i:identbecause it's two identifiers (barandCOMPLETION_MARKER). Or wait, the marker is inserted in place of the cursor.Let me look at the flow more carefully.
- Assistant
Let me look at the test infrastructure for completion and search for existing tests related to macro path completion:
- Assistant
Let me check the
completes_in_simple_macro_calltest andcompletes_macro_segmenttest to understand the current behavior: - Assistant
Now let me look at the completes_in_simple_macro_call test to understand how it works - it seems to complete paths inside a macro call when the macro takes an
$e:expr. But the issue is about completing a macro path when the macro takes$i:ident.Let me look at the completions_in_macro_body test:
- Assistant
Let me understand the actual issue. The issue is about completing macro paths inside a macro call. Let me look at the relevant code path for completion inside macro calls.
Looking at the code snippet:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0afterbarinsidefoo!(bar$0), we should be able to completebaras a macro name. But the issue is that we can't complete it.The key is in the
expandfunction. When we're insidefoo!(bar$0), the code inserts a fake identifier at the cursor position, making it something likefoo!(bar$COMPLETION_MARKER). Then it tries to speculatively expand this macro. - Assistant
Let me understand the exact completion marker placement. The marker is inserted at the cursor, and the fake_ident is picked right-biased. When
foo!(bar$0)has the marker inserted, we getfoo!(barraCompletionMarker)- wait, that would bebarraCompletionMarkeras one token. Let me check more carefully: - Assistant
Let me check the lib.rs file
- Assistant
Now let me look at how the completion context handles being inside a macro call. Let me search for
is_inside_macro_call: - Assistant
Let me look at
is_inside_macro_call: - Assistant
Let me look at the test cases more carefully. Let me first search for tests about completion inside macro calls:
- Assistant
Let me understand how the speculative file is created:
- Assistant
Let me look at the completion lib.rs to understand how the speculative file is created:
- Assistant
Let me search for where the completion context is initially set up:
- Assistant
Let me look at the completion context creation to understand how the speculative file is created.
- Assistant
Let me look at the context.rs file around line 754 to understand how the speculative file is created.
- Assistant
Let me trace through the logic for this specific case. Let me check the
analyzefunction to see how it handles the case where we're inside a macro call's token tree: - Assistant
Let me understand the issue better. Let me look at how the expansion works for
$i:identpatterns. - Assistant
Let me look more carefully at how the completion marker is inserted and what the resulting AST looks like:
- Assistant
Let me look at what the
ast::Macroandast::MacroCallkinds are: - Assistant
Let me understand the issue by looking at what happens when we try to complete inside a macro call. The issue is that
expand_maybe_stopis returningNonein this case, causing no completions. Let me look at the logic flow more carefully. - Assistant
Let me look at how the completion handles the case where the token tree is inside a macro call (as opposed to a macro definition):
- Assistant
Now I need to understand the actual issue. Let me look at the
expandfunction to see how fn-like macro expansion handles$i:ident: - Assistant
OK, so
expand_macro_callreturnsOption<InFile<SyntaxNode>>. That makes sense. Let me now checkspeculative_expand_macro_call. - Assistant
Now let me understand what the speculative file looks like when we have
foo!(bar$0)and insert the completion marker. Let me trace through the actual expansion: - Assistant
Let me understand the issue better. The completion marker "raCompletionMarker" is inserted at the cursor position, so
foo!(bar$0)becomesfoo!(bar raCompletionMarker)— wait, the$0is right afterbarwith no space. Let me check exactly what happens: - Assistant
Let me now understand what's happening with the expansion when the user is inside
foo!(bar$0). The key issue is in theexpand_maybe_stopfunction. Let me first write a test to reproduce the issue: - Assistant
Let me look at the test infrastructure to understand how to write a test for this:
- Assistant
Let me search for where
checkis defined or imported. - Assistant
Let me first run the existing tests to make sure they pass:
- Assistant
Let me check what toolchain is available:
- Assistant
No toolchain is installed. Let me check if there's a rust toolchain available in the environment.
- Assistant
Let me check the version and the toolchain setup.
- Assistant
Let me check the rustup toolchain configuration.
- Assistant
Let me check the rust-version file and cargo config.
- Assistant
Let me check if there's a way to run tests with the existing setup.
- Assistant
OK so there IS a default toolchain but
rustupisn't finding it. Let me try to use cargo directly with the correct environment: - Assistant
Good, the test infrastructure works. Now let me write a test to reproduce the issue first:
- Assistant
Now let me trace through the problem more carefully. I need to understand the exact flow. Let me look at how the expansion works for
foo!(bar$0)whenfoohas$i:ident: - Assistant
Let me trace through the code carefully. The issue is that when the cursor is inside
foo!(bar$0), the expansion process fails because:- The speculative file has
foo!(bar raCompletionMarker)(two tokens because there's a space) - Wait, let me check how the insertion actually works - is there a space or not?
- The speculative file has
- Assistant
Let me understand the exact issue. When we have
foo!(bar$0)wherefootakes$i:ident, the insertion of the completion marker at the cursor makes itfoo!(barr aCompletionMarker)(since the marker is inserted right afterbarwith no space). The$i:identpattern matches this single identifier tokenbarr aCompletionMarker, and the expansion producesbarr aCompletionMarker!().The issue is in
expand_maybe_stop: when the expansion of the speculative macro call produces a different macro call path (barr aCompletionMarker!()vsbar!()), the paths don't match,expandreturnsNone, and then theis_inside_macro_callcheck prevents returning the base case.The fix should be: when
expandreturnsNone, we should still return the base case, because the speculative file is still useful for completion analysis. Theis_inside_macro_callcheck is overly conservative here.Let me implement the fix:
- Assistant
Let me think about the exact fix needed. The issue is in
expand_maybe_stop- whenexpand()returnsNonebut the token IS inside a macro call, it returnsNoneinstead of returning the base case. This is overly conservative becauseexpand()returningNonemeans we can't expand any further, so we should return the current state regardless.Let me check git history for context on this check:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the exact flow:
foo!(bar$0)→ speculative file hasfoo!(barr aCompletionMarker)(marker appended tobar)expandsuccessfully expandsfoo!()→ actual:bar!(), speculative:barr aCompletionMarker!()- Recursive call to
expand_maybe_stopwithbar!()andbarr aCompletionMarker!() - Inner
expand()finds macro callsbar!()andbarr aCompletionMarker!()but paths differ → returnsNone is_inside_macro_callcheck:baris insidebar!()→ returnstrue→!true = false→ returnsNone- This propagates up, causing the outer expand to also return
None - Falls back to unexpanded files, and in
analyze(), the token is insideTOKEN_TREEofMacroCallwhich is not handled → returnsNone
The fix is to remove the
is_inside_macro_callcheck and always return the base case whenexpandreturnsNone. The reasoning: already tried everything it could; returning just loses the expansion work already done. - Assistant
Now let me implement the fix. The key change is to always return the base case when
expandreturnsNone: - Assistant
Let me use the open command first, then edit:
- Assistant
- Assistant
The edit command doesn't seem to accept
--fileoption. Let me try using thereplacecommand instead. - Assistant
Let me use sed to make the replacement:
- Assistant
Let me verify the change looks correct:
- Assistant
Good, the change looks correct. Now let me also check if
semais still used in the function since we removed theis_inside_macro_callcheck: - Assistant
Let me check if
semais still used inexpand_maybe_stopafter the change: - Assistant
Good,
semais still used by theexpandcall. Now let me run the existing tests to make sure nothing is broken: - Assistant
One test fails. Let me look at it:
- Assistant
Let me look at this test:
- Assistant
Let me see the full expected output:
- Assistant
I see, there's a test that's failing because my change is too broad. The
is_inside_macro_callcheck exists to distinguish between tokens that are inside a macro's token tree (where completions can't work) vs tokens at valid AST positions. Let me look at the failing test more carefully: - Assistant
I see. The test expects completions but now gets nothing. The issue is that my change is too broad. Let me understand what's happening.
The test has
m!(v$0)wheremexpands$v:identto{{ helper!($v); $v }}. The expansion has the token both insidehelper!()(in its token tree) and as a standalone expression. My change now picks the wrong mapped token.Let me revert my change and think more carefully:
- Assistant
OK, let me reconsider the approach. The issue is more nuanced. I need to handle the case where macro paths differ during recursive expansion, without breaking the case where the mapped token is inside a token tree.
Let me look at the exact place where paths are compared in the
expandfunction: - Assistant
Now I see the key part. When the paths don't match (line 390),
expandreturnsNone. This is the case for our issue:barvsbarr aCompletionMarker.The fix should be: when paths don't match, instead of returning
None, return the current state. This way theanalyzefunction gets the expanded result where the fake_ident_token is at a valid AST position.Let me implement this targeted fix:
- Assistant
Now I see the precise location. When the macro paths don't match (line 388-391),
expandreturnsNone. Instead, we should return the current state as anExpansionResultso thatanalyze()can work with it: - Assistant
Let me verify the change:
- Assistant
Let me clean up the extra blank lines:
- Assistant
There are extra blank lines. Let me clean them up.
- Assistant
Let me verify again:
- Assistant
Good. Now let me run the tests to make sure this fix works and doesn't break anything:
- Assistant
All 732 tests pass. Now let me write a test to verify the fix resolves the issue. Let me find the right test file and add the test:
- Assistant
Let me first check what completions our fix actually provides for the issue case. Let me create a quick test:
- Assistant
Let me add a test to the special.rs test file:
- Assistant
I need to find a good place to add the test. Let me add it right after the
completes_in_simple_macro_calltest. - Assistant
Let me add a test right before the
completes_macro_segmenttest: - Assistant
Let me verify the test was added correctly:
- Assistant
There's an extra
}after the test. Let me check the structure: - Assistant
There's an extra
}that shouldn't be there. Let me fix it: - Assistant
Let me verify:
- Assistant
There's still an extra
}on line 498. Let me check the broader context: - Assistant
I see the issue - the test was inserted inside the function body, not between functions. Let me look at the broader context:
- Assistant
The test was incorrectly inserted. Let me fix the test file by reverting it and re-inserting properly:
- Assistant
Now let me add the test properly. Let me find the exact line to insert after:
- Assistant
Let me add the test before
completes_macro_segment. I need to insert before line 485. - Assistant
Good. Line 482 closes the previous function. Let me insert my test between lines 482 and 483:
- Assistant
Let me verify the test was inserted correctly:
- Assistant
Now let me run the test to see if it passes:
- Assistant
The test fails - no completions are produced. My fix to the path mismatch in
expand()isn't sufficient because the issue happens earlier: whenexpand_maybe_stoprecurses, the token at the new offset is at the path of the macro call (not inside a TokenTree), soexpand()returnsNonebefore even reaching the path check. Theis_inside_macro_callcheck then prevents returning the base case.Let me revert my
expandchange and instead fixexpand_maybe_stop: - Assistant
Let me re-examine the problem more carefully. The issue is that
expand()in the recursive call can't find TokenTree ancestors because the tokenbaris the PATH ofbar!(), not inside its TokenTree. Soexpand()returnsNoneearly, and thenis_inside_macro_callprevents returning the base case.The fix should be: instead of checking
is_inside_macro_call, check if the token is actually inside a macro's TokenTree. If the token is the path of a macro call (not inside a TokenTree), we should still return the base case. - Assistant
Let me implement the fix. The key change is to replace the
is_inside_macro_callcheck with a check for TOKEN_TREE ancestors: - Assistant
Let me verify the change:
- Assistant
There are extra blank lines. Let me clean them up:
- Assistant
Let me verify the change again:
- Assistant
Now let me check if
SyntaxKindis imported: - Assistant
Good,
SyntaxKindis already imported. Now let me run the tests: - Assistant
Only the new test fails, existing tests pass! Let me see what the actual output is:
- Assistant
The fix works! Completions are now being produced. Let me update the test expectations to match the actual output:
- Assistant
The actual output is:
codefn main() fn() ma bar macro_rules! bar ma foo macro_rules! foo bt u32 u32So the macros are shown without
!(…)because the completion is for a macro path (not a macro call). Andfn main()andbt u32are also shown because they're valid identifiers.Let me update the test expectations.
- Assistant
Let me verify the change:
- Assistant
Now let me run the test to verify:
- Assistant
The new test passes. Now let me run all tests to make sure nothing is broken:
- Assistant
There's a failing test
completes_qualified_macros. Let me check it: - Assistant
Let me look at that test:
- Assistant
Wait, this test was already there and was passing. Let me check if I accidentally modified it:
- Assistant
I see! My earlier sed commands accidentally modified the
completes_qualified_macrostest expectations too. Let me fix that: - Assistant
Let me re-add my test properly this time:
- Assistant
I need to add the test before line 485 (the
completes_macro_segmenttest). Line 483 is the#[test]attribute forcompletes_macro_segment, and line 482 is the closing}of the previous function. I need to insert after line 482.Let me use the
openandeditcommands. - Assistant
I need to insert a test between lines 482 and 483. Let me use sed to insert the test.
- Assistant
Let me insert the test using the edit command:
- Assistant
Good, the test was inserted correctly. Now let me run all the tests:
- Assistant
All 733 tests pass (732 original + 1 new). Let me verify all changes I made:
- Assistant
Let me review the changes I've made:
-
crates/ide-completion/src/context/analysis.rs: Changed the condition inexpand_maybe_stopfrom checkingis_inside_macro_callto checking if the token has aTOKEN_TREEancestor. This is more precise:- Old:
!sema.is_inside_macro_call(...)- returnstrueif ANY ancestor is aMacroCall, including when the token is the path of the macro call - New:
!original_token.parent_ancestors().any(|a| a.kind() == SyntaxKind::TOKEN_TREE)- returnstrueonly if the token is inside a macro's token tree (arguments)
- Old:
-
crates/ide-completion/src/tests/special.rs: Added a new testcompletes_macro_path_in_macro_callthat reproduces the exact issue from the bug report.
Now let me do a final review of the fix:
-
- Assistant
Let me also verify that
semais no longer used inexpand_maybe_stop(it's now only passed toexpand): - Assistant
Good,
semais still used by theexpandcall. Let me also check if there are unused imports by verifying the code compiles: - Assistant
The code compiles cleanly. Let me run all tests one final time:
- Assistant
All 733 tests pass. Let me now do a final review of the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.