Rollup merge of #112520 - chenyukang:yukang-fix-112505, r=fee1-dead

Fix the overflow issue for transmute_generic_consts

Fixes #112505
This commit is contained in:
Matthias Krüger 2023-06-14 06:25:49 +02:00 committed by GitHub
commit 269ea4bd6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -82,8 +82,17 @@ pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
// Try to display a sensible error with as much information as possible.
let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
Ok(SizeSkeleton::Known(size)) => {
if let Some(v) = u128::from(size.bytes()).checked_mul(8) {
format!("{} bits", v)
} else {
// `u128` should definitely be able to hold the size of different architectures
// larger sizes should be reported as error `are too big for the current architecture`
// otherwise we have a bug somewhere
bug!("{:?} overflow for u128", size)
}
}
Ok(SizeSkeleton::Generic(size)) => {
if let Some(size) = size.try_eval_target_usize(tcx, self.param_env) {
format!("{size} bytes")

View File

@ -0,0 +1,7 @@
#![feature(transmute_generic_consts)]
fn overflow(v: [[[u32; 8888888]; 9999999]; 777777777]) -> [[[u32; 9999999]; 777777777]; 239] {
unsafe { std::mem::transmute(v) } //~ ERROR cannot transmute between types of different sizes
}
fn main() { }

View File

@ -0,0 +1,12 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> $DIR/issue-112505-overflow.rs:4:14
|
LL | unsafe { std::mem::transmute(v) }
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `[[[u32; 8888888]; 9999999]; 777777777]` (values of the type `[[[u32; 8888888]; 9999999]; 777777777]` are too big for the current architecture)
= note: target type: `[[[u32; 9999999]; 777777777]; 239]` (59484438436515561504 bits)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0512`.