Skip to content

Commit

Permalink
Merge #343
Browse files Browse the repository at this point in the history
343: Add test for issue 342 r=taiki-e a=taiki-e

Add test for #342

Co-authored-by: Taiki Endo <[email protected]>
  • Loading branch information
bors[bot] and taiki-e authored Mar 12, 2022
2 parents 3be2e0d + 189fc3c commit ae833a7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
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)

0 comments on commit ae833a7

Please sign in to comment.