Skip to content

Commit 4104f80

Browse files
authored
Rollup merge of rust-lang#148394 - jieyouxu:remark-typeid-no-layout-guarantees, r=scottmcm
Make explicit that `TypeId`'s layout and size are unstable Or worded differently, explicitly remark non-stable-guarantee of `TypeId` layout and size. This PR makes no *additional* guarantees or non-guarantees, it only emphasizes that `TypeId`'s size and layout are unstable like any other `#[repr(Rust)]` types. This was discussed during [#t-compiler/meetings > &rust-lang#91;weekly&rust-lang#93; 2025-10-30 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/238009-t-compiler.2Fmeetings/topic/.5Bweekly.5D.202025-10-30/near/547949347), where the compiler team discussed a request rust-lang#148265 to have the standard library (and language) commit to `TypeId` guaranteeing a size upper bound of 16 bytes. In the meeting, the consensus was: - We were sympathetic to the use case discussed in the request PR, however we feel like this stability guarantee is premature, given that there are unresolved questions surrounding the intended purpose of `TypeId`, and concerns surrounding its collision-resistance properties rust-lang#10389 and rust-lang#129014. We would prefer not making any of such guarantee until the collision-resistance concerns are resolved. - Committing to a stability guarantee on the size upper bound now would close the door to making `TypeId` larger (even if unlikely for perf reasons). Given that we have previously broken people who asserted the size of `TypeId` is 8 bytes, it was also discussed in the meeting that we should *explicitly* note that the size and layout of `TypeId` is not a stable guarantee, and is subject to changes between Rust releases, and thus cannot be relied upon -- if breakage in people's code is due to that assumption, it will be considered a won't-fix. - So even if `#[repr(Rust)]` types have unstable size and layout, this PR makes it explicit for `TypeId` since this type can feel "special" and users can be lead into thinking its size and layout is something they can rely upon. r? `@scottmcm` (or libs/libs-api/lang)
2 parents 01ab3e3 + 625b6f5 commit 4104f80

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/core/src/any.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,15 @@ impl dyn Any + Send + Sync {
611611
/// noting that the hashes and ordering will vary between Rust releases. Beware
612612
/// of relying on them inside of your code!
613613
///
614+
/// # Layout
615+
///
616+
/// Like other [`Rust`-representation][repr-rust] types, `TypeId`'s size and layout are unstable.
617+
/// In particular, this means that you cannot rely on the size and layout of `TypeId` remaining the
618+
/// same between Rust releases; they are subject to change without prior notice between Rust
619+
/// releases.
620+
///
621+
/// [repr-rust]: https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.rust.unspecified
622+
///
614623
/// # Danger of Improper Variance
615624
///
616625
/// You might think that subtyping is impossible between two static types,

0 commit comments

Comments
 (0)