Skip to content

Commit 92a7fec

Browse files
committed
fix variant resolve for type alias
1 parent 79ec2c5 commit 92a7fec

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

crates/hir-ty/src/infer.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1152,20 +1152,14 @@ impl<'a> InferenceContext<'a> {
11521152
(ty, variant)
11531153
}
11541154
TypeNs::TypeAliasId(it) => {
1155-
let container = it.lookup(self.db.upcast()).container;
1156-
let parent_subst = match container {
1157-
ItemContainerId::TraitId(id) => {
1158-
let subst = TyBuilder::subst_for_def(self.db, id, None)
1159-
.fill_with_inference_vars(&mut self.table)
1160-
.build();
1161-
Some(subst)
1162-
}
1163-
// Type aliases do not exist in impls.
1164-
_ => None,
1155+
let resolved_seg = match unresolved {
1156+
None => path.segments().last().unwrap(),
1157+
Some(n) => path.segments().get(path.segments().len() - n - 1).unwrap(),
11651158
};
1166-
let ty = TyBuilder::def_ty(self.db, it.into(), parent_subst)
1167-
.fill_with_inference_vars(&mut self.table)
1168-
.build();
1159+
let substs = ctx.substs_from_path_segment(resolved_seg, Some(it.into()), true, None);
1160+
let ty = self.db.ty(it.into());
1161+
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1162+
11691163
self.resolve_variant_on_alias(ty, unresolved, mod_path)
11701164
}
11711165
TypeNs::AdtSelfType(_) => {

crates/hir-ty/src/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'a> TyLoweringContext<'a> {
768768
}
769769
}
770770

771-
fn substs_from_path_segment(
771+
pub(super) fn substs_from_path_segment(
772772
&self,
773773
segment: PathSegment<'_>,
774774
def: Option<GenericDefId>,

crates/hir-ty/src/tests/patterns.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1129,3 +1129,27 @@ fn foo() {
11291129
"#,
11301130
);
11311131
}
1132+
1133+
#[test]
1134+
fn generic_alias() {
1135+
check_types(
1136+
r#"
1137+
type Wrap<T> = T;
1138+
1139+
enum X {
1140+
A { cool: u32, stuff: u32 },
1141+
B,
1142+
}
1143+
1144+
fn main() {
1145+
let wrapped = Wrap::<X>::A {
1146+
cool: 100,
1147+
stuff: 100,
1148+
};
1149+
1150+
if let Wrap::<X>::A { cool, ..} = &wrapped {}
1151+
//^^^^ &u32
1152+
}
1153+
"#,
1154+
);
1155+
}

crates/ide-completion/src/tests/pattern.rs

+30
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,36 @@ fn outer(Foo { bar$0 }: Foo) {}
354354
)
355355
}
356356

357+
358+
#[test]
359+
fn completes_in_record_field_pat_with_generic_type_alias() {
360+
check_empty(
361+
r#"
362+
type Wrap<T> = T;
363+
364+
enum X {
365+
A { cool: u32, stuff: u32 },
366+
B,
367+
}
368+
369+
fn main() {
370+
let wrapped = Wrap::<X>::A {
371+
cool: 100,
372+
stuff: 100,
373+
};
374+
375+
if let Wrap::<X>::A { $0 } = &wrapped {};
376+
}
377+
"#,
378+
expect![[r#"
379+
fd cool u32
380+
fd stuff u32
381+
kw mut
382+
kw ref
383+
"#]],
384+
)
385+
}
386+
357387
#[test]
358388
fn completes_in_fn_param() {
359389
check_empty(

0 commit comments

Comments
 (0)