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
Let's add a temporary test to reproduce the issue, run it, then remove afterward.
Assistant
That's downloading things but seems it worked already (cached). Let's build/test now.
Assistant
Good, reproduced the issue - no completions shown. Now let's dig into the analysis logic to understand why.
Assistant
Now let's build & run the test.
Assistant
Also let's add debug prints at the "parent" of path.parent() call before segment matching to see what kind of parent node we get overall.
Assistant
Good, that file's diff is now empty (only the semantics.rs change remains). Now let's remove the temp tests added earlier.
Assistant
Now let's run the full test suites for the affected crates to check for regressions.
Assistant
All 732 existing ide-completion tests pass. Now let's run tests for ide-db (search.rs) and hir-expand crates too, and broader tests that might use
is_inside_macro_call.Assistant
All pass. Now let's also run ide (find_all_refs uses search.rs) and hir-expand tests, plus a broader check.
Assistant
All pass. Let's run the complete workspace test suite to be thorough (or at least the significant crates), then check final diff.
Assistant
Let's check for any failures in the full run by grepping "FAILED" or "test result: FAILED".
Assistant
All tests pass across the entire workspace. Let's view the final diff.
Assistant
Let's do a final sanity check with the exact issue reproduction, then clean up.
Assistant
The fix confirms completions now appear (including
ma barandma foo), resolving the reported issue. The test file has been reverted to its original state.Assistant
Only the intended fix remains as a diff. Let's go through the checklist.
Checklist Review
1. If an error was reported in the issue description, I have successfully reproduced it. Yes — I created a temporary test replicating the exact snippet from the issue (
foo!(bar$0)wherebarshould be completable as a macro path sincefoo!'s expansion turns it into$i!()). Before the fix, this produced zero completions, confirming the bug.2. If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes — after applying the fix to
Semantics::is_inside_macro_callincrates/hir/src/semantics.rs, the same reproduction now yields completions includingma bar(the macro) and other applicable expression-position completions, matching the behavior of an equivalent non-nested macro-call-name completion scenario I used as a cross-check (bar$0!();). This confirms the fix generalizes correctly rather than just patching the exact reported case.Root cause & fix explanation: The completion engine speculatively expands macros recursively to find where the cursor lands. When it reaches a point where further textual/token-tree expansion isn't possible, it falls back to a "base case" only if the current token is not considered "inside a macro call" (per doc comment: strictly inside the macro's input). However, the original implementation of
is_inside_macro_calltreated any descendant of aMacroCallnode as "inside" it — including the macro's own path/name segment, the!token, and the trailing;— not just its arguments (token_tree). In the issue's scenario, after expandingfoo!(bar)intobar!(), the completion cursor lands exactly on thebarpath segment of the new macro callbar!()itself (not inside its arguments, which are empty). This token is a descendant of aMacroCallnode, so the old check wrongly reported "inside a macro call" and refused to use the fallback, causing the whole completion analysis to bail out with no results. I fixed this by restricting the check to specifically whether the token lies within the macro call'stoken_tree(its actual "input"/arguments), consistent with the existing doc comment's stated intent.3. I have thought about edge cases and made sure my fix handles them as well. Yes — I considered:
- Macro calls with empty arguments (
bar!()) — the exact failing case, now fixed. - Macro calls with non-empty arguments — still correctly detected as "inside" since the token would fall within the
token_treerange. - The other caller of
is_inside_macro_callincrates/ide-db/src/search.rs(used for reference search to decide whether to descend into macro expansions) — verified via the fullide-dbtest suite that this didn't regress; semantically this call site benefits from the same, more precise interpretation (a macro's own invocation name token doesn't need macro-descent resolution, only extra care for identifiers that are literal macro-call arguments). - Attribute/derive macros go through a different branch (
ast::Item::cast) unaffected by this change. - Ran the exact non-nested macro-call-name completion scenario to confirm consistent behavior instead of an arbitrarily different one.
4. I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. Yes — ran
cargo test -p ide-completion(732 passed),cargo test -p ide-db(196 passed),cargo test -p ide(1304 passed),cargo test -p hir, and finallycargo test --workspace, confirming no failures anywhere in the repository after the change.All temporary debug prints and test additions were removed; only the minimal source fix remains in the diff.
- Macro calls with empty arguments (
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 →
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 →