Skip to content

Commit

Permalink
Drive-by-cleanup: Don't emit thir::ExprKind::NeverToAny for ! -> !
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed May 19, 2023
1 parent f542778 commit 140cdcb
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 135 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl<'tcx> Cx<'tcx> {
ExprKind::Pointer { cast: PointerCast::Unsize, source: self.thir.exprs.push(expr) }
}
Adjust::Pointer(cast) => ExprKind::Pointer { cast, source: self.thir.exprs.push(expr) },
Adjust::NeverToAny if adjustment.target.is_never() => return expr,
Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
Adjust::Deref(None) => {
adjust_span(&mut expr);
Expand Down
6 changes: 2 additions & 4 deletions src/tools/miri/tests/fail/never_say_never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

fn main() {
let y = &5;
let x: ! = unsafe {
*(y as *const _ as *const !) //~ ERROR: entering unreachable code
};
f(x)
let x: ! = unsafe { *(y as *const _ as *const !) };
f(x) //~ ERROR: entering unreachable code
}

fn f(x: !) -> ! {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/never_say_never.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: entering unreachable code
--> $DIR/never_say_never.rs:LL:CC
|
LL | *(y as *const _ as *const !)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
LL | f(x)
| ^^^^ entering unreachable code
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _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
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _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
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x00000001)) }
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _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
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _1 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _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
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x0000000000000001)) }
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:41: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_3); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_3 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_2 = move _3 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_2); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_2 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_1 = move _2 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
unreachable; // scope 2 at $DIR/transmute.rs:+1:29: +1:42
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:41: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_3); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_3 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_2 = move _3 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_2); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_2 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
_1 = move _2 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
unreachable; // scope 2 at $DIR/transmute.rs:+1:29: +1:42
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
- _3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _3 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
- _2 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _2 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: &mut Never, val: Value(Scalar(0x00000001)) }
_2 = &mut (*_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
StorageDead(_3); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
StorageLive(_4); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
_1 = &mut (*_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
StorageDead(_2); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
- _3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _3 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
- _2 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ _2 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: &mut Never, val: Value(Scalar(0x0000000000000001)) }
_2 = &mut (*_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
StorageDead(_3); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
StorageLive(_4); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
_1 = &mut (*_2); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
StorageDead(_2); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@

fn unreachable_ref() -> ! {
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
let _2: &Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
let _1: &Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
scope 1 {
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
}
scope 2 {
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _2 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _1 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ _1 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: &Never, val: Value(Scalar(0x00000001)) }
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@

fn unreachable_ref() -> ! {
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
let _2: &Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
let _1: &Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
scope 1 {
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
debug x => _1; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
}
scope 2 {
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _2 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
- _1 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ _1 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
+ // mir::Constant
+ // + span: no-location
+ // + literal: Const { ty: &Never, val: Value(Scalar(0x0000000000000001)) }
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
}
}
Expand Down
14 changes: 0 additions & 14 deletions tests/mir-opt/issue_72181_1.f.built.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@
fn f(_1: Void) -> ! {
debug v => _1; // in scope 0 at $DIR/issue_72181_1.rs:+0:6: +0:7
let mut _0: !; // return place in scope 0 at $DIR/issue_72181_1.rs:+0:18: +0:19
let mut _2: !; // in scope 0 at $DIR/issue_72181_1.rs:+0:20: +2:2
let mut _3: !; // in scope 0 at $DIR/issue_72181_1.rs:+1:5: +1:15

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

bb1: {
unreachable; // scope 0 at $DIR/issue_72181_1.rs:+1:5: +1:15
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/issue_72181_1.rs:+1:14: +1:15
unreachable; // scope 0 at $DIR/issue_72181_1.rs:+0:20: +2:2
}

bb3: {
StorageDead(_2); // scope 0 at $DIR/issue_72181_1.rs:+2:1: +2:2
return; // scope 0 at $DIR/issue_72181_1.rs:+2:2: +2:2
}
}
Loading

0 comments on commit 140cdcb

Please sign in to comment.