-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Reject #[repr(packed)] on #[pin_v2] types
#157542
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! `#[pin_v2]` is not allowed on `#[repr(packed)]` types. | ||
| //! | ||
| //! Drop glue for a packed type moves an over-aligned field to an aligned location before running | ||
| //! its destructor. That move carries along any structurally pinned leaf, so a value handed out as | ||
| //! `Pin<&mut _>` would be moved before it is dropped, violating `Pin`'s invariant. | ||
| //! | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/157011>. | ||
|
|
||
| #![feature(pin_ergonomics)] | ||
| #![allow(incomplete_features)] | ||
|
|
||
| use std::marker::PhantomPinned; | ||
|
|
||
| #[pin_v2] | ||
| #[repr(packed)] | ||
| struct Packed { //~ ERROR `#[pin_v2]` types may not have `#[repr(packed)]` | ||
| field: PhantomPinned, | ||
| } | ||
|
|
||
| // The generic case from the issue: alignment of `T` is unknown at definition time, so this is | ||
| // rejected regardless of how it is later monomorphized. | ||
| #[pin_v2] | ||
| #[repr(C, packed(4))] | ||
| struct PackedN<T>(T); | ||
| //~^ ERROR `#[pin_v2]` types may not have `#[repr(packed)]` | ||
|
|
||
| #[pin_v2] | ||
| #[repr(packed)] | ||
| union PackedUnion { //~ ERROR `#[pin_v2]` types may not have `#[repr(packed)]` | ||
| field: (), | ||
| } | ||
|
|
||
| // Allowed: `#[pin_v2]` without `#[repr(packed)]` still compiles. | ||
| #[pin_v2] | ||
| #[repr(C)] | ||
| struct Unpacked<T>(T); | ||
|
|
||
| // Allowed: `#[repr(packed)]` without `#[pin_v2]` is unaffected by the ban. | ||
| #[repr(packed)] | ||
| struct PackedNoPin(u8); | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| error: `#[pin_v2]` types may not have `#[repr(packed)]` | ||
| --> $DIR/pin_v2-packed.rs:16:1 | ||
| | | ||
| LL | struct Packed { | ||
| | ^^^^^^^^^^^^^ | ||
| | | ||
| = note: fields of a `#[repr(packed)]` type can be under-aligned, so a structurally pinned field may be moved to a properly aligned location, which `Pin` does not allow | ||
| note: `Packed` is marked `#[pin_v2]` here | ||
| --> $DIR/pin_v2-packed.rs:14:1 | ||
| | | ||
| LL | #[pin_v2] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: `#[pin_v2]` types may not have `#[repr(packed)]` | ||
| --> $DIR/pin_v2-packed.rs:24:1 | ||
| | | ||
| LL | struct PackedN<T>(T); | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: fields of a `#[repr(packed)]` type can be under-aligned, so a structurally pinned field may be moved to a properly aligned location, which `Pin` does not allow | ||
| note: `PackedN` is marked `#[pin_v2]` here | ||
| --> $DIR/pin_v2-packed.rs:22:1 | ||
| | | ||
| LL | #[pin_v2] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: `#[pin_v2]` types may not have `#[repr(packed)]` | ||
| --> $DIR/pin_v2-packed.rs:29:1 | ||
| | | ||
| LL | union PackedUnion { | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: fields of a `#[repr(packed)]` type can be under-aligned, so a structurally pinned field may be moved to a properly aligned location, which `Pin` does not allow | ||
| note: `PackedUnion` is marked `#[pin_v2]` here | ||
| --> $DIR/pin_v2-packed.rs:27:1 | ||
| | | ||
| LL | #[pin_v2] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error should be emitted whether or not the attribute can be found. Your best option is to use
Option<Span>instead. Take for examplerust/compiler/rustc_hir_analysis/src/check/always_applicable.rs
Lines 408 to 415 in 61d7280