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); }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!
Assistant
I'll start by exploring the repository structure to understand the codebase and find the relevant code for macro path completion.
Assistant
This is rust-analyzer. Let me explore the structure and find the completion code for macros.
Assistant
The issue is about macro path completion in a macro. Let me look at the macro completion code. The code snippet shows:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }So when completing a macro path inside a macro invocation (the
$itoken in the macro body), rust-analyzer cannot complete macro names.Let me look at the relevant completion files. The key thing here is that
bar$0is being typed inside a macro callfoo!(bar$0)wherefoo!expands to$i!(), so the$itoken is being expanded. The cursor is atbar$0which is a path that needs to be completed with macro names.Let me look at the completion code for macro references/paths.
Assistant
That's not the right file. Let me look at the expr.rs since the macro call is inside a function body, which is an expression context.
Assistant
Now let me understand the completion context analysis. Let me look at the analysis.rs in the context directory.
Assistant
Let me look at the expand function more closely.
Assistant
Let me continue to look at the macro call expansion section.
Assistant
Let me look at the analyze function and how it identifies the path context.
Assistant
Let me continue to look at the analyze function to see how it handles the path context.
Assistant
Let me look at the
classify_name_reffunction which handles the path context.Assistant
Let me look at the macro call handling section in classify_name_ref.
Assistant
Let me look for where
ttsis defined in the expand function.Assistant
Let me look at the
ancestors_at_offsetfunction.Assistant
ancestors_at_offsetis from the syntax crate. Let me check its definition.Assistant
Let me look at the existing completion tests for macros to understand the test format.
Assistant
Let me look at the test around line 1185 which deals with
$i::bar.Assistant
Let me look at how the
checkfunction works and create a reproduction test.Assistant
Let me look at the
completion_listfunction to understand the test harness.Assistant
Now let me create a reproduction test. I'll add a temporary test to the expression.rs file to observe the current behavior.
Assistant
Let me add a temporary test at the end of the expression.rs file to reproduce the issue.
Assistant
Now let me run this test to see the actual completions (the empty expect will fail and show the actual).
Assistant
There's no rust toolchain configured. Let me check what's available. The issue mentions rustc 1.97.0-nightly. Let me look for the actual rustc binaries.
Assistant
OK so there's a toolchain at 1.96.0. Let me check if rust-analyzer builds with this version. The issue mentions nightly 1.97.0. Let me check the rust-version file. It said
ba0949ab745985a442e274ba52e8fb348cb0c662which seems weird. Let me check it more carefully.Assistant
The rust-version is "1.95" and we have 1.96.0, so that should be fine. Let me set up the toolchain and try to run the test.
Assistant
The test passed with empty expect, confirming NO completions are returned. Now let me investigate the root cause. Let me look at
is_inside_macro_call.Assistant
The test passed with empty expect, confirming NO completions are returned. Now let me investigate the root cause. Let me look at the git history of the relevant code.
Assistant
Let me add some debug output to understand the flow. First, let me add eprintln debugging to
expand_maybe_stop.Assistant
Let me add temporary debug output to understand the structure.
Assistant
Now I understand the flow. Let me check all usages of
is_inside_macro_call.Assistant
Let me look at the search.rs usage to understand the impact of changing
is_inside_macro_call.Assistant
Let me look at the git history of this base case check to understand its intent.
Assistant
Interesting. There was a previous
might_be_inside_macro_call. Let me look at that commit and the function.Assistant
Let me look at the tests added in that commit to understand the expected behavior.
Assistant
Let me check all current usages of
is_inside_macro_callmore thoroughly.Assistant
Let me look at the commit that added the recursion stop condition to understand the intent.
Assistant
Now I understand the design. The base case stops when "not inside a macro call". But
is_inside_macro_callincorrectly counts the path of a macro call as "inside" — its docstring says "we strictly check if it lies inside the input of a macro call", and the path is NOT the input. Let me first revert my debug patch, then make the real fix.Assistant
Now let me look at the
is_inside_macro_callimplementation once more.Assistant
Now I understand the design and the bug. The
is_inside_macro_calldocstring says "we strictly check if it lies inside the input of a macro call", but the implementation returnstruefor the macro path (e.g.barinbar!()), which is NOT the input. This causes the completion's recursion base case to incorrectly abandon when the cursor lands on a macro path inside an expansion.Let me look at the exact code once more and implement the fix.
Assistant
astandAstNodeare imported. Now let me implement the fix tois_inside_macro_call.Assistant
Now let me run the reproduction test to see what completions are now produced.
Assistant
parent_ancestorsis for tokens; for nodes I need.ancestors().skip(1). Let me fix.Assistant
Now let me rebuild and run the reproduction test.
Assistant
The fix works! Now
barandfoomacros are completed. Let me update the temporary test with the correct expected output to verify it passes.Assistant
The test passes. Let me check the warnings to clean them up.
Assistant
The 4 warnings are pre-existing (at lines 1043, 1312, 1370, 1407 — not my line 1096, which operates on a
SyntaxNode, not aSyntaxToken). Let me confirm these are pre-existing.Assistant
The original has 0 deprecation warnings but my change shows 4. Let me investigate the actual lines flagged.
Assistant
Let me check whether these
.ancestors().skip(1)lines are part of my change or pre-existing.Assistant
My
sedwas too broad — it replaced.parent_ancestors()in 4 other places (onSyntaxToken, where it's the correct non-deprecated method). Let me revert the file and re-apply only my intended change carefully.Assistant
Now let me re-apply only my intended change using the precise
editcommand.Assistant
I created a duplicate block. Let me fix it cleanly — I'll replace lines 1094-1108 to remove the duplication and use the clean node
.ancestors().skip(1).Assistant
Let me consolidate the redundant comments.
Assistant
The edit left duplicated lines again. Let me replace the entire block from 1090 to 1106 cleanly.
Assistant
The edit tool keeps leaving duplicates. Let me revert and use a Python script for precise replacement.
Assistant
Now the function is clean. Let me build and run the reproduction test.
Assistant
No deprecation warnings and the test passes. Now let me run the full ide-completion test suite to check for regressions.
Assistant
All 733 ide-completion tests pass. Now let me run the ide-db tests (which use
is_inside_macro_callin search) and related crates.Assistant
All ide-db tests pass. Let me run the broader
idecrate tests (find references, search, highlight, etc.) which are most likely affected by search behavior changes.Assistant
All 1304 ide tests pass. Let me run the ide-ssr and hir tests too.
Assistant
Let me check each crate's summary separately.
Assistant
Let me run ide-ssr with all test targets to be thorough, since the commit modified ide-ssr.
Assistant
All tests pass. Let me verify the edit functionality and a few more edge cases with temporary tests.
Assistant
The edit test failed. Let me look at the edit test result specifically.
Assistant
The edit added
!()(producingfoo!(bar!())). Let me check how macro completion edits normally work to understand if this!()addition is expected behavior.Assistant
The edit added
!()which would be incorrect here (it should just complete the namebarsince the!()is added by thefoo!macro). Let me investigate whyhas_macro_bangappears to be false. Let me add a temporary debug print inkind_macro_call.Assistant
has_macro_bangis true, soneeds_bangshould be false — yet a bang was added. Let me check how the completionsource_rangeand edit application work for expanded completions. Let me look atcheck_editandsource_range.Assistant
Let me look at how
check_editpicks and applies the completion item.Assistant
Let me revert the debug and write a focused debug test that prints all completion items with their lookup and edits.
Assistant
Interesting — the
baritem inserts justbar(no bang). Let me re-run the edit test cleanly now (the analysis.rs debug is reverted) to recheck.Assistant
The item inserts just
barperget_all_items, yetcheck_editproducesbar!(). Let me replicatecheck_editlogic in the temp test to see the actual item and edits being applied.Assistant
Let me check the current state of the temp edit test.
Assistant
I see — the expected was already
foo!(bar!()). Let me directly rewrite the edit test to debug the actual edit application, and also run it as-is to confirm.Assistant
It still fails, so the actual isn't
foo!(bar!())either. Let me see the full diff.Assistant
Let me look at the exact test source bytes.
Assistant
This is confusing. Let me look at
assert_eq_textto understand the Left/Right convention.Assistant
I had the Left/Right convention backwards!
assert_eq_text!(expected, actual)→ Left=expected, Right=actual. So the actual result isfoo!(bar);— which is CORRECT (just completes the name, no spurious!()). My temp test's expected was simply wrong. Let me fix it and verify.Assistant
The edit test passes. Now let me handle the
with_argstemp test — let me capture its actual output.Assistant
All scenarios work. Now let me clean up by removing all my temporary tests. First, let me confirm the only changes to expression.rs are my temporary additions.
Assistant
The only changes to expression.rs are my temporary additions. Let me revert it entirely.
Assistant
Now only
semantics.rsis modified. Let me review the final diff.Assistant
The diff is clean and minimal. Now let me run the key test suites once more with the final clean state to confirm everything passes.
Assistant
All pass. Let me also run ide-ssr, hir, and hir-def to be thorough.
Assistant
All tests pass. Let me do a final build check for warnings and verify the issue scenario one more time with a temporary test.
Assistant
No warnings. Let me do a final confirmation of the exact issue scenario with a temporary test, then remove it.
Assistant
The fix works for the exact issue scenario. Now let me remove the temporary test.
Assistant
The final diff is clean and minimal. Let me run the find-references / search-related tests specifically, since search is the other consumer of
is_inside_macro_calland the most likely area for subtle regressions.Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.