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 to understand its structure and locate relevant files for this issue. The issue is about "Cannot complete macro path in macro". Let me first examine the repository root.
- Assistant
Let me look for completion-related files.
- Assistant
Let me understand the issue better. The issue is about macro completion - specifically, when you have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The
$0is the cursor position, and the user is trying to completebarinside thefoo!macro call. The problem is that rust-analyzer cannot complete the macro pathbarinside the macrofoo.Let me look at the completion context and how macro invocations are handled.
- Assistant
Let me look at the issue more carefully. The problem is about completing a macro path inside a macro call. In the code:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0afterbarinsidefoo!(...), the user expects to get completion forbar(the macro). Let me look at how completion works for macro arguments. - Assistant
Let me look at the completions directory to understand the structure better.
- Assistant
Let me look at how completion context analysis works, particularly for macro arguments:
- Assistant
Let me look at the key file - context/analysis.rs - to understand how completion works inside macro calls:
- Assistant
Let me look at the
expand_maybe_stopfunction to understand how macro expansion handles completions: - Assistant
Let me look at the expand_maybe_stop function.
- Assistant
Let me understand how the completion works inside a macro call. Let me search for relevant functions:
- Assistant
Let me look at how the expand function handles macro calls:
- Assistant
Let me look at the fn-like macro expansion section more carefully:
- Assistant
Let me look at the COMPLETION_MARKER and understand how the completion works:
- Assistant
Let me look at the expand function to understand how it handles macro calls:
- Assistant
Let me first understand how existing macro completion tests work. Let me look at the test files:
- Assistant
Let me look at how completions work in general. Let me look at the completions directory:
- Assistant
Let me look at the rest of the expand function for fn-like macro calls:
- Assistant
Let me look at the expand function's handling of fn-like macro calls:
- Assistant
Let me first create a test to reproduce the issue and understand what's happening. Let me look at existing tests:
- Assistant
Let me search for tests related to completion inside macro calls:
- Assistant
Let me look at those tests to understand the context better.
- Assistant
Let me look at how the completion system works for macro paths. Let me look at how the path completion context works:
- Assistant
Let me look at the PathKind enum and understand the different contexts for path completion.
- Assistant
Let me look at the PathKind enum to understand the different completion contexts:
- Assistant
- Assistant
Let me look at how
is_in_macro_matcheris used: - Assistant
Let me look at how the analyze function determines completion context, particularly for tokens inside macro call token trees:
- Assistant
Let me look at the lib.rs to understand how the completion is triggered:
- Assistant
Let me look at the main completion function:
- Assistant
Let me continue reading the lib.rs:
- Assistant
Now let me understand how the completion system works inside macro calls. Let me look at the analysis that happens when we can't find a
NameLikenode: - Assistant
Now I understand the issue. Let me trace through the code flow for the given scenario:
When we have
foo!(bar$0):- The completion marker is inserted:
foo!(barraCompletionMarker) expand_maybe_stopis called, which callsexpandexpandfindsfoo!()macro and expands it:foo!(bar)→bar!(),foo!(barraCompletionMarker)→barraCompletionMarker!()- It then calls
expand_maybe_stoprecursively with the expansion - In the recursive call,
expandtries to find token trees, butbarinbar!()is the path of the macro call, not inside a token tree. SoexpandreturnsNone. - The check
!sema.is_inside_macro_callis false (becausebarIS insidebar!()), so it returnsNone - This propagates up, causing the entire expansion to return
None
The issue is that when
expandreturnsNoneand the token is inside a macro call,expand_maybe_stopreturnsNoneinstead of returning the result. But sinceexpandalready tried and failed to expand further, we should stop and return the result. - The completion marker is inserted:
- Assistant
Now I understand the issue fully. The problem is in
expand_maybe_stop. WhenexpandreturnsNoneand the token is inside a macro call, the function returnsNoneinstead of returning the expansion result. Butexpandalready tried and failed to expand further, so we should stop and return the result.Let me write a test first to reproduce the issue:
- Assistant
Let me now create a test to verify the fix. First, let me write a small test script to understand how the completions work:
- Assistant
Let me check if there are existing tests for completing inside macro arguments:
- Assistant
Let me understand how
is_inside_macro_callworks: - Assistant
Let me look at the
is_inside_macro_callfunction: - Assistant
Now I understand the issue. Let me trace through the code flow:
- When we have
foo!(bar$0), the completion marker is inserted making itfoo!(barraCompletionMarker) expand_maybe_stopcallsexpandwhich findsfoo!()macro callfoo!(bar)expands tobar!(),foo!(barraCompletionMarker)expands tobarraCompletionMarker!()- Both expansions succeed, and
expandrecursively callsexpand_maybe_stopon the expansion result - In the recursive call, the token
barraCompletionMarkeris the PATH ofbarraCompletionMarker!(), NOT inside a token tree. SoexpandreturnsNone. - Then
is_inside_macro_callreturnstrue(the token is insidebarraCompletionMarker!()), soexpand_maybe_stopreturnsNone - This propagates up causing the entire expansion to fail
The problem is: when
expandreturnsNone(because the token is the path of a macro call, not inside a token tree), and the token IS inside a macro call, we returnNoneinstead of the result. But since already tried and failed, we should stop and return the result. - When we have
- Assistant
Now I understand the issue fully. The problem is in
expand_maybe_stop- whenexpandreturnsNone(because the token is the path of a macro call, not inside a TokenTree), and the token IS inside a macro call, the function returnsNoneinstead of the result. This causes the entire expansion chain to fail.Let me implement the fix:
- Assistant
Now I understand the full picture. The issue is:
- When
expandreturnsNone(because the token is the path of a macro call likebarinbar!(), not inside a TokenTree) - And the token IS inside a macro call
expand_maybe_stopreturnsNoneinstead of the result- This propagates up, causing the entire expansion to fail
The fix is: when
expandreturnsNone, we should always return the result, becauseexpandalready tried and failed to expand further. There's nothing more to try.Let me implement the fix:
- When
- Assistant
Now let me also remove the
semaparameter from the function since it's no longer used directly inexpand_maybe_stop(only passed through toexpand): - Assistant
Good, the fix is in place. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
Let me check what Rust toolchain is available:
- Assistant
No Rust toolchain is installed. Let me check if there's a rustup or cargo available elsewhere.
- Assistant
Let me check the toolchain settings.
- Assistant
Let me try setting up the toolchain:
- Assistant
Now let me check if there's a rust-toolchain.toml file that specifies a particular version:
- Assistant
Let me try to run the tests now:
- Assistant
Let me see the full test output to understand what changed:
- Assistant
Let me see the actual output:
- Assistant
The actual output is empty - my fix caused no completions at all! The problem is that my change is too broad. Let me think more carefully.
The issue is that removing the
is_inside_macro_callcheck entirely changes behavior for cases where the token is inside a macro call's token tree (like insidehelper!($v)in the existing test). In that case, the expansion finds the token tree and tries to expand, but gets no useful result. We need to distinguish between:- Token is the PATH of a macro call (like
barinbar!()) -expandcan't find a TokenTree → should stop and return result - Token is INSIDE a TokenTree of a macro call (like
vinhelper!(v)) -expandfinds TokenTree but expansion gives no useful result → should return None
Let me fix this properly:
- Token is the PATH of a macro call (like
- Assistant
I see there's a syntax error - I forgot to close the
if letblock properly. Let me fix the function: - Assistant
I see the issue - the code has a syntax error because the
if letblock isn't properly closed. Let me fix it: - Assistant
Good, the code looks correct now. Let me run the test again to see if it passes:
- Assistant
The existing test passes now. Let me run all the completion tests to make sure nothing is broken:
- Assistant
All 732 tests pass. Now let me write a test to verify that the fix resolves the issue described in the issue:
- Assistant
Let me write a quick script to test the actual scenario from the issue:
- Assistant
Let me write a test directly in the test file. I'll add a test for the issue:
- Assistant
Let me add a test for the issue. I'll add it after the
macro_that_ignores_completion_markertest: - Assistant
I see there's a duplicate
#[test]andfn excluded_trait_method_is_excluded(). Let me fix that: - Assistant
Good, the test file looks correct now. Let me run the new test to see what happens:
- Assistant
Let me see the full diff to understand what's different:
- Assistant
The completions are working now! I can see
barandfoomacros in the actual output. I need to update the expected output. Let me useUPDATE_EXPECT=1: - Assistant
The test passes now. Let me see the updated expected output:
- Assistant
The test now passes. The output shows that
barmacro is now being suggested as a completion when insidefoo!(bar$0). Let me run all the tests to make sure nothing is broken: - Assistant
All 733 tests pass (including the new one). Let me now run a broader set of tests to make sure I haven't broken anything else:
- Assistant
All tests pass. Let me also run the tests for the
hircrate to make sure nothing is broken there: - Assistant
Wait, I need to re-read the instruction. It says "UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository." I need to remove the test I added.
- Assistant
I see there's still a duplicate. Let me fix this properly:
- Assistant
I keep making a mess. Let me restore the file from git and only make the change to the analysis file:
- Assistant
Good, the test file is restored. Now let me verify that only the analysis.rs file has changes:
- Assistant
Good, only the analysis.rs file has changes. Let me run the tests again to make sure everything still passes:
- Assistant
All 732 tests pass. Let me review the final changes:
- Assistant
The diff looks good. Let me verify there are no warnings:
- Assistant
No warnings or errors. Let me now review the final change 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.