Skip to content

Commit 3088c4b

Browse files
committed
move subtyper change reveal_all
1 parent cf9fd95 commit 3088c4b

10 files changed

+143
-55
lines changed

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
2424
rvalue: &mut Rvalue<'tcx>,
2525
location: Location,
2626
) {
27+
// We don't need to do anything for deref temps as they are
28+
// not part of the source code, but used for desugaring purposes.
29+
if self.local_decls[place.local].is_deref_temp() {
30+
return;
31+
}
2732
let mut place_ty = place.ty(self.local_decls, self.tcx).ty;
2833
let mut rval_ty = rvalue.ty(self.local_decls, self.tcx);
2934
// Not erasing this causes `Free Regions` errors in validator,
@@ -48,7 +53,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
4853
// // gets transformed to
4954
// let temp: rval_ty = rval;
5055
// let place: place_ty = temp as place_ty;
51-
//
5256
pub fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5357
let patch = MirPatch::new(body);
5458
let mut checker = SubTypeChecker { tcx, patcher: patch, local_decls: &body.local_decls };

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ pub fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
467467
/// After this series of passes, no lifetime analysis based on borrowing can be done.
468468
fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
469469
let passes: &[&dyn MirPass<'tcx>] = &[
470-
&add_subtyping_projections::Subtyper,
471470
&cleanup_post_borrowck::CleanupPostBorrowck,
472471
&remove_noop_landing_pads::RemoveNoopLandingPads,
473472
&simplify::SimplifyCfg::EarlyOpt,
@@ -483,6 +482,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
483482
// These next passes must be executed together
484483
&add_call_guards::CriticalCallEdges,
485484
&reveal_all::RevealAll, // has to be done before drop elaboration, since we need to drop opaque types, too.
485+
&add_subtyping_projections::Subtyper, // calling this after reveal_all ensures that we don't deal with opaque types
486486
&elaborate_drops::ElaborateDrops,
487487
// This will remove extraneous landing pads which are no longer
488488
// necessary as well as well as forcing any call in a non-unwinding

compiler/rustc_mir_transform/src/reveal_all.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ impl<'tcx> MutVisitor<'tcx> for RevealAllVisitor<'tcx> {
4646
.filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(_)))
4747
.collect::<Vec<_>>(),
4848
);
49+
self.super_place(place, _context, _location);
4950
}
5051

5152
#[inline]
52-
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, _: Location) {
53+
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, location: Location) {
5354
// We have to use `try_normalize_erasing_regions` here, since it's
5455
// possible that we visit impossible-to-satisfy where clauses here,
5556
// see #91745
5657
if let Ok(c) = self.tcx.try_normalize_erasing_regions(self.param_env, constant.const_) {
5758
constant.const_ = c;
5859
}
60+
self.super_constant(constant, location);
5961
}
6062

6163
#[inline]

tests/mir-opt/inline/inline_generator.main.Inline.panic-abort.diff

