Skip to content

Commit 5a2ef8d

Browse files
committed
Added support for const generics in impl generation
1 parent 4cb3ecc commit 5a2ef8d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

crates/ide_assists/src/handlers/generate_impl.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ mod tests {
6969
"struct Foo<'a, T: Foo<'a>> {$0}",
7070
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
7171
);
72+
check_assist(
73+
generate_impl,
74+
r#"
75+
struct MyOwnArray<T, const S: usize> {}$0"#,
76+
r#"
77+
struct MyOwnArray<T, const S: usize> {}
78+
79+
impl<T, const S: usize> MyOwnArray<T, S> {
80+
$0
81+
}"#,
82+
);
7283
check_assist(
7384
generate_impl,
7485
r#"
@@ -114,11 +125,11 @@ mod tests {
114125
check_assist(
115126
generate_impl,
116127
r#"
117-
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}$0"#,
128+
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}$0"#,
118129
r#"
119-
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}
130+
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
120131
121-
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b> Defaulted<'a, 'b, T> {
132+
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
122133
$0
123134
}"#,
124135
);

crates/ide_assists/src/utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str
434434
}
435435
buf
436436
});
437-
let generics = lifetimes.chain(type_params).format(", ");
437+
let const_params = generic_params.const_params().map(|t| t.syntax().to_string());
438+
let generics = lifetimes.chain(type_params).chain(const_params).format(", ");
438439
format_to!(buf, "<{}>", generics);
439440
}
440441
buf.push(' ');
@@ -452,7 +453,11 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str
452453
.type_params()
453454
.filter_map(|it| it.name())
454455
.map(|it| SmolStr::from(it.text()));
455-
format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", "))
456+
let const_params = generic_params
457+
.const_params()
458+
.filter_map(|it| it.name())
459+
.map(|it| SmolStr::from(it.text()));
460+
format_to!(buf, "<{}>", lifetime_params.chain(type_params).chain(const_params).format(", "))
456461
}
457462

458463
match adt.where_clause() {

0 commit comments

Comments
 (0)