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
I'll start by exploring the repository structure to understand the codebase and then locate the relevant code for macro path completion.
Assistant
This is the rust-analyzer repository. The issue is about macro path completion not working. Let me explore the crates directory to understand the structure better.
Assistant
The issue is about macro path completion. The
ide-completioncrate is likely where completion logic lives. Let me explore that crate to find the relevant code.Assistant
I need to make sure I include exactly one function call per response. Let me continue exploring the ide-completion crate to understand the structure.
Assistant
Let me search for files related to path completion and macros in the completion crate:
Assistant
Let me look at the context analysis file to understand how completion contexts are determined:
Assistant
I need to make sure I include exactly one function call per response. Let me search for where macro path completion is handled in the context analysis.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "PathExprCtx|PathKind" /rust-analyzer/crates/ide-completion/src/context/analysis.rs | head -30 </tool_call>
Assistant
Let me use the proper function call format to search for where macro path completion is handled in the context analysis.
Assistant
Let me understand the issue better. The problem is about macro path completion inside a macro invocation. When you have:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The user is trying to complete
barinside thefoo!macro invocation. The cursor is at$0position afterbar. The issue says "Cannot complete macro path in macro".Let me search for how path completion works inside macros. )
I need to understand the issue better. The problem is about completing a macro path inside a macro invocation. Let me search for how macro expansion and path completion interact.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "MacroCall|macro.*call" /rust-analyzer/crates/ide-completion/src/context/analysis.rs | head -30 </tool_call>
Assistant
Let me search for how macro call context is handled in the completion analysis.
Assistant
Let me look at the
kind_macro_callfunction and the surrounding code:Assistant
Looking at this code, I can see the
kind_macro_callfunction. When a path is inside a macro call, it checks if the parent isMacroExpr,MacroPat, orMacroType. But when you writefoo!(bar$0), thebaris a direct argument to the macro call, not wrapped in those types.Let me look for tests related to macro completion to understand the expected behavior better:
Assistant
I need to continue investigating the issue. Let me search for tests related to macro completion in the ide-completion crate.
Assistant
I need to continue investigating the issue. Let me search for existing tests related to macro path completion to understand the expected behavior better.
Assistant
The bash command failed because of the
!character being interpreted by bash. I need to escape it or use single quotes to prevent bash history expansion.Assistant
I found a test case around line 1185 that looks relevant. Let me examine it more closely.
Assistant
I found a relevant test case. Now let me understand the issue better by looking for tests about completing macro names inside macro invocations.
Assistant
I found a test case that shows completing a module name inside a macro invocation. But the issue is different - it's about completing a macro name inside another macro invocation. Let me search for how macros are completed:
Assistant
The command was executed successfully. It shows that there is a macro_def module and a function add_macro in completions.rs. Let me look at the completions.rs file to understand how completions are added for different contexts.
Assistant
Now I understand the structure. The
complete_name_reffunction handles different path kinds. ForPathKind::Expr, it callsexpr::complete_expr_path. Let me look at that file to see how macros are completed:Assistant
The command was executed successfully. Let me examine the expr.rs file to understand how expression path completions work and where macros should be added:
Assistant
I can see the issue now. In the
complete_expr_pathfunction, there's a filterscope_def_applicablethat only includes macros if they are fn-like:rustlet scope_def_applicable = |def| match def { ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) => false, ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db), _ => true, };This means macros should be included in expression path completions. But the issue is about completing a macro path inside a macro invocation argument. Let me look back at the context analysis to see what happens when we're inside a macro call's token tree.
Let me search for how the completion context is determined for tokens inside macro invocations:
Assistant
Let me look at the code around line 256 to understand how token trees in macro calls are handled.
Assistant
Let me continue reading to see what happens when macro expansion fails:
Assistant
Let me look at what happens when the macro expansion fails or when we're analyzing the original file without expansion:
Assistant
Let me understand the issue better by creating a test file to reproduce the problem:
Assistant
Let me look at the existing tests in the ide-completion crate to understand the test format and see if there are any tests for macro completion.
Assistant
The grep command failed because of the pattern. Let me try a simpler approach.
Assistant
Let me look at the test around line 2643 to understand the test format and see how macro completion is tested.
Assistant
Excellent! I found a test that shows macro completion inside a macro invocation works. The test shows that when completing
v$0insidem!(v$0), macros likehelper!,m!, andmakro!are available for completion.Now let me understand the issue better. The issue says "Cannot complete macro path in macro" with this code:
rustmacro_rules! bar { () => (); } macro_rules! foo { ($i:ident) => { $i!() }; } fn main() { foo!(bar$0); }The user expects to complete
bar(a macro name) insidefoo!(bar$0). The cursor is afterbar, expecting to seebarin the completion list. Let me trace through the code to understand what's happening.Looking back at the
kind_macro_callfunction in analysis.rs, I see that when we have a path inside a macro call, it checks if the parent isMacroExpr,MacroPat, orMacroType. But in the case offoo!(bar$0), thebaris just an identifier/argument to the macro, not wrapped in those types.Let me search for how the issue can be reproduced and where the problem might be:
Assistant
Looking at the code, I can see the issue. In the
kind_macro_callfunction around line 1484, when analyzing a path inside a macro call, it only handles cases where the parent isMacroExpr,MacroPat, orMacroType. But when the path is just an argument to a macro (likebarinfoo!(bar$0)), and the expanded result is aMacroCalldirectly (not wrapped in those types), the function returnsNone, preventing completion.Let me examine the exact code:
Assistant
Now I understand the problem! The
kind_macro_callfunction handles the case when a path is part of a macro call (e.g., when completing the macro name itself likefo$0!()). However, when the parent is neitherMacroExpr,MacroPat, norMacroType, it returnsNoneat line 1494.The issue is that when you have
foo!(bar$0)and you're trying to completebar, the pathbaris inside aMacroCallnode, but its parent is just aTokenTree, notMacroExpr,MacroPat, orMacroType. In this case, the function should treat it as an expression context, since the macro could be invoked as an expression.Let me check what happens when the parent is a TokenTree or similar:
Assistant
I found the issue. The
kind_macro_callfunction only handles cases where the macro call's parent isMacroExpr,MacroPat, orMacroType. When the parent is something else (like just a TokenTree inside an expression context), it returnsNone, which prevents completion.The fix should be to handle the case where we can't determine the specific context by treating it as an expression context. Let me create a test to confirm the issue first:
Assistant
Now let me look at the structure more carefully. I need to understand when a path is inside a macro call's token tree. Let me trace through what happens when we have
foo!(bar$0):- The path
baris inside aMacroCallnode (foo!(...)) - The parent of the
MacroCallwould be determined by wherefoo!()is used - In the test case,
foo!(bar$0)is used as a statement inmain(), so the parent should be something likeExprStmt
The problem is that when we're analyzing the path
bar, the code goes to thekind_macro_callhandler because the path's parent is aMacroCall. But thekind_macro_callfunction checks the parent of thatMacroCallnode. If that parent is notMacroExpr,MacroPat, orMacroType, it returnsNone.The fix should be to default to an expression context when we can't determine the specific context. Let me look at the
kind_itemfunction to see if it handles this case:- The path
Assistant
Let me search for the kind_item function definition:
Assistant
Now I understand the issue! The
kind_macro_callfunction at line 1484-1499 only handles three specific parent types (MacroExpr,MacroPat,MacroType). When the parent is something else (like a TokenTree directly in an expression context), it returnsNone, preventing completion.The fix is to add a fallback case that treats the path as an expression when we can't determine a more specific context. Let me implement the fix:
Assistant
I can see the issue now. When the parent of the
MacroCallis notMacroExpr,MacroPat, orMacroType, it returnsNone(line 1494). This causes completion to fail when the macro call is used in a context that's not explicitly wrapped in those types.The fix should be to default to treating it as an expression when we can't determine the specific context. Let me apply the fix:
Assistant
Wait, that's not quite right. The issue is that when we're inside a
TokenTree(the arguments of a macro call), the path is the argument, not a MacroCall itself. Let me re-examine the code to understand the AST structure better.When we have
foo!(bar$0):- The path
baris inside aMacroCallnode (which isfoo!(...)) - The parent of the path would be... let me think
Actually, looking at the code at line 1570, when the parent is a
MacroCall, it callskind_macro_call(it)?. So when we're analyzing the pathbarinsidefoo!(bar$0), the parent of the pathbarwould be theMacroCallnodefoo!(...).But wait, that doesn't make sense. Let me think about this more carefully. The path
baris an argument to the macro callfoo!(). The parent of the pathbarwould be... it could be aTokenTreeor it could be theMacroCallitself.Let me look at what
make_path_kind_exprexpects:- The path
Assistant
I need to continue investigating. Let me look at what happens when the path is directly inside a TokenTree.
Assistant
I see! The code handles the case where a path's parent is a
MacroCall. The issue is in thekind_macro_callclosure. When a macro call's parent is notMacroExpr,MacroPat, orMacroType, it returnsNone, which breaks the completion.The real question is: what should happen when we're completing a path that's the name of a macro being invoked inside another macro? For example, in
foo!(bar$0), we're trying to completebar, which is a macro that will be invoked.Let me think about the AST structure:
foo!(bar)is aMacroCall- Inside it,
baris likely aPathorPathExpror just an identifier - The parent of
bar(as a path) would be... let me check what the actual parent is
Let me look for similar handling of TokenTree:
Assistant
I made a mistake by just putting code without proper function call formatting. Let me continue with a proper function call.
Assistant
Now I understand the issue better. When completing
barinsidefoo!(bar$0), the path's parent is aMacroCall. Thekind_macro_callfunction checks if the parent of thatMacroCallisMacroExpr,MacroPat, orMacroType. If not, it returnsNone, which breaks completion.The fix should be to handle the fallback case by defaulting to an expression context. Let me implement the fix:
Assistant
Now I understand the issue. When completing
barinsidefoo!(bar$0), the path's parent is aMacroCall. Thekind_macro_callfunction checks if the parent of thatMacroCallisMacroExpr,MacroPat, orMacroType. If not, it returnsNone, which breaks completion.The fix should be to handle the fallback case when the parent is not one of the expected types. Let me implement the fix:
Assistant
I'll continue working on the fix for the macro path completion issue.
Assistant
The user is telling me that I need to include a function call in my response. Let me continue working on the fix for the macro path completion issue.
Looking at the code, the issue is in the
kind_macro_callclosure. When the parent of aMacroCallis notMacroExpr,MacroPat, orMacroType, it returnsNone, which breaks completion.The fix should be to handle the fallback case when the parent is not one of the expected types. Instead of returning
None, we should treat it as an expression context.Let me apply the fix by editing the file.
Assistant
Now I need to understand the issue better. Let me look at what happens when we're completing inside a macro invocation's token tree.
Assistant
Let me look at the full context of where the
kind_macro_callis used to understand the issue better:Assistant
Assistant
The user is telling me I need to include a function call. Let me search for MacroExpr to understand the AST structure better.
Assistant
Let me look at what MacroExpr is and understand the AST structure.
Assistant
Now I understand the issue!
MacroExpr,MacroPat, andMacroTypeare wrapper nodes aroundMacroCallthat indicate the context where the macro is used. When aMacroCallis not wrapped in any of these (e.g., when it's directly in a statement list asbar!();), thekind_macro_callfunction returnsNone, which breaks completion.The fix should be to handle the fallback case by treating the macro call as an expression, since that's the most common context for macro invocations. Let me implement the fix:
Assistant
I need to fix the
kind_macro_callfunction in/rust-analyzer/crates/ide-completion/src/context/analysis.rs. The issue is on line 1494 where it returnsNonefor cases that are notMacroExpr,MacroPat, orMacroType. Instead, it should treat these as expression contexts by callingmake_path_kind_exprwith the parent node.Let me look at what
make_path_kind_exprexpects - it takes anast::Expr. But the parent might not be an directly. Let me check what types of nodes could be the parent.Assistant
The user is telling me that I forgot the required arguments "command". Let me continue investigating the issue and make sure I include the command parameter in my function calls.
Assistant
I see the grep didn't find a direct conversion. Let me search for how MacroCall is used as an expression in the codebase.
Assistant
The user is asking me to continue working on the issue. Let me search for how MacroCall relates to Expr in the codebase to understand the proper fix.
Assistant
The user is asking me to continue working on the issue. Let me look at the Expr enum to understand how MacroCall fits in.
Assistant
Looking at the code, I need to fix the
kind_macro_callclosure. The issue is that when the parent of aMacroCallis notMacroExpr,MacroPat, orMacroType, it returnsNone, which breaks completion. Let me implement the fix by changingmake_path_kind_exprto accept aSyntaxNodeinstead of anast::Expr, so we can pass theMacroCall's syntax node directly in the fallback case.Assistant
I need to understand the
make_path_kind_exprfunction better to know how to fix the issue. Let me look at the beginning of the function to see what it expects.Assistant
Assistant
The user seems to be waiting for me to continue working on the issue. Let me continue with my analysis and fix.
Based on my investigation, I need to fix the
kind_macro_callfunction in/rust-analyzer/crates/ide-completion/src/context/analysis.rs. The issue is that when the parent of aMacroCallis notMacroExpr,MacroPat, orMacroType, it returnsNone, which breaks completion.The fix should be to handle the fallback case by treating the
MacroCallas an expression. I'll implement this by modifying the fallback case to try to find an expression context or create a default one.Let me implement the fix now.
Assistant
Let me look at the PathExprCtx struct to understand what fields it has and if it implements Default.
Assistant
Now I understand the structure. Let me implement the fix. The issue is that when the parent of a
MacroCallis notMacroExpr,MacroPat, orMacroType, the function returnsNone. Instead, we should treat it as an expression context.Assistant
You're right, I need to check if my fix is correct. The issue is that
it.clone().into()assumesMacroCallcan be converted toExpr, which may not be true. Let me check and fix this properly.Assistant
I need to reconsider my approach. Let me check what types can be converted to Expr and understand the AST structure better.
Assistant
Looking at my fix, I see that
it.clone().into()won't work becauseMacroCallcannot be directly converted toExpr. Let me revert and implement a proper fix.Assistant
Now let me look at the code more carefully to understand the proper fix. The issue is that when the parent of a
MacroCallis notMacroExpr,MacroPat, orMacroType, we need to handle it properly.Looking at the code, I see that
make_path_kind_exprexpects anast::Expr. ButMacroCallis not anast::Expr- it's wrapped inMacroExprwhen used as an expression.However, I notice that in the fallback case, we have the
parentnode. If the parent is not one of the expected types, it might be aStmtList,ExprStmt, or another container. We can try to find an expression from these.Let me look at a simpler approach: instead of trying to convert
MacroCalltoExpr, we can check if the parent can be cast to an expression-related type and use that.Assistant
Assistant
The user is telling me that my action could not be parsed because the "command" key's value was malformed - the line 5 missing. This is likely referring to my previous thought block where I had code that wasn't properly formatted.
Let me continue with the fix. I need to look at the helper functions to understand what parameters they take, and then implement the fallback case properly.
Assistant
The user is telling me that my action could not be parsed. Looking at my previous response, I had a bash_terminal command that should have been fine. Let me try again with a simpler approach.
I need to look at what helper functions are available and then implement the fix. Let me search for the helper functions that take SyntaxNode parameters.
Assistant
I need to find all the helper functions used in
make_path_kind_exprto understand what parameters they take.Assistant
Let me check where these helper functions are defined. They might be in another file like completions/postfix.rs.
Assistant
So
is_in_conditionandis_in_valuetake&ast::Expr. This means I can't use them directly with aSyntaxNode. Let me check the fullmake_path_kind_exprto see all the helper functions and their parameters.Assistant
The user is pointing out that there was a problem with line 5 being missing in my action. Let me look at my previous thought block - I see that I was thinking about the helper functions and their parameters. The issue is that I need to implement a proper fix for the
kind_macro_callfunction.Let me now create the fix. The key insight is that in the fallback case, I should treat the
MacroCallas an expression context. Since many of the helper functions requireast::Expr, I'll need to use a simpler approach with default values.Assistant
Now I understand the issue better. The
make_path_kind_exprfunction takes anast::Exprparameter, but in the fallback case ofkind_macro_call, we don't have anast::Expr- we have aMacroCall.The simplest fix is to create a new helper that works with a
SyntaxNodeinstead ofast::Expr, or to simply create a defaultPathKind::Exprwith minimal context.Let me implement the fix by adding a fallback case that creates a default
PathKind::Expr.Assistant
The user seems to be indicating that my fix was successfully applied. Let me verify the fix by checking the diff and then testing if it compiles.
Assistant
The user is telling me that the changes look good. Let me verify the fix compiles by building the project.
Assistant
The user wants me to continue working. I need to set up the Rust toolchain first before I can build the project.
Assistant
The user is telling me the build succeeded. Now I need to run the tests to make sure my fix works correctly and doesn't break any existing functionality.
Assistant
All 732 tests passed. Now let me verify my fix handles the issue correctly by looking for relevant test cases and ensuring the fix is complete.
Assistant
All 732 tests passed! Let me verify my fix is correct by examining the test case from the issue and understanding how it relates to my fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.