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
Let me understand the issue:
- There's a macro
bar!()that exists - There's a macro
foo!that takes anidentand calls it as a macro - In
main(), we havefoo!(bar$0);where$0represents the cursor position
The issue seems to be about "Cannot complete macro path in macro" - i.e., when the user tries to get completions at the position
barinside the macro invocationfoo!(bar), the macro path completion doesn't work.Let me explore the repository structure first, then try to reproduce the issue and understand the codebase.
- There's a macro
Assistant
Let me explore the crate structure, focusing on completions.
Assistant
Let's look at the ide-completion crate, specifically for macro-related completions.
Assistant
Let me look at relevant files for macro completion. The
context/analysis.rslikely has macro-related completion logic.Assistant
Let me look at the relevant parts of the analysis.rs file, particularly around line 1484 and the macro call handling.
Let me also look at how completions for macro calls are triggered - specifically, how
barinsidefoo!(bar)would be resolved.Let me first look at the macro path completion context and see how it works.
Assistant
Let me look at the expr.rs completion file since
barinsidefoo!(bar)would likely be treated as an expression.Assistant
Let me search more broadly for macro-related completions.
Assistant
Let me look at the
kind_macro_callfunction around line 1484 and the surrounding context:Assistant
Let me just use bash to read the file.
Assistant
Let me look at the test infrastructure and search for existing tests related to macro completion:
Assistant
Let me look at the test around line 1185, which seems like it involves ident macro
Assistant
Let me look for the specific test scenario and try to understand the issue better. Let me look at the analysis.rs more carefully around how
kind_macro_callworksAssistant
Let me check how
foo!(bar)is parsed by looking at the parser or creating a test script.Assistant
Let me check the toolchain setup.
Assistant
There's no active toolchain. Let me check if there's a rust-version file or something in the repo to know which version to use.
Assistant
Hmm, that's a git commit. Let me check what rust version is needed.
Assistant
Let me install the Rust toolchain.
Assistant
Now let me create a simple test script to reproduce the issue. Let me first understand the completions infrastructure by looking at how tests work
Assistant
Let me try a quick compile to see the structure, while also reading the key code more carefully:
Assistant
Good, it compiles. Now let me look at the analysis.rs file more carefully to understand the parent resolution chain, especially around TokenTree handling:
Assistant
Let me look at the full match statement and verify that TokenTree is not handled. Let me also check how the
barinsidefoo!(bar)is parsed.Assistant
Let me look at the generated nodes to understand TokenTree and its relationship to paths
Assistant
Let me look at the grammar definition to understand what children TokenTree can have
Assistant
Let me look at the ungrammar file to understand the grammar structure
Assistant
Let me check the rust.ungram file for TokenTree definition
Assistant
Let me look at lines 112-120 to see the full TokenTree definition
Assistant
Let me look at the grammar around
TokenTreeMetaat line 162:Assistant
Let me look at how the code finds the path at the cursor position and how it resolves the context:
Assistant
Let me check how the parser handles the content inside token trees and what the parent of
barwould be:Assistant
Let me look at the
token_treefunction in items.rs to understand what happens when parsing the contentAssistant
Let me look at how the completion system finds the name reference at the cursor:
Assistant
Let me look at the
classify_name_reffunction at line 940Assistant
Let me look at how
classify_name_refis called:Assistant
Let me trace the speculative expansion flow more carefully:
Assistant
Let me look at the earlier part to understand how the expansion finds macro
Assistant
Let me look at the
MacroExprdefinition inAssistant
Let me see the full MacroExpr definition
Assistant
Let me look at the
expand_maybe_stopfunction:Assistant
Let me look at the
expand_maybe_stopfunctionAssistant
Let me look at how the expansion result is used and where
classify_name_refis calledAssistant
Now let me look at the
analyzefunction:Assistant
Let me look at the analyze
Assistant
Let me try to write a test to reproduce the issue. Let me look at the test infrastructure first:
Assistant
Let me look at the test fixtures and how to run tests
Assistant
Let me look at the BASE_ITEMS_FIXTURE
Assistant
Let me write a quick test to reproduce the issue. First, let me check how to run a specific test
Assistant
Good, the tests run. Let me now write a script to reproduce the issue. Let me create a test file that checks the completion for
foo!(bar$0):Assistant
Let me look at how the speculative file is parsed and what happens when the token is inside a token tree:
Assistant
Let me look at how
speculative_fileis created. Let me search for whereanalyzeis called.Assistant
Let me look at the
expand_and_analyzefunctionAssistant
Let me look at the
expand_and_analyzefunction definitionAssistant
Let me look at the
expand_and_analyzefunctionAssistant
Let me look at the calling code in context.rs to see how
speculative_fileisAssistant
Let me look at how
file_with_fake_identAssistant
Let me look at the code around line 751
Assistant
Let me check what happens with reparsing a token tree. Let me look at the
reparsefunction:Assistant
Let me take a different approach. Let me write a test to reproduce the issue and debug from there. Let me first look at the test structure more carefully and then create a reproduction test.
Assistant
Let me look at the existing test for macro path completion to see a working example:
Assistant
Let me look at the test at line 810 which tests macro completion in expression context
Assistant
Let me look at the test at line 2643 which seems
Assistant
Let me try to write and run a reproduction test. Let me add a simple test to the expression test
Assistant
Let me look at the end of the file to see where I can add a test
Assistant
Let me write a test to reproduce the issue. I'll add it at the end of the file
Assistant
Now let me run this test to see what
Assistant
I've confirmed the issue. The completions are empty. Let me remove my test and trace the code path more carefully.
Assistant
Now I understand the issue. Let me trace through the code path and implement a fix.
The issue is in the
expand_maybe_stopfunction at line 132. Whenexpandreturns None (which happens when the cursor is at a macro call path likebarinbar!()after expansion), it checksis_inside_macro_call. Since the path IS inside a macro call, it returns None. But we should actually stop at this point because we want to complete the macro path.Let me look at the code
Assistant
Now let me implement the fix. The issue is in
expand_maybe_stop: whenexpandreturns None and the token is at a macro call path, we should still return the base case instead of NoneAssistant
Let me use the edit command to modify the file
Assistant
Now let me implement the fix. I'll modify the
expand_maybe_stopfunction to handle the case where the token is at a macro call path:Assistant
Let me try using the heredoc syntax for the edit command
Assistant
Let me try using sed to make the changes
Assistant
Let me try using sed directly:
Assistant
Let me use sed to make the edit
Assistant
Let me use sed to make the change. I'll replace lines 166-170 (the else block) with the new code:
Assistant
Let me verify the edit looks correct:
Assistant
The edit has a problem - the first
Some(ExpansionResult {block lost its closing}). Let me restore the file and redo properlyAssistant
Let me verify the original state and then make the edit more
Assistant
So lines 166-170 are:
- 166:
}) - 167:
} else { - 168:
None - 169:
} - 170:
}
I need to replace lines 167-169 with the new code. Let me do this more carefully.
- 166:
Assistant
I see the problem — my replacement text is a single string with
\ncharacters, not separate lines. Let me fix this properly by reverting and redoing with properly split lines.Assistant
Let me use the
replacecommand with heredoc syntax:Assistant
I need to open the file first. Let me do that
Assistant
Now let me use the replace command:
Assistant
The edit looks correct. Let me compile and check for errors:
Assistant
Good, it compiles. Let me now add my test and run it:
Assistant
Now let me run the test:
Assistant
Progress! Completions are now appearing! The actual output is slightly different from what I expected. Let me update the test to match the actual output:
Assistant
The test passes now. Let me check the updated expected output:
Assistant
The test passes. Now let me run the full test suite to make sure I haven't broken anything:
Assistant
All 733 tests pass. Let me also check the rendering to understand the
!(…)suffix display. But first, let me think about edge cases.Let me also check: does the current fix handle the case where the expansion directly returns the macro call without needing
expand_maybe_stop? Let me think about a scenario where the fix might not work.Actually, let me also check if there's an issue with the
has_macro_bangrendering. Let me look at the rendering code:Assistant
Let me look at the rendering code for macros:
Assistant
Let me find the
labelfunction:Assistant
Let me look at the label function:
Assistant
The fix is working. Let me also think about edge cases and run the full test suite one more time
Assistant
All 733 tests pass. Let me review my changes by looking at the diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.