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

Add test for issue 342 #343

Merged
merged 1 commit into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,6 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: Option<&Fields>) -> Result
// See also https://github.com/taiki-e/pin-project/pull/34.
//
// Note:
// - pin-project v0.4.3 or later (#135, v0.4.0-v0.4.2 are already yanked for
// another reason) is internally proc-macro-derive, so they are not
// affected by the problem that the struct definition is rewritten by
// another macro after the #[pin_project] is expanded.
// So this is probably no longer necessary, but it keeps it for now.
//
// - Lint-based tricks aren't perfect, but they're much better than nothing:
// https://github.com/taiki-e/pin-project-lite/issues/26
//
Expand Down
13 changes: 11 additions & 2 deletions tests/auxiliary/macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ fn tokens2(tokens: TokenStream) -> proc_macro2::TokenStream {

#[proc_macro_attribute]
pub fn hidden_repr(args: TokenStream, input: TokenStream) -> TokenStream {
let (args, input) = (tokens2(args), tokens2(input));
quote!(#[repr(#args)] #input).into()
let args = tokens2(args);
let mut item = syn::parse_macro_input!(input as ItemStruct);
item.attrs.push(parse_quote!(#[repr(#args)]));
quote!(#item).into()
}

#[proc_macro_attribute]
pub fn hidden_repr2(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut item = syn::parse_macro_input!(input as ItemStruct);
item.attrs.push(parse_quote!(#[auxiliary_macro::hidden_repr(packed)] ));
quote!(#item).into()
}

#[proc_macro]
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/pin_project/packed_sneaky-1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::pin::Pin;

use auxiliary_macro::hidden_repr;
use auxiliary_macro::{hidden_repr, hidden_repr2};
use pin_project::{pin_project, pinned_drop, UnsafeUnpin};

#[pin_project] //~ ERROR may not be used on #[repr(packed)] types
Expand All @@ -10,6 +10,13 @@ struct A {
f: u32,
}

#[hidden_repr2]
#[pin_project] //~ ERROR may not be used on #[repr(packed)] types
struct B {
#[pin]
f: u32,
}

#[pin_project(UnsafeUnpin)] //~ ERROR may not be used on #[repr(packed)] types
#[hidden_repr(packed)]
struct C {
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/pin_project/packed_sneaky-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ error: #[pin_project] attribute may not be used on #[repr(packed)] types
| ^^^^^^

error: #[pin_project] attribute may not be used on #[repr(packed)] types
--> tests/ui/pin_project/packed_sneaky-1.rs:14:15
--> tests/ui/pin_project/packed_sneaky-1.rs:13:1
|
14 | #[hidden_repr(packed)]
13 | #[hidden_repr2]
| ^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `hidden_repr2` (in Nightly builds, run with -Z macro-backtrace for more info)

error: #[pin_project] attribute may not be used on #[repr(packed)] types
--> tests/ui/pin_project/packed_sneaky-1.rs:21:15
|
21 | #[hidden_repr(packed)]
| ^^^^^^

error: #[pin_project] attribute may not be used on #[repr(packed)] types
--> tests/ui/pin_project/packed_sneaky-1.rs:23:15
--> tests/ui/pin_project/packed_sneaky-1.rs:30:15
|
23 | #[hidden_repr(packed)]
30 | #[hidden_repr(packed)]
| ^^^^^^
15 changes: 15 additions & 0 deletions tests/ui/pin_project/packed_sneaky-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://github.com/taiki-e/pin-project/issues/342

#![allow(unaligned_references)]

use auxiliary_macro::hidden_repr2;
use pin_project::pin_project;

#[pin_project] //~ ERROR reference to packed field is unaligned
#[hidden_repr2]
struct A {
#[pin]
f: u32,
}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/pin_project/packed_sneaky-4.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: reference to packed field is unaligned
--> tests/ui/pin_project/packed_sneaky-4.rs:8:1
|
8 | #[pin_project] //~ ERROR reference to packed field is unaligned
| ^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> tests/ui/pin_project/packed_sneaky-4.rs:8:1
|
8 | #[pin_project] //~ ERROR reference to packed field is unaligned
| ^^^^^^^^^^^^^^
= 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 #82523 <https://github.com/rust-lang/rust/issues/82523>
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
= note: this error originates in the derive macro `::pin_project::__private::__PinProjectInternalDerive` (in Nightly builds, run with -Z macro-backtrace for more info)