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
Let me start by exploring the repository structure to understand the codebase.
Assistant
This is the rust-analyzer repository. The issue is about macro path completion in macros. Let me explore the structure more.
Assistant
Let me look at the test structure first to understand how tests are organized and find a relevant test for this issue.
Assistant
Let me look at this test more fully.
Assistant
Let me look at how check tests work and create a reproduction test.
Assistant
Let me write a quick test to reproduce the issue. Let me first look at how the existing macro completion test in expression works.
Assistant
Let me create a test to reproduce the issue first.
Assistant
Let me add a temporary test to the expression test file.
Assistant
I need to use
completion_listfrom tests. Let me modify my test to usecheckwithexpect![[r#""#]]or print. Actually, let me use thecheckapproach but print. Actually it's easier to write a test that uses check but with an empty expectation and let it fail, printing the actual. Or I can addcompletion_listto the import. Let me just usecheck_with_base_itemsstyle but print the actual.Let me rewrite the test to print the actual completion list. I'll import completion_list.
Assistant
Wait, my earlier append already added the test. Now I added another one. Let me fix this. Let me remove the broken one first.
Assistant
The ACTUAL output is empty! So no completions are returned at all. This confirms the issue. Let me investigate the completion context analysis for macro fragments.
Assistant
Let me look at how
classify_name_refhandles macro paths.Assistant
Let me understand the flow. The completion is in
foo!(bar$0). The macrofooexpands$i!()tobar!(). So after expansion, the cursor is at thebarident which is the path of a macro callbar!().Wait, but the actual issue is whether the expansion succeeds. Let me check if the expansion is the problem. The expansion of
foo!(bar)should givebar!(). But wait — when completing, the user has typedfoo!(bar$0). The fake ident insertion would produce... Let me look at how the fake ident is inserted.Actually let me re-look. The cursor is at
foo!(bar$0). Thebaris already typed. So the original token isbar. The speculative file inserts a fake ident. Let me checkcontext.rsaround line 748-770.Assistant
Let me check
is_inside_macro_call.Assistant
Let me check the callers of
is_inside_macro_callto understand the intended semantics.Assistant
Let me add debug prints to confirm my hypothesis about the flow.
Assistant
Let me add a debug print to confirm the flow.
Assistant
Confirmed. The first check (
barat offset 98-101 in thebar!()expansion) is the problem - the pathbarofbar!()is being treated as inside the macro call. Let me revert the debug print and fixis_inside_macro_call.Assistant
Now let me look at the
is_inside_macro_callimplementation again and fix it.Assistant
Let me check tests related to
is_inside_macro_calland search behavior to ensure I don't break anything.Assistant
Let me look at how
find_nodesis used and the broader search context.Assistant
This is for the "search for usage" feature (find references). When searching for
bar, it finds textual matches. For each match token, if it's inside a macro call, it descends into macros to find the actual reference node in the expansion. Otherwise, it uses the token's parent directly.Assistant
Let me look at the original commit that introduced
is_inside_macro_call.Assistant
So
is_inside_macro_callwas originally a syntactic check introduced as an optimization for search. The MacroCall check was a syntactic heuristic. Later (commit 87529e8631) it was "Properly implementmight_be_inside_macro_call()using semantic information". Let me check that.Actually wait, there are two functions:
is_inside_macro_callandmight_be_inside_macro_call. Let me check.Assistant
Now I understand the history. The
MacroCallcheck was a syntactic heuristic. The intent is to check if the token is inside a macro call's input (token tree), not its path. Let me implement the fix.Assistant
ast::TokenTreeis available. Let me check ifast::TokenTree::can_castandparent()work. SyntaxNode hasparent(). Let me check whatancestoris in the loop — it's a SyntaxNode (fromparent_ancestors()). Let me verify.value.parent_ancestors()returnsSyntaxNodeiterator. Soancestoris aSyntaxNode.ast::MacroCall::can_cast(ancestor.kind())works.ast::TokenTree::can_cast(ancestor.kind())works.ancestor.parent()returnsOption<SyntaxNode>.Let me implement.
Assistant
Now we get completions. Let me see the full output.
Assistant
Now
barandfoomacros are offered. The completion works. Now let me remove the temporary test and add a proper test, then run existing tests. First, let me remove the temp test.Assistant
Now let me run the full ide-completion test suite to check for regressions.
Assistant
All 732 completion tests pass. Now let me run the search/find-references tests, which use
is_inside_macro_callinsearch.rs.Assistant
Let me run the find-references tests which are likely in the
idecrate.Assistant
Let me run the rename tests too, since rename uses search.
Assistant
All rename tests pass. Let me run the broader
ideandhirtest suites.Assistant
Now let me add a proper test. Let me find a good place. Let me look for existing tests with
$i!()patterns.Assistant
Let me look at the
special.rsmacro segment tests.Assistant
Let me look at the
completes_macro_segmenttest.Assistant
Let me capture the
check_no_kwoutput for a cleaner test. Let me add a temporary test.Assistant
Let me also verify
check_editworks for the issue scenario.Assistant
check_editworks. Now let me test the exact issue scenario with cursor right after the fullbartext.Assistant
The exact issue scenario works. Now let me write the final test. Let me remove the temp test and add a proper one.
Assistant
Let me check the imports at the top of expression.rs to see what's available (check, check_edit, check_no_kw).
Assistant
checkandcheck_editare imported.check_no_kwis not. Let me check tests.rs forcheck_no_kw- it exists. Let me add it to the import. Actually, I can usecheckwhich includes keywords, but that makes the expected output noisy. Let me usecheckfor consistency with the macro tests in expression.rs, OR add to imports.Assistant
The test passes with the exact expected output. Now let me run a broader set of tests to make sure the
is_inside_macro_callchange doesn't break assists, SSR, or diagnostics which also use search/descend.Assistant
All pass. Let me run the full ide-completion suite once more to confirm everything passes with the new test included.
Assistant
733 tests pass now (was 732 + my new test). Let me review my final diff.
Assistant
Let me verify there's a references test for macros specifically, to make sure macro call-site references are still found correctly with the changed
is_inside_macro_call.Assistant
This test
test_find_all_refs_macro_defsearches for references to macrom1and expects the call sites at63..65and73..75(them1path tokens inm1()). Let me run it specifically to confirm it still passes with my change.Assistant
All macro-related references tests pass. Let me do a final build check for warnings.
Assistant
No warnings. The fix is complete and minimal.
Let me do a final review of the complete diff before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.