Skip to content

Commit 192c426

Browse files
committed
Fix dataflow_const_prop behavior for malsized transmute
1 parent 461e973 commit 192c426

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,10 @@ fn try_write_constant<'tcx>(
861861
if layout.backend_repr.is_scalar()
862862
&& let Some(value) = propagatable_scalar(place, state, map)
863863
{
864+
if value.size() != layout.backend_repr.scalar_size(ecx).unwrap() {
865+
throw_machine_stop_str!("mismatched sizes");
866+
}
867+
864868
return ecx.write_immediate(Immediate::Scalar(value), dest);
865869
}
866870

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@compile-flags: -Zmir-enable-passes=+DataflowConstProp --crate-type lib
2+
//@ edition:2021
3+
pub async fn a() -> u32 {
4+
unsafe { std::mem::transmute(1usize) }
5+
//~^error: cannot transmute between types of different sizes, or dependently-sized types
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2+
--> $DIR/transmute-in-async-fn.rs:4:14
3+
|
4+
LL | unsafe { std::mem::transmute(1usize) }
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: source type: `usize` (64 bits)
8+
= note: target type: `u32` (32 bits)
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0512`.

0 commit comments

Comments
 (0)