Skip to content

Commit 2ee17bc

Browse files
committed
Auto merge of rust-lang#15970 - Austaras:master, r=Veykril
fix variant resolve for type alias Closes rust-lang#15943
2 parents 4ab6729 + 2411f13 commit 2ee17bc

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

crates/hir-ty/src/infer.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1152,20 +1152,15 @@ 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 =
1160+
ctx.substs_from_path_segment(resolved_seg, Some(it.into()), true, None);
1161+
let ty = self.db.ty(it.into());
1162+
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1163+
11691164
self.resolve_variant_on_alias(ty, unresolved, mod_path)
11701165
}
11711166
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

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

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

0 commit comments

Comments
 (0)