I see that MACRO_EXPR is indeed a syntax kind in the parser. When a macro call is in expression position, it's parsed as a MACRO_EXPR node. So MacroExpr and MacroCall are different syntax node types.
Looking at the test data, MACRO_EXPR is used for macro calls in expression positions. So when we have let _ = foo!(), the foo!() is a MACRO_EXPR node.
Now, the issue is that when completing inside foo!(bar$0), the parent of the path bar is a MacroCall node. And when the macro expands, the expanded code has a MACRO_EXPR node (for bar!()).
But in the expansion, the MacroCall node is a MACRO_CALL node, not a MACRO_EXPR node. Wait, no - looking at the parser test data, when a macro call is in expression position, it's a MACRO_EXPR node.
Actually, let me re-read the issue. The problem is:
- When completing
foo!(bar$0), the code expands foo!(bar<fake_ident>) to bar<fake_ident>!()
- The code then analyzes
bar<fake_ident> as a path
kind_macro_call is called with the MacroCall node for bar!()
- The parent of this
MacroCall node is None (because it's the root of the expansion)
- The closure returns
None, and completions fail
But wait, the expansion of foo! is bar<fake_ident>!(). This is parsed as a syntax tree. In this tree, bar<fake_ident>!() is a MACRO_EXPR node (because it's in expression position).
So the parent of the path bar<fake_ident> should be a MACRO_EXPR node, not None.
Hmm, let me re-read the code more carefully.
Actually, looking at the kind_macro_call closure:
let parent = it.syntax().parent()?;
Here, it is a MacroCall, and it.syntax() is its syntax node. The parent of this syntax node is what we're looking for.
In the case of foo!(bar<fake_ident>):
- The
foo!() is a MacroCall in the original file
- The expansion is
bar<fake_ident>!()
- The code analyzes the position in the expansion
In the expansion bar<fake_ident>!(), the syntax tree is:
MACRO_EXPR
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
So the parent of the NAME_REF bar<fake_ident> is a PATH, and the parent of the PATH is the MACRO_EXPR.
In classify_name_ref, the code looks for the path and then checks its parent:
let parent = path.syntax().parent()?;
...
ast::MacroCall(it) => {
kind_macro_call(it)?
}
So the parent of the path is checked. If it's a MacroCall, kind_macro_call is called.
But wait, in the expansion bar<fake_ident>!(), the parent of the path bar<fake_ident> is a MACRO_EXPR, not a MACRO_CALL. So kind_macro_call wouldn't be called.
Hmm, I'm getting confused. Let me re-read the code more carefully.
Actually, looking at the code:
ast::MacroCall(it) => {
kind_macro_call(it)?
}
This is in the match_ast! block in classify_name_ref. It checks if the parent of the path is a MacroCall. If so, it calls kind_macro_call.
In the expansion bar<fake_ident>!(), the parent of the path bar<fake_ident> is a MACRO_EXPR, not a MACRO_CALL. So the code would match:
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
Wait, but the code doesn't have an arm for MacroExpr in the main match_ast! block. Let me re-read the code.
Looking at the code in classify_name_ref:
match_ast! {
match parent {
ast::PathType(it) => make_path_kind_type(it.into()),
ast::PathExpr(it) => {
...
},
ast::TupleStructPat(it) => {
...
},
ast::RecordPat(it) => {
...
},
ast::PathPat(it) => {
...
},
ast::MacroCall(it) => {
kind_macro_call(it)?
},
ast::Meta(meta) => make_path_kind_attr(meta)?,
ast::VisibilityInner(it) => PathKind::Vis { has_in_token: it.in_token().is_some() },
ast::UseTree(_) => PathKind::Use,
// completing inside a qualifier
ast::Path(parent) => {
...
match_ast! {
match parent {
ast::PathType(it) => make_path_kind_type(it.into()),
...
ast::MacroCall(it) => {
kind_macro_call(it)?
},
...
}
}
},
...
}
}
So the code checks if the parent of the path is a MacroCall. If so, it calls kind_macro_call.
But in the expansion bar<fake_ident>!(), the parent of the path bar<fake_ident> is a MACRO_EXPR, not a MACRO_CALL. So the code would not match ast::MacroCall(it).
Wait, but a MACRO_EXPR is an expression. So the parent of the path should be a PathExpr or something similar.
Actually, looking at the syntax tree:
MACRO_EXPR
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
The PATH is a child of the MACRO_EXPR. So the parent of the PATH is the MACRO_EXPR.
In classify_name_ref, the code checks if the parent of the path is a MacroCall. But the parent is a MACRO_EXPR, not a MACRO_CALL.
So the code would not match ast::MacroCall(it). Instead, it would fall through to the default case or another case.
Let me look at what cases would match:
ast::PathType(it) - no, the parent is not a PathType
ast::PathExpr(it) - no, the parent is not a PathExpr
ast::TupleStructPat(it) - no
ast::RecordPat(it) - no
ast::PathPat(it) - no
ast::MacroCall(it) - no, the parent is a MACRO_EXPR, not a MACRO_CALL
ast::Meta(meta) - no
ast::VisibilityInner(it) - no
ast::UseTree(_) - no
ast::Path(parent) - no, the parent is not a Path
So none of the cases match, and the code returns None from classify_name_ref.
Wait, but I need to look at the full classify_name_ref code to see what happens when none of the cases match.
Actually, looking at the code, the default case is:
_ => return None,
So if none of the cases match, the code returns None.
But wait, there's also the ast::Path(parent) case, which handles completing inside a qualifier. Let me look at that case more carefully.
Actually, looking at the code:
ast::Path(parent) => {
path_ctx.parent = Some(parent.clone());
let parent = iter::successors(Some(parent), |it| it.parent_path()).last()?.syntax().parent()?;
match_ast! {
match parent {
ast::PathType(it) => make_path_kind_type(it.into()),
ast::PathExpr(it) => {
...
make_path_kind_expr(it.into())
},
ast::TupleStructPat(it) => {
...
},
ast::RecordPat(it) => {
...
},
ast::PathPat(it) => {
...
},
ast::MacroCall(it) => {
kind_macro_call(it)?
},
ast::Meta(meta) => make_path_kind_attr(meta)?,
ast::VisibilityInner(it) => PathKind::Vis { has_in_token: it.in_token().is_some() },
ast::UseTree(_) => PathKind::Use,
ast::RecordExpr(it) => make_path_kind_expr(it.into()),
_ => return None,
}
}
},
This case handles completing inside a qualifier (i.e., foo::bar::$0). The code finds the outermost path and then checks its parent.
But in our case, the parent of the path is a MACRO_EXPR, not a Path. So this case wouldn't match.
Hmm, I'm still confused about the exact issue. Let me re-read the issue description.
The issue says:
Cannot complete macro path in macro
And the code snippet is:
macro_rules! bar { () => (); }
macro_rules! foo { ($i:ident) => { $i!() }; }
fn main() {
foo!(bar$0);
}
So the user wants to complete bar inside foo!(bar$0). The macro foo takes an identifier and expands it to $i!(). So completing bar inside foo!(bar) should suggest macro names.
But wait, in the original file foo!(bar$0), the bar is an argument to the macro foo!. It's not a MacroCall; it's the path of the MacroCall foo!.
So in the original file, the syntax tree is:
BLOCK_EXPR
...
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF: foo
EXCL_TOKEN
TOKEN_TREE
NAME_REF: bar
Wait, no. The foo!(bar) is a MACRO_CALL. The foo is the path of the MACRO_CALL, and bar is the argument (inside the TOKEN_TREE).
So when the user types foo!(bar$0), the cursor is inside the TOKEN_TREE of the MACRO_CALL. The bar is a NAME_REF inside the TOKEN_TREE.
Now, the code analyzes this position by:
- Looking for token trees at the cursor position
- Expanding the macro call with speculative args
- Analyzing the position in the expansion
In step 1, the code finds the TOKEN_TREE containing bar.
In step 2, the code expands foo!(bar<fake_ident>) to bar<fake_ident>!().
In step 3, the code analyzes the position in the expansion. The expansion is:
MACRO_CALL (or MACRO_EXPR)
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
So the parent of the NAME_REF bar<fake_ident> is a PATH, and the parent of the PATH is the MACRO_CALL (or MACRO_EXPR).
In classify_name_ref, the code checks if the parent of the path is a MacroCall. If so, it calls kind_macro_call.
Now, the issue is:
- If the expansion has a
MACRO_CALL node, the parent of the path is the MACRO_CALL, and kind_macro_call is called.
kind_macro_call then checks the parent of the MACRO_CALL. If it's None (e.g., root of expansion), the closure returns None.
So the issue is that kind_macro_call returns None when the parent of the MACRO_CALL is None.
My fix changes kind_macro_call to handle this case by treating the path as an expression path. But the fix uses ast::MacroExpr { syntax: ... } to create a MacroExpr, which doesn't work because the syntax field is private.
I need to find another way to create a MacroExpr or to create a PathKind::Expr directly.
Looking at the code, I see that make_path_kind_expr takes an ast::Expr and returns a PathKind::Expr. If I can create an ast::Expr from the MacroCall, I can pass it to make_path_kind_expr.
But MacroCall is not an Expr. The Expr enum has a MacroExpr variant, which wraps a MacroExpr.
Wait, but looking at the code, I see that MacroExpr is created from a syntax node with kind MACRO_EXPR. And a MACRO_EXPR node can be cast to Expr::MacroExpr.
So if the parent of the MACRO_CALL is a MACRO_EXPR node, the code would match:
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
And make_path_kind_expr(expr.into()) would pass expr.into(), which is Expr::MacroExpr(expr), to make_path_kind_expr.
But wait, the match_ast! macro tries to cast the parent to ast::MacroExpr. If the parent is a MACRO_CALL node (not a MACRO_EXPR node), the cast would fail, and the default arm would be matched.
So the issue is that when the parent of the path is a MACRO_CALL node (not a MACRO_EXPR node), the match_ast! macro tries to cast it to ast::MacroCall, which succeeds, and then kind_macro_call is called.
In kind_macro_call, the code checks the parent of the MACRO_CALL. If it's None, the closure returns None.
But wait, a MACRO_CALL node can have a parent. For example, if the MACRO_CALL is inside a BLOCK_EXPR, the parent would be the BLOCK_EXPR.
Let me re-think this.
In the expansion bar<fake_ident>!(), the syntax tree is:
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
Wait, is this a MACRO_CALL node or a MACRO_EXPR node?
Looking at the parser test data, when a macro call is in expression position, it's parsed as a MACRO_EXPR node. So the expansion bar<fake_ident>!() would be a MACRO_EXPR node.
But in classify_name_ref, the code checks if the parent of the path is a MacroCall. If the parent is a MACRO_EXPR node, the cast to ast::MacroCall would fail.
Hmm, let me check if MACRO_EXPR can be cast to ast::MacroCall.
Actually, looking at the AstNode trait, each node type has a cast method that checks if the syntax node can be cast to that type. For example:
impl AstNode for MacroCall {
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_CALL }
...
}
So a MACRO_EXPR node cannot be cast to MacroCall.
But wait, looking at the classify_name_ref code:
ast::MacroCall(it) => {
kind_macro_call(it)?
}
The match_ast! macro tries to cast the parent to ast::MacroCall. If the parent is a MACRO_EXPR node, the cast would fail, and this arm wouldn't be matched.
So the question is: what happens when the parent is a MACRO_EXPR node?
Looking at the match_ast! usage:
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => ...,
ast::Meta(meta) => ...,
ast::VisibilityInner(it) => ...,
ast::UseTree(_) => ...,
ast::Path(parent) => ...,
ast::RecordExpr(it) => ...,
_ => return None,
}
}
If the parent is a MACRO_EXPR node, none of the arms would match (since MACRO_EXPR is not a MACRO_CALL), and the default arm _ => return None would be executed.
So the issue is that when the parent is a MACRO_EXPR node, the code returns None.
But wait, I need to check if there's an arm for MacroExpr in the match_ast! block. Looking at the code, there's no arm for MacroExpr.
Hmm, but looking at the code in the ast::Path(parent) case:
ast::Path(parent) => {
...
match_ast! {
match parent {
ast::PathType(it) => make_path_kind_type(it.into()),
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => ...,
ast::Meta(meta) => ...,
ast::VisibilityInner(it) => ...,
ast::UseTree(_) => ...,
ast::RecordExpr(it) => make_path_kind_expr(it.into()),
_ => return None,
}
}
},
There's also no arm for MacroExpr in this case.
So the issue is that when the parent is a MACRO_EXPR node, the code returns None because none of the arms match.
But wait, the original code has:
ast::MacroCall(it) => {
kind_macro_call(it)?
}
And kind_macro_call is defined as:
let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
let parent = it.syntax().parent()?;
if let Some(kind) = kind_item(it.syntax()) {
return Some(kind);
}
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
ast::MacroPat(it) => PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())},
ast::MacroType(ty) => make_path_kind_type(ty.into()),
_ => return None,
}
};
Some(kind)
};
So kind_macro_call is called when the parent of the path is a MacroCall. Then it checks the parent of the MacroCall to determine the path kind.
In the expansion bar<fake_ident>!(), the syntax tree is:
MACRO_CALL (or MACRO_EXPR)
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
The parent of the PATH is the MACRO_CALL (or MACRO_EXPR). So in classify_name_ref, the code would match ast::MacroCall(it) (if the parent is a MACRO_CALL node) or fall through to the default arm (if the parent is a MACRO_EXPR node).
If the parent is a MACRO_CALL node, kind_macro_call is called. In kind_macro_call, the parent of the MACRO_CALL is checked. If the MACRO_CALL is at the root of the expansion (no parent), the closure returns None.
If the parent is a MACRO_EXPR node, the code falls through to the default arm and returns None.
So the issue is that when the parent is a MACRO_EXPR node or when the parent of the MACRO_CALL is None, the code returns None.
My fix handles the case where the parent of the MACRO_CALL is None by treating the path as an expression path. But I need to also handle the case where the parent is a MACRO_EXPR node.
Actually, looking at the code again, I see that kind_macro_call is only called when the parent of the path is a MacroCall. So if the parent is a MACRO_EXPR node, kind_macro_call is not called.
So the issue is different than I thought. Let me re-read the code.
Actually, wait. Looking at the match_ast! block in classify_name_ref:
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => {
kind_macro_call(it)?
},
ast::Meta(meta) => ...,
ast::VisibilityInner(it) => ...,
ast::UseTree(_) => ...,
ast::Path(parent) => ...,
ast::RecordExpr(it) => ...,
_ => return None,
}
}
The parent of the path can be a MacroCall, PathExpr, etc. If the parent is a MacroCall, kind_macro_call is called.
But in the expansion bar<fake_ident>!(), the parent of the path is the MACRO_CALL node itself (not wrapped in anything). So the cast to ast::MacroCall would succeed, and kind_macro_call would be called.
Wait, but a PATH is a child of a MACRO_CALL. So the parent of the PATH is the MACRO_CALL. So the cast to ast::MacroCall would succeed.
But then, in kind_macro_call, the code checks the parent of the MACRO_CALL. If the MACRO_CALL is at the root of the expansion, its parent would be None.
So the issue is that when the MACRO_CALL is at the root of the expansion, kind_macro_call returns None.
My fix handles this case by treating the path as an expression path when the parent of the MACRO_CALL is None.
But the fix uses ast::MacroExpr { syntax: ... }, which doesn't work because the syntax field is private.
I need to find another way to create a MacroExpr or to create a PathKind::Expr directly.
Looking at the make_path_kind_expr function, it creates a PathKind::Expr by extracting information from the expression. If I can't create a MacroExpr, I could create a PathKind::Expr directly with default values.
But that would require duplicating the logic in make_path_kind_expr.
Alternatively, I could check if the syntax node has kind MACRO_EXPR and if so, cast it to ast::MacroExpr. But the syntax node has kind MACRO_CALL, not MACRO_EXPR.
Hmm, let me think about this differently. The issue is that when completing inside a macro invocation, the code fails to provide completions because kind_macro_call returns None.
The fix should handle the case where the parent of the MacroCall is None by treating the path as an expression path.
One way to do this is to create a synthetic PathKind::Expr directly, without calling make_path_kind_expr. But this would require duplicating the logic.
Another way is to use the AstNode::cast method to try to cast the syntax node to different types. But the syntax node has kind MACRO_CALL, so it can only be cast to ast::MacroCall, not ast::MacroExpr.
Wait, I just realized something. The MacroExpr struct wraps a SyntaxNode. The SyntaxNode can have any kind. So I could create a MacroExpr by wrapping a MACRO_CALL node.
But the syntax field is private, so I can't directly create a MacroExpr.
Let me check if there's a public way to create a MacroExpr.
Looking at the code, I see that MacroExpr is created by casting a syntax node with kind MACRO_EXPR. There's no public constructor for MacroExpr.
But wait, looking at the AstNode trait, it has a cast method:
fn cast(syntax: SyntaxNode) -> Option<Self>
So I could use ast::MacroExpr::cast(parent) to try to cast a syntax node to MacroExpr. But this would return None if the syntax node has kind MACRO_CALL.
Hmm, this is tricky. Let me think about this differently.
Actually, looking at the code more carefully, I see that the issue is not about creating a MacroExpr. The issue is that kind_macro_call returns None when the parent of the MacroCall is None.
The fix should handle this case by returning a PathKind::Expr instead of None.
But make_path_kind_expr takes an ast::Expr as input. If I can't create an ast::Expr from the MacroCall, I need to find another way.
Looking at the make_path_kind_expr function:
let make_path_kind_expr = |expr: ast::Expr| {
let it = expr.syntax();
...
PathKind::Expr {
expr_ctx: PathExprCtx {
...
},
}
};
The function extracts information from the expression and creates a PathKind::Expr. If I can extract the same information from the MacroCall's syntax node, I could create the PathKind::Expr directly.
But this would require duplicating the logic.
Actually, let me look at the PathExprCtx struct. It has many fields with default values. If I create a PathExprCtx with default values, it might work.
Let me check what the default values are for PathExprCtx:
Looking at the code, I see that PathExprCtx is created with various values extracted from the expression. If I create a synthetic PathExprCtx with default values (e.g., in_block_expr: false, in_breakable: None, etc.), it might work for the case where the parent is None.
But this is a bit hacky. Let me see if there's a better way.
Actually, looking at the code again, I notice that the match_ast! macro is used to cast a syntax node to different AST types. If the cast fails, the default arm is executed.
So when the parent is None, the code currently returns None. But if I change the default arm to handle the None case, it might work.
But wait, the match_ast! macro operates on a syntax node, not an Option. So if the parent is None, the match_ast! macro wouldn't be called at all.
OK, I think the cleanest fix is to:
- Handle the
None case separately (before calling match_ast!)
- Use
match_ast! with the unwrapped parent
For the None case, I need to create a PathKind::Expr. Since I can't create an Expr from the MacroCall, I'll need to create a PathKind::Expr directly.
Looking at the code, I see that PathKind::Expr contains a PathExprCtx. I can create a synthetic PathExprCtx with default values.
But actually, looking at the kind_macro_call closure, I see that it sets path_ctx.has_macro_bang = it.excl_token().is_some() at the beginning. So the PathCompletionCtx already has some information.
The issue is that kind_macro_call returns a PathKind, which is used to set path_ctx.kind. If I return a PathKind::Expr with a default PathExprCtx, it might work.
Let me try a different approach. Instead of calling make_path_kind_expr, I can create a PathKind::Expr directly.
Looking at the code, PathKind::Expr is defined as:
PathKind::Expr {
expr_ctx: PathExprCtx {
in_block_expr: bool,
in_breakable: Option<BreakableKind>,
after_if_expr: bool,
before_else_kw: bool,
in_condition: bool,
ref_expr_parent: Option<ast::RefExpr>,
after_amp: bool,
is_func_update: Option<ast::RecordExpr>,
innermost_ret_ty: Option<hir::Type>,
innermost_breakable_ty: Option<hir::Type>,
self_param: Option<Either<hir::SelfParam, hir::Param>>,
in_value: bool,
incomplete_let: bool,
after_incomplete_let: bool,
impl_: Option<ast::Impl>,
in_match_guard: bool,
},
}
I can create a synthetic PathExprCtx with default values. But this is a bit verbose.
Actually, looking at the code, I see that PathExprCtx has a default implementation or can be created with default values. Let me check.
Actually, I think the cleanest fix is to use the fact that MacroExpr can be created from a MACRO_EXPR node. If the syntax node has kind MACRO_EXPR, I can cast it to MacroExpr. But if the syntax node has kind MACRO_CALL, I need to handle it differently.
Wait, let me re-read the issue. The issue is about completing inside foo!(bar$0). In this case:
- The user types
foo!(bar and triggers completion.
- The code analyzes the position.
- The code finds the token tree
bar inside the macro invocation.
- The code expands
foo!(bar<fake_ident>) to bar<fake_ident>!().
- The code analyzes the position in the expansion.
In step 5, the code finds the name-like node in the expansion. The name-like node is the NAME_REF bar<fake_ident>. The parent of the NAME_REF is a PATH, and the parent of the PATH is the MACRO_CALL (or MACRO_EXPR) bar<fake_ident>!().
Now, the question is: what is the kind of the syntax node for bar<fake_ident>!()?
Looking at the parser test data, when a macro call is in expression position, it's parsed as a MACRO_EXPR node. So bar<fake_ident>!() would be a MACRO_EXPR node.
But wait, the match_ast! block in classify_name_ref has an arm for ast::MacroCall(it). A MACRO_EXPR node is not a MACRO_CALL node, so the cast would fail.
So the code would not match ast::MacroCall(it). Instead, it would fall through to the default arm _ => return None.
Hmm, but this doesn't match my earlier analysis. Let me re-check.
Actually, looking at the AstNode trait implementation for MacroCall and MacroExpr:
impl AstNode for MacroCall {
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_CALL }
...
}
impl AstNode for MacroExpr {
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_EXPR }
...
}
So a MACRO_EXPR node can be cast to MacroExpr, not MacroCall.
But wait, looking at the match_ast! block:
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => ...,
...
}
}
If the parent is a MACRO_EXPR node, none of these arms would match (since MACRO_EXPR is not MACRO_CALL, PathType, etc.), and the default arm _ => return None would be executed.
So the issue is that when the parent is a MACRO_EXPR node, the code returns None.
But wait, there's also an arm for ast::Path(parent):
ast::Path(parent) => {
...
}
This arm handles completing inside a qualifier. If the parent is a Path, this arm is executed.
But in our case, the parent is a MACRO_EXPR, not a Path. So this arm wouldn't match.
OK so the issue is that when the parent of the path is a MACRO_EXPR node, the code returns None.
But wait, I need to check if MACRO_EXPR can be cast to ast::PathExpr. Looking at the code:
ast::PathExpr(it) => ...,
PathExpr is a different AST node type. A MACRO_EXPR node is not a PathExpr node.
Hmm, but a MACRO_EXPR is an expression. So it should be handled somewhere.
Actually, looking at the code more carefully, I see that the match_ast! block checks for various parent types. If none of them match, the code returns None.
But a MACRO_EXPR is an expression, so it should be handled. Let me check if there's an arm for MacroExpr in the match_ast! block.
Looking at the code, I don't see an arm for ast::MacroExpr. So when the parent is a MACRO_EXPR node, the code falls through to the default arm and returns None.
Wait, but this would mean that completions don't work for any macro call in expression position. That doesn't seem right.
Let me re-read the code more carefully.
Actually, looking at the classify_name_ref function, I see that it's called with a name_ref and its parent. The parent is the parent of the name_ref, not the path.
So if the syntax tree is:
MACRO_EXPR
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
The parent would be the PATH_SEGMENT, not the MACRO_EXPR.
Let me re-read the code:
let segment = match_ast! {
match parent {
ast::PathSegment(segment) => segment,
...
}
};
So the code first checks if the parent is a PathSegment. If so, it extracts the segment.
Then the code gets the path:
let path = segment.parent_path();
Then the code checks the parent of the path:
let parent = path.syntax().parent()?;
let kind = 'find_kind: {
...
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => ...,
...
}
}
}
So the parent here is the parent of the PATH, which is the MACRO_EXPR node.
So when the parent of the path is a MACRO_EXPR node, the match_ast! block would not match any arm (since there's no arm for MacroExpr), and the code would return None.
Wait, but I need to check if MACRO_EXPR can be cast to any of the other AST types.
Looking at the AstNode trait implementations:
PathType::can_cast returns true only for PATH_TYPE kind
PathExpr::can_cast returns true only for PATH_EXPR kind
TupleStructPat::can_cast returns true only for TUPLE_STRUCT_PAT kind
RecordPat::can_cast returns true only for RECORD_PAT kind
PathPat::can_cast returns true only for PATH_PAT kind
MacroCall::can_cast returns true only for MACRO_CALL kind
Meta::can_cast returns true only for META kind
VisibilityInner::can_cast returns true only for VISIBILITY_INNER kind
UseTree::can_cast returns true only for USE_TREE kind
Path::can_cast returns true only for PATH kind
RecordExpr::can_cast returns true only for RECORD_EXPR kind
So a MACRO_EXPR node cannot be cast to any of these types. The match_ast! block would fall through to the default arm and return None.
So the issue is that when the parent of the path is a MACRO_EXPR node, the code returns None.
But wait, I also need to check if there's a MacroExpr arm in the match_ast! block. Let me look at the code again.
Looking at the code:
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => {
kind_macro_call(it)?
},
ast::Meta(meta) => ...,
ast::VisibilityInner(it) => ...,
ast::UseTree(_) => ...,
ast::Path(parent) => ...,
ast::RecordExpr(it) => ...,
_ => return None,
}
}
There's no arm for ast::MacroExpr. So when the parent is a MACRO_EXPR node, the code falls through to the default arm and returns None.
But wait, this would mean that completions don't work for any macro call in expression position. Let me check if this is the case by looking at existing tests.
Actually, looking at the test file, I see there are tests for macro completions. Let me check if they pass.
But first, let me re-think the issue. The issue is about completing inside foo!(bar$0) where foo is ($i:ident) => { $i!() }.
In this case:
- The user types
foo!(bar and triggers completion.
- The code finds the token tree
bar inside the macro invocation.
- The code expands
foo!(bar<fake_ident>) to bar<fake_ident>!().
- The code analyzes the position in the expansion.
In step 4, the code finds the name-like node in the expansion. The name-like node is the NAME_REF bar<fake_ident>.
But wait, in the expansion bar<fake_ident>!(), is the parent of the NAME_REF a MACRO_EXPR node or a MACRO_CALL node?
Looking at the parser test data, when a macro call is in expression position, it's parsed as a MACRO_EXPR node. So bar<fake_ident>!() would be a MACRO_EXPR node.
So the parent of the PATH (which contains the NAME_REF) would be a MACRO_EXPR node.
And in classify_name_ref, the match_ast! block doesn't have an arm for MacroExpr. So the code would fall through to the default arm and return None.
So the issue is that the match_ast! block in classify_name_ref doesn't have an arm for MacroExpr.
But wait, there's also an arm for ast::MacroCall(it). And MacroCall is different from MacroExpr.
So the issue is that when the parent of the path is a MACRO_EXPR node, the code doesn't know how to handle it.
OK so I think the fix should be to add an arm for MacroExpr in the match_ast! block. Or, alternatively, I should add an arm for MacroExpr in the kind_macro_call closure.
But wait, kind_macro_call is only called when the parent of the path is a MacroCall, not a MacroExpr. So adding an arm to kind_macro_call wouldn't help.
I need to add an arm for MacroExpr in the match_ast! block in classify_name_ref.
Let me look at the code again:
match_ast! {
match parent {
ast::PathType(it) => ...,
ast::PathExpr(it) => ...,
ast::TupleStructPat(it) => ...,
ast::RecordPat(it) => ...,
ast::PathPat(it) => ...,
ast::MacroCall(it) => {
kind_macro_call(it)?
},
ast::Meta(meta) => ...,
ast::VisibilityInner(it) => ...,
ast::UseTree(_) => ...,
ast::Path(parent) => ...,
ast::RecordExpr(it) => ...,
_ => return None,
}
}
I need to add an arm for ast::MacroExpr(expr):
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
This would handle the case where the parent of the path is a MacroExpr.
But wait, this is similar to what I tried earlier. The issue is that I also need to handle the case where the parent is a MacroCall and its parent is None.
OK let me think about this more carefully.
The original code has:
ast::MacroCall(it) => {
kind_macro_call(it)?
}
And kind_macro_call is defined as:
let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
let parent = it.syntax().parent()?;
if let Some(kind) = kind_item(it.syntax()) {
return Some(kind);
}
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
ast::MacroPat(it) => ...,
ast::MacroType(ty) => ...,
_ => return None,
}
};
Some(kind)
};
So kind_macro_call is called when the parent of the path is a MacroCall. Then it checks the parent of the MacroCall to determine the path kind.
In the case of foo!(bar$0) in the original file, the syntax tree is:
MACRO_CALL
PATH
PATH_SEGMENT
NAME_REF: foo
EXCL_TOKEN
TOKEN_TREE
NAME_REF: bar
So the parent of the NAME_REF foo is a PATH, and the parent of the PATH is the MACRO_CALL foo!().
In classify_name_ref, the code checks if the parent of the path is a MacroCall. If so, kind_macro_call is called.
In kind_macro_call, the parent of the MACRO_CALL is checked. If the MACRO_CALL is inside a fn main() { ... }, its parent would be a BLOCK_EXPR or EXPR_STMT.
So the match in kind_macro_call:
match_ast! {
match parent {
ast::MacroExpr(expr) => ...,
ast::MacroPat(it) => ...,
ast::MacroType(ty) => ...,
_ => return None,
}
}
Would not match any arm (since the parent is a BLOCK_EXPR or EXPR_STMT, not a MacroExpr, MacroPat, or MacroType). So it would return None.
So the issue is that when the parent of the MacroCall is a BLOCK_EXPR or EXPR_STMT, the code returns None.
But wait, this would mean that completions don't work for any macro call in expression position in a function body. That doesn't seem right.
Let me re-read the issue description. The issue is about completing inside foo!(bar$0), where bar is the argument to the macro foo!.
So in the original file foo!(bar$0), the user is completing bar, which is inside the TOKEN_TREE of the MacroCall.
The code:
- Finds the token tree
bar inside the macro invocation.
- Expands
foo!(bar<fake_ident>) to bar<fake_ident>!().
- Analyzes the position in the expansion.
In step 3, the code finds the name-like node in the expansion. The name-like node is the NAME_REF bar<fake_ident>.
But in the expansion bar<fake_ident>!(), the syntax tree is:
MACRO_CALL (or MACRO_EXPR)
PATH
PATH_SEGMENT
NAME_REF: bar<fake_ident>
EXCL_TOKEN
TOKEN_TREE: ()
The parent of the PATH is the MACRO_CALL (or MACRO_EXPR).
So in classify_name_ref, the code checks if the parent of the path is a MacroCall. If so, kind_macro_call is called.
In kind_macro_call, the parent of the MACRO_CALL is checked. If the MACRO_CALL is at the root of the expansion (no parent), the closure returns None.
So the issue is that when the MACRO_CALL is at the root of the expansion, kind_macro_call returns None.
My fix handles this case by treating the path as an expression path. But the fix uses ast::MacroExpr { syntax: ... }, which doesn't work because the syntax field is private.
So I need to find another way to handle this case.
One option is to add an arm for MacroExpr in the match_ast! block in classify_name_ref. This would handle the case where the parent of the path is a MacroExpr.
But this doesn't directly address the issue. The issue is that kind_macro_call returns None when the parent of the MacroCall is None.
Another option is to modify kind_macro_call to handle the None case. Instead of returning None, it should return a PathKind::Expr.
To do this, I need to create a PathKind::Expr. I can do this by creating a PathExprCtx with default values and wrapping it in a PathKind::Expr.
Let me look at how PathExprCtx can be created with default values.
Actually, looking at the code, I see that PathExprCtx doesn't have a Default implementation. So I need to create it manually.
But creating a PathExprCtx manually would be verbose and error-prone.
An alternative is to use the make_path_kind_expr function. But this function takes an ast::Expr as input.
Wait, but if the parent of the path is a MACRO_EXPR node, I can cast it to ast::MacroExpr and pass it to make_path_kind_expr.
Let me check if MacroExpr can be cast to Expr:
Looking at the code:
impl From<MacroExpr> for Expr {
fn from(node: MacroExpr) -> Expr { Expr::MacroExpr(node) }
}
So yes, MacroExpr can be converted to Expr.
So if I have a MacroExpr, I can pass it to make_path_kind_expr by converting it to Expr.
Now, the issue is that in kind_macro_call, I have a MacroCall, not a MacroExpr. So I can't directly pass it to make_path_kind_expr.
But wait, looking at the code again:
let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
let parent = it.syntax().parent()?;
...
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
...
}
};
...
};
The match_ast! block tries to cast the parent to ast::MacroExpr. If the parent is a MACRO_EXPR node, the cast succeeds, and the code calls make_path_kind_expr(expr.into()).
So the issue is that when the parent is None, the code returns None early (due to the ? operator).
My fix handles this case by checking if the parent is None before calling match_ast!. If the parent is None, the code treats the path as an expression path.
But to treat the path as an expression path, I need to create a PathKind::Expr. And to create a PathKind::Expr, I need an ast::Expr.
The issue is that I have a MacroCall, not an Expr. So I can't directly create an Expr from the MacroCall.
But wait, looking at the code, I see that the parent of the path is a MacroCall. And the MacroCall is a syntax node. If the syntax node has kind MACRO_CALL, it can be cast to MacroCall. If it has kind MACRO_EXPR, it can be cast to MacroExpr.
So if the parent of the path is a MACRO_EXPR node, the cast to ast::MacroExpr would succeed, and the code would call make_path_kind_expr(expr.into()).
But if the parent of the path is a MACRO_CALL node (not a MACRO_EXPR node), the cast to ast::MacroExpr would fail, and the default arm would be executed.
Hmm, but looking at the parser test data, when a macro call is in expression position, it's parsed as a MACRO_EXPR node. So the parent of the path should be a MACRO_EXPR node, not a MACRO_CALL node.
But wait, the match_ast! block in classify_name_ref has an arm for ast::MacroCall(it). This arm is executed when the parent of the path is a MACRO_CALL node.
So if the parent of the path is a MACRO_EXPR node, the ast::MacroCall(it) arm would not match. Instead, the code would fall through to the default arm and return None.
So the issue is that the match_ast! block in classify_name_ref doesn't have an arm for ast::MacroExpr.
Let me add an arm for ast::MacroExpr(expr):
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
This would handle the case where the parent of the path is a MacroExpr.
But I also need to handle the case where the parent is a MacroCall and its parent is None. This is handled by my fix to kind_macro_call.
So the complete fix should be:
- Modify
kind_macro_call to handle the None parent case.
- Add an arm for
ast::MacroExpr(expr) in the match_ast! block in classify_name_ref.
Actually, let me re-read the issue description again. The issue is "Cannot complete macro path in macro". The code snippet is:
macro_rules! bar { () => (); }
macro_rules! foo { ($i:ident) => { $i!() }; }
fn main() {
foo!(bar$0);
}
So the user is completing bar inside foo!(bar$0).
In this case:
- The user types
foo!(bar and triggers completion.
- The code finds the token tree
bar inside the macro invocation foo!(...).
- The code expands
foo!(bar<fake_ident>) to bar<fake_ident>!().
- The code analyzes the position in the expansion.
In step 4, the code finds the name-like node in the expansion. The name-like node is the NAME_REF bar<fake_ident>. The parent of the NAME_REF is a PATH, and the parent of the PATH is the MACRO_CALL (or MACRO_EXPR) bar<fake_ident>!().
If the parent is a MACRO_EXPR node, the match_ast! block in classify_name_ref doesn't have an arm for MacroExpr. So the code falls through to the default arm and returns None.
If the parent is a MACRO_CALL node, the ast::MacroCall(it) arm is executed, and kind_macro_call is called. In kind_macro_call, the parent of the MACRO_CALL is checked. If the MACRO_CALL is at the root of the expansion (no parent), the closure returns None.
So the issue could be either:
- The parent of the path is a
MACRO_EXPR node, and the match_ast! block doesn't have an arm for MacroExpr.
- The parent of the path is a
MACRO_CALL node, and the parent of the MACRO_CALL is None.
In both cases, the code returns None, and completions fail.
My fix handles case 2 by modifying kind_macro_call to handle the None parent case. But it doesn't handle case 1.
I need to also handle case 1 by adding an arm for ast::MacroExpr(expr) in the match_ast! block in classify_name_ref.
But wait, let me check if the parent of the path is a MACRO_EXPR node or a MACRO_CALL node.
Looking at the parser test data:
crates/parser/test_data/parser/inline/ok/path_expr.rast:84: MACRO_EXPR
This shows that a macro call in expression position is parsed as a MACRO_EXPR node.
So in the expansion bar<fake_ident>!(), the syntax node should be a MACRO_EXPR node.
So the parent of the path should be a MACRO_EXPR node, and the match_ast! block in classify_name_ref should have an arm for ast::MacroExpr(expr).
Let me check if there's already an arm for MacroExpr in the match_ast! block.
Looking at the code, I don't see an arm for ast::MacroExpr. So the code falls through to the default arm and returns None.
So the fix should be to add an arm for ast::MacroExpr(expr) in the match_ast! block in classify_name_ref.
But I also need to handle the case where the parent is a MacroCall and its parent is None. This is handled by my fix to kind_macro_call.
So the complete fix should include both changes.
But my fix to kind_macro_call uses ast::MacroExpr { syntax: ... }, which doesn't work because the syntax field is private.
So I need to find another way to handle the None parent case.
One option is to check if the syntax node has kind MACRO_EXPR and if so, cast it to MacroExpr. But the syntax node has kind MACRO_CALL, not MACRO_EXPR.
Wait, but in the None parent case, the syntax node of the MacroCall is the root of the expansion. If the expansion is a macro call in expression position, the syntax node should have kind MACRO_EXPR, not MACRO_CALL.
Hmm, but a MacroCall struct wraps a syntax node with kind MACRO_CALL. So if the syntax node has kind MACRO_EXPR, it would be wrapped in a MacroExpr struct, not a MacroCall struct.
So in the None parent case, the syntax node should have kind MACRO_CALL (since it's a MacroCall struct).
But wait, if the expansion is a macro call in expression position, the syntax node should have kind MACRO_EXPR. So it would be wrapped in a MacroExpr struct.
So the kind_macro_call closure wouldn't be called in this case. Instead, the ast::MacroExpr(expr) arm in the match_ast! block would be executed.
But there's no arm for ast::MacroExpr in the match_ast! block. So the code falls through to the default arm and returns None.
So the issue is that the match_ast! block in classify_name_ref doesn't have an arm for ast::MacroExpr.
OK so I think the fix should be to add an arm for ast::MacroExpr(expr) in the match_ast! block in classify_name_ref.
And also, I should handle the case where the parent of the path is a MacroCall and its parent is None. This is handled by my fix to kind_macro_call, but I need to find a way to create a PathKind::Expr without using ast::MacroExpr { syntax: ... }.
Let me think about this differently. In the None parent case, the syntax node of the MacroCall is the root of the expansion. If the expansion is a macro call, the syntax node should have kind MACRO_CALL (since it's a MacroCall struct).
But if the expansion is a macro call in expression position, the syntax node should have kind MACRO_EXPR. So it would be wrapped in a MacroExpr struct, not a MacroCall struct.
So the kind_macro_call closure wouldn't be called in this case. Instead, the code would check the parent of the path, which would be a MacroExpr node. And the match_ast! block in classify_name_ref would try to match it.
But there's no arm for ast::MacroExpr in the match_ast! block. So the code falls through to the default arm and returns None.
So the issue is that the match_ast! block in classify_name_ref doesn't have an arm for ast::MacroExpr.
And also, there's a case where the parent of the path is a MacroCall and its parent is None. This is handled by my fix to kind_macro_call.
But actually, if the parent of the path is a MacroCall, it means the syntax node has kind MACRO_CALL. And if the syntax node has kind MACRO_CALL, it means the macro call is not in expression position (or it's parsed as a MACRO_CALL node for some other reason).
In this case, the parent of the MacroCall could be None (e.g., root of expansion) or something else (e.g., BLOCK_EXPR).
If the parent is None, the closure returns None. If the parent is something else (e.g., BLOCK_EXPR), the match_ast! block doesn't match any arm, and the closure returns None.
So the issue is that kind_macro_call returns None when the parent of the MacroCall is None or not a recognized type.
My fix handles this case by treating the path as an expression path. But the fix uses ast::MacroExpr { syntax: ... }, which doesn't work.
Let me find another way to handle this case.
One option is to create a PathKind::Expr directly without calling make_path_kind_expr. I can do this by creating a PathExprCtx with default values.
Looking at the code, PathExprCtx has many fields. Creating it manually would be verbose.
But actually, looking at the make_path_kind_expr function, it extracts information from the expression and creates a PathKind::Expr. If I can extract the same information from the MacroCall's syntax node, I could create the PathKind::Expr directly.
But this would require duplicating the logic in make_path_kind_expr.
Another option is to use the fact that the MacroCall has an excl_token() method. I can check if the macro call has a bang token and set path_ctx.has_macro_bang accordingly.
Actually, looking at the code, I see that kind_macro_call already sets path_ctx.has_macro_bang = it.excl_token().is_some() at the beginning. So the PathCompletionCtx already has some information.
The issue is that kind_macro_call returns a PathKind, which is used to set path_ctx.kind. If I return a PathKind::Expr with a default PathExprCtx, it might work.
Let me try to create a PathKind::Expr directly.
Looking at the code, PathKind::Expr is defined as:
PathKind::Expr {
expr_ctx: PathExprCtx {
in_block_expr: bool,
in_breakable: Option<BreakableKind>,
...
},
}
I can create a synthetic PathExprCtx with default values. But this would be verbose.
Actually, looking at the code, I see that PathExprCtx is created with various values extracted from the expression. If I create a synthetic PathExprCtx with default values (e.g., in_block_expr: false, in_breakable: None, etc.), it might work for the case where the parent is None.
Let me try this approach.
But actually, I think there's a simpler fix. The issue is that when the parent of the MacroCall is None, the code returns None. But if the parent of the MacroCall is None, it means the MacroCall is at the root of the syntax tree. In this case, the syntax node should have kind MACRO_EXPR (since it's in expression position).
But if the syntax node has kind MACRO_EXPR, it would be wrapped in a MacroExpr struct, not a MacroCall struct. So the kind_macro_call closure wouldn't be called.
So the None parent case might not be reachable in practice. The issue might be that the parent of the path is a MacroCall, and the parent of the MacroCall is not a recognized type (e.g., BLOCK_EXPR).
In this case, the match_ast! block in kind_macro_call doesn't match any arm, and the closure returns None.
My fix changes the default arm to treat the path as an expression path. But the fix uses ast::MacroExpr { syntax: ... }, which doesn't work.
Let me think about this differently. If the parent of the MacroCall is not a recognized type (e.g., BLOCK_EXPR), I want to treat the path as an expression path. To do this, I need to create a PathKind::Expr.
One way to do this is to check the syntax node of the MacroCall and see if it has kind MACRO_EXPR. If so, I can cast it to MacroExpr and pass it to make_path_kind_expr.
But the MacroCall struct wraps a syntax node with kind MACRO_CALL, not MACRO_EXPR. So the cast to MacroExpr would fail.
Hmm, this is tricky.
Actually, let me re-read the code more carefully.
Looking at the kind_macro_call closure:
let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
let parent = it.syntax().parent()?;
...
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
...
}
};
...
};
Here, it is a MacroCall, and it.syntax() is its syntax node. The parent of this syntax node is what we're looking for.
If the parent is a MacroExpr, the code calls make_path_kind_expr(expr.into()).
But if the parent is None, the code returns None early.
And if the parent is not a MacroExpr, MacroPat, or MacroType, the code returns None from the default arm.
So the issue is that the code returns None when the parent is None or not a recognized type.
My fix handles this case by treating the path as an expression path. But the fix uses ast::MacroExpr { syntax: ... }, which doesn't work.
Let me try a different approach. Instead of creating a MacroExpr, I can check if the syntax node of the MacroCall has kind MACRO_EXPR. If so, I can cast it to MacroExpr and pass it to make_path_kind_expr.
But the MacroCall struct wraps a syntax node with kind MACRO_CALL, not MACRO_EXPR. So this check would always fail.
Hmm, let me think about this differently.
Actually, I think the issue is that I'm overcomplicating this. Let me re-read the code.
The match_ast! block in kind_macro_call tries to cast the parent to different AST types. If the cast succeeds, the corresponding arm is executed.
If the parent is a MacroExpr, the arm ast::MacroExpr(expr) => make_path_kind_expr(expr.into()) is executed.
If the parent is not a MacroExpr, MacroPat, or MacroType, the default arm _ => return None is executed.
So the issue is that the code returns None when the parent is not a recognized type.
My fix changes the default arm to treat the path as an expression path. To do this, I need to create a PathKind::Expr.
But I can't create a MacroExpr from a MacroCall because the syntax field is private.
One option is to check if the syntax node of the MacroCall can be cast to MacroExpr. But the syntax node has kind MACRO_CALL, so the cast would fail.
Another option is to create a PathKind::Expr directly without creating a MacroExpr.
Looking at the code, I see that PathKind::Expr contains a PathExprCtx. I can create a synthetic PathExprCtx with default values.
Let me try this approach.
Actually, wait. Let me re-read the match_ast! macro definition:
macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { $crate::match_ast!(match ($node) { $($tt)* }) };
(match ($node:expr) {
$( $( $path:ident )::+ ($it:pat) => $res:expr, )*
_ => $catch_all:expr $(,)?
}) => {{
$( if let Some($it) = $($path::)+cast($node.clone()) { $res } else )*
{ $catch_all }
}};
}
So match_ast! tries to cast the node to different AST types using the cast method. If the cast succeeds, the corresponding arm is executed.
In kind_macro_call:
match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
ast::MacroPat(it) => ...,
ast::MacroType(ty) => ...,
_ => return None,
}
}
The parent is a SyntaxNode. The macro tries to cast it to MacroExpr, MacroPat, and MacroType. If none of the casts succeed, the default arm _ => return None is executed.
So if the parent is a BLOCK_EXPR node, none of the casts would succeed, and the default arm would be executed.
My fix changes the default arm to create a PathKind::Expr. But to create a PathKind::Expr, I need an ast::Expr.
Actually, let me check if SyntaxNode has a method to check its kind. If the syntax node has kind MACRO_EXPR, I can cast it to MacroExpr.
But the parent is already a SyntaxNode. If it has kind MACRO_EXPR, the cast to MacroExpr would succeed, and the ast::MacroExpr(expr) arm would be executed.
So the issue is that the parent doesn't have kind MACRO_EXPR. It has kind BLOCK_EXPR or something else.
OK I think I understand the issue now. When the parent of the MacroCall is a BLOCK_EXPR (or other unrecognized type), the code returns None. My fix changes the default arm to treat the path as an expression path.
But to create a PathKind::Expr, I need an ast::Expr. And I can't create an Expr from a MacroCall.
Let me check if there's a way to create an Expr from a SyntaxNode.
Looking at the code, I see that Expr is an enum with various variants. Each variant can be created by casting a syntax node of the corresponding kind.
So if the syntax node has kind MACRO_EXPR, I can cast it to Expr::MacroExpr. But if the syntax node has kind BLOCK_EXPR, I can cast it to Expr::BlockExpr.
But in our case, the syntax node has kind BLOCK_EXPR, not MACRO_EXPR. So I can cast it to Expr::BlockExpr.
But wait, the syntax node I have is the parent of the MacroCall, not the MacroCall itself. So if I cast the parent to Expr::BlockExpr, I get a BlockExpr, not a MacroExpr.
Hmm, this doesn't help.
OK let me think about this differently. The issue is that kind_macro_call returns None when the parent of the MacroCall is not a recognized type.
The fix should return a PathKind::Expr instead of None.
To create a PathKind::Expr, I need to create a PathExprCtx. The PathExprCtx is created by extracting information from the expression.
If I can't create an Expr from the MacroCall, I need to find another way to create the PathExprCtx.
Actually, looking at the code, I see that the PathExprCtx has many fields with default values. If I create a synthetic PathExprCtx with default values, it might work.
Let me check if PathExprCtx can be created with default values.
Looking at the code, I don't see a Default implementation for PathExprCtx. So I need to create it manually.
But creating it manually would be verbose and error-prone.
Actually, looking at the kind_macro_call closure, I see that it sets path_ctx.has_macro_bang = it.excl_token().is_some() at the beginning. So the PathCompletionCtx already has some information.
The issue is that kind_macro_call returns a PathKind, which is used to set path_ctx.kind. If I return a PathKind::Expr with a default PathExprCtx, it might work.
Let me try to create a PathKind::Expr directly.
Looking at the code in context.rs, I see that PathExprCtx has the following fields:
- in_block_expr: bool
- in_breakable: Option
- after_if_expr: bool
- before_else_kw: bool
- in_condition: bool
- ref_expr_parent: Optionast::RefExpr
- after_amp: bool
- is_func_update: Optionast::RecordExpr
- innermost_ret_ty: Optionhir::Type
- innermost_breakable_ty: Optionhir::Type
- self_param: Option<Either<hir::SelfParam, hir::Param>>
- in_value: bool
- incomplete_let: bool
- after_incomplete_let: bool
- impl_: Optionast::Impl
- in_match_guard: bool
I can create a synthetic PathExprCtx with default values:
- in_block_expr: false
- in_breakable: None
- after_if_expr: false
- before_else_kw: false
- in_condition: false
- ref_expr_parent: None
- after_amp: false
- is_func_update: None
- innermost_ret_ty: None
- innermost_breakable_ty: None
- self_param: None
- in_value: false
- incomplete_let: false
- after_incomplete_let: false
- impl_: None
- in_match_guard: false
Let me try this approach.
But wait, I also need to consider the case where the parent of the path is a MacroCall. In this case, the MacroCall is the parent of the path, and the parent of the MacroCall is None (root of expansion).
In this case, I want to treat the path as an expression path. To do this, I can check if the syntax node of the MacroCall has kind MACRO_EXPR. But it doesn't (it has kind MACRO_CALL).
Hmm, but actually, if the MacroCall is at the root of the expansion, it might be parsed as a MACRO_EXPR node. So the MacroCall struct would wrap a syntax node with kind MACRO_EXPR.
But looking at the AstNode trait implementation for MacroCall:
impl AstNode for MacroCall {
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_CALL }
...
}
So a syntax node with kind MACRO_EXPR cannot be cast to MacroCall. It would be cast to MacroExpr instead.
So if the syntax node has kind MACRO_EXPR, it would be wrapped in a MacroExpr struct, not a MacroCall struct.
So the kind_macro_call closure wouldn't be called in this case. Instead, the code would check the parent of the path, which would be a MacroExpr node. And the match_ast! block in classify_name_ref would try to match it.
But there's no arm for ast::MacroExpr in the match_ast! block. So the code falls through to the default arm and returns None.
So the issue is that the match_ast! block in classify_name_ref doesn't have an arm for ast::MacroExpr.
OK so I think the fix should be to add an arm for ast::MacroExpr(expr) in the match_ast! block in classify_name_ref.
And also, I should handle the case where the parent of the path is a MacroCall and its parent is None. This is handled by my fix to kind_macro_call.
But my fix to kind_macro_call uses ast::MacroExpr { syntax: ... }, which doesn't work.
Let me think about how to handle the None parent case.
If the parent of the MacroCall is None, it means the MacroCall is at the root of the syntax tree. In this case, I want to treat the path as an expression path.
To do this, I need to create a PathKind::Expr. But I can't create an Expr from a MacroCall.
One option is to check if the syntax node of the MacroCall has kind MACRO_EXPR. But it doesn't (it has kind MACRO_CALL).
Another option is to create a synthetic PathExprCtx with default values.
Let me try this approach.
Actually, I just realized something. Looking at the code in context/analysis.rs, I see that there's a make_path_kind_expr function that creates a PathKind::Expr. This function takes an ast::Expr as input.
But I can also look at how PathKind::Expr is created in other parts of the code. For example, in the ast::PathExpr(it) arm:
ast::PathExpr(it) => {
...
make_path_kind_expr(it.into())
},
Here, it is a PathExpr, and it.into() converts it to an Expr. Then make_path_kind_expr is called with the Expr.
So the pattern is to convert the AST node to an Expr and pass it to make_path_kind_expr.
But in the None parent case, I have a MacroCall, not an Expr. And I can't convert a MacroCall to an Expr.
Hmm, let me check if there's a way to convert a MacroCall to an Expr.
Looking at the code, I see that MacroExpr can be converted to Expr:
impl From<MacroExpr> for Expr {
fn from(node: MacroExpr) -> Expr { Expr::MacroExpr(node) }
}
But MacroCall cannot be directly converted to Expr.
Wait, but looking at the syntax tree, a MACRO_CALL node is the same as a MACRO_EXPR node. The difference is just how it's wrapped.
So if I have a MacroCall that wraps a syntax node with kind MACRO_CALL, and I want to treat it as a MacroExpr, I need to find a way to create a MacroExpr from the same syntax node.
But the syntax field of MacroExpr is private, so I can't directly create a MacroExpr.
Hmm, let me check if there's a public method to create a MacroExpr.
Actually, looking at the code, I see that AstNode has a cast method:
fn cast(syntax: SyntaxNode) -> Option<Self>
So I can use ast::MacroExpr::cast(syntax) to try to cast a syntax node to MacroExpr. But this would return None if the syntax node has kind MACRO_CALL, not MACRO_EXPR.
OK I think I need to take a different approach. Let me check if the SyntaxNode has a method to check its kind.
Looking at the code, I see that SyntaxNode has a kind() method that returns the kind of the node.
So I can check if the syntax node of the MacroCall has kind MACRO_EXPR:
if it.syntax().kind() == SyntaxKind::MACRO_EXPR {
// The syntax node has kind MACRO_EXPR, so I can cast it to MacroExpr
if let Some(macro_expr) = ast::MacroExpr::cast(it.syntax().clone()) {
return Some(make_path_kind_expr(macro_expr.into()));
}
}
But wait, if the syntax node has kind MACRO_EXPR, it wouldn't be wrapped in a MacroCall struct. It would be wrapped in a MacroExpr struct.
So the kind_macro_call closure wouldn't be called in this case.
Hmm, this is confusing.
OK let me just try a different approach. Instead of trying to create a MacroExpr, I'll create a PathKind::Expr directly with a synthetic PathExprCtx.
Let me look at the PathExprCtx struct and see if I can create one with default values.
Actually, I just realized that I should look at what imports are available in the file. Maybe there's a way to create a PathKind::Expr using the available types.
Let me check the imports in the file.
Actually, let me just try to create the PathKind::Expr directly with default values and see if it compiles.
<tool_call>
<function=bash_terminal>
<parameter=command>
cd /rust-analyzer && python3 << 'PYTHON_EOF'
Read the file
with open('crates/ide-completion/src/context/analysis.rs', 'r') as f:
content = f.read()
The current (broken) code
old_code = ''' let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
// First check if it's an item position
if let Some(kind) = kind_item(it.syntax()) {
return Some(kind);
}
// Get the parent
let parent = it.syntax().parent();
// Handle None parent case (e.g., root of expansion)
let Some(parent) = parent else {
// When macro has no parent, treat as expression path by creating
// a MacroExpr wrapper around the macro call's syntax node
let macro_expr = ast::MacroExpr { syntax: it.syntax().clone() };
return Some(make_path_kind_expr(macro_expr.into()));
};
// Use match_ast! to determine the path kind
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
ast::MacroPat(it) => PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())},
ast::MacroType(ty) => make_path_kind_type(ty.into()),
// For other parent types (e.g., BlockExpr, ExprStmt), treat as expression path
_ => {
let macro_expr = ast::MacroExpr { syntax: it.syntax().clone() };
make_path_kind_expr(macro_expr.into())
},
}
};
Some(kind)
};'''
The new code - create PathKind::Expr directly without MacroExpr
new_code = ''' let mut kind_macro_call = |it: ast::MacroCall| {
path_ctx.has_macro_bang = it.excl_token().is_some();
// First check if it's an item position
if let Some(kind) = kind_item(it.syntax()) {
return Some(kind);
}
// Get the parent
let parent = it.syntax().parent();
// Use match_ast! to determine the path kind
let kind = match_ast! {
match parent {
ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
ast::MacroPat(it) => PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())},
ast::MacroType(ty) => make_path_kind_type(ty.into()),
// For other parent types (including None - root of expansion),
// treat as expression path with a synthetic PathExprCtx
_ => PathKind::Expr {
expr_ctx: PathExprCtx {
in_block_expr: false,
in_breakable: None,
after_if_expr: false,
before_else_kw: false,
in_condition: false,
ref_expr_parent: None,
after_amp: false,
is_func_update: None,
innermost_ret_ty: None,
innermost_breakable_ty: None,
self_param: None,
in_value: false,
incomplete_let: false,
after_incomplete_let: false,
impl_: None,
in_match_guard: false,
},
},
}
};
Some(kind)
};'''
Check if old code exists
if old_code in content:
content = content.replace(old_code, new_code)
with open('crates/ide-completion/src/context/analysis.rs', 'w') as f:
f.write(content)
print("Successfully replaced the code")
else:
print("ERROR: Old code not found in file")
# Let's check what's actually there
import re
match = re.search(r'let mut kind_macro_call.?Some(kind)\s};', content, re.DOTALL)
if match:
print("Found similar code:")
print(match.group(0)[:800])
else:
print("No similar code found")
PYTHON_EOF
</tool_call>