Skip to content

Commit b637be7

Browse files
authored
Rollup merge of #113217 - ericmarkmartin:lower-type-relative-ctor-to-adt, r=cjgillot
resolve typerelative ctors to adt Associated issue: #110508 r? ``@spastorino``
2 parents 14aeef3 + 261c023 commit b637be7

File tree

8 files changed

+529
-16
lines changed

8 files changed

+529
-16
lines changed

compiler/rustc_mir_build/src/thir/cx/expr.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,18 @@ impl<'tcx> Cx<'tcx> {
215215
// so we wouldn't have to compute and store the actual value
216216

217217
let hir::ExprKind::Path(ref qpath) = source.kind else {
218-
return ExprKind::Cast { source: self.mirror_expr(source)};
218+
return ExprKind::Cast { source: self.mirror_expr(source) };
219219
};
220220

221221
let res = self.typeck_results().qpath_res(qpath, source.hir_id);
222222
let ty = self.typeck_results().node_type(source.hir_id);
223223
let ty::Adt(adt_def, substs) = ty.kind() else {
224-
return ExprKind::Cast { source: self.mirror_expr(source)};
224+
return ExprKind::Cast { source: self.mirror_expr(source) };
225225
};
226226

227-
let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res else {
228-
return ExprKind::Cast { source: self.mirror_expr(source)};
227+
let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res
228+
else {
229+
return ExprKind::Cast { source: self.mirror_expr(source) };
229230
};
230231

231232
let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
@@ -358,19 +359,35 @@ impl<'tcx> Cx<'tcx> {
358359
});
359360
}
360361
}
361-
let adt_data =
362-
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = fun.kind {
363-
// Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
364-
expr_ty.ty_adt_def().and_then(|adt_def| match path.res {
365-
Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
362+
363+
// Tuple-like ADTs are represented as ExprKind::Call. We convert them here.
364+
let adt_data = if let hir::ExprKind::Path(ref qpath) = fun.kind
365+
&& let Some(adt_def) = expr_ty.ty_adt_def() {
366+
match qpath {
367+
hir::QPath::Resolved(_, ref path) => {
368+
match path.res {
369+
Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => {
370+
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
371+
}
372+
Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)),
373+
_ => None,
374+
}
375+
}
376+
hir::QPath::TypeRelative(_ty, _) => {
377+
if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) =
378+
self.typeck_results().type_dependent_def(fun.hir_id)
379+
{
366380
Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id)))
381+
} else {
382+
None
367383
}
368-
Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)),
369-
_ => None,
370-
})
371-
} else {
372-
None
373-
};
384+
385+
}
386+
_ => None,
387+
}
388+
} else {
389+
None
390+
};
374391
if let Some((adt_def, index)) = adt_data {
375392
let substs = self.typeck_results().node_substs(fun.hir_id);
376393
let user_provided_types = self.typeck_results().user_provided_types();
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// EMIT_MIR issue_110508.{impl#0}-BAR.built.after.mir
2+
// EMIT_MIR issue_110508.{impl#0}-SELF_BAR.built.after.mir
3+
4+
enum Foo {
5+
Bar(()),
6+
}
7+
8+
impl Foo {
9+
const BAR: Foo = Foo::Bar(());
10+
const SELF_BAR: Foo = Self::Bar(());
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// MIR for `<impl at $DIR/issue_110508.rs:8:1: 8:9>::BAR` after built
2+
3+
const <impl at $DIR/issue_110508.rs:8:1: 8:9>::BAR: Foo = {
4+
let mut _0: Foo;
5+
let mut _1: ();
6+
7+
bb0: {
8+
StorageLive(_1);
9+
_1 = ();
10+
_0 = Foo::Bar(move _1);
11+
StorageDead(_1);
12+
return;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// MIR for `<impl at $DIR/issue_110508.rs:8:1: 8:9>::SELF_BAR` after built
2+
3+
const <impl at $DIR/issue_110508.rs:8:1: 8:9>::SELF_BAR: Foo = {
4+
let mut _0: Foo;
5+
let mut _1: ();
6+
7+
bb0: {
8+
StorageLive(_1);
9+
_1 = ();
10+
_0 = Foo::Bar(move _1);
11+
StorageDead(_1);
12+
return;
13+
}
14+
}

tests/ui/nll/user-annotations/normalization-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ LL | fn test_variants<'a, 'b, 'c>() {
147147
| -- lifetime `'b` defined here
148148
...
149149
LL | <Ty<'b>>::Tuple();
150-
| ^^^^^^^^^^^^^^^ requires that `'b` must outlive `'static`
150+
| ^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'static`
151151

152152
error: lifetime may not live long enough
153153
--> $DIR/normalization-2.rs:93:5

tests/ui/pattern/issue-110508.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run-pass
2+
3+
#[derive(PartialEq, Eq)]
4+
pub enum Foo {
5+
FooA(()),
6+
FooB(Vec<()>),
7+
}
8+
9+
impl Foo {
10+
const A1: Foo = Foo::FooA(());
11+
const A2: Foo = Self::FooA(());
12+
const A3: Self = Foo::FooA(());
13+
const A4: Self = Self::FooA(());
14+
}
15+
16+
fn main() {
17+
let foo = Foo::FooA(());
18+
19+
match foo {
20+
Foo::A1 => {},
21+
_ => {},
22+
}
23+
24+
match foo {
25+
Foo::A2 => {},
26+
_ => {},
27+
}
28+
29+
match foo {
30+
Foo::A3 => {},
31+
_ => {},
32+
}
33+
34+
match foo {
35+
Foo::A4 => {},
36+
_ => {},
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Z unpretty=thir-flat
2+
// check-pass
3+
4+
// Previously, the constants with `Self::Bar(())` would be `Call`s instead of
5+
// `Adt`s in THIR.
6+
7+
pub enum Foo {
8+
Bar(()),
9+
}
10+
11+
impl Foo {
12+
const BAR1: Foo = Foo::Bar(());
13+
const BAR2: Foo = Self::Bar(());
14+
const BAR3: Self = Foo::Bar(());
15+
const BAR4: Self = Self::Bar(());
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)