Skip to content

Don't ICE on non-TypeId metadata within TypeId #144256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,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()))
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/consts/const_transmute_type_id6.rs
Original file line number Diff line number Diff line change
@@ -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() {}
15 changes: 15 additions & 0 deletions tests/ui/consts/const_transmute_type_id6.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading