Add CastKind::Transmute to MIR

Updates `interpret`, `codegen_ssa`, and `codegen_cranelift` to consume the new cast instead of the intrinsic.

Includes `CastTransmute` for custom MIR building, to be able to test the extra UB.
This commit is contained in:
Scott McMurray 2023-02-24 18:32:52 -08:00
parent a266f11990
commit 64cce5fc7d
55 changed files with 955 additions and 190 deletions

View file

@ -176,6 +176,9 @@ fn check_rvalue<'tcx>(
// FIXME(dyn-star)
unimplemented!()
},
Rvalue::Cast(CastKind::Transmute, _, _) => {
Err((span, "transmute can attempt to turn pointers into integers, so is unstable in const fn".into()))
},
// binops are fine on integers
Rvalue::BinaryOp(_, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(_, box (lhs, rhs)) => {
check_operand(tcx, lhs, span, body)?;

View file

@ -7,6 +7,6 @@ struct Human;
fn main() {
let _x: ! = unsafe {
std::mem::transmute::<Human, !>(Human) //~ ERROR: transmuting to uninhabited
std::mem::transmute::<Human, !>(Human) //~ ERROR: entering unreachable code
};
}

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: transmuting to uninhabited type
error: Undefined Behavior: entering unreachable code
--> $DIR/never_transmute_humans.rs:LL:CC
|
LL | std::mem::transmute::<Human, !>(Human)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ transmuting to uninhabited type
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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

View file

@ -10,11 +10,13 @@ mod m {
pub struct Void(VoidI);
pub fn f(v: Void) -> ! {
match v.0 {} //~ ERROR: entering unreachable code
match v.0 {}
//~^ ERROR: entering unreachable code
}
}
fn main() {
let v = unsafe { std::mem::transmute::<(), m::Void>(()) };
m::f(v); //~ NOTE: inside `main`
m::f(v);
//~^ NOTE: inside `main`
}