+22-26
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
let mut _2: std::pin::Pin<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>;
88
let mut _3: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
99
let mut _4: {generator@$DIR/inline_generator.rs:16:5: 16:8};
10-
+ let mut _6: bool;
10+
+ let mut _5: bool;
1111
scope 1 {
1212
debug _r => _1;
1313
}
1414
+ scope 2 (inlined g) {
15-
+ let mut _5: {generator@$DIR/inline_generator.rs:16:5: 16:8};
1615
+ }
1716
+ scope 3 (inlined Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>::new) {
1817
+ debug pointer => _3;
@@ -23,10 +22,10 @@
2322
+ }
2423
+ }
2524
+ scope 6 (inlined g::{closure#0}) {
26-
+ debug a => _6;
27-
+ let mut _7: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
28-
+ let mut _8: u32;
29-
+ let mut _9: i32;
25+
+ debug a => _5;
26+
+ let mut _6: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
27+
+ let mut _7: u32;
28+
+ let mut _8: i32;
3029
+ }
3130

3231
bb0: {
@@ -35,28 +34,25 @@
3534
StorageLive(_3);
3635
StorageLive(_4);
3736
- _4 = g() -> [return: bb1, unwind unreachable];
38-
+ StorageLive(_5);
39-
+ _5 = {generator@$DIR/inline_generator.rs:16:5: 16:8 (#0)};
40-
+ _4 = move (_5 as subtype {generator@$DIR/inline_generator.rs:16:5: 16:8});
41-
+ StorageDead(_5);
37+
+ _4 = {generator@$DIR/inline_generator.rs:16:5: 16:8 (#0)};
4238
+ _3 = &mut _4;
4339
+ _2 = Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}> { pointer: move _3 };
4440
+ StorageDead(_3);
41+
+ StorageLive(_5);
42+
+ _5 = const false;
4543
+ StorageLive(_6);
46-
+ _6 = const false;
4744
+ StorageLive(_7);
48-
+ StorageLive(_8);
49-
+ _7 = (_2.0: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8});
50-
+ _8 = discriminant((*_7));
51-
+ switchInt(move _8) -> [0: bb3, 1: bb7, 3: bb8, otherwise: bb9];
45+
+ _6 = (_2.0: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8});
46+
+ _7 = discriminant((*_6));
47+
+ switchInt(move _7) -> [0: bb3, 1: bb7, 3: bb8, otherwise: bb9];
5248
}
5349

5450
bb1: {
5551
- _3 = &mut _4;
5652
- _2 = Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>::new(move _3) -> [return: bb2, unwind unreachable];
57-
+ StorageDead(_8);
5853
+ StorageDead(_7);
5954
+ StorageDead(_6);
55+
+ StorageDead(_5);
6056
+ StorageDead(_2);
6157
+ drop(_4) -> [return: bb2, unwind unreachable];
6258
}
@@ -73,27 +69,27 @@
7369
bb3: {
7470
- StorageDead(_2);
7571
- drop(_4) -> [return: bb4, unwind unreachable];
76-
+ StorageLive(_9);
77-
+ switchInt(_6) -> [0: bb4, otherwise: bb5];
72+
+ StorageLive(_8);
73+
+ switchInt(_5) -> [0: bb4, otherwise: bb5];
7874
}
7975

8076
bb4: {
8177
- StorageDead(_4);
8278
- _0 = const ();
8379
- StorageDead(_1);
8480
- return;
85-
+ _9 = const 13_i32;
81+
+ _8 = const 13_i32;
8682
+ goto -> bb6;
8783
+ }
8884
+
8985
+ bb5: {
90-
+ _9 = const 7_i32;
86+
+ _8 = const 7_i32;
9187
+ goto -> bb6;
9288
+ }
9389
+
9490
+ bb6: {
95-
+ _1 = GeneratorState::<i32, bool>::Yielded(move _9);
96-
+ discriminant((*_7)) = 3;
91+
+ _1 = GeneratorState::<i32, bool>::Yielded(move _8);
92+
+ discriminant((*_6)) = 3;
9793
+ goto -> bb1;
9894
+ }
9995
+
@@ -102,10 +98,10 @@
10298
+ }
10399
+
104100
+ bb8: {
105-
+ StorageLive(_9);
106-
+ StorageDead(_9);
107-
+ _1 = GeneratorState::<i32, bool>::Complete(_6);
108-
+ discriminant((*_7)) = 1;
101+
+ StorageLive(_8);
102+
+ StorageDead(_8);
103+
+ _1 = GeneratorState::<i32, bool>::Complete(_5);
104+
+ discriminant((*_6)) = 1;
109105
+ goto -> bb1;
110106
+ }
111107
+

tests/mir-opt/inline/inline_generator.main.Inline.panic-unwind.diff

+22-26
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
let mut _2: std::pin::Pin<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>;
88
let mut _3: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
99
let mut _4: {generator@$DIR/inline_generator.rs:16:5: 16:8};
10-
+ let mut _6: bool;
10+
+ let mut _5: bool;
1111
scope 1 {
1212
debug _r => _1;
1313
}
1414
+ scope 2 (inlined g) {
15-
+ let mut _5: {generator@$DIR/inline_generator.rs:16:5: 16:8};
1615
+ }
1716
+ scope 3 (inlined Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>::new) {
1817
+ debug pointer => _3;
@@ -23,10 +22,10 @@
2322
+ }
2423
+ }
2524
+ scope 6 (inlined g::{closure#0}) {
26-
+ debug a => _6;
27-
+ let mut _7: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
28-
+ let mut _8: u32;
29-
+ let mut _9: i32;
25+
+ debug a => _5;
26+
+ let mut _6: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8};
27+
+ let mut _7: u32;
28+
+ let mut _8: i32;
3029
+ }
3130

3231
bb0: {
@@ -38,10 +37,7 @@
3837
- }
3938
-
4039
- bb1: {
41-
+ StorageLive(_5);
42-
+ _5 = {generator@$DIR/inline_generator.rs:16:5: 16:8 (#0)};
43-
+ _4 = move (_5 as subtype {generator@$DIR/inline_generator.rs:16:5: 16:8});
44-
+ StorageDead(_5);
40+
+ _4 = {generator@$DIR/inline_generator.rs:16:5: 16:8 (#0)};
4541
_3 = &mut _4;
4642
- _2 = Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}>::new(move _3) -> [return: bb2, unwind: bb5];
4743
- }
@@ -50,20 +46,20 @@
5046
+ _2 = Pin::<&mut {generator@$DIR/inline_generator.rs:16:5: 16:8}> { pointer: move _3 };
5147
StorageDead(_3);
5248
- _1 = <{generator@$DIR/inline_generator.rs:16:5: 16:8} as Generator<bool>>::resume(move _2, const false) -> [return: bb3, unwind: bb5];
49+
+ StorageLive(_5);
50+
+ _5 = const false;
5351
+ StorageLive(_6);
54-
+ _6 = const false;
5552
+ StorageLive(_7);
56-
+ StorageLive(_8);
57-
+ _7 = (_2.0: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8});
58-
+ _8 = discriminant((*_7));
59-
+ switchInt(move _8) -> [0: bb5, 1: bb9, 3: bb10, otherwise: bb11];
53+
+ _6 = (_2.0: &mut {generator@$DIR/inline_generator.rs:16:5: 16:8});
54+
+ _7 = discriminant((*_6));
55+
+ switchInt(move _7) -> [0: bb5, 1: bb9, 3: bb10, otherwise: bb11];
6056
}
6157

6258
- bb3: {
6359
+ bb1: {
64-
+ StorageDead(_8);
6560
+ StorageDead(_7);
6661
+ StorageDead(_6);
62+
+ StorageDead(_5);
6763
StorageDead(_2);
6864
- drop(_4) -> [return: bb4, unwind: bb6];
6965
+ drop(_4) -> [return: bb2, unwind: bb4];
@@ -89,23 +85,23 @@
8985
+ }
9086
+
9187
+ bb5: {
92-
+ StorageLive(_9);
93-
+ switchInt(_6) -> [0: bb6, otherwise: bb7];
88+
+ StorageLive(_8);
89+
+ switchInt(_5) -> [0: bb6, otherwise: bb7];
9490
+ }
9591
+
9692
+ bb6: {
97-
+ _9 = const 13_i32;
93+
+ _8 = const 13_i32;
9894
+ goto -> bb8;
9995
+ }
10096
+
10197
+ bb7: {
102-
+ _9 = const 7_i32;
98+
+ _8 = const 7_i32;
10399
+ goto -> bb8;
104100
+ }
105101
+
106102
+ bb8: {
107-
+ _1 = GeneratorState::<i32, bool>::Yielded(move _9);
108-
+ discriminant((*_7)) = 3;
103+
+ _1 = GeneratorState::<i32, bool>::Yielded(move _8);
104+
+ discriminant((*_6)) = 3;
109105
+ goto -> bb1;
110106
+ }
111107
+
@@ -114,10 +110,10 @@
114110
+ }
115111
+
116112
+ bb10: {
117-
+ StorageLive(_9);
118-
+ StorageDead(_9);
119-
+ _1 = GeneratorState::<i32, bool>::Complete(_6);
120-
+ discriminant((*_7)) = 1;
113+
+ StorageLive(_8);
114+
+ StorageDead(_8);
115+
+ _1 = GeneratorState::<i32, bool>::Complete(_5);
116+
+ discriminant((*_6)) = 1;
121117
+ goto -> bb1;
122118
+ }
123119
+

tests/ui/impl-trait/impl-subtyper.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
3+
#![crate_type = "lib"]
4+
fn checkpoints() -> impl Iterator {
5+
Some(()).iter().flat_map(|_| std::iter::once(()))
6+
}
7+
8+
fn block_checkpoints() -> impl Iterator {
9+
checkpoints()
10+
}
11+
12+
fn iter_raw() -> impl Iterator {
13+
let mut iter = block_checkpoints();
14+
15+
(0..9).map(move |_| {
16+
iter.next();
17+
})
18+
}

tests/ui/impl-trait/impl-subtyper2.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
fn ages() -> Option<impl Iterator> {
4+
None::<std::slice::Iter<()>>
5+
}
6+
7+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// check-pass
2+
// compile-flags: -Z mir-opt-level=3
3+
#![feature(type_alias_impl_trait)]
4+
#![crate_type = "lib"]
5+
pub trait Tr {
6+
fn get(&self) -> u32;
7+
}
8+
9+
impl Tr for (u32,) {
10+
#[inline]
11+
fn get(&self) -> u32 { self.0 }
12+
}
13+
14+
pub fn tr1() -> impl Tr {
15+
(32,)
16+
}
17+
18+
pub fn tr2() -> impl Tr {
19+
struct Inner {
20+
x: X,
21+
}
22+
type X = impl Tr;
23+
impl Tr for Inner {
24+
fn get(&self) -> u32 {
25+
self.x.get()
26+
}
27+
}
28+
29+
Inner {
30+
x: tr1(),
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
3+
#![feature(type_alias_impl_trait)]
4+
5+
fn enum_upvar() {
6+
type T = impl Copy;
7+
let foo: T = Some((1u32, 2u32));
8+
let x = move || match foo {
9+
None => (),
10+
Some((a, b)) => (),
11+
};
12+
}
13+
14+
fn main(){}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
// compile-flags: -Zvalidate-mir
3+
trait Duh {}
4+
5+
impl Duh for i32 {}
6+
7+
trait Trait {
8+
type Assoc: Duh;
9+
}
10+
11+
impl<R: Duh, F: FnMut() -> R> Trait for F {
12+
type Assoc = R;
13+
}
14+
15+
fn foo() -> impl Trait<Assoc = impl Send> {
16+
|| 42
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)