Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repr_transparent_external_private_fields lint fires on ManuallyDrop containing a ZST #129470

Closed
davidhewitt opened this issue Aug 23, 2024 · 1 comment · Fixed by #129487
Closed
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. L-repr_transparent_external_private_fields Lint: repr_transparent_external_private_fields T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@davidhewitt
Copy link
Contributor

Consider the following code:

struct ZST;

#[repr(transparent)]
struct TransparentWithManuallyDropZST {
    value: i32,
    md: std::mem::ManuallyDrop<ZST>,  // error: repr_transparent_external_private_fields
    zst: ZST,                         // no error
}

the field md fires a lint warning:

warning: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
 --> src/lib.rs:7:5
  |
7 |     md: std::mem::ManuallyDrop<ZST>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
  = note: this struct contains `ManuallyDrop<ZST>`, which contains private fields, and makes it not a breaking change to become non-zero-sized in the future.
  = note: `#[warn(repr_transparent_external_private_fields)]` on by default

While the spirit of the lint seems reasonable, the docs state:

ManuallyDrop<T> is guaranteed to have the same layout and bit validity as T

So in this case, it seems like ManuallyDrop changing size would be a breaking change, and the lint at the very least misleading here. Perhaps an exclusion is needed on this lint for ManuallyDrop?


More generally, quoting the nomicon:

This repr is only considered part of the public ABI of a type if either the single field is pub, or if its layout is documented in prose. Otherwise, the layout should not be relied upon by other crates.

So, in ManuallyDrop's case, the single field is not pub, but the layout is documented in prose and so should be considered part of the ABI. Is it possible there are other types out there with such a layout guarantee in prose, which might also be incorrectly flagged by this lint?

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 23, 2024
@jieyouxu jieyouxu added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. L-repr_transparent_external_private_fields Lint: repr_transparent_external_private_fields T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 25, 2024
…rnal_private_fields, r=compiler-errors

repr_transparent_external_private_fields: special-case some std types

Fixes rust-lang#129470

`@rustbot` label +A-lint +L-repr_transparent_external_private_fields
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 25, 2024
…rnal_private_fields, r=compiler-errors

repr_transparent_external_private_fields: special-case some std types

Fixes rust-lang#129470

``@rustbot`` label +A-lint +L-repr_transparent_external_private_fields
@bors bors closed this as completed in 9c59e97 Aug 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 25, 2024
Rollup merge of rust-lang#129487 - GrigorenkoPV:repr_transparent_external_private_fields, r=compiler-errors

repr_transparent_external_private_fields: special-case some std types

Fixes rust-lang#129470

```@rustbot``` label +A-lint +L-repr_transparent_external_private_fields
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 26, 2024
@davidhewitt
Copy link
Contributor Author

Thanks @GrigorenkoPV @compiler-errors !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. L-repr_transparent_external_private_fields Lint: repr_transparent_external_private_fields T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants