about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-04-22 11:43:42 +0000
committerbors <bors@rust-lang.org>2023-04-22 11:43:42 +0000
commit2feabc4dc462644287372922928110eea4c60ca7 (patch)
tree575e4b2aad3c44ee70cb97897da4974c568b81a7
parent5750d81e3032cf925aa9422a8ac128a2b3a1950c (diff)
parente275f77b2cb070e84f76e9e493560d2d9a531bff (diff)
downloadrust-2feabc4dc462644287372922928110eea4c60ca7.tar.gz
rust-2feabc4dc462644287372922928110eea4c60ca7.zip
Auto merge of #14622 - alibektas:make_ty_alias, r=alibektas
Add syntax::make::ty_alias

There was until now no function that returns TypeAlias. This commit introduces a func that is fully compliant with the Rust Reference. I had problems working with Ident so for now the function uses simple string manipulation until ast_from_text function is called. I am however open to any ideas that could replace ident param in such a way that it accepts syntax::ast::Ident
-rw-r--r--crates/syntax/src/ast/make.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index c56ddb51609..cc0d7f4b48e 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -158,6 +158,47 @@ fn ty_from_text(text: &str) -> ast::Type {
     ast_from_text(&format!("type _T = {text};"))
 }
 
+/// Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html)
+/// Type Alias syntax is
+/// ```
+/// TypeAlias :
+/// type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ;
+/// ```
+/// FIXME : ident should be of type ast::Ident
+pub fn ty_alias(
+    ident: &str,
+    generic_param_list: Option<ast::GenericParamList>,
+    type_param_bounds: Option<ast::TypeParam>,
+    where_clause: Option<ast::WhereClause>,
+    assignment: Option<(ast::Type, Option<ast::WhereClause>)>,
+) -> ast::TypeAlias {
+    let mut s = String::new();
+    s.push_str(&format!("type {}", ident));
+
+    if let Some(list) = generic_param_list {
+        s.push_str(&list.to_string());
+    }
+
+    if let Some(list) = type_param_bounds {
+        s.push_str(&format!(" : {}", &list));
+    }
+
+    if let Some(cl) = where_clause {
+        s.push_str(&format!(" {}", &cl.to_string()));
+    }
+
+    if let Some(exp) = assignment {
+        if let Some(cl) = exp.1 {
+            s.push_str(&format!("= {} {}", &exp.0.to_string(), &cl.to_string()));
+        } else {
+            s.push_str(&format!("= {}", &exp.0.to_string()));
+        }
+    }
+
+    s.push_str(";");
+    ast_from_text(&s)
+}
+
 pub fn assoc_item_list() -> ast::AssocItemList {
     ast_from_text("impl C for D {}")
 }