Skip to content

Commit 140cdcb

Browse files
committed
Drive-by-cleanup: Don't emit thir::ExprKind::NeverToAny for ! -> !
1 parent f542778 commit 140cdcb

16 files changed

+77
-135
lines changed

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);

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
}

tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.64bit.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(0x0000000000000001)) }
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
}

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

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

44
fn unreachable_ref() -> ! {
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: &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: &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 &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
19-
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
14+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
15+
- _1 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
16+
+ _1 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
2017
+ // mir::Constant
2118
+ // + span: no-location
2219
+ // + literal: Const { ty: &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_ref.ConstProp.64bit.diff

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

44
fn unreachable_ref() -> ! {
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: &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: &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 &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
19-
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
14+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
15+
- _1 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
16+
+ _1 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
2017
+ // mir::Constant
2118
+ // + span: no-location
2219
+ // + literal: Const { ty: &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/issue_72181_1.f.built.after.mir

-14
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,13 @@
33
fn f(_1: Void) -> ! {
44
debug v => _1; // in scope 0 at $DIR/issue_72181_1.rs:+0:6: +0:7
55
let mut _0: !; // return place in scope 0 at $DIR/issue_72181_1.rs:+0:18: +0:19
6-
let mut _2: !; // in scope 0 at $DIR/issue_72181_1.rs:+0:20: +2:2
7-
let mut _3: !; // in scope 0 at $DIR/issue_72181_1.rs:+1:5: +1:15
86

97
bb0: {
10-
StorageLive(_2); // scope 0 at $DIR/issue_72181_1.rs:+0:20: +2:2
11-
StorageLive(_3); // scope 0 at $DIR/issue_72181_1.rs:+1:5: +1:15
128
FakeRead(ForMatchedPlace(None), _1); // scope 0 at $DIR/issue_72181_1.rs:+1:11: +1:12
139
unreachable; // scope 0 at $DIR/issue_72181_1.rs:+1:11: +1:12
1410
}
1511

1612
bb1: {
17-
unreachable; // scope 0 at $DIR/issue_72181_1.rs:+1:5: +1:15
18-
}
19-
20-
bb2: {
21-
StorageDead(_3); // scope 0 at $DIR/issue_72181_1.rs:+1:14: +1:15
22-
unreachable; // scope 0 at $DIR/issue_72181_1.rs:+0:20: +2:2
23-
}
24-
25-
bb3: {
26-
StorageDead(_2); // scope 0 at $DIR/issue_72181_1.rs:+2:1: +2:2
2713
return; // scope 0 at $DIR/issue_72181_1.rs:+2:2: +2:2
2814
}
2915
}

0 commit comments

Comments
 (0)