Skip to content

Commit 269ea4b

Browse files
authored
Rollup merge of #112520 - chenyukang:yukang-fix-112505, r=fee1-dead
Fix the overflow issue for transmute_generic_consts Fixes #112505
2 parents 98f6e96 + 3bbc598 commit 269ea4b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

compiler/rustc_hir_typeck/src/intrinsicck.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8282

8383
// Try to display a sensible error with as much information as possible.
8484
let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
85-
Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
8685
Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
86+
Ok(SizeSkeleton::Known(size)) => {
87+
if let Some(v) = u128::from(size.bytes()).checked_mul(8) {
88+
format!("{} bits", v)
89+
} else {
90+
// `u128` should definitely be able to hold the size of different architectures
91+
// larger sizes should be reported as error `are too big for the current architecture`
92+
// otherwise we have a bug somewhere
93+
bug!("{:?} overflow for u128", size)
94+
}
95+
}
8796
Ok(SizeSkeleton::Generic(size)) => {
8897
if let Some(size) = size.try_eval_target_usize(tcx, self.param_env) {
8998
format!("{size} bytes")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(transmute_generic_consts)]
2+
3+
fn overflow(v: [[[u32; 8888888]; 9999999]; 777777777]) -> [[[u32; 9999999]; 777777777]; 239] {
4+
unsafe { std::mem::transmute(v) } //~ ERROR cannot transmute between types of different sizes
5+
}
6+
7+
fn main() { }
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/issue-112505-overflow.rs:4:14
3+
|
4+
LL | unsafe { std::mem::transmute(v) }
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[[u32; 8888888]; 9999999]; 777777777]` are too big for the current architecture)
8+
= note: target type: `[[[u32; 9999999]; 777777777]; 239]` (59484438436515561504 bits)
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0512`.

0 commit comments

Comments
 (0)