Skip to content

Commit cd23810

Browse files
committed
Document TypeId internals
1 parent 7655372 commit cd23810

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

library/core/src/any.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,11 @@ impl dyn Any + Send + Sync {
709709
#[stable(feature = "rust1", since = "1.0.0")]
710710
#[lang = "type_id"]
711711
pub struct TypeId {
712+
/// This needs to be an array of pointers, since there is provenance
713+
/// in the first array field. This provenance knows exactly which type
714+
/// the TypeId actually is, allowing CTFE and miri to operate based off it.
715+
/// At runtime all the pointers in the array contain bits of the hash, making
716+
/// the entire `TypeId` actually just be a `u128` hash of the type.
712717
pub(crate) data: [*const (); 16 / size_of::<usize>()],
713718
}
714719

tests/ui/consts/const_transmute_type_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::any::TypeId;
44

55
const _: () = {
66
let id = TypeId::of::<u8>();
7-
let id: u8 = unsafe { (&id as *const TypeId).cast::<u8>().read() };
7+
let id: u8 = unsafe { (&raw const id).cast::<u8>().read() };
88
//~^ ERROR: unable to turn pointer into integer
99
};
1010

tests/ui/consts/const_transmute_type_id.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: unable to turn pointer into integer
22
--> $DIR/const_transmute_type_id.rs:7:27
33
|
4-
LL | let id: u8 = unsafe { (&id as *const TypeId).cast::<u8>().read() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
4+
LL | let id: u8 = unsafe { (&raw const id).cast::<u8>().read() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
66
|
77
= help: this code performed an operation that depends on the underlying bytes representing a pointer
88
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported

0 commit comments

Comments
 (0)