Skip to content

Commit 7364ce4

Browse files
Fix extract_type_alias with const generics
This fixes SourceAnalyzer::type_of_type to replace const infer vars in types with their resolved value.
1 parent 2ea1c88 commit 7364ce4

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

crates/hir/src/source_analyzer.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use hir_def::{
2424
lang_item::LangItems,
2525
nameres::MacroSubNs,
2626
resolver::{Resolver, TypeNs, ValueNs, resolver_for_scope},
27-
type_ref::{Mutability, TypeRefId},
27+
type_ref::{ConstRef, Mutability, TypeRefId},
2828
};
2929
use hir_expand::{
3030
HirFileId, InFile,
@@ -411,7 +411,25 @@ impl<'db> SourceAnalyzer<'db> {
411411
self.types.types.error
412412
}
413413
}
414-
fn next_const_var(&mut self, _span: hir_ty::Span) -> hir_ty::next_solver::Const<'db> {
414+
fn next_const_var(&mut self, span: hir_ty::Span) -> hir_ty::next_solver::Const<'db> {
415+
if let Some(infer) = self.infer {
416+
match span {
417+
hir_ty::Span::TypeRefId(type_ref) => {
418+
if let Some(const_) = infer.const_of_const_placeholder(type_ref.into())
419+
{
420+
return const_;
421+
}
422+
}
423+
hir_ty::Span::ExprId(expr) => {
424+
if let Some(const_) =
425+
infer.const_of_const_placeholder(ConstRef { expr }.into())
426+
{
427+
return const_;
428+
}
429+
}
430+
_ => {}
431+
}
432+
}
415433
self.types.consts.error
416434
}
417435
fn next_region_var(&mut self, _span: hir_ty::Span) -> Region<'db> {

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,48 @@ fn main() {
427427
)
428428
}
429429

430+
#[test]
431+
fn inferred_array_length() {
432+
check_assist(
433+
extract_type_alias,
434+
r#"
435+
fn main() {
436+
let a: $0[i32; _]$0 = [1, 2, 3];
437+
}
438+
"#,
439+
r#"
440+
type $0Type = [i32; 3];
441+
442+
fn main() {
443+
let a: Type = [1, 2, 3];
444+
}
445+
"#,
446+
)
447+
}
448+
449+
#[test]
450+
fn inferred_generic_const_parameter() {
451+
check_assist(
452+
extract_type_alias,
453+
r#"
454+
struct Wrap<const N: usize>([i32; N]);
455+
456+
fn main() {
457+
let wrap: $0Wrap<_>$0 = Wrap([1, 2, 3]);
458+
}
459+
"#,
460+
r#"
461+
struct Wrap<const N: usize>([i32; N]);
462+
463+
type $0Type = Wrap<3>;
464+
465+
fn main() {
466+
let wrap: Type = Wrap([1, 2, 3]);
467+
}
468+
"#,
469+
)
470+
}
471+
430472
#[test]
431473
fn inferred_type() {
432474
check_assist(

0 commit comments

Comments
 (0)