Skip to content

Commit 1015a17

Browse files
committed
Have to_generic_args return ast::GenericArgList
1 parent bfe6ec9 commit 1015a17

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

crates/syntax/src/ast/edit_in_place.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,22 @@ impl ast::GenericParamList {
236236
}
237237
}
238238

239-
/// Extracts the const, type, and lifetime names into a new [`ast::GenericParamList`]
240-
pub fn to_generic_args(&self) -> ast::GenericParamList {
241-
let params = self.generic_params().filter_map(|param| match param {
242-
ast::GenericParam::ConstParam(it) => {
243-
Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None)))
244-
}
239+
/// Constructs a matching [`ast::GenericArgList`]
240+
pub fn to_generic_args(&self) -> ast::GenericArgList {
241+
let args = self.generic_params().filter_map(|param| match param {
245242
ast::GenericParam::LifetimeParam(it) => {
246-
Some(ast::GenericParam::LifetimeParam(make::lifetime_param(it.lifetime()?)))
243+
Some(ast::GenericArg::LifetimeArg(make::lifetime_arg(it.lifetime()?)))
247244
}
248245
ast::GenericParam::TypeParam(it) => {
249-
Some(ast::GenericParam::TypeParam(make::type_param(it.name()?, None)))
246+
Some(ast::GenericArg::TypeArg(make::type_arg(make::ext::ty_name(it.name()?))))
247+
}
248+
ast::GenericParam::ConstParam(it) => {
249+
// Name-only const params get parsed as `TypeArg`s
250+
Some(ast::GenericArg::TypeArg(make::type_arg(make::ext::ty_name(it.name()?))))
250251
}
251252
});
252253

253-
make::generic_param_list(params)
254+
make::generic_arg_list(args)
254255
}
255256
}
256257

@@ -317,7 +318,7 @@ impl Removable for ast::TypeBoundList {
317318
impl ast::PathSegment {
318319
pub fn get_or_create_generic_arg_list(&self) -> ast::GenericArgList {
319320
if self.generic_arg_list().is_none() {
320-
let arg_list = make::generic_arg_list().clone_for_update();
321+
let arg_list = make::generic_arg_list(empty()).clone_for_update();
321322
ted::append_child(self.syntax(), arg_list.syntax());
322323
}
323324
self.generic_arg_list().unwrap()

crates/syntax/src/ast/make.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub mod ext {
8888
block_expr(None, None)
8989
}
9090

91+
pub fn ty_name(name: ast::Name) -> ast::Type {
92+
ty_path(ident_path(&format!("{name}")))
93+
}
9194
pub fn ty_bool() -> ast::Type {
9295
ty_path(ident_path("bool"))
9396
}
@@ -160,6 +163,7 @@ pub fn assoc_item_list() -> ast::AssocItemList {
160163
ast_from_text("impl C for D {}")
161164
}
162165

166+
// FIXME: `ty_params` should be `ast::GenericArgList`
163167
pub fn impl_(
164168
ty: ast::Path,
165169
params: Option<ast::GenericParamList>,
@@ -185,10 +189,6 @@ pub fn impl_trait(
185189
ast_from_text(&format!("impl{ty_params} {trait_} for {ty}{ty_params} {{}}"))
186190
}
187191

188-
pub(crate) fn generic_arg_list() -> ast::GenericArgList {
189-
ast_from_text("const S: T<> = ();")
190-
}
191-
192192
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
193193
ast_from_text(&format!("type __ = {name_ref};"))
194194
}
@@ -718,6 +718,21 @@ pub fn generic_param_list(
718718
ast_from_text(&format!("fn f<{args}>() {{ }}"))
719719
}
720720

721+
pub fn type_arg(ty: ast::Type) -> ast::TypeArg {
722+
ast_from_text(&format!("const S: T<{ty}> = ();"))
723+
}
724+
725+
pub fn lifetime_arg(lifetime: ast::Lifetime) -> ast::LifetimeArg {
726+
ast_from_text(&format!("const S: T<{lifetime}> = ();"))
727+
}
728+
729+
pub(crate) fn generic_arg_list(
730+
args: impl IntoIterator<Item = ast::GenericArg>,
731+
) -> ast::GenericArgList {
732+
let args = args.into_iter().join(", ");
733+
ast_from_text(&format!("const S: T<{args}> = ();"))
734+
}
735+
721736
pub fn visibility_pub_crate() -> ast::Visibility {
722737
ast_from_text("pub(crate) struct S")
723738
}

0 commit comments

Comments
 (0)