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 understanding the issue about macro path completion inside macros.
- Assistant
Let me search for existing tests and code related to macro path completion.
- Assistant
rg is not available, let me use grep.
- Assistant
There's a macro_def.rs - but the issue is about completing macro paths inside macro invocations. Let me look at path completions and how macros work with tt (token trees).
- Assistant
Let me look at how path completion works and how macros in token trees are handled. The issue mentions completing a path that's an argument to a macro where the metavar is
$i:identand it's used as ... - Assistant
Let me look at how completion context is determined for macros and look at similar issues or tests related to completing inside macros.
- Assistant
There's a test
complete_missing_macro_argand related special tests. Let me look at those and also understand how the path kind is determined when inside a macro. - Assistant
The issue is specifically about completing a macro path when it's used inside another macro that uses it with
!(). Let me look at how completion works inside macros and how the expected type/kind is dete... - Assistant
Let me look at how completion gets the expected context when inside macro args. There's likely some logic that looks at what fragment kinds are expected.
- Assistant
Let me look at the analysis of what happens for macro arguments and search for related issues or PR discussions. Also look at git history for similar fixes.
- Assistant
So the expansion happens via speculative expansion. When the user writes
foo!(bar), and the macro is$i:ident=>$i!(), the expansion would try to expand tobar!(). Let me try ... - Assistant
Let me look at special.rs tests related to macros and also how expression completions handle macros.
- Assistant
There's
in_macro_expr_fragtest. Let me look at that. - Assistant
I'll write a quick test to reproduce the issue and see what currently happens with completion.
- Assistant
Let me add a temporary test case to reproduce the issue.
- Assistant
Let me add a temporary test and run it. Also I should look more carefully at what analysis does when expansion succeeds for a incomplete macro path.
- Assistant
Let me add a temporary test at the end of the file.
- Assistant
I'll add a temporary test at the end.
- Assistant
There's a problem - I replaced the last closing brace but I may have broken the previous test. Looking at the file, line 1769 shows
}which is correct and I added after it. Good.But wait - the original last line was
}at line 1769, and my edit said to replace 1769:1769, so it should have replaced the last}offn_genericعمله_params_const_param_snippet- oh no, looking at the output:1768: ); 1769:} 1770: 1771:#[test]Actually it looks correct - the
}of fn_generic_params is there. Wait, looking at my edit:edit 1769:1769 << 'EOF' } #[test] ... EOFSo I replaced line 1769
}with}\n\n#[test]...which is correct.Let me run the test.
- Assistant
Need to set up rustup.
- Assistant
There's a rust-version of 1.95. Let me try running the test.
- Assistant
So completion returns nothing. This is the bug. When the macro expands to
$i!(), the path should be in a MacroCall path position. Let me dig into why completion is empty. - Assistant
When we have:
macro_rules! foo { ($i:ident) => { $i!() }; } foo!(bar$array);The expansion would be
bar!(). But wait - the completion marker replaces theidentity - when the original expands successfully and the path is a real macro, the path is consumed and disappears from the expansion. - Assistant
So the recursion:
-
Expand
foo!(bar)-> actual expands tobar!()which then becomes the new file, but wait - expand_macro_call expands one level? -
Looking at expand - if expand succeeds for foo, it recursively calls expand_maybe_stop on the expansion which is
bar!()with completion marker somehow.
But
bar!()as a MacroCall would attempt to expand further. Whenbar!()expands, the resulting tokens wouldn't contain the completion marker (since bar is completed to a real macro that expands to()), so...Looking at what happens:
// After expanding foo!(bar): // actual: bar!() // speculative: bar__ra_completion_marker__() or somethingWhen trying to expand the MacroCall
bar!(), actual expands successfully to()(no marker). Speculative might expand ifbar__ra_completion_marker__!()is also somehow valid, or fail.Actually the path identity is different - fake path might be
barra_completeor whatever the COMPLETION_MARKER is.Let me look more carefully.
-
- Assistant
So COMPLETION_MARKER is
"raCompletionMarker". The speculative expansion hasbarraCompletionMarkeras the identifier path used as$i!()so it becomesbarraCompletionMarker!(). When originalbar!()... - Assistant
Looking at
kind_macro_call, it looks at the parent of MacroCall. When MacroCall occurs... at item position of expansion? Looking at the expansion offoo!(bar)- $i!() expands somewhere? - Assistant
So
is_inside_macro_callchecks if any ancestor is MacroCall. For the tokenbarin expansionbar!(), the ancestors include Path, MacroCall - so yes, MacroCall is an ancestor! So is_inside_macro_call returns true for the path of a MacroCall itself!That means:
- Expand foo! -> get bar!()
- Trying to expand bar!() - actual expands to (), speculative fails (no barraCompletionMarker macro)
- expand returns None
- expand_maybe_stop checks is_inside_macro_call which is true for MacroCall path token
- Returns None!
- Completion fails entirely
That's the bug! When we're positioned on a macro path itself (because the identifier was used as
$i!()), we're considered "inside a macro call" and the base case returns None. - Assistant
Looking at the expand flow more carefully when we're on MacroCall path after first expansion:
After expand foos successfully:
- original_file is expansion of bar!() which is... wait,
expand_macro_callreturns the full expansion of foo which isbar!()as tokens, but are those re-parsed as MacroCall?
When expand tries again on
bar!():let orig_tt = ancestors_at_offset(&original_file.value, original_offset) .map_while(Either::<ast::TokenTree, ast::Meta>::cast) .last()?;Is the path token inside a TokenTree? In
bar!(), the path is outside the token tree. The packed MacroCall structure is:MacroCall Path (bar) bang (!) TokenTree (empty)So the cursor is on Path, and ancestors wouldn't include TokenTree. So
orig_ttwould be None, expand returns None via?.Then expand_maybe_stop:
if token_at_offset_ignore_whitespace(...).is_some_and(|original_token| { !sema.is_inside_macro_call(...) })The original_token's ancestors include MacroCall, so is_inside_macro_call is true, so we return None!
── That's confirmed as the bug.
The fix: In
expand_maybe_stop, we should also stop (accept the expansion result) when the token is ont the path of a MacroCall (not inside its arguments). Or more generally: when the token is not inside the input/token tree of a macro. - original_file is expansion of bar!() which is... wait,
- Assistant
Looking at search.rs - if we fix is_inside_macro_call to not consider the macro path itself as "inside", that matches the docs which say "inside the input of a macro call". The path is not input; the TokenTree is. Let me verify and fix it.
- Assistant
Let me look at MacroCall definition:
- Assistant
I'll fix
is_inside_macro_callso paths of macro calls aren't treated as inside the macro input. - Assistant
The edit went to the wrong file — I need to revert that and apply the fix to semantics.rs instead.
- Assistant
Good, now I can edit the right file.
- Assistant
There's an extra blank line before the next method but that's fine. Let me also check if special.rs change is still there and run the test.
- Assistant
We're making progress! Now we get completions. The macros are rendered without
!()becausehas_macro_bangis true from the expansion — which is correct for source since the bang comes from$i!(). Let me check filtering and refine the test. - Assistant
So when has_macro_bang is true, we still get all expr completions. Let me check how normal completion
foo$0!()looks: - Assistant
There's a transfer issue with has_macro_bang - when the bang is only in the expanded form but not in the original source, we shouldn't set has_macro_bang, or rather has_macro_bang meaning "bang already written in source" is wrong when bang only exists in expansion.
Wait - currently for our case: actual expansion shows has_macro_bang=true because we complete on স্
bar!()path But when editing originalfoo!(bar$0), inserting justbaris correct! Insertingbar!would be wrong for source.So the current behavior of labeling as
ma_barwithout bang and inserting just the name is CORRECT for the issueiled case. Let me verify normal completion and update the test. - Assistant
Need to open the file and go边上 that line first.
- Assistant
Let me replace the test.
- Assistant
When the prefix는 is
ba(not a complete match for an existing macro), does the actual expansion offoo!(ba)succeed? Our analysis suggests yes — let's run the tests. - Assistant
The test passes. Now let me run related tests to make sure nothing is broken - especially search tests and completion tests involving macros.
- Assistant
All ide-completion tests pass. Let me also run ide-db tests (search) and hir tests.
- Assistant
Let me also check if there are tests that specifically test is_inside_macro_call or search for references of macros. And also run ide tests related to macros.
- Assistant
All tests pass. Let me also think about more edge cases and verify searchbehavior when on tenga macro path remains correct.
- Assistant
For macro references - the Find All References test already passed. Let me review the final diff and clean up.
- Assistant
The instructions say not to add new tests — I need to remove the test I added before submitting.
- Assistant
I'll remove thetest I added and keep only the fix.
- Assistant
Good. Let me verify the fix still works with a temporary test script rather than adding a test to the repo.
- Assistant
Good, verified. Remove the test again.
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 →