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); }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 me understand the issue. The problem is about "Cannot complete macro path in macro". Looking at the code snippet:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }So when the cursor is at
$0afterbarinsidefoo!(bar), rust-analyzer should be able to complete the macro pathbar(which is a macro). But the issue says "Cannot complete macro path in macro" - meaning completion doesn't work for macro names used inside macro invocations.Let me explore the repository structure first to understand the codebase.
Assistant
Let me explore the relevant crates for code completion.
Assistant
Let me look at the ide-completion crate, particularly around macro completion.
Assistant
Let me look for macro-related completion files.
Assistant
Let me look at how completions are triggered and how macro paths are completed. Let me first check the context module
Assistant
Let me look at the analysis context to understand how completions determine if the cursor is in a macro.
Assistant
Let me continue reading the expand function, particularly the part about macro_call token trees
Assistant
Let me look at lines around 256-400 to see the macro call expansion logic.
Assistant
Let me use grep to show the lines around 256 in the file.
Assistant
Let me look at how completions are dispatched and what kinds of completions exist for macros
Assistant
Let me look at how
complete_expr_pathhandles macro paths inAssistant
Let me look at the analyze function to see how it determines the context for completions, especially for paths.
Assistant
Let me look at the
classify_name_reffunction more closely.Assistant
Let me use sed to view the relevant sections.
Assistant
Let me search for where the path kind is determined, especially around macro calls.
Assistant
Let me search for related test cases and understand how completion of macros works in general
Assistant
Let me look at the item.rs tests for macro
Assistant
Let me first create a simple test to reproduce the issue. Let me find a test file to look at how tests are written
Assistant
Let me look at the tests module
Assistant
Let me understand the AST structure for token trees inside macro calls. Let me look at the syntax types
Assistant
Let me try to reproduce the issue with a test. First, let me look at the expression test file.
Assistant
Let me write a small Rust script to test the completion behavior. Actually, let me look at how tests are run and try to run the existing tests first
Assistant
Let me set up the Rust toolchain first
Assistant
Let me try a simpler approach - I'll look at how other tests handle completion inside macros, and also look at the
expandfunction more carefully to understand the issue.Let me look at the
expandfunction at lines 256-450 more carefully, particularly the case where the actual expansion succeeds but the speculative oneAssistant
Now I have a good understanding of the issue. Let me trace through the code flow:
When we try to complete
barinsidefoo!(bar), the expansion system tries to expandfoo!(bar)→bar!()(actual) andfoo!(bar$0)→bar$0!()(speculative). The innerbar$0!()fails to expand sincebar$0is not a macro, so we fall back to the unexpanded state.In the unexpanded state,
classify_name_reflooks at the parent of the pathbar. The parent is aTokenTree, not aMacroCall. But the match statement inclassify_name_refdoesn't handleTokenTreeas a parent, so it returnsNoneand no completions are generated.Let me verify by checking the AST structure:
Assistant
Let me confirm by searching for
MacroCallin the generated nodesAssistant
Let me look at the exact code in classify_name_ref where it matches the parent path. Let me search for the exact match arms
Assistant
Now I understand the issue clearly. In
classify_name_ref, when a path likebaris inside a macro invocationfoo!(bar), the parent of the path is aTokenTree, not aMacroCall. The match arms don't handleTokenTree, so it falls through to_ => return Noneand no completions are generated.The fix should handle
ast::TokenTreeas a parent and delegate to the MacroCall parent of the TokenTree.Let me look at the exact area I need to modify
Assistant
Now let me also check the case when the parent is a
Path(qualified path) to see if there's also a missingTokenTreearmAssistant
Let me also search for "TokenTree" in the analysis.rs file to see if it's handled elsewhere
Assistant
Now I have a clear understanding. The fix should add a
TokenTreematch arm inclassify_name_ref. When the parent of the path is aTokenTree, we need to find the enclosingMacroCalland usekind_macro_callto determine the path context.Let me implement this
Assistant
Now let me implement the fix. I need to add
TokenTreehandling in two places inclassify_name_ref. Let me first modify the outer match:Assistant
Let me use replace to make the change
Assistant
Let me navigate to the right location
Assistant
Let me try the open command directly.
Assistant
Let me use sed to add the TokenTree handling. First, for the outer match:
Assistant
Let me verify the change
Assistant
Now let me add the same TokenTree handling in the inner match (line 1606):
Assistant
Let me verify the changes
Assistant
Let me try to compile to check for errors
Assistant
Let me check how
add_path_resolutionrenders macros, specifically if it adds the!suffixAssistant
Let me look at
render_path_resolutionto see how it handles macros.Assistant
Let me look at
render_path_resolutionin render.rs.Assistant
Let me look at
render_resolution_pathAssistant
Let me look at that function
Assistant
Let me look at the expression tests to see how macros are handled in expression position
Assistant
Let me look at the tests around line 50
Assistant
Let me check what
is_fn_likedoes for macrosAssistant
Let me check both definitions
Assistant
Let me also check the other one
Assistant
Let me check what macro kinds exist
Assistant
Let me look at the definition
Assistant
Let me now run the existing completion tests to make sure my changes don't break anything
Assistant
All 732 tests pass. Now let me also run the integration tests to make sure nothing is broken at a higher level
Assistant
I need to also handle nested TokenTrees. Let me update the fix to walk up through TokenTree parents to find the MacroCall.
Assistant
Let me check the current state of my changes
Assistant
Let me look at both TokenTree blocks
Assistant
Let me fix both TokenTree handlers to walk up through nested TokenTrees
Assistant
Let me check the result
Assistant
Let me check the second one
Assistant
Let me verify
Assistant
Let me run the tests
Assistant
Let me look at how expand_and_analyze is called and how the original file is obtained
Assistant
Let me search in the file
Assistant
Let me look at context.rs around line 790
Assistant
Let me find the beginning of this function
Assistant
Let me look at the new function
Assistant
Let me check how
has_macro_bangis used in completionAssistant
I see another issue: when the path is inside a TokenTree (not the macro name itself),
has_macro_bangshould NOT be set to true. Thehas_macro_bangflag indicates the path is immediately followed by!, which is only true when the path IS the macro name. Let me fix the TokenTree handling to not sethas_macro_bang:Assistant
Let me verify the
Assistant
Let me check the second one too
Assistant
Let me compile and test
Assistant
Let me run all tests
Assistant
Let me check the regression_10042 test
Assistant
Let me look at the regression_10042 test I saw
Assistant
Let me look at the specific regression_10042 test
Assistant
Let me check how scope is resolved. In
CompletionContext::new, afterexpand_and_analyze, scope is computed:rustlet scope = sema.scope_at_offset(&token.parent()?, original_offset)?;If we're in the fallback state (unexpanded), the
tokenis from the original (unexpanded) file.token.parent()would be the parent of thebartoken infoo!(bar). Let me check if this works correctly.But wait, the token is
barinfoo!(bar). The parent ofbaris the TokenTree, not the enclosing function body. Soscope_at_offsetmight not work correctly.Hmm, but
scope_at_offsetis called on&token.parent()?, which is the TokenTree. Doesscope_at_offsethandle TokenTree nodes? Let me check.Let me look at
scope_at_offsetin the semantics.Let me check how scope_at_offset is implementedAssistant
Let me look at the implementation
Assistant
Let me look at analyze_with_offset_no_infer
Assistant
Let me look at it
Assistant
Let me now look at the full diff of my changes
Assistant
Let me run the tests one more time to be sure
Assistant
The fix looks complete. Let me verify with a final review of the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.