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); }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
The issue is about "Cannot complete macro path in macro". Let me understand the problem:
Given this code:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }When the cursor is at
$0(afterbar), rust-analyzer should be able to complete the macro call inside another macro. The issue is that macro path completion doesn't work inside macro calls.Let me explore the repository structure first to understand how it's organized.
Assistant
Let me explore the crates directory to find the completion-related code.
Assistant
The issue is about completions, so let me look at the
ide-completioncrateAssistant
Let me look at completions related to macros to understand the code.
Assistant
Let me look at the macro-related completions:
Assistant
That's about macro segments (like
$i:ident). Let me look at the item_list.rs for macro call completions.Assistant
Let me look at
item_list.rsto understand how completions work for macro calls:Assistant
Let me look at how completions are dispatched in `completions
Assistant
Let me look at the context module to understand how the completion context is
Assistant
Let me look at the main context.rs file:
Assistant
Let me look at the key parts of context.rs - particularly macro-related logic
Assistant
Let me look at the context.rs file, particularly around the ItemListKind determination
Assistant
Let me look at the analysis module and the earlier parts of context.rs:
Assistant
Let me search for where PathKind::Item is determined:
Assistant
Let me look at the area around line 1450 where PathKind::Item is determined
Assistant
Let me look at how paths inside macro calls are handled:
Assistant
Let me search for TokenTree handling in the analysis code
Assistant
Let me look at the TokenTree handling around line 505:
Assistant
Let me look at the
classify_name_refAssistant
Let me look at
classify_name_ref:Assistant
Let me look at the AST definition for TokenTree
Assistant
Let me look at existing tests for macro completions
Assistant
Let me search for tests related to completing paths inside macro calls:
Assistant
Let me escape the search better.
Assistant
Let me look at the test for completing in simple macro calls
Assistant
Let me look at the TokenTree definition in the generated nodes:
Assistant
Let me look at the expand function handling of macro calls:
Assistant
Let me see the full expand function:
Assistant
Let me look at the complete match arms in classify_name_ref to understand exactly where TokenTree needs to be handled:
Assistant
Let me create a test script to understand the AST structure:
Assistant
Let me look for existing tests that complete inside macro calls and check what's tested
Assistant
Let me escape the parentheses:
Assistant
Let me look at the test at line 375 in special.rs and also run the existing completion tests to see what currently works
Assistant
Let me look at how macro completions are currently tested and see what's working
Assistant
Let me look at
make_path_kind_expr:Assistant
Let me look at
make_path_kind_exprat line 1326Assistant
Let me check the MacroCall AST definition:
Assistant
Let me look at the MacroCall definition more closely:
Assistant
Now I have a clear understanding of the issue. Let me first look at the full
classify_name_reffunction to understand the complete flowAssistant
Now I understand the issue clearly. The problem is in
classify_name_refincrates/ide-completion/src/context/analysis.rs. When a path likebaris inside a macro call's token tree (e.g.,foo!(bar)), the path's parent is aTokenTreenode. The match arms in the path kind inference don't handleTokenTree, so it falls through to_ => return None.The fix is to add
ast::TokenTree(_)as a match arm in both the outer and inner match statements, walking up ancestors to find the enclosingMacroCalland usingkind_macro_callto determine the context.Let me open the file and make the edits
Assistant
Let me find the exact line numbers for the outer match
Assistant
Let me look at both
_ => return Nonein context for the outer and inner matchAssistant
Now I'll make the edits. First, let me edit the inner match (around line 1606
Assistant
I see there's a duplicate
ast::RecordExprline. Let me fix that:Assistant
Let me remove the duplicate line
Assistant
Let me fix the duplicate by replacing both lines
Assistant
Good, the inner match is fixed. Now let me add the outer match's TokenTree handling. Let me look at the outer match
Assistant
Now let me add the TokenTree handling in the outer match
Assistant
Now let me verify the changes look correct by viewing the full modified region
Assistant
Now let me try to build the project to verify the changes compile
Assistant
Let me check the rust version available
Assistant
Let me check if there's a rust-version file or cargo available
Assistant
Let me check if rustc is available and set up
Assistant
Let me check installed toolchains
Assistant
Let me install a stable toolchain first
Assistant
Let me try cargo check again
Assistant
The code compiles. Now let me run the completion tests to verify nothing is broken
Assistant
All 732 tests passed. Now let me create a test to verify the fix works for the specific case. Let me first look at how similar tests are structured
Assistant
Let me look at the test utilities
Assistant
Let me look at the test helpers and write a quick script to verify the
Assistant
Let me create a quick test to verify the fix works by writing a small test
Assistant
Let me instead write a standalone test to verify the fix
Assistant
Let me also handle TokenTree in
kind_macro_callfor nested macro calls. Let me look at thekind_macro_calldefinitionAssistant
Let me also add TokenTree handling in
kind_macro_callfor nested macro callsAssistant
There are extra lines from the edit. Let me fix them
Assistant
Let me verify the full modified file looks correct and compile
Assistant
Good. Let me also check the inner match
Assistant
Let me compile and test again
Assistant
I need to fix the recursive call issue. Let me restructure the TokenTree handling in
kind_macro_callto avoid recursionAssistant
Let me compile and test
Assistant
Now let me run the
Assistant
All tests pass. Let me now run the broader test suite to make sure nothing is
Assistant
Let me verify there aren't other places in the codebase that might need similar TokenTree handling
Assistant
Let me check if other match_ast blocks at lines 884, 906, 1078 might also need TokenTree handling
Assistant
These are for lifetimes and names, not path contexts. They don't need TokenTree handling.
Now let me check the match_ast at line 1078:
Assistant
These are helper closures, not path parent matching. Let me also check lines 1260 and 1279:
Assistant
These are for type locations, not path parent matching. Let me check line 1370:
Assistant
Good. All the other match_ast blocks are for different purposes (type locations, self params, etc.) and don't need TokenTree handling.
Let me do a final review of all changes made:
Assistant
The changes look clean and correct. Let me run the full test suite one more time:
Assistant
All 732 tests pass. Let me also run the ide crate tests to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.