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); }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 to understand the structure and locate relevant code for macro completion. Let's first check the current directory and see what's available.
- Assistant
Let me explore the repository structure more carefully. The issue is about macro completion in rust-analyzer. The problem is that when typing
foo!(bar$0)(where$0is the cursor), the IDE should suggestbaras a completion becausebaris a macro that can be used inside thefoo!macro invocation. But it's not working.Let me first understand the repository structure and find the relevant code for macro completions.
- Assistant
Let me understand the issue. The problem is about macro path completion inside a macro call. When you have:
macro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The user expects to get completion for
barwhen typing insidefoo!(bar$0). But currently this doesn't work.Let me look at the completion-related code in the codebase.
- Assistant
Let me search more broadly for completion-related code.
- Assistant
Let me find the ide-completion crate.
- Assistant
The issue is about completing a macro path inside a macro call. Let me look at how macro completions work in the ide-completion crate.
- Assistant
Let me look at the context module since the issue is about where completions are offered.
- Assistant
Let me look at the context analysis to understand how completions work in macro calls.
- Assistant
Let me look at how completions are resolved in macro contexts and understand the issue.
- Assistant
Let me look at the specific issue more carefully. The problem is completing
barinsidefoo!(bar$0)- completing a macro path as an argument to another macro call. - Assistant
Let me look at the
is_inside_macro_callfunction and understand the flow. The issue is about macro path completion inside a macro call. Let me search for relevant code. - Assistant
Let me look at the completion context and how it handles macro call arguments. The issue is about completing a macro name (path) inside a macro call's token tree. Let me examine the context analysis more carefully.
- Assistant
Let me understand the problem better. The issue is that when the user writes
foo!(bar$0)and expects to get completion for the macro namebarinside the token tree offoo!, it doesn't work.Let me look at how the completion context handles being inside a macro call's token tree:
- Assistant
Let me search for the classify_name_ref function and understand how paths are classified in macro calls.
- Assistant
Let me look at how the expansion result is determined when we're inside a macro call's token tree:
- Assistant
Now let me understand the issue better. When we have
foo!(bar$0), we're inside a macro callfoo!. The completion system needs to figure out thatbaris an identifier argument to the macrofoo. Sincefootakes$i:ident, this should provide completions for macro paths (because the expansion$i!()expects a macro name).Let me look at the
classify_name_reffunction to understand how macro call argument completions are handled. - Assistant
Let me look at the actual error scenario more carefully. I need to understand how completion works inside macro call token trees. Let me check
is_inside_macro_call: - Assistant
Let me look at the
expandfunction to understand the flow better. - Assistant
Let me understand the issue better. The problem is about completing
barinsidefoo!(bar$0). Thefoomacro takes$i:identand expands to$i!(). Sobaris an identifier token inside the token tree offoo!.Let me look at how completions are done when we're inside a macro call's token tree. The issue might be that the expansion fails because the macro can't expand (the identifier is incomplete) and then the system doesn't fall back to providing completions for the token tree.
Let me check what happens when the macro expansion fails and how the system handles that case.
- Assistant
Let me look at the context around line 518-530 to understand the MacroSegment handling.
- Assistant
Let me understand the issue by looking at
ast::Macrovsast::MacroCall: - Assistant
Let me look for the Macro struct
- Assistant
Let me look at the issue more carefully. The problem is that when the user types
foo!(bar$0), the completion should suggestbaras a macro name (since it's in an ident position inside a macro call). The current code path for completing inside a macro call token tree isMacroSegment, but that seems to only handle$ident: $namepatterns.Let me look at what
ast::Macro::can_castchecks: - Assistant
Let me search for how
Macrois defined: - Assistant
Let me understand the exact flow. The issue is about completing a macro name inside a macro call's token tree arguments. Let me look at the test files for macro completions:
- Assistant
Let me search properly:
- Assistant
Let me search for macro completion tests:
- Assistant
Let me look at the relevant tests:
- Assistant
Now I need to understand the specific issue. The problem is about completing
barinsidefoo!(bar$0)wherefootakes$i:identand then calls$i!(). The user wants to get macro completions inside the macro call's token tree.Let me look at how the completion system handles being inside a macro call's token tree, and specifically what the
PathKindis set to when inside a macro call.Let me search for where
PathKindis determined: - Assistant
Let me look at PathKind enum to understand what options exist.
- Assistant
Let me look at what the expand flow does when the macro expansion fails:
- Assistant
Let me understand the issue better. The issue is about completing a macro path inside a macro call. When you have:
macro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The user expects to get completion for
barwhen typing insidefoo!(bar$0). The$0marks the cursor position.Let me look at how the completion works in the analysis. The key is understanding how the expansion flow works when the cursor is inside a macro call's token tree.
Let me first look at the existing tests to see if there are similar test cases.
- Assistant
Let me look at the tests for macro-related completions more closely:
- Assistant
Let me look more closely at how the macro completion context works:
- Assistant
Let me understand the problem better. The issue is about completing macro names inside a macro call's token tree. When you type
foo!(bar$0), you should get completion forbar. Let me check what happens with the current code by examining how macro call token trees are handled in the analysis: - Assistant
I see! Now I need to understand the real issue. The code at line 518 checks for
ast::Macro::can_cast(it.kind())but this checks forMACROkind (i.e.,macro foo {}syntax), notMACRO_CALLkind (i.e.,foo!()invocations). The issue is about completing inside a macro call's token tree.Let me first look at what
ast::Macrocorresponds to: - Assistant
Let me search more carefully.
- Assistant
Let me search for how Macro is defined:
- Assistant
Let me try a different approach:
- Assistant
Let me try to find the Macro struct in the AST nodes file.
- Assistant
Let me try a different approach to find the Macro struct:
- Assistant
Let me try to search for the specific SyntaxKind.
- Assistant
Let me search for how
ast::Macrois defined differently: - Assistant
So
ast::Macrodoesn't exist as a standalone type. There isast::MacroDef(forMACRO_DEF),ast::MacroCall(forMACRO_CALL),ast::MacroRules(forMACRO_RULES), etc.But the code uses
ast::Macro::can_cast(it.kind()). Let me search for whatast::Macroactually is in the codebase. - Assistant
Let me search for where
ast::Macrois defined. It might be a type alias or a re-export. - Assistant
Let me search for the definition of
ast::Macroin the syntax crate. - Assistant
Let me check the
astmodule for theMacrotype: - Assistant
Let me look at the
Macroenum innode_ext.rs: - Assistant
Let me understand the full flow. Let me look at
ancestors_at_offset: - Assistant
Let me look at how
expand_macro_callworks: - Assistant
Let me look at
speculative_expand_macro_callto understand how it works: - Assistant
Now I understand the issue. Let me trace through the code carefully. The problem is that when
foo!(bar$0)expands, the expansion chain fails and falls back. Let me look at the key code paths: - Assistant
Now I understand the issue. Let me trace through the code flow:
foo!(bar$0)with cursor atbarinside the token tree offoo!()- The expansion tries to expand
foo!():foo!(bar)→bar!()andfoo!(bar_completion_marker)→bar_completion_marker!() - Then it recursively tries to expand the result. But
barinbar!()is the PATH of the macro call, not inside its token tree expandcan't find a TokenTree ancestor → returns Noneexpand_maybe_stopchecks!sema.is_inside_macro_call(...)—barIS insidebar!(), so returns None- The whole expansion chain fails, falls back to the original unexpanded file
- In the fallback,
baris inside a token tree of a MacroCall (not a Macro definition), so the else branch doesn't match → returns None → no completions
The fix is: in
expand_maybe_stop, whenexpandreturns None and the token is at the path of a macro call (not inside its token tree), we should stop expanding and return the current result.Let me first write a test to confirm the issue:
- Assistant
Let me first run the existing tests to see if they pass:
- Assistant
Let me check what build system is available:
- Assistant
Let me check if there's a specific way to run tests in this repo.
- Assistant
Let me check what's available in the environment.
- Assistant
Let me set up the default toolchain:
- Assistant
Let me run the existing tests to confirm they pass.
- Assistant
Good. Now let me write a test to confirm the issue. Let me first look at how tests are structured:
- Assistant
Let me first check what the current behavior is by creating a small test script:
- Assistant
Let me first check what completions are currently produced by writing a simple test:
- Assistant
Now let me implement the fix. The key issue is in
expand_maybe_stop- when the cursor maps to the PATH of a macro call (not inside its token tree) after expansion, the system gives up. I need to handle this case: - Assistant
Let me use the proper command:
- Assistant
Now I can see the
expand_maybe_stopfunction. I need to modify it to handle the case where the cursor is at the path of a macro call. Let me implement the fix: - Assistant
Let me use the proper edit command:
- Assistant
Good, the edit was applied. Let me now check if the code compiles and existing tests pass:
- Assistant
Good, the existing test passes. Let me run more tests to make sure nothing is broken:
- Assistant
All 54 special tests pass. Let me also run more tests:
- Assistant
Good. Let me run the full test suite:
- Assistant
All 732 tests pass. Now let me write a test to verify the fix works for the issue case. Let me look at the test structure:
- Assistant
Let me add a test for the issue. First, let me see the end of the file to add the test:
- Assistant
Let me find the exact line to insert the test after:
- Assistant
Let me see the context around line 470:
- Assistant
Now let me add a test for the issue. I'll add it after
completes_in_simple_macro_call: - Assistant
Good. Now let me run the test to see if it passes:
- Assistant
The test is close! The actual output is slightly different from what I expected. Let me update the test expectations:
- Assistant
Wait, I see a problem. There's a stray
}on line 502 that might break the module. Let me check: - Assistant
I see there's a stray
}on line 502. Let me check if this was already there before my edit: - Assistant
I see the stray
}was introduced by my edit. Let me fix that: - Assistant
I see there's now a duplicate
#[test]on lines 502-503. Let me fix that: - Assistant
There's now a duplicate
fn completes_macro_segment()line. Let me fix that: - Assistant
Let me verify the diff is correct:
- Assistant
Good, the diff looks clean. Now let me run the test:
- Assistant
The test passes. Now let me run the full test suite to make sure nothing is broken:
- Assistant
All 733 tests pass (732 original + 1 new test). Let me also run the broader test suite for related crates:
- Assistant
Let me also run some related context tests:
- Assistant
All tests pass. Let me review the complete diff to make sure everything looks good:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.