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 start by understanding the issue. The issue is titled "Cannot complete macro path in macro" and provides a code snippet:
macro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The
$0indicates the cursor position. So the user is trying to completebarwhen typingfoo!(bar— this is a macro call within a macro where the$i:identparameter is being used as a macro name. The issue is that completions forbarare not working in this context.Let me explore the repository structure first to understand where the code lives.
Assistant
Let me explore the structure and find completion-related code
Assistant
The completion code is in
crates/ide-completion. Let me explore this.Assistant
Let me look at the source files in ide-completion/src.
Assistant
Let me first find relevant test files and existing tests for macro completion.
Assistant
Let me look at
item_list.rssince macro calls in expression position might be handled there, orexpression.rs. Actually, let me search for "macro" in the test files.Assistant
Let me look at where macro completions are handled. Let me search for relevant files:
Assistant
Let me look at how macro completions work in item_list context:
Assistant
Let me look at the full item_list.rs to understand the context:
Assistant
Let me search for how macro call completions are handled:
Assistant
Let me look at the
analysis.rsfile, specifically around the macro call resolution code:Assistant
Let me look at the
expand_maybe_stopfunction:Assistant
Let me look at that function.
Assistant
Let me continue viewing the expand function
Assistant
Let me view the rest of the expand function and the
Assistant
Let me look at the
classify_name_reffunction:Assistant
Let me view that function
Assistant
Let me continue viewing the classify_name_ref function
Assistant
Let me search for PathKind to understand the different path kinds:
Assistant
Let me look at lines 1320-1600 to understand path kind determination
Assistant
Let me first create a test to reproduce the issue. Let me look at the test infrastructure:
Assistant
Let me look at the test file for item_list completions:
Assistant
Let me look at how tests are structured:
Assistant
Let me look at the expression completion code and how macros are completed there:
Assistant
Let me list all completions files:
Assistant
Let me look at
expr.rs:Assistant
Let me understand the speculative expansion better. Let me look at the ide_db crate for the speculative expansion functions
Assistant
Let me look at the speculative_expand_macro_call function in the semantics file
Assistant
Let me first create a small test script to reproduce the issue:
Assistant
Let me look at the test infrastructure
Assistant
Let me look at how
completion_listworks:Assistant
Let me first create a simple test to reproduce the issue. Let me look at the completion_list function:
Assistant
Let me search for existing tests that involve macro expansions in
Assistant
Let me look at the test in special.rs for completes_in_simple_macro_call
Assistant
Now let me try to reproduce the issue by writing a test. Let me first look at how tests are structured:
Assistant
Let me first create a simple script to reproduce the issue
Assistant
Let me add a test to the special.rs test file to reproduce the issue:
Assistant
Let me look at the end of the special.rs file
Assistant
Let me add a test to reproduce the issue:
Assistant
Now let me run this test to see if it passes or fails:
Assistant
Let me check the available toolchains:
Assistant
Let me check if there's a rustc available directly:
Assistant
Let me check if cargo is available:
Assistant
Let me check the .cargo directory and any toolchain configuration
Assistant
Let me check the rust-version file and try to set up a toolchain
Assistant
Let me check if there's a toolchain binary somewhere
Assistant
Let me try to install a toolchain:
Assistant
Now let me run the test:
Assistant
The test confirms the issue - no completions are returned. Now let me understand the root cause. Let me trace through the expansion logic more carefully.
The problem is:
foo!(bar)expands tobar!(). The speculative expansion with$0produces$0bar!(). When the code tries to expand the inner macro call$0bar!(), it compares the paths and finds they differ (barvs$0bar), then returnsNone. This causes the whole completion to fail.Let me look at the specific code path:
Assistant
Now I understand the root cause. When
foo!(bar)expands tobar!(), the speculative expansion with$0produces$0bar!(). At line 389, the code compares the macro call paths (barvs$0bar) and sees they differ, so it returnsNone. This causes the entire completion to fail.The fix should be: when the paths differ and the speculative path contains the
COMPLETION_MARKER(meaning the fake identifier ended up in the macro call path), we should stop expanding and return the current expansion result instead ofNone.Let me implement this fix
Assistant
Let me find the COMPLETION_MARKER constant:
Assistant
Now let me implement the fix. The idea is: when the macro call paths differ because the fake identifier (
COMPLETION_MARKER) appears in the speculative path, we should return the current expansion result instead ofNone, because the user is trying to complete the macro call path itself.Assistant
Now let me run the test to see if the fix works:
Assistant
The
SyntaxTexttype doesn't have.contains(). Let me fix that:Assistant
Now let me run the test again:
Assistant
Let me add some debug output to trace the execution
Assistant
Let me run the test to see if the debug message is printed:
Assistant
Let me add more debug prints to trace the execution
Assistant
Let me revert the file and start fresh:
Assistant
Let me re-read the relevant section:
Assistant
Now I understand the problem better. The issue is at lines 257-262: when the expansion produces a macro call like
bar!(), and the cursor is onbar(the macro path),ancestors_at_offsetcan't find any token tree ancestors becausebaris not inside a token tree - it's the path of the macro call itself.The fix should be: when no token tree is found, check if the position is inside a macro call's path. If so, return the expansion result as-is to allow completions.
Let me look at what happens after the return and implement the fix properly.
Assistant
Let me implement the fix. The idea is: when no token tree is found, check if the position is inside a macro call's path. If so, return the expansion result as-is
Assistant
Now let me run the test:
Assistant
The completion is working now! The
barmacro is being suggested. Let me update the test to match the actual output:Assistant
The file got corrupted. Let me revert and redo properly
Assistant
Now let me add the test properly at the end of the file
Assistant
Let me append the test to the end of the file.
Assistant
Now let me run the test:
Assistant
The test passes. Let me fix the unused variable warning:
Assistant
Let me use a different approach for the edit
Assistant
Let me open the file at the right line and replace
Assistant
Now let me replace the unused variable
Assistant
Let me run the test again to confirm:
Assistant
Now let me run the full test suite to make sure I didn't break anything:
Assistant
All 733 tests pass. Let me now think about edge cases and verify my fix handles them properly.
Let me also add a few more test cases to make sure the fix is robust:
Assistant
Let me run this test:
Assistant
Let me run the full test suite one more time:
Assistant
All tests pass. Let me now verify the final state of my changes:
Assistant
The changes look good. Let me also run a broader set of 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.