Skip to content

Commit ea27351

Browse files
authored
Merge pull request #19591 from snprajwal/fix-make-macro
fix: use `ast::TokenTree` in `make::expr_macro`
2 parents 4bef2bf + 2438542 commit ea27351

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

crates/ide-assists/src/handlers/destructure_struct_binding.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ fn build_usage_edit(
302302
}),
303303
None => Some((
304304
usage.name.syntax().as_node().unwrap().clone(),
305-
make.expr_macro(ast::make::ext::ident_path("todo"), make.arg_list([])).syntax().clone(),
305+
make.expr_macro(
306+
ast::make::ext::ident_path("todo"),
307+
make.token_tree(syntax::SyntaxKind::L_PAREN, []),
308+
)
309+
.syntax()
310+
.clone(),
306311
)),
307312
}
308313
}

crates/ide-assists/src/utils/gen_trait_fn_body.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
230230
}
231231
None => {
232232
let fmt_string = make::expr_literal(&(format!("\"{name}\""))).into();
233-
let args = make::arg_list([target, fmt_string]);
233+
let args = make::ext::token_tree_from_node(
234+
make::arg_list([target, fmt_string]).syntax(),
235+
);
234236
let macro_name = make::ext::ident_path("write");
235237
let macro_call = make::expr_macro(macro_name, args);
236238

crates/syntax/src/ast/make.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ pub mod ext {
3232
use super::*;
3333

3434
pub fn simple_ident_pat(name: ast::Name) -> ast::IdentPat {
35-
return from_text(&name.text());
36-
37-
fn from_text(text: &str) -> ast::IdentPat {
38-
ast_from_text(&format!("fn f({text}: ())"))
39-
}
35+
ast_from_text(&format!("fn f({}: ())", name.text()))
4036
}
37+
4138
pub fn ident_path(ident: &str) -> ast::Path {
4239
path_unqualified(path_segment(name_ref(ident)))
4340
}
@@ -81,7 +78,6 @@ pub mod ext {
8178
pub fn expr_self() -> ast::Expr {
8279
expr_from_text("self")
8380
}
84-
8581
pub fn zero_number() -> ast::Expr {
8682
expr_from_text("0")
8783
}
@@ -116,6 +112,10 @@ pub mod ext {
116112
pub fn ty_result(t: ast::Type, e: ast::Type) -> ast::Type {
117113
ty_from_text(&format!("Result<{t}, {e}>"))
118114
}
115+
116+
pub fn token_tree_from_node(node: &ast::SyntaxNode) -> ast::TokenTree {
117+
ast_from_text(&format!("todo!{node}"))
118+
}
119119
}
120120

121121
pub fn name(name: &str) -> ast::Name {
@@ -643,8 +643,8 @@ pub fn expr_method_call(
643643
) -> ast::MethodCallExpr {
644644
expr_from_text(&format!("{receiver}.{method}{arg_list}"))
645645
}
646-
pub fn expr_macro(path: ast::Path, arg_list: ast::ArgList) -> ast::MacroExpr {
647-
expr_from_text(&format!("{path}!{arg_list}"))
646+
pub fn expr_macro(path: ast::Path, tt: ast::TokenTree) -> ast::MacroExpr {
647+
expr_from_text(&format!("{path}!{tt}"))
648648
}
649649
pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
650650
expr_from_text(&if exclusive { format!("&mut {expr}") } else { format!("&{expr}") })
@@ -1226,7 +1226,7 @@ pub fn meta_path(path: ast::Path) -> ast::Meta {
12261226

12271227
pub fn token_tree(
12281228
delimiter: SyntaxKind,
1229-
tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>,
1229+
tt: impl IntoIterator<Item = NodeOrToken<ast::TokenTree, SyntaxToken>>,
12301230
) -> ast::TokenTree {
12311231
let (l_delimiter, r_delimiter) = match delimiter {
12321232
T!['('] => ('(', ')'),

crates/syntax/src/ast/syntax_factory/constructors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,15 @@ impl SyntaxFactory {
584584
ast
585585
}
586586

587-
pub fn expr_macro(&self, path: ast::Path, args: ast::ArgList) -> ast::MacroExpr {
588-
let ast = make::expr_macro(path.clone(), args.clone()).clone_for_update();
587+
pub fn expr_macro(&self, path: ast::Path, tt: ast::TokenTree) -> ast::MacroExpr {
588+
let ast = make::expr_macro(path.clone(), tt.clone()).clone_for_update();
589589

590590
if let Some(mut mapping) = self.mappings() {
591591
let macro_call = ast.macro_call().unwrap();
592592
let mut builder = SyntaxMappingBuilder::new(macro_call.syntax().clone());
593593
builder.map_node(path.syntax().clone(), macro_call.path().unwrap().syntax().clone());
594594
builder
595-
.map_node(args.syntax().clone(), macro_call.token_tree().unwrap().syntax().clone());
595+
.map_node(tt.syntax().clone(), macro_call.token_tree().unwrap().syntax().clone());
596596
builder.finish(&mut mapping);
597597
}
598598

0 commit comments

Comments
 (0)