Skip to content

Commit 5c52e05

Browse files
committed
Auto merge of #13110 - DesmondWillowbrook:issue-11197, r=jonas-schievink
fix: make "Extract type as type alias" assist work with const generics in array fixes #11197
2 parents ca8093e + 0d7ba13 commit 5c52e05

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

crates/ide-assists/src/handlers/extract_type_alias.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ fn collect_used_generics<'gp>(
171171
ast::Type::RefType(ref_) => generics.extend(
172172
ref_.lifetime().and_then(|lt| known_generics.iter().find(find_lifetime(&lt.text()))),
173173
),
174+
ast::Type::ArrayType(ar) => {
175+
if let Some(expr) = ar.expr() {
176+
if let ast::Expr::PathExpr(p) = expr {
177+
if let Some(path) = p.path() {
178+
if let Some(name_ref) = path.as_single_name_ref() {
179+
if let Some(param) = known_generics.iter().find(|gp| {
180+
if let ast::GenericParam::ConstParam(cp) = gp {
181+
cp.name().map_or(false, |n| n.text() == name_ref.text())
182+
} else {
183+
false
184+
}
185+
}) {
186+
generics.push(param);
187+
}
188+
}
189+
}
190+
}
191+
}
192+
}
174193
_ => (),
175194
});
176195
// stable resort to lifetime, type, const
@@ -357,4 +376,29 @@ impl<'outer, Outer, const OUTER: usize> () {
357376
"#,
358377
);
359378
}
379+
380+
#[test]
381+
fn issue_11197() {
382+
check_assist(
383+
extract_type_alias,
384+
r#"
385+
struct Foo<T, const N: usize>
386+
where
387+
[T; N]: Sized,
388+
{
389+
arr: $0[T; N]$0,
390+
}
391+
"#,
392+
r#"
393+
type $0Type<T, const N: usize> = [T; N];
394+
395+
struct Foo<T, const N: usize>
396+
where
397+
[T; N]: Sized,
398+
{
399+
arr: Type<T, N>,
400+
}
401+
"#,
402+
);
403+
}
360404
}

0 commit comments

Comments
 (0)