Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix variant resolve for type alias #15970

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,20 +1152,15 @@ impl<'a> InferenceContext<'a> {
(ty, variant)
}
TypeNs::TypeAliasId(it) => {
let container = it.lookup(self.db.upcast()).container;
let parent_subst = match container {
ItemContainerId::TraitId(id) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem possible to encounter this -- trait assoc type would either results in Self or Trait.

let subst = TyBuilder::subst_for_def(self.db, id, None)
.fill_with_inference_vars(&mut self.table)
.build();
Some(subst)
}
// Type aliases do not exist in impls.
_ => None,
let resolved_seg = match unresolved {
None => path.segments().last().unwrap(),
Some(n) => path.segments().get(path.segments().len() - n - 1).unwrap(),
};
let ty = TyBuilder::def_ty(self.db, it.into(), parent_subst)
.fill_with_inference_vars(&mut self.table)
.build();
let substs =
ctx.substs_from_path_segment(resolved_seg, Some(it.into()), true, None);
let ty = self.db.ty(it.into());
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));

self.resolve_variant_on_alias(ty, unresolved, mod_path)
}
TypeNs::AdtSelfType(_) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ impl<'a> TyLoweringContext<'a> {
}
}

fn substs_from_path_segment(
pub(super) fn substs_from_path_segment(
&self,
segment: PathSegment<'_>,
def: Option<GenericDefId>,
Expand Down
24 changes: 24 additions & 0 deletions crates/hir-ty/src/tests/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,27 @@ fn foo() {
"#,
);
}

#[test]
fn generic_alias() {
check_types(
r#"
type Wrap<T> = T;

enum X {
A { cool: u32, stuff: u32 },
B,
}

fn main() {
let wrapped = Wrap::<X>::A {
cool: 100,
stuff: 100,
};

if let Wrap::<X>::A { cool, ..} = &wrapped {}
//^^^^ &u32
}
"#,
);
}
29 changes: 29 additions & 0 deletions crates/ide-completion/src/tests/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,35 @@ fn outer(Foo { bar$0 }: Foo) {}
)
}

#[test]
fn completes_in_record_field_pat_with_generic_type_alias() {
check_empty(
r#"
type Wrap<T> = T;

enum X {
A { cool: u32, stuff: u32 },
B,
}

fn main() {
let wrapped = Wrap::<X>::A {
cool: 100,
stuff: 100,
};

if let Wrap::<X>::A { $0 } = &wrapped {};
}
"#,
expect![[r#"
fd cool u32
fd stuff u32
kw mut
kw ref
"#]],
)
}

#[test]
fn completes_in_fn_param() {
check_empty(
Expand Down