Skip to content

Commit df86200

Browse files
authored
Rollup merge of #111501 - WaffleLapkin:drivebycleanupuwu, r=oli-obk
MIR drive-by cleanups Some random drive-by cleanups I did while working with MIR/THIR.
2 parents 2fe47b9 + 140cdcb commit df86200

21 files changed

+124
-179
lines changed

compiler/rustc_middle/src/mir/syntax.rs

+23
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,29 @@ pub enum TerminatorKind<'tcx> {
749749
},
750750
}
751751

752+
impl TerminatorKind<'_> {
753+
/// Returns a simple string representation of a `TerminatorKind` variant, independent of any
754+
/// values it might hold (e.g. `TerminatorKind::Call` always returns `"Call"`).
755+
pub const fn name(&self) -> &'static str {
756+
match self {
757+
TerminatorKind::Goto { .. } => "Goto",
758+
TerminatorKind::SwitchInt { .. } => "SwitchInt",
759+
TerminatorKind::Resume => "Resume",
760+
TerminatorKind::Terminate => "Terminate",
761+
TerminatorKind::Return => "Return",
762+
TerminatorKind::Unreachable => "Unreachable",
763+
TerminatorKind::Drop { .. } => "Drop",
764+
TerminatorKind::Call { .. } => "Call",
765+
TerminatorKind::Assert { .. } => "Assert",
766+
TerminatorKind::Yield { .. } => "Yield",
767+
TerminatorKind::GeneratorDrop => "GeneratorDrop",
768+
TerminatorKind::FalseEdge { .. } => "FalseEdge",
769+
TerminatorKind::FalseUnwind { .. } => "FalseUnwind",
770+
TerminatorKind::InlineAsm { .. } => "InlineAsm",
771+
}
772+
}
773+
}
774+
752775
/// Action to be taken when a stack unwind happens.
753776
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
754777
#[derive(TypeFoldable, TypeVisitable)]

compiler/rustc_mir_build/src/build/scope.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -644,24 +644,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
644644
}
645645
};
646646

647-
if let Some(destination) = destination {
648-
if let Some(value) = value {
647+
match (destination, value) {
648+
(Some(destination), Some(value)) => {
649649
debug!("stmt_expr Break val block_context.push(SubExpr)");
650650
self.block_context.push(BlockFrame::SubExpr);
651651
unpack!(block = self.expr_into_dest(destination, block, value));
652652
self.block_context.pop();
653-
} else {
653+
}
654+
(Some(destination), None) => {
654655
self.cfg.push_assign_unit(block, source_info, destination, self.tcx)
655656
}
656-
} else {
657-
assert!(value.is_none(), "`return` and `break` should have a destination");
658-
if self.tcx.sess.instrument_coverage() {
657+
(None, Some(_)) => {
658+
panic!("`return`, `become` and `break` with value and must have a destination")
659+
}
660+
(None, None) if self.tcx.sess.instrument_coverage() => {
659661
// Unlike `break` and `return`, which push an `Assign` statement to MIR, from which
660662
// a Coverage code region can be generated, `continue` needs no `Assign`; but
661663
// without one, the `InstrumentCoverage` MIR pass cannot generate a code region for
662664
// `continue`. Coverage will be missing unless we add a dummy `Assign` to MIR.
663665
self.add_dummy_assignment(span, block, source_info);
664666
}
667+
(None, None) => {}
665668
}
666669

667670
let region_scope = self.scopes.breakable_scopes[break_index].region_scope;
@@ -671,12 +674,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
671674
} else {
672675
self.scopes.breakable_scopes[break_index].continue_drops.as_mut().unwrap()
673676
};
674-
let mut drop_idx = ROOT_NODE;
675-
for scope in &self.scopes.scopes[scope_index + 1..] {
676-
for drop in &scope.drops {
677-
drop_idx = drops.add_drop(*drop, drop_idx);
678-
}
679-
}
677+
678+
let drop_idx = self.scopes.scopes[scope_index + 1..]
679+
.iter()
680+
.flat_map(|scope| &scope.drops)
681+
.fold(ROOT_NODE, |drop_idx, &drop| drops.add_drop(drop, drop_idx));
682+
680683
drops.add_entry(block, drop_idx);
681684

682685
// `build_drop_trees` doesn't have access to our source_info, so we

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

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl<'tcx> Cx<'tcx> {
130130
ExprKind::Pointer { cast: PointerCast::Unsize, source: self.thir.exprs.push(expr) }
131131
}
132132
Adjust::Pointer(cast) => ExprKind::Pointer { cast, source: self.thir.exprs.push(expr) },
133+
Adjust::NeverToAny if adjustment.target.is_never() => return expr,
133134
Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
134135
Adjust::Deref(None) => {
135136
adjust_span(&mut expr);

compiler/rustc_mir_transform/src/coverage/debug.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use rustc_middle::mir::spanview::{self, SpanViewable};
118118

119119
use rustc_data_structures::fx::FxHashMap;
120120
use rustc_middle::mir::coverage::*;
121-
use rustc_middle::mir::{self, BasicBlock, TerminatorKind};
121+
use rustc_middle::mir::{self, BasicBlock};
122122
use rustc_middle::ty::TyCtxt;
123123
use rustc_span::Span;
124124

@@ -796,36 +796,15 @@ fn bcb_to_string_sections<'tcx>(
796796
}
797797
let non_term_blocks = bcb_data.basic_blocks[0..len - 1]
798798
.iter()
799-
.map(|&bb| format!("{:?}: {}", bb, term_type(&mir_body[bb].terminator().kind)))
799+
.map(|&bb| format!("{:?}: {}", bb, mir_body[bb].terminator().kind.name()))
800800
.collect::<Vec<_>>();
801801
if non_term_blocks.len() > 0 {
802802
sections.push(non_term_blocks.join("\n"));
803803
}
804804
sections.push(format!(
805805
"{:?}: {}",
806806
bcb_data.basic_blocks.last().unwrap(),
807-
term_type(&bcb_data.terminator(mir_body).kind)
807+
bcb_data.terminator(mir_body).kind.name(),
808808
));
809809
sections
810810
}
811-
812-
/// Returns a simple string representation of a `TerminatorKind` variant, independent of any
813-
/// values it might hold.
814-
pub(super) fn term_type(kind: &TerminatorKind<'_>) -> &'static str {
815-
match kind {
816-
TerminatorKind::Goto { .. } => "Goto",
817-
TerminatorKind::SwitchInt { .. } => "SwitchInt",
818-
TerminatorKind::Resume => "Resume",
819-
TerminatorKind::Terminate => "Terminate",
820-
TerminatorKind::Return => "Return",
821-
TerminatorKind::Unreachable => "Unreachable",
822-
TerminatorKind::Drop { .. } => "Drop",
823-
TerminatorKind::Call { .. } => "Call",
824-
TerminatorKind::Assert { .. } => "Assert",
825-
TerminatorKind::Yield { .. } => "Yield",
826-
TerminatorKind::GeneratorDrop => "GeneratorDrop",
827-
TerminatorKind::FalseEdge { .. } => "FalseEdge",
828-
TerminatorKind::FalseUnwind { .. } => "FalseUnwind",
829-
TerminatorKind::InlineAsm { .. } => "InlineAsm",
830-
}
831-
}

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::debug::term_type;
21
use super::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph, START_BCB};
32

43
use itertools::Itertools;
@@ -40,7 +39,7 @@ impl CoverageStatement {
4039
"{}: @{}.{}: {:?}",
4140
source_range_no_file(tcx, span),
4241
bb.index(),
43-
term_type(&term.kind),
42+
term.kind.name(),
4443
term.kind
4544
)
4645
}

compiler/rustc_mir_transform/src/coverage/tests.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
//! to: `rustc_span::create_default_session_globals_then(|| { test_here(); })`.
2626
2727
use super::counters;
28-
use super::debug;
2928
use super::graph;
3029
use super::spans;
3130

@@ -188,12 +187,12 @@ fn debug_basic_blocks(mir_body: &Body<'_>) -> String {
188187
| TerminatorKind::Goto { target }
189188
| TerminatorKind::InlineAsm { destination: Some(target), .. }
190189
| TerminatorKind::Yield { resume: target, .. } => {
191-
format!("{}{:?}:{} -> {:?}", sp, bb, debug::term_type(kind), target)
190+
format!("{}{:?}:{} -> {:?}", sp, bb, kind.name(), target)
192191
}
193192
TerminatorKind::SwitchInt { targets, .. } => {
194-
format!("{}{:?}:{} -> {:?}", sp, bb, debug::term_type(kind), targets)
193+
format!("{}{:?}:{} -> {:?}", sp, bb, kind.name(), targets)
195194
}
196-
_ => format!("{}{:?}:{}", sp, bb, debug::term_type(kind)),
195+
_ => format!("{}{:?}:{}", sp, bb, kind.name()),
197196
}
198197
})
199198
.collect::<Vec<_>>()
@@ -215,7 +214,7 @@ fn print_mir_graphviz(name: &str, mir_body: &Body<'_>) {
215214
" {:?} [label=\"{:?}: {}\"];\n{}",
216215
bb,
217216
bb,
218-
debug::term_type(&data.terminator().kind),
217+
data.terminator().kind.name(),
219218
mir_body
220219
.basic_blocks
221220
.successors(bb)
@@ -244,7 +243,7 @@ fn print_coverage_graphviz(
244243
" {:?} [label=\"{:?}: {}\"];\n{}",
245244
bcb,
246245
bcb,
247-
debug::term_type(&bcb_data.terminator(mir_body).kind),
246+
bcb_data.terminator(mir_body).kind.name(),
248247
basic_coverage_blocks
249248
.successors(bcb)
250249
.map(|successor| { format!(" {:?} -> {:?};", bcb, successor) })

src/tools/miri/tests/fail/never_say_never.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
fn main() {
88
let y = &5;
9-
let x: ! = unsafe {
10-
*(y as *const _ as *const !) //~ ERROR: entering unreachable code
11-
};
12-
f(x)
9+
let x: ! = unsafe { *(y as *const _ as *const !) };
10+
f(x) //~ ERROR: entering unreachable code
1311
}
1412

1513
fn f(x: !) -> ! {

src/tools/miri/tests/fail/never_say_never.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: entering unreachable code
22
--> $DIR/never_say_never.rs:LL:CC
33
|
4-
LL | *(y as *const _ as *const !)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
4+
LL | f(x)
5+
| ^^^^ entering unreachable code
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.32bit.diff

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33

44
fn unreachable_box() -> ! {
55
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6-
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7-
let _2: std::boxed::Box<Never>; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8-
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
6+
let _1: std::boxed::Box<Never>; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
97
scope 1 {
10-
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
8+
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
119
}
1210
scope 2 {
1311
}
1412

1513
bb0: {
16-
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
17-
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18-
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
19-
+ _2 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
14+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
15+
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
16+
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
2017
+ // mir::Constant
2118
+ // + span: no-location
2219
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x00000001)) }
23-
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
2420
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
2521
}
2622
}

tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.64bit.diff

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33

44
fn unreachable_box() -> ! {
55
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6-
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7-
let _2: std::boxed::Box<Never>; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8-
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
6+
let _1: std::boxed::Box<Never>; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
97
scope 1 {
10-
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
8+
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
119
}
1210
scope 2 {
1311
}
1412

1513
bb0: {
16-
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
17-
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18-
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
19-
+ _2 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
14+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
15+
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
16+
+ _1 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
2017
+ // mir::Constant
2118
+ // + span: no-location
2219
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x0000000000000001)) }
23-
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
2420
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
2521
}
2622
}

tests/mir-opt/const_prop/transmute.unreachable_direct.ConstProp.32bit.diff

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33

44
fn unreachable_direct() -> ! {
55
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:39: +0:40
6-
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:41: +3:2
7-
let _2: Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8-
let mut _3: (); // in scope 0 at $DIR/transmute.rs:+1:39: +1:41
9-
let mut _4: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:15
6+
let _1: Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
7+
let mut _2: (); // in scope 0 at $DIR/transmute.rs:+1:39: +1:41
108
scope 1 {
11-
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
9+
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
1210
}
1311
scope 2 {
1412
}
1513

1614
bb0: {
17-
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:41: +3:2
18-
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
19-
StorageLive(_3); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
20-
_3 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
21-
_2 = move _3 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
15+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
16+
StorageLive(_2); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
17+
_2 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
18+
_1 = move _2 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
2219
unreachable; // scope 2 at $DIR/transmute.rs:+1:29: +1:42
2320
}
2421
}

tests/mir-opt/const_prop/transmute.unreachable_direct.ConstProp.64bit.diff

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33

44
fn unreachable_direct() -> ! {
55
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:39: +0:40
6-
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:41: +3:2
7-
let _2: Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8-
let mut _3: (); // in scope 0 at $DIR/transmute.rs:+1:39: +1:41
9-
let mut _4: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:15
6+
let _1: Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
7+
let mut _2: (); // in scope 0 at $DIR/transmute.rs:+1:39: +1:41
108
scope 1 {
11-
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
9+
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
1210
}
1311
scope 2 {
1412
}
1513

1614
bb0: {
17-
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:41: +3:2
18-
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
19-
StorageLive(_3); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
20-
_3 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
21-
_2 = move _3 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
15+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
16+
StorageLive(_2); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
17+
_2 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
18+
_1 = move _2 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
2219
unreachable; // scope 2 at $DIR/transmute.rs:+1:29: +1:42
2320
}
2421
}

tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.32bit.diff

+9-13
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@
33

44
fn unreachable_mut() -> ! {
55
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6-
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7-
let _2: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8-
let mut _3: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:34: +1:52
9-
let mut _4: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
6+
let _1: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
7+
let mut _2: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:34: +1:52
108
scope 1 {
11-
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
9+
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
1210
}
1311
scope 2 {
1412
}
1513

1614
bb0: {
17-
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
18-
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
19-
StorageLive(_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
20-
- _3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
21-
+ _3 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
15+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
16+
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
17+
- _2 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
18+
+ _2 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
2219
+ // mir::Constant
2320
+ // + span: no-location
2421
+ // + literal: Const { ty: &mut Never, val: Value(Scalar(0x00000001)) }
25-
_2 = &mut (*_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
26-
StorageDead(_3); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
27-
StorageLive(_4); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
22+
_1 = &mut (*_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
23+
StorageDead(_2); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
2824
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
2925
}
3026
}

0 commit comments

Comments
 (0)