diff options
| author | DropDemBits <r3usrlnd@gmail.com> | 2022-10-09 20:45:20 -0400 |
|---|---|---|
| committer | DropDemBits <r3usrlnd@gmail.com> | 2022-10-09 21:03:38 -0400 |
| commit | 1015a177d4a9f1c8ed853dee68dc2bf13e0cb02d (patch) | |
| tree | 30bc4882529fe0e670cfb4117723de5382c8da5e | |
| parent | bfe6ec9b778d12287fce1fdf2df7e7b0d58ee775 (diff) | |
| download | rust-1015a177d4a9f1c8ed853dee68dc2bf13e0cb02d.tar.gz rust-1015a177d4a9f1c8ed853dee68dc2bf13e0cb02d.zip | |
Have `to_generic_args` return `ast::GenericArgList`
| -rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 21 | ||||
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 23 |
2 files changed, 30 insertions, 14 deletions
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 173c064ccc9..229e7419b73 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -236,21 +236,22 @@ impl ast::GenericParamList { } } - /// Extracts the const, type, and lifetime names into a new [`ast::GenericParamList`] - pub fn to_generic_args(&self) -> ast::GenericParamList { - let params = self.generic_params().filter_map(|param| match param { - ast::GenericParam::ConstParam(it) => { - Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None))) - } + /// Constructs a matching [`ast::GenericArgList`] + pub fn to_generic_args(&self) -> ast::GenericArgList { + let args = self.generic_params().filter_map(|param| match param { ast::GenericParam::LifetimeParam(it) => { - Some(ast::GenericParam::LifetimeParam(make::lifetime_param(it.lifetime()?))) + Some(ast::GenericArg::LifetimeArg(make::lifetime_arg(it.lifetime()?))) } ast::GenericParam::TypeParam(it) => { - Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None))) + Some(ast::GenericArg::TypeArg(make::type_arg(make::ext::ty_name(it.name()?)))) + } + ast::GenericParam::ConstParam(it) => { + // Name-only const params get parsed as `TypeArg`s + Some(ast::GenericArg::TypeArg(make::type_arg(make::ext::ty_name(it.name()?)))) } }); - make::generic_param_list(params) + make::generic_arg_list(args) } } @@ -317,7 +318,7 @@ impl Removable for ast::TypeBoundList { impl ast::PathSegment { pub fn get_or_create_generic_arg_list(&self) -> ast::GenericArgList { if self.generic_arg_list().is_none() { - let arg_list = make::generic_arg_list().clone_for_update(); + let arg_list = make::generic_arg_list(empty()).clone_for_update(); ted::append_child(self.syntax(), arg_list.syntax()); } self.generic_arg_list().unwrap() diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 83f8bbac588..c9a21e12c08 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -88,6 +88,9 @@ pub mod ext { block_expr(None, None) } + pub fn ty_name(name: ast::Name) -> ast::Type { + ty_path(ident_path(&format!("{name}"))) + } pub fn ty_bool() -> ast::Type { ty_path(ident_path("bool")) } @@ -160,6 +163,7 @@ pub fn assoc_item_list() -> ast::AssocItemList { ast_from_text("impl C for D {}") } +// FIXME: `ty_params` should be `ast::GenericArgList` pub fn impl_( ty: ast::Path, params: Option<ast::GenericParamList>, @@ -185,10 +189,6 @@ pub fn impl_trait( ast_from_text(&format!("impl{ty_params} {trait_} for {ty}{ty_params} {{}}")) } -pub(crate) fn generic_arg_list() -> ast::GenericArgList { - ast_from_text("const S: T<> = ();") -} - pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("type __ = {name_ref};")) } @@ -718,6 +718,21 @@ pub fn generic_param_list( ast_from_text(&format!("fn f<{args}>() {{ }}")) } +pub fn type_arg(ty: ast::Type) -> ast::TypeArg { + ast_from_text(&format!("const S: T<{ty}> = ();")) +} + +pub fn lifetime_arg(lifetime: ast::Lifetime) -> ast::LifetimeArg { + ast_from_text(&format!("const S: T<{lifetime}> = ();")) +} + +pub(crate) fn generic_arg_list( + args: impl IntoIterator<Item = ast::GenericArg>, +) -> ast::GenericArgList { + let args = args.into_iter().join(", "); + ast_from_text(&format!("const S: T<{args}> = ();")) +} + pub fn visibility_pub_crate() -> ast::Visibility { ast_from_text("pub(crate) struct S") } |
