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); }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
I'll start by exploring the repository to understand the codebase and locate relevant files for macro completion. Let's first check the structure.
- Assistant
Let me explore the repository structure more to understand how it's organized.
- Assistant
The issue is about macro path completion inside a macro call. When you have
foo!(bar$0), the cursor is afterbarand inside a macro call. The completion should suggest macro names likebar(which is defined as a macro). Let me look at the completion logic, particularly for macros.Let me search for macro completion related files.
- Assistant
Let me look at the context analysis to understand how completion works inside macro invocations.
- Assistant
Let me understand the issue better. The problem is about completing a macro path
barinside a macro callfoo!(bar$0). Let me look at the context analysis to understand how completions inside macro invocations work. - Assistant
Let me search for how macro invocations are handled in the completion context.
- Assistant
Let me look at the section around line 379 where fn-like macro calls are expanded:
- Assistant
Let me look at how the completion context handles macro arguments and the specific case of completing a macro name inside a macro invocation:
- Assistant
Let me search for how macro arguments are being handled in the completion context:
- Assistant
Let me first look for existing tests related to macro completion to understand how the feature is supposed to work:
- Assistant
Let me search for tests related to macro path completion:
- Assistant
Let me look for how the completion context determines if we're inside a macro call and how it handles completions there:
- Assistant
Let me look at the relevant section around line 155 to understand how the system handles completions inside macro calls:
- Assistant
Let me look at how the completion context is analyzed, specifically how it handles the path context for macro calls:
- Assistant
Let me look at how macro completions are typically handled and search for tests related to this:
- Assistant
Let me look at the
kind_macro_callclosure more closely: - Assistant
Let me look at the special.rs test file to see if there are existing tests for macro completions:
- Assistant
Let me look at the
completes_in_simple_macro_calltest: - Assistant
Let me look at how completions work for macro arguments, specifically the
$i:identcase: - Assistant
Let me search for how completions inside macro arguments are handled, particularly for ident fragments:
- Assistant
Let me look at how the completion system determines what to complete in different contexts:
- Assistant
Let me search for how the completion system handles macro arguments and the
identfragment specifier: - Assistant
Let me search for how completions work for different path kinds:
- Assistant
Let me understand the issue better by looking at how completions work for macros in different contexts:
- Assistant
Let me look at how macro completions are provided in the item_list module:
- Assistant
Let me look at the test file to see if there's already a test for this specific case:
- Assistant
Let me look at the test around line 1163:
- Assistant
Let me look at how the completion context determines what's inside a macro call and how it handles macro arguments:
- Assistant
Let me look at the context.rs file to understand the completion context structure:
- Assistant
Let me look at the PathKind enum to understand all the path kinds:
- Assistant
Let me look at the PathKind enum:
- Assistant
Now let me understand the issue. When we're inside
foo!(bar$0), the cursor is inside the token tree of the macro callfoo!. The issue is that the completion system needs to handle this case where we're completing a macro name that's being passed as an argument to another macro.Let me look at how the completion system handles being inside a macro call's token tree:
- Assistant
Let me look at the
expandfunction more carefully to understand how macro expansion handles completions: - Assistant
Let me look at the beginning of the
expandfunction to understand how it processes macro calls: - Assistant
Let me look at how the completion system determines the context when we're inside a macro call's token tree:
- Assistant
Let me search for how completions work when we're inside a macro call's token tree:
- Assistant
Let me look at the test file for special completions to see if there are tests for completing inside macro calls:
- Assistant
Let me look at the completions for macro calls more carefully. First, let me understand how the completion system handles being inside a macro call's token tree:
- Assistant
Let me look at the completions for item_list.rs to see how macro completions are provided:
- Assistant
Let me look at the test file for special cases to see if there are tests for completing inside macro calls:
- Assistant
Let me search differently.
- Assistant
Let me look at how the completion system handles being inside a macro call's token tree. First, let me search for tests that involve completing inside macro calls with ident parameters:
- Assistant
Let me look at how the expand function works more closely, particularly how it handles macro call token trees:
- Assistant
Let me look at the
expandfunction more carefully to understand how it handles being inside a macro call's token tree: - Assistant
Let me look at the
expandfunction starting from line 172. - Assistant
Let me understand the issue better by looking at how the system handles the specific case of completing inside a macro call. Let me first run the existing tests to see if there are any related tests:
- Assistant
Let me check how to run tests in this repository.
- Assistant
Let me check if cargo is available and how to run tests.
- Assistant
We need to set up the toolchain first. Let me check if there's a toolchain file in the repo.
- Assistant
Let me check what rust toolchain is needed:
- Assistant
Now let me try running the tests to understand the issue better:
- Assistant
Let me try running the relevant tests:
- Assistant
Now let me understand the issue better by looking at how completions work inside macro calls. Let me check what happens when the cursor is inside a macro call's token tree:
- Assistant
Let me look more carefully at the expand function to understand the flow when we're inside a macro call:
- Assistant
Now I understand the issue better. Let me look at how the completion system handles the case where we're inside a macro call's token tree, specifically for ident fragments. Let me look at the
expandfunction to understand how it processes macro call token trees: - Assistant
Let me check what the COMPLETION_MARKER is:
- Assistant
Let me look at how the speculative file is created:
- Assistant
Let me look at how the completion entry point constructs the speculative file:
- Assistant
Let me look at the completions function:
- Assistant
Now let me understand the issue. When we're inside
foo!(bar$0), the macrofooexpands$i:identto$i!(). Sofoo!(bar)would expand tobar!(). The issue is that the completion system needs to recognize that after expansion, we're in a position wherebaris a path being used as a macro call, so it should suggestbaras a macro completion.Let me trace the flow more carefully. The problem is in
expand_maybe_stop- when it tries to expand the macro, and then recursively tries to expand again, it fails becausebarraCompletionMarkercan't be resolved as a macro, and theis_inside_macro_callcheck returnsNoneinstead of yielding the expanded result.Let me verify this theory by looking at the issue from the perspective of what happens after the first expansion succeeds:
- Assistant
Let me understand the issue better. Let me look at the analyze function to see what context the completion is in when we're in the source file:
- Assistant
Let me understand
is_inside_macro_call: - Assistant
Let me look at the
is_inside_macro_callfunction to understand what it does: - Assistant
Now I understand the issue. Let me trace through the logic carefully:
- In
foo!(bar$0), the cursor is atbarinside a macro call expandfindsfoo!and expands it:foo!(bar)→bar!()andfoo!(barraCompletionMarker)→barraCompletionMarker!()- The expansion succeeds, and we recursively call
expand_maybe_stopwith the expansion result - In the recursive call,
expandtries to expandbarraCompletionMarker!(). The paths don't match (barvsbarraCompletionMarker), soexpandreturnsNone - Then the base case checks
!sema.is_inside_macro_call(...)- butbarinbar!()IS inside a macro call (it's the path ofbar!()), so this returnsfalseand we returnNone - The whole expansion chain fails, and we fall back to the original file where the token is inside a
TOKEN_TREEof a macro call, which also can't provide completions
The fix should be in : when returns , we should still allow the base case to be reached even if the token appears to be "inside a macro call". The check is too conservative - it considers the path of a macro call ( in ) as "inside" the call, which prevents us from providing completions.
- In
- Assistant
Now I understand the issue. Let me trace through the logic:
foo!(bar)expands tobar!()- In the recursive
expand_maybe_stop,expandtries to expandbarraCompletionMarker!()but paths don't match (barvsbarraCompletionMarker), soexpandreturnsNone - Then the base case checks
!sema.is_inside_macro_call(...)— butbarinbar!()IS inside a macro call (it's the path of theMacroCall), so it returnsfalse, andexpand_maybe_stopreturnsNone - The entire expansion chain is lost, falling back to the original file context, where
baris inside aTOKEN_TREEoffoo!(...), which can't provide completions either
The fix: in
expand_maybe_stop, whenexpand()returnsNone(meaning we can't expand further), we should always return the current state as the base case. Theis_inside_macro_callcheck is overly conservative — after expandingfoo!(bar)tobar!(), thebartoken is at the path position of the resultingMacroCall, which is a valid completion position. - Assistant
Let me use the correct edit format:
- Assistant
Now let me make the edit. I need to replace lines 150-170.
- Assistant
Let me verify the edit looks correct:
- Assistant
Good. Now let me run the existing tests to make sure this change doesn't break anything:
- Assistant
All special tests pass. Let me run the broader ide-completion test suite:
- Assistant
One test fails. Let me look at it:
- Assistant
Let me look at the failing test.
- Assistant
Let me see the full test output:
- Assistant
Let me run the test with the
--exactflag and capture the full diff: - Assistant
Let me get the actual vs expected output:
- Assistant
I see - the test is failing because the actual output is empty (no completions). The issue is that with my overly broad change, the token inside
helper!(v)(which isvinside the token tree of the macro call) now incorrectly returns the base case result, interfering with the correct completion from the second occurrence ofvin the expansion.The key issue is: I removed the
is_inside_macro_callcheck entirely, but it should only be removed for the case where the token is at the path position of a macro call (not inside its arguments).Let me refine the fix:
- Assistant
Let me now implement a more precise fix:
- Assistant
Now I need to add the
is_macro_call_pathhelper function. Let me add it near theexpand_maybe_stopfunction: - Assistant
Now I need to add the
is_macro_call_pathhelper function. Let me add it before theexpand_maybe_stopfunction: - Assistant
Let me verify the edit looks correct:
- Assistant
I see the doc comment got misplaced. Let me fix the placement:
- Assistant
Now let me add the
is_macro_call_pathfunction with proper doc comment beforeexpand_maybe_stop: - Assistant
Wait, I need to fix the structure. The doc comments are getting mixed up. Let me look at the current state and fix properly:
- Assistant
The doc comment is now incorrectly attached to
is_macro_call_path. Let me fix the structure: - Assistant
Now let me add the
is_macro_call_pathfunction betweentoken_at_offset_ignore_whitespaceand the doc comment forexpand_maybe_stop: - Assistant
Let me verify the structure is correct now:
- Assistant
Good, the structure looks correct now. Let me run the tests again:
- Assistant
All 732 tests pass. Now let me write a test for the specific issue case to make sure it works:
- Assistant
Let me create a test to verify the fix works for the specific issue:
- Assistant
Let me add this test to the special.rs test file. Let me find the end of the file:
- Assistant
Let me add the test to the end of the special.rs file:
- Assistant
Let me look at the top of the test file to see what imports and helper functions are available:
- Assistant
Now let me add the test to the end of the file:
- Assistant
Now let me run the new test:
- Assistant
The test is almost passing but the expected output needs to be updated. Let me see the full actual output:
- Assistant
The test passed with
UPDATE_EXPECT=1which updated the expected output. Let me check what the actual output is: - Assistant
Good, the test passes and the expected output looks correct.
barandfooare both suggested as macro completions. Let me run the full test suite one more time to confirm everything passes: - Assistant
All 733 tests pass (732 existing + 1 new). Let me also run tests for related packages to make sure nothing is broken:
- Assistant
Let me also check for any compilation warnings:
- Assistant
No warnings. Let me also run the broader
idetests to make sure nothing else is broken: - Assistant
All tests pass. Let me now verify the diff of our changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.