about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-07-23 15:59:30 +0200
committerGitHub <noreply@github.com>2025-07-23 15:59:30 +0200
commit16c10c9145b606ea323b1faa0aa935f0d19ea53b (patch)
tree5e389d423afa84b6da7ead3ee6412d72edfc9932
parent80342618e1f7dfb32b5d35d6fcb204b302e70cdf (diff)
parentb2f8b406335310f885a0d2d21ff8472b6a5f9ce5 (diff)
downloadrust-16c10c9145b606ea323b1faa0aa935f0d19ea53b.tar.gz
rust-16c10c9145b606ea323b1faa0aa935f0d19ea53b.zip
Rollup merge of #144256 - oli-obk:type-id-ice, r=RalfJung
Don't ICE on non-TypeId metadata within TypeId

fixes rust-lang/rust#144253

r? ``````````@RalfJung``````````
-rw-r--r--compiler/rustc_const_eval/src/interpret/memory.rs2
-rw-r--r--tests/ui/consts/const_transmute_type_id6.rs16
-rw-r--r--tests/ui/consts/const_transmute_type_id6.stderr15
3 files changed, 32 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs
index 34297a61648..47bebf5371a 100644
--- a/compiler/rustc_const_eval/src/interpret/memory.rs
+++ b/compiler/rustc_const_eval/src/interpret/memory.rs
@@ -1000,7 +1000,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
         ptr: Pointer<Option<M::Provenance>>,
     ) -> InterpResult<'tcx, (Ty<'tcx>, u64)> {
         let (alloc_id, offset, _meta) = self.ptr_get_alloc_id(ptr, 0)?;
-        let GlobalAlloc::TypeId { ty } = self.tcx.global_alloc(alloc_id) else {
+        let Some(GlobalAlloc::TypeId { ty }) = self.tcx.try_get_global_alloc(alloc_id) else {
             throw_ub_format!("invalid `TypeId` value: not all bytes carry type id metadata")
         };
         interp_ok((ty, offset.bytes()))
diff --git a/tests/ui/consts/const_transmute_type_id6.rs b/tests/ui/consts/const_transmute_type_id6.rs
new file mode 100644
index 00000000000..668eb0bb2b0
--- /dev/null
+++ b/tests/ui/consts/const_transmute_type_id6.rs
@@ -0,0 +1,16 @@
+//! Test that we do not ICE and that we do report an error
+//! when placing non-TypeId provenance into a TypeId.
+
+#![feature(const_trait_impl, const_cmp)]
+
+use std::any::TypeId;
+use std::mem::transmute;
+
+const X: bool = {
+    let a = ();
+    let id: TypeId = unsafe { transmute([&raw const a; 16 / size_of::<*const ()>()]) };
+    id == id
+    //~^ ERROR: invalid `TypeId` value: not all bytes carry type id metadata
+};
+
+fn main() {}
diff --git a/tests/ui/consts/const_transmute_type_id6.stderr b/tests/ui/consts/const_transmute_type_id6.stderr
new file mode 100644
index 00000000000..f5d90256e7c
--- /dev/null
+++ b/tests/ui/consts/const_transmute_type_id6.stderr
@@ -0,0 +1,15 @@
+error[E0080]: invalid `TypeId` value: not all bytes carry type id metadata
+  --> $DIR/const_transmute_type_id6.rs:12:5
+   |
+LL |     id == id
+   |     ^^^^^^^^ evaluation of `X` failed inside this call
+   |
+note: inside `<TypeId as PartialEq>::eq`
+  --> $SRC_DIR/core/src/any.rs:LL:COL
+note: inside `<TypeId as PartialEq>::eq::compiletime`
+  --> $SRC_DIR/core/src/any.rs:LL:COL
+   = note: this error originates in the macro `$crate::intrinsics::const_eval_select` which comes from the expansion of the macro `crate::intrinsics::const_eval_select` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0080`.