Skip to content

Make #[repr(transparent)] trivial field types match the types with trivial ABI - #166170

Open
beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:fix-trivial-abi
Open

beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:fix-trivial-abi

Conversation

@beetrees

@beetrees beetrees commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

#157973 changed the rules for what is considered "trivial ABI", but repr(transparent) checking has not been updated.
When checking that a #[repr(transparent)] type has at most 1 non-trivial field, there are currently two differences between what the compiler currently does and what types are defined as having a trivial ABI.

  • The compiler currently accepts 0-length arrays with non-ZST element types that are not #[repr(C)] (e.g. [u8; 0]), but the trivial ABI documentation states that an array has a trivial ABI only if its element type does. This means that #[repr(transparent)] struct Foo(u64, [u8; 0]); currently compiles, even though it shouldn't. This PR adds a FCW lint for this case.
  • The compiler currently requires all fields of #[repr(Rust)] ADTs and tuples to have a trivial ABI, even though that is not required (checking the fields is only required in the case of #[repr(transparent)]). This means that #[repr(transparent)] struct Foo(u64, (ReprC1Zst,)); does not currently compile, even though it should. This PR makes those cases compile, while continuing to error if types contain #[non_exhaustive] or private fields from another crate.

As this PR adds a FCW and allows code to compile that currently doesn't, this PR requires a FCP.

r? @bjorn3

Tracking issue: #166183

@rustbot rustbot added 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. labels Nov 19, 2026
@rust-log-analyzer

This comment has been minimized.

@Jules-Bertholet

Jules-Bertholet commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

A few notes:

  • What rustc_abi says has trivial ABI can be a superset of what our docs say has trivial ABI. For types with unspecified ABI, we can change our minds at any time about what the ABI implementation is. (As long as Miri continues to correctly reject anything not blessed by the docs)
  • The repr(transparent) checks were simply never updated following the introduction of the "trivial ABI" concept. I had a PR for this, but never got around to updating it: repr(transparent): don't consider most length-0 arrays trivial #155984. (Thanks for picking this up!) Note that the question of how strict we want this check to be (allow all trivial ABI types, or rule out some where a library might not have intended to make a promise about a type's ABI) is still open and needs a lang team decision.

@RalfJung

Copy link
Copy Markdown
Member

This means that #[repr(transparent)] struct Foo(u64, [u8; 0]); currently compiles, even though it shouldn't.

This is a breaking change so it'll have to be just a warning for now.

Comment thread compiler/rustc_abi/src/lib.rs Resolved
@RalfJung

Copy link
Copy Markdown
Member

This seems to do at least two things that are entirely unrelated:

  • adjust the logic for repr(transparent)
  • adjust our ABI computation code

Please split that into two separate PRs. These are both non-trivial parts of the compiler, let's not make review even harder to having to do it all in one PR.

@beetrees
beetrees marked this pull request as draft November 19, 2026 15:05
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 19, 2026
@beetrees
beetrees force-pushed the fix-trivial-abi branch 2 times, most recently from 24018e9 to eae5919 Compare November 20, 2026 02:57
@beetrees
beetrees marked this pull request as ready for review November 20, 2026 02:58
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 20, 2026
@beetrees beetrees changed the title Fix trivial ABI computation for arrays, tuples and #[repr(Rust)] ADTs Make #[repr(transparent)] trivial field types match the types with trivial ABI Nov 20, 2026
@beetrees

beetrees commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

I hadn't realised that the update for the #[repr(transparent)] trivial field types in #155984 hadn't merged - I've re-scoped this PR to be just those changes (and updated the PR description to match). I've split the fix for the ABI trivial ABI check out into #166232. I've set the FCW issue number to #166183 for now, but I imagine we'll want to change it to a separate dedicated issue before this merges.

@beetrees beetrees added T-lang Relevant to the language team needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-lang-nominated Nominated for discussion during a lang team meeting. labels Nov 20, 2026
@beetrees

Copy link
Copy Markdown
Contributor Author

As this requires a lang team decision/FCP, I've nominated it. The PR description provides a list of the changes from the status quo.

@Jules-Bertholet

Jules-Bertholet commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The relevant section of the Reference will also need an update. As part of that, we may want to uplift part or all of the ABI compatibility docs into the Reference.

@RalfJung

Copy link
Copy Markdown
Member

I've set the FCW issue number to #166183 for now, but I imagine we'll want to change it to a separate dedicated issue before this merges.

I think that issue is fine, though we may want to extend the description.

@RalfJung RalfJung left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation mostly LGTM, thanks. :)

View changes since this review

Comment on lines +1854 to +1856
// An array of length 0 can have a non-ZST element type and still be a ZST itself.
// The alignment of the element must be 1 or the array wouldn't be a 1-ZST.
if !layout.is_1zst() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. I can't relate this comment to what the code does.

