From b133841bfca673f26e6f1b779b1c8cdd36600c84 Mon Sep 17 00:00:00 2001 From: yukang Date: Sun, 11 Jun 2023 16:12:59 +0800 Subject: [PATCH] Fix the overflow issue for transmute_generic_consts --- compiler/rustc_hir_typeck/src/intrinsicck.rs | 8 +++++++- tests/ui/const-generics/issue-112505-overflow.rs | 7 +++++++ tests/ui/const-generics/issue-112505-overflow.stderr | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/ui/const-generics/issue-112505-overflow.rs create mode 100644 tests/ui/const-generics/issue-112505-overflow.stderr diff --git a/compiler/rustc_hir_typeck/src/intrinsicck.rs b/compiler/rustc_hir_typeck/src/intrinsicck.rs index 3c5eafd9484..e1837eb5582 100644 --- a/compiler/rustc_hir_typeck/src/intrinsicck.rs +++ b/compiler/rustc_hir_typeck/src/intrinsicck.rs @@ -82,8 +82,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // 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 { + format!("{} bytes", size.bytes()) + } + } Ok(SizeSkeleton::Generic(size)) => { if let Some(size) = size.try_eval_target_usize(tcx, self.param_env) { format!("{size} bytes") diff --git a/tests/ui/const-generics/issue-112505-overflow.rs b/tests/ui/const-generics/issue-112505-overflow.rs new file mode 100644 index 00000000000..0dd7776d595 --- /dev/null +++ b/tests/ui/const-generics/issue-112505-overflow.rs @@ -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() { } diff --git a/tests/ui/const-generics/issue-112505-overflow.stderr b/tests/ui/const-generics/issue-112505-overflow.stderr new file mode 100644 index 00000000000..0432f2fa8be --- /dev/null +++ b/tests/ui/const-generics/issue-112505-overflow.stderr @@ -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`.