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

resolve typerelative ctors to adt #113217

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 27 additions & 12 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,34 @@ impl<'tcx> Cx<'tcx> {
});
}
}
let adt_data =
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = fun.kind {
let adt_data = if let hir::ExprKind::Path(qpath) = fun.kind {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let adt_data = if let hir::ExprKind::Path(qpath) = fun.kind {
let adt_data = if let hir::ExprKind::Path(qpath) = fun.kind
&& let Some(adt_def) = expr_ty.ty_adt_def()
{

To avoid having to do it in all branches.

match qpath {
// Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
expr_ty.ty_adt_def().and_then(|adt_def| match path.res {
Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
}
Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)),
_ => None,
})
} else {
None
};
hir::QPath::Resolved(_, ref path) => {
expr_ty.ty_adt_def().and_then(|adt_def| match path.res {
Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
}
Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)),
_ => None,
})
}
hir::QPath::TypeRelative(_ty, _) => {
expr_ty.ty_adt_def().and_then(|adt_def| {
if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) =
self.typeck_results().type_dependent_def(fun.hir_id)
{
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
} else {
None
}
})
}
_ => None,
}
} else {
None
};
if let Some((adt_def, index)) = adt_data {
let substs = self.typeck_results().node_substs(fun.hir_id);
let user_provided_types = self.typeck_results().user_provided_types();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/nll/user-annotations/normalization-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ LL | fn test_variants<'a, 'b, 'c>() {
| -- lifetime `'b` defined here
...
LL | <Ty<'b>>::Tuple();
| ^^^^^^^^^^^^^^^ requires that `'b` must outlive `'static`
| ^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'static`

error: lifetime may not live long enough
--> $DIR/normalization-2.rs:93:5
Expand Down
38 changes: 38 additions & 0 deletions tests/ui/pattern/issue-110508.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// run-pass

#[derive(PartialEq, Eq)]
pub enum Foo {
FooA(()),
FooB(Vec<()>),
}

impl Foo {
const A1: Foo = Foo::FooA(());
const A2: Foo = Self::FooA(());
const A3: Self = Foo::FooA(());
const A4: Self = Self::FooA(());
}

fn main() {
let foo = Foo::FooA(());

match foo {
Foo::A1 => {},
_ => {},
}

match foo {
Foo::A2 => {},
_ => {},
}

match foo {
Foo::A3 => {},
_ => {},
}

match foo {
Foo::A4 => {},
_ => {},
}
}
18 changes: 18 additions & 0 deletions tests/ui/thir-print/thir-flat-const-variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// compile-flags: -Z unpretty=thir-flat
// check-pass

// Previously, the constants with `Self::Bar(())` would be `Call`s instead of
// `Adt`s in THIR.

pub enum Foo {
Bar(()),
}

impl Foo {
const BAR1: Foo = Foo::Bar(());
const BAR2: Foo = Self::Bar(());
const BAR3: Self = Foo::Bar(());
const BAR4: Self = Self::Bar(());
}

fn main() {}
Loading