Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
d489638 to
94d647d
Compare
|
A few notes:
|
This is a breaking change so it'll have to be just a warning for now. |
|
This seems to do at least two things that are entirely unrelated:
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. |
24018e9 to
eae5919
Compare
#[repr(Rust)] ADTs#[repr(transparent)] trivial field types match the types with trivial ABI
eae5919 to
4242175
Compare
|
I hadn't realised that the update for the |
|
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. |
|
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. |
I think that issue is fine, though we may want to extend the description. |
| // 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() { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
| /// 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. |
There was a problem hiding this comment.
Actually that's wrong, for arrays we might encounter non-1-ZST here. What is the function even checking in that case?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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,)); |
There was a problem hiding this comment.
Do we have a test ensuring that we do recurse into ReprC1Zst for the semver check?
There was a problem hiding this comment.
I've added a test in repr-transparent-non-exhaustive.rs.
|
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 |
| }, | ||
| ) | ||
| } | ||
| _ => ControlFlow::Continue(()), |
There was a problem hiding this comment.
| _ => 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.
There was a problem hiding this comment.
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_abiin const eval handles them. - Unstable view types are
unimplemented!()as that what almost all other match arms in the compiler do for those types.
I tested it, it does not. |
4242175 to
98cecb1
Compare
Correct - specifically just the check that the element type was a non-ZST was missing. Types like
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 |
The trade-off with this is that with a looser check, you can have types that are accepted in |
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.#[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.#[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