Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/ast/make.rs')
-rw-r--r--crates/syntax/src/ast/make.rs99
1 files changed, 97 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index dba39204e3..b2904ce3c0 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -658,7 +658,7 @@ pub fn expr_if(
};
expr_from_text(&format!("if {condition} {then_branch} {else_branch}"))
}
-pub fn expr_for_loop(pat: ast::Pat, expr: ast::Expr, block: ast::BlockExpr) -> ast::Expr {
+pub fn expr_for_loop(pat: ast::Pat, expr: ast::Expr, block: ast::BlockExpr) -> ast::ForExpr {
expr_from_text(&format!("for {pat} in {expr} {block}"))
}
@@ -1016,7 +1016,19 @@ pub fn item_static(
}
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
- ast_from_text(&format!("fn f({ty}) {{ }}"))
+ quote! {
+ Param {
+ #ty
+ }
+ }
+}
+
+pub fn untyped_param(pat: ast::Pat) -> ast::Param {
+ quote! {
+ Param {
+ #pat
+ }
+ }
}
pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
@@ -1456,3 +1468,86 @@ pub mod tokens {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use expect_test::expect;
+
+ use super::*;
+
+ #[track_caller]
+ fn check(node: impl AstNode, expect: expect_test::Expect) {
+ let node_debug = format!("{:#?}", node.syntax());
+ expect.assert_eq(&node_debug);
+ }
+
+ #[test]
+ fn test_unnamed_param() {
+ check(
+ unnamed_param(ty("Vec")),
+ expect![[r#"
+ "#]],
+ );
+
+ check(
+ unnamed_param(ty("Vec<T>")),
+ expect![[r#"
+ "#]],
+ );
+ }
+
+ #[test]
+ fn test_untyped_param() {
+ check(
+ untyped_param(path_pat(ext::ident_path("name"))),
+ expect![[r#"
+ "#]],
+ );
+
+ check(
+ untyped_param(
+ range_pat(
+ Some(path_pat(ext::ident_path("start"))),
+ Some(path_pat(ext::ident_path("end"))),
+ )
+ .into(),
+ ),
+ expect![[r#"
+ "#]],
+ );
+ }
+}