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); }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!
(Current directory: /rust-analyzer, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure and searching for related completion/macro code to understand how path completion works inside macros.
- Assistant
I'll search the completion code and tests for macro-related path completion behavior.
- Assistant
The bang caused an issue with bash history expansion. Let me escape properly.
- Assistant
rg is not found. Let me use grep -r instead.
- Assistant
Let me dig into expression completion and macro-related path handling.
- Assistant
The analysis layer looks central — I'll inspect how it handles macro args and path completion.
- Assistant
Next I'll look for how expected types and name-ref kinds treat macros, especially inside macro arguments.
- Assistant
I'll inspect how completion tests are written, then try to reproduce the failing case.
- Assistant
Let me search for similar tests about completing inside macros, and also look for how speculative expansion works with ident metavariables used as macros.
- Assistant
Bash history expansion is annoying with
!. Let me use single quotes. - Assistant
There's a similar test around line 1185 in expression.rs. Let me look at those and the special.rs completes_in_simple_macro_call.
- Assistant
I'll add a temporary test for this case and run it to confirm the failure.
- Assistant
There's no target directory. Building the whole rust-analyzer might take a long time. Let me look more carefully at the code to understand the issue first, and maybe check GIT history for related issu...
- Assistant
I need to see how the completion marker and speculative expansion interact with nested macros.
- Assistant
Let me search for COMPLETION_MARKER definition and how speculative files are prepared.
- Assistant
Checking what
is_inside_macro_callmeans and how nested expansion behaves when the expansion result is a macro call. - Assistant
Let me read that function.
- Assistant
Found the likely bug: when completing a macro path produced by expansion, path mismatch aborts expansion incorrectly. Checking related code and history.
- Assistant
Looking at expand_maybe_stop more carefully: I see the core issue now. The
expand_maybe_stopfunction is designed to stop expansion when it hits a macro call that isn't part of a path, but it's failing here because:- The nested macro call
$i!()is being treated as part of a path (viais_inside_macro_call true) - The completion marker creates a path difference (
mac_call_path0 != mac_call_path1) - This causes
expand_maybe_stopto returnNoneeven when we're completing the macro path itself
The key insight is that when the paths differ due to the completion marker, we should treat that as the base case - we're completing the macro name, so we should stop expanding and use the current expansion state.
Let me think about the fix:
We need to modify the condition in
expand_maybe_stopto distinguish between:- When paths differ because of the completion marker (which should be the base case - stop here)
- When paths differ for other reasons (which should still return None)
The current logic is trying to handle cases where macros expand to something that doesn't include our completion marker, but in this nested case, the completion marker is on the path of the inner macro call, which is exactly what we want to complete.
So the fix should be to check specifically for the path difference case and treat it as a successful base case:
if mac_call_path0 != mac_call_path1 { // This is the completion marker case - we're completing the macro path itself // Return a proper ExpansionResult instead of None (or modify the base case logic) return Some(ExpansionResult { original_file: original_file.value, speculative_file, original_offset, speculative_offset: fake_ident_token.text_range().start(), fake_ident_token, derive_ctx: None, }); }But looking at the structure,
expand_maybe_stopis called recursively, and the base case is only triggered when we hit a non-macro-call context. Since the path difference indicates we're completing the macro name, we should stop expansion here. - The nested macro call
- Assistant
Looking at another approach - when path0 != path1, we're likely completing the macro path. In that case, we should stop expanding at this level and use the current context.
- Assistant
I've confirmed the bug: after expanding into
$i!(), the base case rejects the location because the token is treated as inside a macro call. I'll implement a fix and verify with a test. - Assistant
I'll apply the base-case fix and add a regression test for macro path completion.
- Assistant
I'll navigate to the stretch of code I need to change, then apply the patch.
- Assistant
Now I can use the edit command with line range.
- Assistant
Next I'll drop a regression test into
special.rsnear the other macro-completion cases. - Assistant
Let me check the imports at the top of special.rs
- Assistant
I'll add a test after completes_in_simple_macro_call.
- Assistant
I need to remove that in-repo test and use a temporary script instead, per the instructions.
- Assistant
I accidentally left a duplicated function header — fixing that next.
- Assistant
I'll restore the test file from git and keep only the analysis fix.
- Assistant
I'll run the related completion tests next to confirm nothing regressed.
- Assistant
Need to set up rustup toolchain.
- Assistant
There's a toolchain 1.96.0 installed but not leasesas default. And rust-versionjest has acommit hash which is odd.
- Assistant
Let me set the defaulttoolchain to 1.96:0
- Assistant
I'll add a temporary test, run it, then remove it afterward.
- Assistant
I'll insert a temporary test, run it, then strip it before submit.
- Assistant
Compiling and running the temporary regression test now.
- Assistant
Macros are showing up now. I'll check what normal top-level
ba$0!()completion looks like for comparison. - Assistant
Let me run the test again and also check check_edit for what gets inserted.
- Assistant
Both cases match. Next I'll verify the completion edit inserts just the macro name.
- Assistant
There's a little issue - if has_macro_bang is true, the completion might try to cn insert just the name. But the source doesn't have
!- the has_macro_bang is about the expanded trees. There could b... - Assistant
I'll run a few related macro-completion tests to check for regressions.
- Assistant
Checking how prefix filtering works in these tests to ensure the expectations are accurate.
- Assistant
Checking how completion lists are filtered and rendered in tests.
- Assistant
I'll add a couple more temporary edge-case checks, then clean the temp test out.
- Assistant
The edit landed in the wrong file — I'll clean that up and put the temp test back in special.rs.
- Assistant
Good. Now let me add the temp test to special.rs only and run it carefully.
- Assistant
I'll craft a temp test in a separate file under /tmp or use crate's tests carefully. Actually the simplest is to put temp test in special.rs, run all space variants by fixing the expected values after...
- Assistant
Interesting about the empty-input case - with
$0only, the speculative isinsert COMPLETION_MARKER which becomes the path. For original with empty, what doesfoo!()expand to?`anging - Assistant
Empty input already offers macros; I'll adjust the expectations and re-run.
- Assistant
Regardless - actual shows macros without bang which means path with bang. Fix works for empty too somehow.
- Assistant
Just a whitespace alignment issue — the tt-muncher case is fine.
- Assistant
Also need to fix empty caso - why does it have has_macro_bang without bang in labels? If first cases pass and empty was only wrong about harjo bang... fixed already_labels to no bang.
- Assistant
I'll temporarily revert the fix to confirm the original failure, then restore it.
- Assistant
The stash also stashed the test. Let me pop and do a more careful check - maybe manually git apply only analysis change.
- Assistant
Confirmed: without the fix, completion is empty. Restoring the fix and cleaning up the temporary test.
- Assistant
Running a broader set of ide-completion tests next.
- Assistant
All 732 tests passed. Let me do a final review of the fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →