Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4d0fc3

Browse files
committedJun 10, 2024
Add SingleUseConsts mir-opt pass
1 parent d2fb97f commit a4d0fc3

File tree

31 files changed

+1004
-344
lines changed

31 files changed

+1004
-344
lines changed
 

‎compiler/rustc_mir_transform/src/const_debuginfo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub struct ConstDebugInfo;
1616

1717
impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
1818
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
19-
sess.mir_opt_level() > 0
19+
// Disabled in favour of `SingleUseConsts`
20+
sess.mir_opt_level() > 2
2021
}
2122

2223
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

‎compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ mod check_alignment;
106106
pub mod simplify;
107107
mod simplify_branches;
108108
mod simplify_comparison_integral;
109+
mod single_use_consts;
109110
mod sroa;
110111
mod unreachable_enum_branching;
111112
mod unreachable_prop;
@@ -593,6 +594,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
593594
&gvn::GVN,
594595
&simplify::SimplifyLocals::AfterGVN,
595596
&dataflow_const_prop::DataflowConstProp,
597+
&single_use_consts::SingleUseConsts,
596598
&const_debuginfo::ConstDebugInfo,
597599
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
598600
&jump_threading::JumpThreading,
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
use rustc_index::{bit_set::BitSet, IndexVec};
2+
use rustc_middle::bug;
3+
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
4+
use rustc_middle::mir::*;
5+
use rustc_middle::ty::TyCtxt;
6+
7+
/// Various parts of MIR building introduce temporaries that are commonly not needed.
8+
///
9+
/// Notably, `if CONST` and `match CONST` end up being used-once temporaries, which
10+
/// obfuscates the structure for other passes and codegen, which would like to always
11+
/// be able to just see the constant directly.
12+
///
13+
/// At higher optimization levels fancier passes like GVN will take care of this
14+
/// in a more general fashion, but this handles the easy cases so can run in debug.
15+
///
16+
/// This only removes constants with a single-use because re-evaluating constants
17+
/// isn't always an improvement, especially for large ones.
18+
///
19+
/// It also removes *never*-used constants, since it had all the information
20+
/// needed to do that too, including updating the debug info.
21+
pub struct SingleUseConsts;
22+
23+
impl<'tcx> MirPass<'tcx> for SingleUseConsts {
24+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
25+
sess.mir_opt_level() > 0
26+
}
27+
28+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
29+
let mut finder = SingleUseConstsFinder {
30+
ineligible_locals: BitSet::new_empty(body.local_decls.len()),
31+
locations: IndexVec::new(),
32+
};
33+
34+
finder.ineligible_locals.insert_range(..=Local::from_usize(body.arg_count));
35+
36+
finder.visit_body(body);
37+
38+
for (local, locations) in finder.locations.iter_enumerated() {
39+
if finder.ineligible_locals.contains(local) {
40+
continue;
41+
}
42+
43+
let Some(init_loc) = locations.init_loc else {
44+
continue;
45+
};
46+
47+
// We're only changing an operand, not the terminator kinds or successors
48+
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
49+
let init_statement =
50+
basic_blocks[init_loc.block].statements[init_loc.statement_index].replace_nop();
51+
let StatementKind::Assign(place_and_rvalue) = init_statement.kind else {
52+
bug!("No longer an assign?");
53+
};
54+
let (place, rvalue) = *place_and_rvalue;
55+
assert_eq!(place.as_local(), Some(local));
56+
let Rvalue::Use(operand) = rvalue else { bug!("No longer a use?") };
57+
58+
let mut replacer = LocalReplacer { tcx, local, operand: Some(operand) };
59+
60+
for var_debug_info in &mut body.var_debug_info {
61+
replacer.visit_var_debug_info(var_debug_info);
62+
}
63+
64+
let Some(use_loc) = locations.use_loc else { continue };
65+
66+
let use_block = &mut basic_blocks[use_loc.block];
67+
if let Some(use_statement) = use_block.statements.get_mut(use_loc.statement_index) {
68+
replacer.visit_statement(use_statement, use_loc);
69+
} else {
70+
replacer.visit_terminator(use_block.terminator_mut(), use_loc);
71+
}
72+
73+
if replacer.operand.is_some() {
74+
bug!(
75+
"operand wasn't used replacing local {local:?} with locations {locations:?} in body {body:#?}"
76+
);
77+
}
78+
}
79+
}
80+
}
81+
82+
#[derive(Copy, Clone, Debug)]
83+
struct LocationPair {
84+
init_loc: Option<Location>,
85+
use_loc: Option<Location>,
86+
}
87+
88+
impl LocationPair {
89+
fn new() -> Self {
90+
Self { init_loc: None, use_loc: None }
91+
}
92+
}
93+
94+
struct SingleUseConstsFinder {
95+
ineligible_locals: BitSet<Local>,
96+
locations: IndexVec<Local, LocationPair>,
97+
}
98+
99+
impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder {
100+
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
101+
if let Some(local) = place.as_local()
102+
&& let Rvalue::Use(operand) = rvalue
103+
&& let Operand::Constant(_) = operand
104+
{
105+
let locations = self.locations.ensure_contains_elem(local, LocationPair::new);
106+
if locations.init_loc.is_some() {
107+
self.ineligible_locals.insert(local);
108+
} else {
109+
locations.init_loc = Some(location);
110+
}
111+
} else {
112+
self.super_assign(place, rvalue, location);
113+
}
114+
}
115+
116+
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
117+
if let Some(place) = operand.place()
118+
&& let Some(local) = place.as_local()
119+
{
120+
let locations = self.locations.ensure_contains_elem(local, LocationPair::new);
121+
if locations.use_loc.is_some() {
122+
self.ineligible_locals.insert(local);
123+
} else {
124+
locations.use_loc = Some(location);
125+
}
126+
} else {
127+
self.super_operand(operand, location);
128+
}
129+
}
130+
131+
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
132+
match &statement.kind {
133+
// Storage markers are irrelevant to this.
134+
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {}
135+
_ => self.super_statement(statement, location),
136+
}
137+
}
138+
139+
fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
140+
if let VarDebugInfoContents::Place(place) = &var_debug_info.value
141+
&& let Some(_local) = place.as_local()
142+
{
143+
// It's a simple one that we can easily update
144+
} else {
145+
self.super_var_debug_info(var_debug_info);
146+
}
147+
}
148+
149+
fn visit_local(&mut self, local: Local, _context: PlaceContext, _location: Location) {
150+
// If there's any path that gets here, rather than being understood elsewhere,
151+
// then we'd better not do anything with this local.
152+
self.ineligible_locals.insert(local);
153+
}
154+
}
155+
156+
struct LocalReplacer<'tcx> {
157+
tcx: TyCtxt<'tcx>,
158+
local: Local,
159+
operand: Option<Operand<'tcx>>,
160+
}
161+
162+
impl<'tcx> MutVisitor<'tcx> for LocalReplacer<'tcx> {
163+
fn tcx(&self) -> TyCtxt<'tcx> {
164+
self.tcx
165+
}
166+
167+
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _location: Location) {
168+
if let Operand::Copy(place) | Operand::Move(place) = operand
169+
&& let Some(local) = place.as_local()
170+
&& local == self.local
171+
{
172+
*operand = self.operand.take().unwrap_or_else(|| {
173+
bug!("there was a second use of the operand");
174+
});
175+
}
176+
}
177+
178+
fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) {
179+
if let VarDebugInfoContents::Place(place) = &var_debug_info.value
180+
&& let Some(local) = place.as_local()
181+
&& local == self.local
182+
{
183+
let const_op = self
184+
.operand
185+
.as_ref()
186+
.unwrap_or_else(|| {
187+
bug!("the operand was already stolen");
188+
})
189+
.constant()
190+
.unwrap()
191+
.clone();
192+
var_debug_info.value = VarDebugInfoContents::Const(const_op);
193+
}
194+
}
195+
}

‎tests/mir-opt/building/match/sort_candidates.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ fn constant_eq(s: &str, b: bool) -> u32 {
55
// Check that we only test "a" once
66

77
// CHECK-LABEL: fn constant_eq(
8-
// CHECK: bb0: {
9-
// CHECK: [[a:_.*]] = const "a";
10-
// CHECK-NOT: {{_.*}} = const "a";
8+
// CHECK-NOT: const "a"
9+
// CHECK: {{_[0-9]+}} = const "a" as &[u8] (Transmute);
10+
// CHECK-NOT: const "a"
1111
match (s, b) {
1212
("a", _) if true => 1,
1313
("b", true) => 2,

‎tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
bb0: {
1919
StorageLive(_1);
2020
StorageLive(_2);
21-
_2 = const 1_u32;
21+
nop;
2222
_1 = Un { us: const 1_u32 };
2323
StorageDead(_2);
2424
StorageLive(_3);

‎tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
bb0: {
1919
StorageLive(_1);
2020
StorageLive(_2);
21-
_2 = const 1_u32;
21+
nop;
2222
_1 = Un { us: const 1_u32 };
2323
StorageDead(_2);
2424
StorageLive(_3);

‎tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
_3 = &_4;
3030
_2 = move _3 as &[T] (PointerCoercion(Unsize));
3131
StorageDead(_3);
32-
_5 = const 3_usize;
33-
_6 = const true;
32+
nop;
33+
nop;
3434
goto -> bb2;
3535
}
3636

‎tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
_3 = &_4;
3030
_2 = move _3 as &[T] (PointerCoercion(Unsize));
3131
StorageDead(_3);
32-
_5 = const 3_usize;
33-
_6 = const true;
32+
nop;
33+
nop;
3434
goto -> bb2;
3535
}
3636

‎tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
let mut _12: isize;
2424
let _13: std::alloc::AllocError;
2525
let mut _14: !;
26-
let _15: &str;
27-
let mut _16: &dyn std::fmt::Debug;
28-
let mut _17: &std::alloc::AllocError;
26+
let mut _15: &dyn std::fmt::Debug;
27+
let mut _16: &std::alloc::AllocError;
2928
scope 7 {
3029
}
3130
scope 8 {
3231
}
3332
}
3433
scope 9 (inlined NonNull::<[u8]>::as_ptr) {
35-
let mut _18: *const [u8];
34+
let mut _17: *const [u8];
3635
}
3736
}
3837
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -87,36 +86,33 @@
8786
StorageDead(_8);
8887
StorageDead(_7);
8988
StorageLive(_12);
90-
StorageLive(_15);
9189
_12 = discriminant(_6);
9290
switchInt(move _12) -> [0: bb6, 1: bb5, otherwise: bb1];
9391
}
9492

9593
bb5: {
96-
_15 = const "called `Result::unwrap()` on an `Err` value";
94+
StorageLive(_15);
9795
StorageLive(_16);
98-
StorageLive(_17);
99-
_17 = &_13;
100-
_16 = move _17 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
101-
StorageDead(_17);
102-
_14 = result::unwrap_failed(move _15, move _16) -> unwind unreachable;
96+
_16 = &_13;
97+
_15 = move _16 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
98+
StorageDead(_16);
99+
_14 = result::unwrap_failed(const "called `Result::unwrap()` on an `Err` value", move _15) -> unwind unreachable;
103100
}
104101

105102
bb6: {
106103
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
107-
StorageDead(_15);
108104
StorageDead(_12);
109105
StorageDead(_6);
110-
- StorageLive(_18);
106+
- StorageLive(_17);
111107
+ nop;
112-
_18 = (_5.0: *const [u8]);
113-
- _4 = move _18 as *mut [u8] (PtrToPtr);
114-
- StorageDead(_18);
115-
+ _4 = _18 as *mut [u8] (PtrToPtr);
108+
_17 = (_5.0: *const [u8]);
109+
- _4 = move _17 as *mut [u8] (PtrToPtr);
110+
- StorageDead(_17);
111+
+ _4 = _17 as *mut [u8] (PtrToPtr);
116112
+ nop;
117113
StorageDead(_5);
118114
- _3 = move _4 as *mut u8 (PtrToPtr);
119-
+ _3 = _18 as *mut u8 (PtrToPtr);
115+
+ _3 = _17 as *mut u8 (PtrToPtr);
120116
StorageDead(_4);
121117
StorageDead(_3);
122118
- StorageDead(_1);

‎tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
let mut _12: isize;
2424
let _13: std::alloc::AllocError;
2525
let mut _14: !;
26-
let _15: &str;
27-
let mut _16: &dyn std::fmt::Debug;
28-
let mut _17: &std::alloc::AllocError;
26+
let mut _15: &dyn std::fmt::Debug;
27+
let mut _16: &std::alloc::AllocError;
2928
scope 7 {
3029
}
3130
scope 8 {
3231
}
3332
}
3433
scope 9 (inlined NonNull::<[u8]>::as_ptr) {
35-
let mut _18: *const [u8];
34+
let mut _17: *const [u8];
3635
}
3736
}
3837
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -87,36 +86,33 @@
8786
StorageDead(_8);
8887
StorageDead(_7);
8988
StorageLive(_12);
90-
StorageLive(_15);
9189
_12 = discriminant(_6);
9290
switchInt(move _12) -> [0: bb6, 1: bb5, otherwise: bb1];
9391
}
9492

9593
bb5: {
96-
_15 = const "called `Result::unwrap()` on an `Err` value";
94+
StorageLive(_15);
9795
StorageLive(_16);
98-
StorageLive(_17);
99-
_17 = &_13;
100-
_16 = move _17 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
101-
StorageDead(_17);
102-
_14 = result::unwrap_failed(move _15, move _16) -> unwind unreachable;
96+
_16 = &_13;
97+
_15 = move _16 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
98+
StorageDead(_16);
99+
_14 = result::unwrap_failed(const "called `Result::unwrap()` on an `Err` value", move _15) -> unwind unreachable;
103100
}
104101

105102
bb6: {
106103
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
107-
StorageDead(_15);
108104
StorageDead(_12);
109105
StorageDead(_6);
110-
- StorageLive(_18);
106+
- StorageLive(_17);
111107
+ nop;
112-
_18 = (_5.0: *const [u8]);
113-
- _4 = move _18 as *mut [u8] (PtrToPtr);
114-
- StorageDead(_18);
115-
+ _4 = _18 as *mut [u8] (PtrToPtr);
108+
_17 = (_5.0: *const [u8]);
109+
- _4 = move _17 as *mut [u8] (PtrToPtr);
110+
- StorageDead(_17);
111+
+ _4 = _17 as *mut [u8] (PtrToPtr);
116112
+ nop;
117113
StorageDead(_5);
118114
- _3 = move _4 as *mut u8 (PtrToPtr);
119-
+ _3 = _18 as *mut u8 (PtrToPtr);
115+
+ _3 = _17 as *mut u8 (PtrToPtr);
120116
StorageDead(_4);
121117
StorageDead(_3);
122118
- StorageDead(_1);

‎tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7-
let mut _12: std::slice::Iter<'_, T>;
7+
let mut _11: std::slice::Iter<'_, T>;
8+
let mut _12: std::iter::Enumerate<std::slice::Iter<'_, T>>;
89
let mut _13: std::iter::Enumerate<std::slice::Iter<'_, T>>;
9-
let mut _14: std::iter::Enumerate<std::slice::Iter<'_, T>>;
10-
let mut _15: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
11-
let mut _16: std::option::Option<(usize, &T)>;
12-
let mut _17: isize;
13-
let mut _20: &impl Fn(usize, &T);
14-
let mut _21: (usize, &T);
15-
let _22: ();
10+
let mut _14: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
11+
let mut _15: std::option::Option<(usize, &T)>;
12+
let mut _16: isize;
13+
let mut _19: &impl Fn(usize, &T);
14+
let mut _20: (usize, &T);
15+
let _21: ();
1616
scope 1 {
17-
debug iter => _14;
18-
let _18: usize;
19-
let _19: &T;
17+
debug iter => _13;
18+
let _17: usize;
19+
let _18: &T;
2020
scope 2 {
21-
debug i => _18;
22-
debug x => _19;
21+
debug i => _17;
22+
debug x => _18;
2323
}
2424
}
2525
scope 3 (inlined core::slice::<impl [T]>::iter) {
2626
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2727
let _3: usize;
28-
let mut _7: bool;
28+
let mut _7: *mut T;
2929
let mut _8: *mut T;
30-
let mut _9: *mut T;
31-
let mut _11: *const T;
30+
let mut _10: *const T;
3231
scope 5 {
3332
let _6: std::ptr::NonNull<T>;
3433
scope 6 {
35-
let _10: *const T;
34+
let _9: *const T;
3635
scope 7 {
3736
}
3837
scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
6160
}
6261

6362
bb0: {
64-
StorageLive(_12);
63+
StorageLive(_11);
6564
StorageLive(_3);
6665
StorageLive(_6);
6766
StorageLive(_4);
@@ -70,62 +69,59 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
7069
_4 = &raw const (*_1);
7170
_5 = _4 as *const T (PtrToPtr);
7271
_6 = NonNull::<T> { pointer: _5 };
73-
StorageLive(_10);
74-
StorageLive(_7);
75-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
76-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
72+
StorageLive(_9);
73+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
7774
}
7875

7976
bb1: {
80-
StorageLive(_9);
8177
StorageLive(_8);
82-
_8 = _4 as *mut T (PtrToPtr);
83-
_9 = Offset(_8, _3);
78+
StorageLive(_7);
79+
_7 = _4 as *mut T (PtrToPtr);
80+
_8 = Offset(_7, _3);
81+
StorageDead(_7);
82+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
8483
StorageDead(_8);
85-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
86-
StorageDead(_9);
8784
goto -> bb3;
8885
}
8986

9087
bb2: {
91-
_10 = _3 as *const T (Transmute);
88+
_9 = _3 as *const T (Transmute);
9289
goto -> bb3;
9390
}
9491

9592
bb3: {
96-
StorageDead(_7);
97-
StorageLive(_11);
98-
_11 = _10;
99-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
100-
StorageDead(_11);
93+
StorageLive(_10);
94+
_10 = _9;
95+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
10196
StorageDead(_10);
97+
StorageDead(_9);
10298
StorageDead(_5);
10399
StorageDead(_4);
104100
StorageDead(_6);
105101
StorageDead(_3);
106-
_13 = Enumerate::<std::slice::Iter<'_, T>> { iter: _12, count: const 0_usize };
107-
StorageDead(_12);
108-
StorageLive(_14);
109-
_14 = _13;
102+
_12 = Enumerate::<std::slice::Iter<'_, T>> { iter: _11, count: const 0_usize };
103+
StorageDead(_11);
104+
StorageLive(_13);
105+
_13 = _12;
110106
goto -> bb4;
111107
}
112108

113109
bb4: {
114-
StorageLive(_16);
115110
StorageLive(_15);
116-
_15 = &mut _14;
117-
_16 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _15) -> [return: bb5, unwind unreachable];
111+
StorageLive(_14);
112+
_14 = &mut _13;
113+
_15 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _14) -> [return: bb5, unwind unreachable];
118114
}
119115

120116
bb5: {
121-
StorageDead(_15);
122-
_17 = discriminant(_16);
123-
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
117+
StorageDead(_14);
118+
_16 = discriminant(_15);
119+
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
124120
}
125121

126122
bb6: {
127-
StorageDead(_16);
128-
StorageDead(_14);
123+
StorageDead(_15);
124+
StorageDead(_13);
129125
drop(_2) -> [return: bb7, unwind unreachable];
130126
}
131127

@@ -134,19 +130,19 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
134130
}
135131

136132
bb8: {
137-
_18 = (((_16 as Some).0: (usize, &T)).0: usize);
138-
_19 = (((_16 as Some).0: (usize, &T)).1: &T);
133+
_17 = (((_15 as Some).0: (usize, &T)).0: usize);
134+
_18 = (((_15 as Some).0: (usize, &T)).1: &T);
135+
StorageLive(_19);
136+
_19 = &_2;
139137
StorageLive(_20);
140-
_20 = &_2;
141-
StorageLive(_21);
142-
_21 = (_18, _19);
143-
_22 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _20, move _21) -> [return: bb9, unwind unreachable];
138+
_20 = (_17, _18);
139+
_21 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _19, move _20) -> [return: bb9, unwind unreachable];
144140
}
145141

146142
bb9: {
147-
StorageDead(_21);
148143
StorageDead(_20);
149-
StorageDead(_16);
144+
StorageDead(_19);
145+
StorageDead(_15);
150146
goto -> bb4;
151147
}
152148

‎tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7-
let mut _12: std::slice::Iter<'_, T>;
7+
let mut _11: std::slice::Iter<'_, T>;
8+
let mut _12: std::iter::Enumerate<std::slice::Iter<'_, T>>;
89
let mut _13: std::iter::Enumerate<std::slice::Iter<'_, T>>;
9-
let mut _14: std::iter::Enumerate<std::slice::Iter<'_, T>>;
10-
let mut _15: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
11-
let mut _16: std::option::Option<(usize, &T)>;
12-
let mut _17: isize;
13-
let mut _20: &impl Fn(usize, &T);
14-
let mut _21: (usize, &T);
15-
let _22: ();
10+
let mut _14: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
11+
let mut _15: std::option::Option<(usize, &T)>;
12+
let mut _16: isize;
13+
let mut _19: &impl Fn(usize, &T);
14+
let mut _20: (usize, &T);
15+
let _21: ();
1616
scope 1 {
17-
debug iter => _14;
18-
let _18: usize;
19-
let _19: &T;
17+
debug iter => _13;
18+
let _17: usize;
19+
let _18: &T;
2020
scope 2 {
21-
debug i => _18;
22-
debug x => _19;
21+
debug i => _17;
22+
debug x => _18;
2323
}
2424
}
2525
scope 3 (inlined core::slice::<impl [T]>::iter) {
2626
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2727
let _3: usize;
28-
let mut _7: bool;
28+
let mut _7: *mut T;
2929
let mut _8: *mut T;
30-
let mut _9: *mut T;
31-
let mut _11: *const T;
30+
let mut _10: *const T;
3231
scope 5 {
3332
let _6: std::ptr::NonNull<T>;
3433
scope 6 {
35-
let _10: *const T;
34+
let _9: *const T;
3635
scope 7 {
3736
}
3837
scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
6160
}
6261

6362
bb0: {
64-
StorageLive(_12);
63+
StorageLive(_11);
6564
StorageLive(_3);
6665
StorageLive(_6);
6766
StorageLive(_4);
@@ -70,62 +69,59 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
7069
_4 = &raw const (*_1);
7170
_5 = _4 as *const T (PtrToPtr);
7271
_6 = NonNull::<T> { pointer: _5 };
73-
StorageLive(_10);
74-
StorageLive(_7);
75-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
76-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
72+
StorageLive(_9);
73+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
7774
}
7875

7976
bb1: {
80-
StorageLive(_9);
8177
StorageLive(_8);
82-
_8 = _4 as *mut T (PtrToPtr);
83-
_9 = Offset(_8, _3);
78+
StorageLive(_7);
79+
_7 = _4 as *mut T (PtrToPtr);
80+
_8 = Offset(_7, _3);
81+
StorageDead(_7);
82+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
8483
StorageDead(_8);
85-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
86-
StorageDead(_9);
8784
goto -> bb3;
8885
}
8986

9087
bb2: {
91-
_10 = _3 as *const T (Transmute);
88+
_9 = _3 as *const T (Transmute);
9289
goto -> bb3;
9390
}
9491

9592
bb3: {
96-
StorageDead(_7);
97-
StorageLive(_11);
98-
_11 = _10;
99-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
100-
StorageDead(_11);
93+
StorageLive(_10);
94+
_10 = _9;
95+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
10196
StorageDead(_10);
97+
StorageDead(_9);
10298
StorageDead(_5);
10399
StorageDead(_4);
104100
StorageDead(_6);
105101
StorageDead(_3);
106-
_13 = Enumerate::<std::slice::Iter<'_, T>> { iter: _12, count: const 0_usize };
107-
StorageDead(_12);
108-
StorageLive(_14);
109-
_14 = _13;
102+
_12 = Enumerate::<std::slice::Iter<'_, T>> { iter: _11, count: const 0_usize };
103+
StorageDead(_11);
104+
StorageLive(_13);
105+
_13 = _12;
110106
goto -> bb4;
111107
}
112108

113109
bb4: {
114-
StorageLive(_16);
115110
StorageLive(_15);
116-
_15 = &mut _14;
117-
_16 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _15) -> [return: bb5, unwind: bb11];
111+
StorageLive(_14);
112+
_14 = &mut _13;
113+
_15 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _14) -> [return: bb5, unwind: bb11];
118114
}
119115

120116
bb5: {
121-
StorageDead(_15);
122-
_17 = discriminant(_16);
123-
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
117+
StorageDead(_14);
118+
_16 = discriminant(_15);
119+
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
124120
}
125121

126122
bb6: {
127-
StorageDead(_16);
128-
StorageDead(_14);
123+
StorageDead(_15);
124+
StorageDead(_13);
129125
drop(_2) -> [return: bb7, unwind continue];
130126
}
131127

@@ -134,19 +130,19 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
134130
}
135131

136132
bb8: {
137-
_18 = (((_16 as Some).0: (usize, &T)).0: usize);
138-
_19 = (((_16 as Some).0: (usize, &T)).1: &T);
133+
_17 = (((_15 as Some).0: (usize, &T)).0: usize);
134+
_18 = (((_15 as Some).0: (usize, &T)).1: &T);
135+
StorageLive(_19);
136+
_19 = &_2;
139137
StorageLive(_20);
140-
_20 = &_2;
141-
StorageLive(_21);
142-
_21 = (_18, _19);
143-
_22 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _20, move _21) -> [return: bb9, unwind: bb11];
138+
_20 = (_17, _18);
139+
_21 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _19, move _20) -> [return: bb9, unwind: bb11];
144140
}
145141

146142
bb9: {
147-
StorageDead(_21);
148143
StorageDead(_20);
149-
StorageDead(_16);
144+
StorageDead(_19);
145+
StorageDead(_15);
150146
goto -> bb4;
151147
}
152148

‎tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7+
let mut _11: std::slice::Iter<'_, T>;
78
let mut _12: std::slice::Iter<'_, T>;
8-
let mut _13: std::slice::Iter<'_, T>;
9-
let mut _14: &mut std::slice::Iter<'_, T>;
10-
let mut _15: std::option::Option<&T>;
11-
let mut _16: isize;
12-
let mut _18: &impl Fn(&T);
13-
let mut _19: (&T,);
14-
let _20: ();
9+
let mut _13: &mut std::slice::Iter<'_, T>;
10+
let mut _14: std::option::Option<&T>;
11+
let mut _15: isize;
12+
let mut _17: &impl Fn(&T);
13+
let mut _18: (&T,);
14+
let _19: ();
1515
scope 1 {
16-
debug iter => _13;
17-
let _17: &T;
16+
debug iter => _12;
17+
let _16: &T;
1818
scope 2 {
19-
debug x => _17;
19+
debug x => _16;
2020
}
2121
}
2222
scope 3 (inlined core::slice::<impl [T]>::iter) {
2323
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2424
let _3: usize;
25-
let mut _7: bool;
25+
let mut _7: *mut T;
2626
let mut _8: *mut T;
27-
let mut _9: *mut T;
28-
let mut _11: *const T;
27+
let mut _10: *const T;
2928
scope 5 {
3029
let _6: std::ptr::NonNull<T>;
3130
scope 6 {
32-
let _10: *const T;
31+
let _9: *const T;
3332
scope 7 {
3433
}
3534
scope 11 (inlined without_provenance::<T>) {
@@ -62,60 +61,57 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
6261
_4 = &raw const (*_1);
6362
_5 = _4 as *const T (PtrToPtr);
6463
_6 = NonNull::<T> { pointer: _5 };
65-
StorageLive(_10);
66-
StorageLive(_7);
67-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
68-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
64+
StorageLive(_9);
65+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
6966
}
7067

7168
bb1: {
72-
StorageLive(_9);
7369
StorageLive(_8);
74-
_8 = _4 as *mut T (PtrToPtr);
75-
_9 = Offset(_8, _3);
70+
StorageLive(_7);
71+
_7 = _4 as *mut T (PtrToPtr);
72+
_8 = Offset(_7, _3);
73+
StorageDead(_7);
74+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
7675
StorageDead(_8);
77-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
78-
StorageDead(_9);
7976
goto -> bb3;
8077
}
8178

8279
bb2: {
83-
_10 = _3 as *const T (Transmute);
80+
_9 = _3 as *const T (Transmute);
8481
goto -> bb3;
8582
}
8683

8784
bb3: {
88-
StorageDead(_7);
89-
StorageLive(_11);
90-
_11 = _10;
91-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
92-
StorageDead(_11);
85+
StorageLive(_10);
86+
_10 = _9;
87+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
9388
StorageDead(_10);
89+
StorageDead(_9);
9490
StorageDead(_5);
9591
StorageDead(_4);
9692
StorageDead(_6);
9793
StorageDead(_3);
98-
StorageLive(_13);
99-
_13 = _12;
94+
StorageLive(_12);
95+
_12 = _11;
10096
goto -> bb4;
10197
}
10298

10399
bb4: {
104-
StorageLive(_15);
105100
StorageLive(_14);
106-
_14 = &mut _13;
107-
_15 = <std::slice::Iter<'_, T> as Iterator>::next(move _14) -> [return: bb5, unwind unreachable];
101+
StorageLive(_13);
102+
_13 = &mut _12;
103+
_14 = <std::slice::Iter<'_, T> as Iterator>::next(move _13) -> [return: bb5, unwind unreachable];
108104
}
109105

110106
bb5: {
111-
StorageDead(_14);
112-
_16 = discriminant(_15);
113-
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
107+
StorageDead(_13);
108+
_15 = discriminant(_14);
109+
switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb10];
114110
}
115111

116112
bb6: {
117-
StorageDead(_15);
118-
StorageDead(_13);
113+
StorageDead(_14);
114+
StorageDead(_12);
119115
drop(_2) -> [return: bb7, unwind unreachable];
120116
}
121117

@@ -124,18 +120,18 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
124120
}
125121

126122
bb8: {
127-
_17 = ((_15 as Some).0: &T);
123+
_16 = ((_14 as Some).0: &T);
124+
StorageLive(_17);
125+
_17 = &_2;
128126
StorageLive(_18);
129-
_18 = &_2;
130-
StorageLive(_19);
131-
_19 = (_17,);
132-
_20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind unreachable];
127+
_18 = (_16,);
128+
_19 = <impl Fn(&T) as Fn<(&T,)>>::call(move _17, move _18) -> [return: bb9, unwind unreachable];
133129
}
134130

135131
bb9: {
136-
StorageDead(_19);
137132
StorageDead(_18);
138-
StorageDead(_15);
133+
StorageDead(_17);
134+
StorageDead(_14);
139135
goto -> bb4;
140136
}
141137

‎tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7+
let mut _11: std::slice::Iter<'_, T>;
78
let mut _12: std::slice::Iter<'_, T>;
8-
let mut _13: std::slice::Iter<'_, T>;
9-
let mut _14: &mut std::slice::Iter<'_, T>;
10-
let mut _15: std::option::Option<&T>;
11-
let mut _16: isize;
12-
let mut _18: &impl Fn(&T);
13-
let mut _19: (&T,);
14-
let _20: ();
9+
let mut _13: &mut std::slice::Iter<'_, T>;
10+
let mut _14: std::option::Option<&T>;
11+
let mut _15: isize;
12+
let mut _17: &impl Fn(&T);
13+
let mut _18: (&T,);
14+
let _19: ();
1515
scope 1 {
16-
debug iter => _13;
17-
let _17: &T;
16+
debug iter => _12;
17+
let _16: &T;
1818
scope 2 {
19-
debug x => _17;
19+
debug x => _16;
2020
}
2121
}
2222
scope 3 (inlined core::slice::<impl [T]>::iter) {
2323
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2424
let _3: usize;
25-
let mut _7: bool;
25+
let mut _7: *mut T;
2626
let mut _8: *mut T;
27-
let mut _9: *mut T;
28-
let mut _11: *const T;
27+
let mut _10: *const T;
2928
scope 5 {
3029
let _6: std::ptr::NonNull<T>;
3130
scope 6 {
32-
let _10: *const T;
31+
let _9: *const T;
3332
scope 7 {
3433
}
3534
scope 11 (inlined without_provenance::<T>) {
@@ -62,60 +61,57 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
6261
_4 = &raw const (*_1);
6362
_5 = _4 as *const T (PtrToPtr);
6463
_6 = NonNull::<T> { pointer: _5 };
65-
StorageLive(_10);
66-
StorageLive(_7);
67-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
68-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
64+
StorageLive(_9);
65+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
6966
}
7067

7168
bb1: {
72-
StorageLive(_9);
7369
StorageLive(_8);
74-
_8 = _4 as *mut T (PtrToPtr);
75-
_9 = Offset(_8, _3);
70+
StorageLive(_7);
71+
_7 = _4 as *mut T (PtrToPtr);
72+
_8 = Offset(_7, _3);
73+
StorageDead(_7);
74+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
7675
StorageDead(_8);
77-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
78-
StorageDead(_9);
7976
goto -> bb3;
8077
}
8178

8279
bb2: {
83-
_10 = _3 as *const T (Transmute);
80+
_9 = _3 as *const T (Transmute);
8481
goto -> bb3;
8582
}
8683

8784
bb3: {
88-
StorageDead(_7);
89-
StorageLive(_11);
90-
_11 = _10;
91-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
92-
StorageDead(_11);
85+
StorageLive(_10);
86+
_10 = _9;
87+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
9388
StorageDead(_10);
89+
StorageDead(_9);
9490
StorageDead(_5);
9591
StorageDead(_4);
9692
StorageDead(_6);
9793
StorageDead(_3);
98-
StorageLive(_13);
99-
_13 = _12;
94+
StorageLive(_12);
95+
_12 = _11;
10096
goto -> bb4;
10197
}
10298

10399
bb4: {
104-
StorageLive(_15);
105100
StorageLive(_14);
106-
_14 = &mut _13;
107-
_15 = <std::slice::Iter<'_, T> as Iterator>::next(move _14) -> [return: bb5, unwind: bb11];
101+
StorageLive(_13);
102+
_13 = &mut _12;
103+
_14 = <std::slice::Iter<'_, T> as Iterator>::next(move _13) -> [return: bb5, unwind: bb11];
108104
}
109105

110106
bb5: {
111-
StorageDead(_14);
112-
_16 = discriminant(_15);
113-
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
107+
StorageDead(_13);
108+
_15 = discriminant(_14);
109+
switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb10];
114110
}
115111

116112
bb6: {
117-
StorageDead(_15);
118-
StorageDead(_13);
113+
StorageDead(_14);
114+
StorageDead(_12);
119115
drop(_2) -> [return: bb7, unwind continue];
120116
}
121117

@@ -124,18 +120,18 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
124120
}
125121

126122
bb8: {
127-
_17 = ((_15 as Some).0: &T);
123+
_16 = ((_14 as Some).0: &T);
124+
StorageLive(_17);
125+
_17 = &_2;
128126
StorageLive(_18);
129-
_18 = &_2;
130-
StorageLive(_19);
131-
_19 = (_17,);
132-
_20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind: bb11];
127+
_18 = (_16,);
128+
_19 = <impl Fn(&T) as Fn<(&T,)>>::call(move _17, move _18) -> [return: bb9, unwind: bb11];
133129
}
134130

135131
bb9: {
136-
StorageDead(_19);
137132
StorageDead(_18);
138-
StorageDead(_15);
133+
StorageDead(_17);
134+
StorageDead(_14);
139135
goto -> bb4;
140136
}
141137

‎tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7-
let mut _12: std::slice::Iter<'_, T>;
7+
let mut _11: std::slice::Iter<'_, T>;
8+
let mut _12: std::iter::Rev<std::slice::Iter<'_, T>>;
89
let mut _13: std::iter::Rev<std::slice::Iter<'_, T>>;
9-
let mut _14: std::iter::Rev<std::slice::Iter<'_, T>>;
10-
let mut _16: std::option::Option<&T>;
11-
let mut _17: isize;
12-
let mut _19: &impl Fn(&T);
13-
let mut _20: (&T,);
14-
let _21: ();
10+
let mut _15: std::option::Option<&T>;
11+
let mut _16: isize;
12+
let mut _18: &impl Fn(&T);
13+
let mut _19: (&T,);
14+
let _20: ();
1515
scope 1 {
16-
debug iter => _14;
17-
let _18: &T;
16+
debug iter => _13;
17+
let _17: &T;
1818
scope 2 {
19-
debug x => _18;
19+
debug x => _17;
2020
}
2121
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
22-
let mut _15: &mut std::slice::Iter<'_, T>;
22+
let mut _14: &mut std::slice::Iter<'_, T>;
2323
}
2424
}
2525
scope 3 (inlined core::slice::<impl [T]>::iter) {
2626
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2727
let _3: usize;
28-
let mut _7: bool;
28+
let mut _7: *mut T;
2929
let mut _8: *mut T;
30-
let mut _9: *mut T;
31-
let mut _11: *const T;
30+
let mut _10: *const T;
3231
scope 5 {
3332
let _6: std::ptr::NonNull<T>;
3433
scope 6 {
35-
let _10: *const T;
34+
let _9: *const T;
3635
scope 7 {
3736
}
3837
scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
6160
}
6261

6362
bb0: {
64-
StorageLive(_12);
63+
StorageLive(_11);
6564
StorageLive(_3);
6665
StorageLive(_6);
6766
StorageLive(_4);
@@ -70,62 +69,59 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
7069
_4 = &raw const (*_1);
7170
_5 = _4 as *const T (PtrToPtr);
7271
_6 = NonNull::<T> { pointer: _5 };
73-
StorageLive(_10);
74-
StorageLive(_7);
75-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
76-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
72+
StorageLive(_9);
73+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
7774
}
7875

7976
bb1: {
80-
StorageLive(_9);
8177
StorageLive(_8);
82-
_8 = _4 as *mut T (PtrToPtr);
83-
_9 = Offset(_8, _3);
78+
StorageLive(_7);
79+
_7 = _4 as *mut T (PtrToPtr);
80+
_8 = Offset(_7, _3);
81+
StorageDead(_7);
82+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
8483
StorageDead(_8);
85-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
86-
StorageDead(_9);
8784
goto -> bb3;
8885
}
8986

9087
bb2: {
91-
_10 = _3 as *const T (Transmute);
88+
_9 = _3 as *const T (Transmute);
9289
goto -> bb3;
9390
}
9491

9592
bb3: {
96-
StorageDead(_7);
97-
StorageLive(_11);
98-
_11 = _10;
99-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
100-
StorageDead(_11);
93+
StorageLive(_10);
94+
_10 = _9;
95+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
10196
StorageDead(_10);
97+
StorageDead(_9);
10298
StorageDead(_5);
10399
StorageDead(_4);
104100
StorageDead(_6);
105101
StorageDead(_3);
106-
_13 = Rev::<std::slice::Iter<'_, T>> { iter: _12 };
107-
StorageDead(_12);
108-
StorageLive(_14);
109-
_14 = _13;
102+
_12 = Rev::<std::slice::Iter<'_, T>> { iter: _11 };
103+
StorageDead(_11);
104+
StorageLive(_13);
105+
_13 = _12;
110106
goto -> bb4;
111107
}
112108

113109
bb4: {
114-
StorageLive(_16);
115110
StorageLive(_15);
116-
_15 = &mut (_14.0: std::slice::Iter<'_, T>);
117-
_16 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _15) -> [return: bb5, unwind unreachable];
111+
StorageLive(_14);
112+
_14 = &mut (_13.0: std::slice::Iter<'_, T>);
113+
_15 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _14) -> [return: bb5, unwind unreachable];
118114
}
119115

120116
bb5: {
121-
StorageDead(_15);
122-
_17 = discriminant(_16);
123-
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
117+
StorageDead(_14);
118+
_16 = discriminant(_15);
119+
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
124120
}
125121

126122
bb6: {
127-
StorageDead(_16);
128-
StorageDead(_14);
123+
StorageDead(_15);
124+
StorageDead(_13);
129125
drop(_2) -> [return: bb7, unwind unreachable];
130126
}
131127

@@ -134,18 +130,18 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
134130
}
135131

136132
bb8: {
137-
_18 = ((_16 as Some).0: &T);
133+
_17 = ((_15 as Some).0: &T);
134+
StorageLive(_18);
135+
_18 = &_2;
138136
StorageLive(_19);
139-
_19 = &_2;
140-
StorageLive(_20);
141-
_20 = (_18,);
142-
_21 = <impl Fn(&T) as Fn<(&T,)>>::call(move _19, move _20) -> [return: bb9, unwind unreachable];
137+
_19 = (_17,);
138+
_20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind unreachable];
143139
}
144140

145141
bb9: {
146-
StorageDead(_20);
147142
StorageDead(_19);
148-
StorageDead(_16);
143+
StorageDead(_18);
144+
StorageDead(_15);
149145
goto -> bb4;
150146
}
151147

‎tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
44
debug slice => _1;
55
debug f => _2;
66
let mut _0: ();
7-
let mut _12: std::slice::Iter<'_, T>;
7+
let mut _11: std::slice::Iter<'_, T>;
8+
let mut _12: std::iter::Rev<std::slice::Iter<'_, T>>;
89
let mut _13: std::iter::Rev<std::slice::Iter<'_, T>>;
9-
let mut _14: std::iter::Rev<std::slice::Iter<'_, T>>;
10-
let mut _16: std::option::Option<&T>;
11-
let mut _17: isize;
12-
let mut _19: &impl Fn(&T);
13-
let mut _20: (&T,);
14-
let _21: ();
10+
let mut _15: std::option::Option<&T>;
11+
let mut _16: isize;
12+
let mut _18: &impl Fn(&T);
13+
let mut _19: (&T,);
14+
let _20: ();
1515
scope 1 {
16-
debug iter => _14;
17-
let _18: &T;
16+
debug iter => _13;
17+
let _17: &T;
1818
scope 2 {
19-
debug x => _18;
19+
debug x => _17;
2020
}
2121
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
22-
let mut _15: &mut std::slice::Iter<'_, T>;
22+
let mut _14: &mut std::slice::Iter<'_, T>;
2323
}
2424
}
2525
scope 3 (inlined core::slice::<impl [T]>::iter) {
2626
scope 4 (inlined std::slice::Iter::<'_, T>::new) {
2727
let _3: usize;
28-
let mut _7: bool;
28+
let mut _7: *mut T;
2929
let mut _8: *mut T;
30-
let mut _9: *mut T;
31-
let mut _11: *const T;
30+
let mut _10: *const T;
3231
scope 5 {
3332
let _6: std::ptr::NonNull<T>;
3433
scope 6 {
35-
let _10: *const T;
34+
let _9: *const T;
3635
scope 7 {
3736
}
3837
scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
6160
}
6261

6362
bb0: {
64-
StorageLive(_12);
63+
StorageLive(_11);
6564
StorageLive(_3);
6665
StorageLive(_6);
6766
StorageLive(_4);
@@ -70,62 +69,59 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
7069
_4 = &raw const (*_1);
7170
_5 = _4 as *const T (PtrToPtr);
7271
_6 = NonNull::<T> { pointer: _5 };
73-
StorageLive(_10);
74-
StorageLive(_7);
75-
_7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
76-
switchInt(move _7) -> [0: bb1, otherwise: bb2];
72+
StorageLive(_9);
73+
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
7774
}
7875

7976
bb1: {
80-
StorageLive(_9);
8177
StorageLive(_8);
82-
_8 = _4 as *mut T (PtrToPtr);
83-
_9 = Offset(_8, _3);
78+
StorageLive(_7);
79+
_7 = _4 as *mut T (PtrToPtr);
80+
_8 = Offset(_7, _3);
81+
StorageDead(_7);
82+
_9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
8483
StorageDead(_8);
85-
_10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
86-
StorageDead(_9);
8784
goto -> bb3;
8885
}
8986

9087
bb2: {
91-
_10 = _3 as *const T (Transmute);
88+
_9 = _3 as *const T (Transmute);
9289
goto -> bb3;
9390
}
9491

9592
bb3: {
96-
StorageDead(_7);
97-
StorageLive(_11);
98-
_11 = _10;
99-
_12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
100-
StorageDead(_11);
93+
StorageLive(_10);
94+
_10 = _9;
95+
_11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
10196
StorageDead(_10);
97+
StorageDead(_9);
10298
StorageDead(_5);
10399
StorageDead(_4);
104100
StorageDead(_6);
105101
StorageDead(_3);
106-
_13 = Rev::<std::slice::Iter<'_, T>> { iter: _12 };
107-
StorageDead(_12);
108-
StorageLive(_14);
109-
_14 = _13;
102+
_12 = Rev::<std::slice::Iter<'_, T>> { iter: _11 };
103+
StorageDead(_11);
104+
StorageLive(_13);
105+
_13 = _12;
110106
goto -> bb4;
111107
}
112108

113109
bb4: {
114-
StorageLive(_16);
115110
StorageLive(_15);
116-
_15 = &mut (_14.0: std::slice::Iter<'_, T>);
117-
_16 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _15) -> [return: bb5, unwind: bb11];
111+
StorageLive(_14);
112+
_14 = &mut (_13.0: std::slice::Iter<'_, T>);
113+
_15 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _14) -> [return: bb5, unwind: bb11];
118114
}
119115

120116
bb5: {
121-
StorageDead(_15);
122-
_17 = discriminant(_16);
123-
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
117+
StorageDead(_14);
118+
_16 = discriminant(_15);
119+
switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
124120
}
125121

126122
bb6: {
127-
StorageDead(_16);
128-
StorageDead(_14);
123+
StorageDead(_15);
124+
StorageDead(_13);
129125
drop(_2) -> [return: bb7, unwind continue];
130126
}
131127

@@ -134,18 +130,18 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
134130
}
135131

136132
bb8: {
137-
_18 = ((_16 as Some).0: &T);
133+
_17 = ((_15 as Some).0: &T);
134+
StorageLive(_18);
135+
_18 = &_2;
138136
StorageLive(_19);
139-
_19 = &_2;
140-
StorageLive(_20);
141-
_20 = (_18,);
142-
_21 = <impl Fn(&T) as Fn<(&T,)>>::call(move _19, move _20) -> [return: bb9, unwind: bb11];
137+
_19 = (_17,);
138+
_20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind: bb11];
143139
}
144140

145141
bb9: {
146-
StorageDead(_20);
147142
StorageDead(_19);
148-
StorageDead(_16);
143+
StorageDead(_18);
144+
StorageDead(_15);
149145
goto -> bb4;
150146
}
151147

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- // MIR for `assign_const_to_return` before SingleUseConsts
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts
3+
4+
fn assign_const_to_return() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
_0 = const <T as MyTrait>::ASSOC_BOOL;
9+
return;
10+
}
11+
}
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- // MIR for `assign_const_to_return` before SingleUseConsts
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts
3+
4+
fn assign_const_to_return() -> bool {
5+
let mut _0: bool;
6+
7+
bb0: {
8+
_0 = const <T as MyTrait>::ASSOC_BOOL;
9+
return;
10+
}
11+
}
12+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- // MIR for `if_const` before SingleUseConsts
2+
+ // MIR for `if_const` after SingleUseConsts
3+
4+
fn if_const() -> i32 {
5+
let mut _0: i32;
6+
let mut _1: bool;
7+
8+
bb0: {
9+
StorageLive(_1);
10+
- _1 = const <T as MyTrait>::ASSOC_BOOL;
11+
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
12+
+ nop;
13+
+ switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
14+
}
15+
16+
bb1: {
17+
_0 = const 7_i32;
18+
goto -> bb3;
19+
}
20+
21+
bb2: {
22+
_0 = const 42_i32;
23+
goto -> bb3;
24+
}
25+
26+
bb3: {
27+
StorageDead(_1);
28+
return;
29+
}
30+
}
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- // MIR for `if_const` before SingleUseConsts
2+
+ // MIR for `if_const` after SingleUseConsts
3+
4+
fn if_const() -> i32 {
5+
let mut _0: i32;
6+
let mut _1: bool;
7+
8+
bb0: {
9+
StorageLive(_1);
10+
- _1 = const <T as MyTrait>::ASSOC_BOOL;
11+
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
12+
+ nop;
13+
+ switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
14+
}
15+
16+
bb1: {
17+
_0 = const 7_i32;
18+
goto -> bb3;
19+
}
20+
21+
bb2: {
22+
_0 = const 42_i32;
23+
goto -> bb3;
24+
}
25+
26+
bb3: {
27+
StorageDead(_1);
28+
return;
29+
}
30+
}
31+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `if_const_debug` before SingleUseConsts
2+
+ // MIR for `if_const_debug` after SingleUseConsts
3+
4+
fn if_const_debug() -> i32 {
5+
let mut _0: i32;
6+
let _1: bool;
7+
let _2: ();
8+
let mut _3: bool;
9+
scope 1 {
10+
- debug my_bool => _1;
11+
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
- _1 = const <T as MyTrait>::ASSOC_BOOL;
17+
+ nop;
18+
StorageLive(_2);
19+
_2 = do_whatever() -> [return: bb1, unwind unreachable];
20+
}
21+
22+
bb1: {
23+
StorageDead(_2);
24+
StorageLive(_3);
25+
- _3 = _1;
26+
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
27+
switchInt(move _3) -> [0: bb3, otherwise: bb2];
28+
}
29+
30+
bb2: {
31+
_0 = const 7_i32;
32+
goto -> bb4;
33+
}
34+
35+
bb3: {
36+
_0 = const 42_i32;
37+
goto -> bb4;
38+
}
39+
40+
bb4: {
41+
StorageDead(_3);
42+
StorageDead(_1);
43+
return;
44+
}
45+
}
46+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `if_const_debug` before SingleUseConsts
2+
+ // MIR for `if_const_debug` after SingleUseConsts
3+
4+
fn if_const_debug() -> i32 {
5+
let mut _0: i32;
6+
let _1: bool;
7+
let _2: ();
8+
let mut _3: bool;
9+
scope 1 {
10+
- debug my_bool => _1;
11+
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
- _1 = const <T as MyTrait>::ASSOC_BOOL;
17+
+ nop;
18+
StorageLive(_2);
19+
_2 = do_whatever() -> [return: bb1, unwind continue];
20+
}
21+
22+
bb1: {
23+
StorageDead(_2);
24+
StorageLive(_3);
25+
- _3 = _1;
26+
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
27+
switchInt(move _3) -> [0: bb3, otherwise: bb2];
28+
}
29+
30+
bb2: {
31+
_0 = const 7_i32;
32+
goto -> bb4;
33+
}
34+
35+
bb3: {
36+
_0 = const 42_i32;
37+
goto -> bb4;
38+
}
39+
40+
bb4: {
41+
StorageDead(_3);
42+
StorageDead(_1);
43+
return;
44+
}
45+
}
46+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- // MIR for `keep_parameter` before SingleUseConsts
2+
+ // MIR for `keep_parameter` after SingleUseConsts
3+
4+
fn keep_parameter(_1: i32) -> () {
5+
debug other => _1;
6+
let mut _0: ();
7+
8+
bb0: {
9+
_1 = const <T as MyTrait>::ASSOC_INT;
10+
_0 = const ();
11+
return;
12+
}
13+
}
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- // MIR for `keep_parameter` before SingleUseConsts
2+
+ // MIR for `keep_parameter` after SingleUseConsts
3+
4+
fn keep_parameter(_1: i32) -> () {
5+
debug other => _1;
6+
let mut _0: ();
7+
8+
bb0: {
9+
_1 = const <T as MyTrait>::ASSOC_INT;
10+
_0 = const ();
11+
return;
12+
}
13+
}
14+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- // MIR for `match_const` before SingleUseConsts
2+
+ // MIR for `match_const` after SingleUseConsts
3+
4+
fn match_const() -> &str {
5+
let mut _0: &str;
6+
let mut _1: i32;
7+
8+
bb0: {
9+
StorageLive(_1);
10+
- _1 = const <T as MyTrait>::ASSOC_INT;
11+
- switchInt(_1) -> [7: bb2, 42: bb3, otherwise: bb1];
12+
+ nop;
13+
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb2, 42: bb3, otherwise: bb1];
14+
}
15+
16+
bb1: {
17+
_0 = const "world";
18+
goto -> bb4;
19+
}
20+
21+
bb2: {
22+
_0 = const "hello";
23+
goto -> bb4;
24+
}
25+
26+
bb3: {
27+
_0 = const "towel";
28+
goto -> bb4;
29+
}
30+
31+
bb4: {
32+
StorageDead(_1);
33+
return;
34+
}
35+
}
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- // MIR for `match_const` before SingleUseConsts
2+
+ // MIR for `match_const` after SingleUseConsts
3+
4+
fn match_const() -> &str {
5+
let mut _0: &str;
6+
let mut _1: i32;
7+
8+
bb0: {
9+
StorageLive(_1);
10+
- _1 = const <T as MyTrait>::ASSOC_INT;
11+
- switchInt(_1) -> [7: bb2, 42: bb3, otherwise: bb1];
12+
+ nop;
13+
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb2, 42: bb3, otherwise: bb1];
14+
}
15+
16+
bb1: {
17+
_0 = const "world";
18+
goto -> bb4;
19+
}
20+
21+
bb2: {
22+
_0 = const "hello";
23+
goto -> bb4;
24+
}
25+
26+
bb3: {
27+
_0 = const "towel";
28+
goto -> bb4;
29+
}
30+
31+
bb4: {
32+
StorageDead(_1);
33+
return;
34+
}
35+
}
36+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
- // MIR for `match_const_debug` before SingleUseConsts
2+
+ // MIR for `match_const_debug` after SingleUseConsts
3+
4+
fn match_const_debug() -> &str {
5+
let mut _0: &str;
6+
let _1: i32;
7+
let _2: ();
8+
scope 1 {
9+
- debug my_int => _1;
10+
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
11+
}
12+
13+
bb0: {
14+
StorageLive(_1);
15+
- _1 = const <T as MyTrait>::ASSOC_INT;
16+
+ nop;
17+
StorageLive(_2);
18+
_2 = do_whatever() -> [return: bb1, unwind unreachable];
19+
}
20+
21+
bb1: {
22+
StorageDead(_2);
23+
- switchInt(_1) -> [7: bb3, 42: bb4, otherwise: bb2];
24+
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb3, 42: bb4, otherwise: bb2];
25+
}
26+
27+
bb2: {
28+
_0 = const "world";
29+
goto -> bb5;
30+
}
31+
32+
bb3: {
33+
_0 = const "hello";
34+
goto -> bb5;
35+
}
36+
37+
bb4: {
38+
_0 = const "towel";
39+
goto -> bb5;
40+
}
41+
42+
bb5: {
43+
StorageDead(_1);
44+
return;
45+
}
46+
}
47+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
- // MIR for `match_const_debug` before SingleUseConsts
2+
+ // MIR for `match_const_debug` after SingleUseConsts
3+
4+
fn match_const_debug() -> &str {
5+
let mut _0: &str;
6+
let _1: i32;
7+
let _2: ();
8+
scope 1 {
9+
- debug my_int => _1;
10+
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
11+
}
12+
13+
bb0: {
14+
StorageLive(_1);
15+
- _1 = const <T as MyTrait>::ASSOC_INT;
16+
+ nop;
17+
StorageLive(_2);
18+
_2 = do_whatever() -> [return: bb1, unwind continue];
19+
}
20+
21+
bb1: {
22+
StorageDead(_2);
23+
- switchInt(_1) -> [7: bb3, 42: bb4, otherwise: bb2];
24+
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb3, 42: bb4, otherwise: bb2];
25+
}
26+
27+
bb2: {
28+
_0 = const "world";
29+
goto -> bb5;
30+
}
31+
32+
bb3: {
33+
_0 = const "hello";
34+
goto -> bb5;
35+
}
36+
37+
bb4: {
38+
_0 = const "towel";
39+
goto -> bb5;
40+
}
41+
42+
bb5: {
43+
StorageDead(_1);
44+
return;
45+
}
46+
}
47+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- // MIR for `never_used_debug` before SingleUseConsts
2+
+ // MIR for `never_used_debug` after SingleUseConsts
3+
4+
fn never_used_debug() -> () {
5+
let mut _0: ();
6+
let _1: i32;
7+
scope 1 {
8+
- debug my_int => _1;
9+
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
10+
}
11+
12+
bb0: {
13+
StorageLive(_1);
14+
- _1 = const <T as MyTrait>::ASSOC_INT;
15+
+ nop;
16+
_0 = const ();
17+
StorageDead(_1);
18+
return;
19+
}
20+
}
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- // MIR for `never_used_debug` before SingleUseConsts
2+
+ // MIR for `never_used_debug` after SingleUseConsts
3+
4+
fn never_used_debug() -> () {
5+
let mut _0: ();
6+
let _1: i32;
7+
scope 1 {
8+
- debug my_int => _1;
9+
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
10+
}
11+
12+
bb0: {
13+
StorageLive(_1);
14+
- _1 = const <T as MyTrait>::ASSOC_INT;
15+
+ nop;
16+
_0 = const ();
17+
StorageDead(_1);
18+
return;
19+
}
20+
}
21+

‎tests/mir-opt/single_use_consts.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//@ test-mir-pass: SingleUseConsts
2+
//@ compile-flags: -C debuginfo=full
3+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4+
5+
trait MyTrait {
6+
const ASSOC_BOOL: bool;
7+
const ASSOC_INT: i32;
8+
}
9+
10+
// EMIT_MIR single_use_consts.if_const.SingleUseConsts.diff
11+
fn if_const<T: MyTrait>() -> i32 {
12+
// CHECK-LABEL: fn if_const(
13+
// CHECK: switchInt(const <T as MyTrait>::ASSOC_BOOL)
14+
if T::ASSOC_BOOL { 7 } else { 42 }
15+
}
16+
17+
// EMIT_MIR single_use_consts.match_const.SingleUseConsts.diff
18+
fn match_const<T: MyTrait>() -> &'static str {
19+
// CHECK-LABEL: fn match_const(
20+
// CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
21+
match T::ASSOC_INT {
22+
7 => "hello",
23+
42 => "towel",
24+
_ => "world",
25+
}
26+
}
27+
28+
// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
29+
fn if_const_debug<T: MyTrait>() -> i32 {
30+
// CHECK-LABEL: fn if_const_debug(
31+
// CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
32+
// FIXME: `if` forces a temporary (unlike `match`), so the const isn't direct
33+
// CHECK: _3 = const <T as MyTrait>::ASSOC_BOOL;
34+
// CHECK: switchInt(move _3)
35+
let my_bool = T::ASSOC_BOOL;
36+
do_whatever();
37+
if my_bool { 7 } else { 42 }
38+
}
39+
40+
// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
41+
fn match_const_debug<T: MyTrait>() -> &'static str {
42+
// CHECK-LABEL: fn match_const_debug(
43+
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
44+
// CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
45+
let my_int = T::ASSOC_INT;
46+
do_whatever();
47+
match my_int {
48+
7 => "hello",
49+
42 => "towel",
50+
_ => "world",
51+
}
52+
}
53+
54+
// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts.diff
55+
#[allow(unused_variables)]
56+
fn never_used_debug<T: MyTrait>() {
57+
// CHECK-LABEL: fn never_used_debug(
58+
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
59+
// CHECK-NOT: ASSOC_INT
60+
// CHECK: nop
61+
// CHECK-NOT: ASSOC_INT
62+
let my_int = T::ASSOC_INT;
63+
}
64+
65+
// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts.diff
66+
fn assign_const_to_return<T: MyTrait>() -> bool {
67+
// CHECK-LABEL: fn assign_const_to_return(
68+
// CHECK: _0 = const <T as MyTrait>::ASSOC_BOOL;
69+
T::ASSOC_BOOL
70+
}
71+
72+
// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts.diff
73+
fn keep_parameter<T: MyTrait>(mut other: i32) {
74+
// CHECK-LABEL: fn keep_parameter(
75+
// CHECK: _1 = const <T as MyTrait>::ASSOC_INT;
76+
// CHECK: _0 = const ();
77+
other = T::ASSOC_INT;
78+
}
79+
80+
fn do_whatever() {}

0 commit comments

Comments
 (0)
Please sign in to comment.