Skip to content

Conversation

@dakr
Copy link

@dakr dakr commented Oct 29, 2025

Document the layout guarantee of TypeId not to exceed 16 bytes.

This is useful for FFI use-cases where a TypeId may be stored within a foreign data structure or buffer.

An example for such a use-case can be found in [1].

Link: https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?id=6f61a2637abe4f89877da3280775565baedb60e0 [1]
Suggested-by: Alice Ryhl aliceryhl@google.com

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 29, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@dakr
Copy link
Author

dakr commented Oct 29, 2025

Cc: @Darksonn

@scottmcm
Copy link
Member

Do you have a reference to a place where there was an agreement to commit to this? Or is this a new libs-api/libs/compiler request?

@scottmcm scottmcm added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Oct 29, 2025
@Darksonn
Copy link
Contributor

Do you have a reference to a place where there was an agreement to commit to this? Or is this a new libs-api/libs/compiler request?

This PR is a request for the stdlib to make this commitment. There is some prior discussion at #rust-for-linux > TypeId layout.

@scottmcm scottmcm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-libs-api-nominated Nominated for discussion during a libs-api team meeting. I-compiler-nominated Nominated for discussion during a compiler team meeting. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 30, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Nov 2, 2025

This was discussed during this Thursday's compiler triage meeting #t-compiler/meetings > [weekly] 2025-10-30 @ 💬. The consensus of those present at the meeting was:

  • We were not ready to commit to any stability guarantees surrounding TypeId's size or layout before the collision-resistance concerns (Collisions in type_id #10389, type_id is not sufficiently collision-resistant #129014) were resolved.
  • Committing to a size upper bound now would close the door for making TypeId larger in the future.
    • Even if we are

    at least soft-bound to not go above 16bytes just from the fact that making the type too large would be bad for perf for people who rely heavily on TypeId

There was also an alternative approach for this use case that was discussed during the meeting. For FFI purposes, can TypeId be boxed and treated as an opaque pointer instead? This seems like something that RfL can wrap and make additional size guarantees on top of.

The TL;DR is that we'd prefer not to guarantee this until we decide on what to do about typeid collisions.

@rustbot label: -I-compiler-nominated

@rustbot rustbot removed the I-compiler-nominated Nominated for discussion during a compiler team meeting. label Nov 2, 2025
@Noratrieb
Copy link
Member

I disagree with that, and I have opened a Zulip thread to further discuss it, so don't take the above message as a final compiler decision yet, it may or may not change:
https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/TypeId.20minimum.20size.20guarantees/with/553234173

@ChrisDenton
Copy link
Member

I would emphasize that restricting TypeId to 16 bytes only restricts the inline size, not the total size in the binary. I don't think we'd be closing the door on 256, 512 or 1024 bit hashes or even full v0 name mangling if we wanted to go down that route.

@programmerjake
Copy link
Member

programmerjake commented Nov 2, 2025

as mentioned on Zulip, I think we can guarantee TypeId's size is no bigger than:

#[repr(C)]
union TypeIdBytes {
    a: u128,
    b: [*const (); 2], // not usize since that may not match pointer size on CHERI
}

To be clear, I'm not proposing that TypeId becomes that union, just that it fits in that union when written with ptr::write_unaligned.

That is 16 bytes except for platforms where pointers are >8 bytes (e.g. CHERI or rv128i).

This properly handles our current implementation of having just a 128-bit hash, as well as larger values by letting us have:

#[repr(C)]
pub struct TypeId {
    small_hash: u64,
    full_data: &'static FullData, // e.g. a ThinCStr or a >=256-bit hash
}

impl PartialEq for TypeId {
    fn eq(&self, other: &Self) -> bool {
        if self.small_hash != other.small_hash {
            false // fast not-equal
        } else if ptr::eq(self.full_data, other.full_data) {
            true // fast equal, can be made the usual case by e.g. linker deduplication
        } else {
            self.full_data == other.full_data // rare slow path
        }
    }
}

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 3, 2025
…uarantees, 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)
rust-timer added a commit that referenced this pull request Nov 3, 2025
Rollup merge of #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 > [weekly] 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 #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 #10389 and #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)
@bors
Copy link
Collaborator

bors commented Nov 3, 2025

☔ The latest upstream changes (presumably #148425) made this pull request unmergeable. Please resolve the merge conflicts.

@dakr
Copy link
Author

dakr commented Nov 3, 2025

Given that #148394 has just been sent and merged, are we further discussing this?

@ChrisDenton
Copy link
Member

#148394 was explicitly not a decision either way. It just documents the status quo.

Document the layout guarantee of `TypeId` not to exceed 16 bytes.
Additionally, use `repr(C)` to avoid additional padding if layout
randomization improved.

This is useful for FFI use-cases where a `TypeId` may be stored within a
foreign data structure or buffer.

An example for such a use-case can be found in [1].

Link: https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?id=6f61a2637abe4f89877da3280775565baedb60e0 [1]
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
@rustbot
Copy link
Collaborator

rustbot commented Nov 3, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@scottmcm
Copy link
Member

scottmcm commented Nov 4, 2025

Given that #148394 has just been sent and merged

For clarity: I merged that as "currently factual", but explicitly expecting that it might change by this PR (or others).

If we end up making a confident decision quickly, great. If it ends up taking while, while at least we have the explicit note in the mean time :)

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 4, 2025
…, 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 > &#91;weekly&#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/rust#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/rust#10389 and rust-lang/rust#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)
@the8472
Copy link
Member

the8472 commented Nov 4, 2025

We discussed this during today's libs-api meeting. Since TypeId is not really implemented by the library itself and its internals are driven by compiler concerns we're largely ok with deferring to T-compiler on which guarantees they're willing to give / how much they want to constrain their future implementations.

One concern we have is the binary size bloat that some of the possible future implementations would cause. Putting the typename behind the pointer could lead to significant binary size increase for crates that make heavy use of type IDs and has long type names. We want TypeId to remain a cheap type so that crates that use it a lot or embedded systems don't run into scaling issues.

@the8472 the8472 removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Nov 4, 2025
@programmerjake
Copy link
Member

should this be nominated again for t-compiler to decide if they want the solution I proposed?

@Darksonn Darksonn added the I-compiler-nominated Nominated for discussion during a compiler team meeting. label Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-compiler-nominated Nominated for discussion during a compiler team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.