/// Check if this type is "trivial" for `repr(transparent)`. If not, return the reason why
/// and the problematic type.
/// and the problematic type. If `only_check_privacy` is set, only checks for things that could
/// become non-1-ZST in the future will happen. This does not check that `ty` itself is a 1-ZST.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// become non-1-ZST in the future will happen. This does not check that `ty` itself is a 1-ZST.
/// become non-1-ZST in the future will happen. This assumes that `ty` has already
/// confirmed to be a 1-ZST.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually that's wrong, for arrays we might encounter non-1-ZST here. What is the function even checking in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encountering 1-ZSTs here is a bug that the FCW is aiming to fix. In the case of non-1-ZSTs we check everything except that the type is a 1-ZST (i.e. struct Foo(u8, [ReprC; 0]) currently emits a hard error and we don't want to regress that).

@RalfJung RalfJung Nov 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the FCW becomes a hard error, can't we encounter non-1-ZST here in "only semver check" mode?

Below arrays, we might end up with non-zero-sized types here.
And below repr(packed) structs, we might end up with non-1-aligned types.

#[repr(transparent)]
pub struct T8(Sized, ReprRust<ReprC1Zst>);
#[repr(transparent)]
pub struct T9(Sized, (ReprC1Zst,));

@RalfJung RalfJung Nov 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test ensuring that we do recurse into ReprC1Zst for the semver check?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test in repr-transparent-non-exhaustive.rs.

@RalfJung

RalfJung commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

So arrays with non-trivial-ABI elements are the only new case we have to worry about here?

The logic here is different from the one in #155984. Could you spell out how they compare? We should describe both options to the lang team so they can say which one they prefer. In particular, I wonder if this proposal will break https://github.com/dtolnay/ghost, which uses a somewhat cursed construction that probably we should add to the test suite:

enum Void {}

#[repr(Rust, packed)]
struct PackedZst<T: ?Sized>([*const T; 0]);

struct PhantomVoid<T: ?Sized> {
    phantom: PackedZst<T>,
    void: Void,
}

enum Phantom<T: ?Sized> {
    __Phantom(PhantomVoid<T>),
    Phantom,
}

#[repr(transparent)]
struct PhantomIsTransparent {
    data: i32,
    ignore: Phantom<[i32]>,
}

It seems to me that this should still work since we only check for semver once we are inside PackedZst. So I think I prefer this PR since the logic is simpler. But it does mean that the same type may be accepted by repr(transparent) on some target and rejected on another.

},
)
}
_ => ControlFlow::Continue(()),

@RalfJung RalfJung Nov 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => ControlFlow::Continue(()),
// Everything else always has trivial ABI when it is a 1-ZST.
_ => ControlFlow::Continue(()),

This fallback feels a bit dangerous though, maybe it'd be better if this was more directly mirroring the docs?

In particular, this accepts 1-ZST closure types are trivial. That does not seem compatible with the intentions of the semver check, so we should probably exclude them? Currently it should not be possible to even hit that case because such types cannot be named, but better safe than sorry.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the match exhaustive. Apart from the bug! for the unreachable cases, the changes (all unreachable on stable) are:

  • Unnameable closure-like types are now always considered non-trivial for semver reasons.
  • Unstable pattern and unsafe binder types are now handled by recursing into them, the same way that has_trivial_abi in const eval handles them.
  • Unstable view types are unimplemented!() as that what almost all other match arms in the compiler do for those types.

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

I wonder if this proposal will break https://github.com/dtolnay/ghost

I tested it, it does not.

@beetrees

beetrees commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

So arrays with non-trivial-ABI elements are the only new case we have to worry about here?

Correct - specifically just the check that the element type was a non-ZST was missing. Types like [ReprC; 0] are already considered non-trivial before this PR, regardless of whether ReprC was a ZST or not.

The logic here is different from the one in #155984. Could you spell out how they compare?

I'll reference the 1st and 2nd bullet points of the PR description as (1) and (2) here.

The main difference is that #155984 only fixes (1) without also fixing (2). This means that the check on arrays is stricter, but the checks on tuples and repr(Rust) structs are still unnecessarily strict. This leads to #155984 needing to support an ad-hoc exemption from the stricter array check fields in repr(Rust, packed) types in order to support the ghost crate. In contrast, this PR implements both (1) and (2), which makes the #[repr(transparent)] field check match the trivial ABI documentation while also not breaking the ghost crate.

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

the checks on tuples and repr(Rust) structs are still unnecessarily strict.

The trade-off with this is that with a looser check, you can have types that are accepted in repr(transparent) only on certain targets, e.g. struct Foo([u16; 0]). I think that's fine (I changed my mind on this since I first wrote #155984), but it's worth noting at least.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang 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-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants