From 88ff0cb3745c9ff75191bb850c93544c07dd5a81 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 28 Mar 2021 21:56:49 +0900 Subject: [PATCH 1/2] Fix unused_must_use warning on unused borrows This fixes `unused_must_use warning` on unused borrows, which will be added to rustc in the future. --- examples/not_unpin-expanded.rs | 6 +++--- examples/pinned_drop-expanded.rs | 6 +++--- examples/project_replace-expanded.rs | 6 +++--- examples/struct-default-expanded.rs | 6 +++--- examples/unsafe_unpin-expanded.rs | 6 +++--- pin-project-internal/src/pin_project/derive.rs | 8 ++++---- tests/expand/tests/expand/default-struct.expanded.rs | 6 +++--- .../tests/expand/default-tuple_struct.expanded.rs | 6 +++--- .../expand/tests/expand/multifields-struct.expanded.rs | 10 +++++----- .../tests/expand/multifields-tuple_struct.expanded.rs | 10 +++++----- tests/expand/tests/expand/naming-struct.expanded.rs | 6 +++--- .../tests/expand/naming-tuple_struct.expanded.rs | 6 +++--- tests/expand/tests/expand/not_unpin-struct.expanded.rs | 6 +++--- .../tests/expand/not_unpin-tuple_struct.expanded.rs | 6 +++--- .../expand/tests/expand/pinned_drop-struct.expanded.rs | 6 +++--- .../tests/expand/pinned_drop-tuple_struct.expanded.rs | 6 +++--- .../tests/expand/project_replace-struct.expanded.rs | 6 +++--- .../expand/project_replace-tuple_struct.expanded.rs | 6 +++--- tests/expand/tests/expand/pub-struct.expanded.rs | 6 +++--- tests/expand/tests/expand/pub-tuple_struct.expanded.rs | 6 +++--- .../tests/expand/unsafe_unpin-struct.expanded.rs | 6 +++--- .../tests/expand/unsafe_unpin-tuple_struct.expanded.rs | 6 +++--- 22 files changed, 71 insertions(+), 71 deletions(-) diff --git a/examples/not_unpin-expanded.rs b/examples/not_unpin-expanded.rs index 96c452eb..63132a72 100644 --- a/examples/not_unpin-expanded.rs +++ b/examples/not_unpin-expanded.rs @@ -117,9 +117,9 @@ const _: () = { // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; diff --git a/examples/pinned_drop-expanded.rs b/examples/pinned_drop-expanded.rs index c6eb7cd7..796fb5d1 100644 --- a/examples/pinned_drop-expanded.rs +++ b/examples/pinned_drop-expanded.rs @@ -124,9 +124,9 @@ const _: () = { // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed<'a, T>(val: &Struct<'a, T>) { - &val.was_dropped; - &val.field; + fn __assert_not_repr_packed<'a, T>(this: &Struct<'a, T>) { + let _ = &this.was_dropped; + let _ = &this.field; } }; diff --git a/examples/project_replace-expanded.rs b/examples/project_replace-expanded.rs index 320f0f9a..e1013f41 100644 --- a/examples/project_replace-expanded.rs +++ b/examples/project_replace-expanded.rs @@ -156,9 +156,9 @@ const _: () = { // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; diff --git a/examples/struct-default-expanded.rs b/examples/struct-default-expanded.rs index 99e599a8..84645efe 100644 --- a/examples/struct-default-expanded.rs +++ b/examples/struct-default-expanded.rs @@ -155,9 +155,9 @@ const _: () = { // // See https://github.com/taiki-e/pin-project/pull/34 for more details. #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; diff --git a/examples/unsafe_unpin-expanded.rs b/examples/unsafe_unpin-expanded.rs index 610c3da1..6532a5ad 100644 --- a/examples/unsafe_unpin-expanded.rs +++ b/examples/unsafe_unpin-expanded.rs @@ -104,9 +104,9 @@ const _: () = { // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; diff --git a/pin-project-internal/src/pin_project/derive.rs b/pin-project-internal/src/pin_project/derive.rs index 6225f2b8..49f3f8e2 100644 --- a/pin-project-internal/src/pin_project/derive.rs +++ b/pin-project-internal/src/pin_project/derive.rs @@ -1208,13 +1208,13 @@ impl<'a> Context<'a> { match fields { Fields::Named(FieldsNamed { named, .. }) => { for Field { ident, .. } in named { - field_refs.push(quote!(&val.#ident;)); + field_refs.push(quote!(&this.#ident)); } } Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => { for (index, _) in unnamed.iter().enumerate() { let index = Index::from(index); - field_refs.push(quote!(&val.#index;)); + field_refs.push(quote!(&this.#index)); } } Fields::Unit => {} @@ -1224,8 +1224,8 @@ impl<'a> Context<'a> { let ident = self.orig.ident; Ok(quote! { #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed #impl_generics (val: &#ident #ty_generics) #where_clause { - #(#field_refs)* + fn __assert_not_repr_packed #impl_generics (this: &#ident #ty_generics) #where_clause { + #(let _ = #field_refs;)* } }) } diff --git a/tests/expand/tests/expand/default-struct.expanded.rs b/tests/expand/tests/expand/default-struct.expanded.rs index 398fe192..8da13a63 100644 --- a/tests/expand/tests/expand/default-struct.expanded.rs +++ b/tests/expand/tests/expand/default-struct.expanded.rs @@ -80,9 +80,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; fn main() {} diff --git a/tests/expand/tests/expand/default-tuple_struct.expanded.rs b/tests/expand/tests/expand/default-tuple_struct.expanded.rs index 24fe2471..6809ac18 100644 --- a/tests/expand/tests/expand/default-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/default-tuple_struct.expanded.rs @@ -65,9 +65,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; fn main() {} diff --git a/tests/expand/tests/expand/multifields-struct.expanded.rs b/tests/expand/tests/expand/multifields-struct.expanded.rs index adee41da..c52f78dc 100644 --- a/tests/expand/tests/expand/multifields-struct.expanded.rs +++ b/tests/expand/tests/expand/multifields-struct.expanded.rs @@ -141,11 +141,11 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned1; - &val.pinned2; - &val.unpinned1; - &val.unpinned2; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned1; + let _ = &this.pinned2; + let _ = &this.unpinned1; + let _ = &this.unpinned2; } }; fn main() {} diff --git a/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs b/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs index 13bd4edf..8810d0b9 100644 --- a/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs @@ -117,11 +117,11 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; - &val.2; - &val.3; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; + let _ = &this.2; + let _ = &this.3; } }; fn main() {} diff --git a/tests/expand/tests/expand/naming-struct.expanded.rs b/tests/expand/tests/expand/naming-struct.expanded.rs index 41072332..e2fcb5ca 100644 --- a/tests/expand/tests/expand/naming-struct.expanded.rs +++ b/tests/expand/tests/expand/naming-struct.expanded.rs @@ -104,9 +104,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; fn main() {} diff --git a/tests/expand/tests/expand/naming-tuple_struct.expanded.rs b/tests/expand/tests/expand/naming-tuple_struct.expanded.rs index 435424f3..55be4779 100644 --- a/tests/expand/tests/expand/naming-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/naming-tuple_struct.expanded.rs @@ -83,9 +83,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; fn main() {} diff --git a/tests/expand/tests/expand/not_unpin-struct.expanded.rs b/tests/expand/tests/expand/not_unpin-struct.expanded.rs index ad3bdb1e..a3fb6968 100644 --- a/tests/expand/tests/expand/not_unpin-struct.expanded.rs +++ b/tests/expand/tests/expand/not_unpin-struct.expanded.rs @@ -71,9 +71,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; fn main() {} diff --git a/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs b/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs index 200d871d..96aec956 100644 --- a/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs @@ -56,9 +56,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; fn main() {} diff --git a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs index 60b90b1b..f9a08d86 100644 --- a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs @@ -82,9 +82,9 @@ const _: () = { } } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; impl ::pin_project::__private::PinnedDrop for Struct { diff --git a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs index a617c3f7..66a532c2 100644 --- a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs @@ -67,9 +67,9 @@ const _: () = { } } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; impl ::pin_project::__private::PinnedDrop for TupleStruct { diff --git a/tests/expand/tests/expand/project_replace-struct.expanded.rs b/tests/expand/tests/expand/project_replace-struct.expanded.rs index adf5b675..996af307 100644 --- a/tests/expand/tests/expand/project_replace-struct.expanded.rs +++ b/tests/expand/tests/expand/project_replace-struct.expanded.rs @@ -109,9 +109,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; fn main() {} diff --git a/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs b/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs index 62a053e1..427c4829 100644 --- a/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs @@ -91,9 +91,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; fn main() {} diff --git a/tests/expand/tests/expand/pub-struct.expanded.rs b/tests/expand/tests/expand/pub-struct.expanded.rs index aa520070..680883a2 100644 --- a/tests/expand/tests/expand/pub-struct.expanded.rs +++ b/tests/expand/tests/expand/pub-struct.expanded.rs @@ -80,9 +80,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; fn main() {} diff --git a/tests/expand/tests/expand/pub-tuple_struct.expanded.rs b/tests/expand/tests/expand/pub-tuple_struct.expanded.rs index a4ddb590..b3be74f1 100644 --- a/tests/expand/tests/expand/pub-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/pub-tuple_struct.expanded.rs @@ -68,9 +68,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; fn main() {} diff --git a/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs b/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs index 552ba8f0..9eff654f 100644 --- a/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs +++ b/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs @@ -69,9 +69,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &Struct) { - &val.pinned; - &val.unpinned; + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; } }; unsafe impl UnsafeUnpin for Struct {} diff --git a/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs b/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs index 81b9ab3a..33236711 100644 --- a/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs @@ -54,9 +54,9 @@ const _: () = { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } #[forbid(safe_packed_borrows)] - fn __assert_not_repr_packed(val: &TupleStruct) { - &val.0; - &val.1; + fn __assert_not_repr_packed(this: &TupleStruct) { + let _ = &this.0; + let _ = &this.1; } }; unsafe impl UnsafeUnpin for Struct {} From 6d00b1f4c32df69faabedc10ca45e5910fef23eb Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 28 Mar 2021 22:01:04 +0900 Subject: [PATCH 2/2] Prepare for removal of safe_packed_borrows lint --- examples/not_unpin-expanded.rs | 2 +- examples/pinned_drop-expanded.rs | 2 +- examples/project_replace-expanded.rs | 2 +- examples/struct-default-expanded.rs | 8 ++--- examples/unsafe_unpin-expanded.rs | 2 +- .../src/pin_project/derive.rs | 29 ++++++++++----- .../tests/expand/default-struct.expanded.rs | 2 +- .../expand/default-tuple_struct.expanded.rs | 2 +- .../expand/multifields-struct.expanded.rs | 2 +- .../multifields-tuple_struct.expanded.rs | 2 +- .../tests/expand/naming-struct.expanded.rs | 2 +- .../expand/naming-tuple_struct.expanded.rs | 2 +- .../tests/expand/not_unpin-struct.expanded.rs | 2 +- .../expand/not_unpin-tuple_struct.expanded.rs | 2 +- .../expand/pinned_drop-struct.expanded.rs | 2 +- .../pinned_drop-tuple_struct.expanded.rs | 2 +- .../expand/project_replace-struct.expanded.rs | 2 +- .../project_replace-tuple_struct.expanded.rs | 2 +- .../tests/expand/pub-struct.expanded.rs | 2 +- .../tests/expand/pub-tuple_struct.expanded.rs | 2 +- .../expand/unsafe_unpin-struct.expanded.rs | 2 +- .../unsafe_unpin-tuple_struct.expanded.rs | 2 +- tests/repr_packed.rs | 5 ++- tests/ui/pin_project/packed.rs | 20 +++++++---- tests/ui/pin_project/packed.stderr | 6 ++++ tests/ui/pin_project/safe_packed_borrows.rs | 24 ++++++++----- .../ui/pin_project/safe_packed_borrows.stderr | 32 +++++++++++++---- tests/ui/pin_project/unaligned_references.rs | 24 +++++++++++++ .../pin_project/unaligned_references.stderr | 36 +++++++++++++++++++ 29 files changed, 168 insertions(+), 56 deletions(-) create mode 100644 tests/ui/pin_project/unaligned_references.rs create mode 100644 tests/ui/pin_project/unaligned_references.stderr diff --git a/examples/not_unpin-expanded.rs b/examples/not_unpin-expanded.rs index 63132a72..f146db45 100644 --- a/examples/not_unpin-expanded.rs +++ b/examples/not_unpin-expanded.rs @@ -116,7 +116,7 @@ const _: () = { // // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/examples/pinned_drop-expanded.rs b/examples/pinned_drop-expanded.rs index 796fb5d1..44dde4dd 100644 --- a/examples/pinned_drop-expanded.rs +++ b/examples/pinned_drop-expanded.rs @@ -123,7 +123,7 @@ const _: () = { // // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed<'a, T>(this: &Struct<'a, T>) { let _ = &this.was_dropped; let _ = &this.field; diff --git a/examples/project_replace-expanded.rs b/examples/project_replace-expanded.rs index e1013f41..472bafc4 100644 --- a/examples/project_replace-expanded.rs +++ b/examples/project_replace-expanded.rs @@ -155,7 +155,7 @@ const _: () = { // // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/examples/struct-default-expanded.rs b/examples/struct-default-expanded.rs index 84645efe..818beaf0 100644 --- a/examples/struct-default-expanded.rs +++ b/examples/struct-default-expanded.rs @@ -143,10 +143,8 @@ const _: () = { // Ensure that it's impossible to use pin projections on a #[repr(packed)] // struct. // - // Taking a reference to a packed field is unsafe, and applying - // #[forbid(safe_packed_borrows)] makes sure that doing this without - // an 'unsafe' block (which we deliberately do not generate) - // is a hard error. + // Taking a reference to a packed field is UB, and applying + // `#[forbid(unaligned_references)]` makes sure that doing this is a hard error. // // If the struct ends up having #[repr(packed)] applied somehow, // this will generate an (unfriendly) error message. Under all reasonable @@ -154,7 +152,7 @@ const _: () = { // a much nicer error above. // // See https://github.com/taiki-e/pin-project/pull/34 for more details. - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/examples/unsafe_unpin-expanded.rs b/examples/unsafe_unpin-expanded.rs index 6532a5ad..3c18e494 100644 --- a/examples/unsafe_unpin-expanded.rs +++ b/examples/unsafe_unpin-expanded.rs @@ -103,7 +103,7 @@ const _: () = { // // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 // for details. - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/pin-project-internal/src/pin_project/derive.rs b/pin-project-internal/src/pin_project/derive.rs index 49f3f8e2..083d9481 100644 --- a/pin-project-internal/src/pin_project/derive.rs +++ b/pin-project-internal/src/pin_project/derive.rs @@ -1162,9 +1162,6 @@ impl<'a> Context<'a> { } } - // As proc-macro-derive can't rewrite the structure definition, - // it's probably no longer necessary, but it keeps it for now. - // Workaround for https://github.com/taiki-e/pin-project/issues/32 // Through the tricky use of proc macros, it's possible to bypass // the above check for the `repr` attribute. @@ -1172,7 +1169,7 @@ impl<'a> Context<'a> { // struct, we generate code like this: // // ```rust - // #[forbid(safe_packed_borrows)] + // #[forbid(unaligned_references)] // fn assert_not_repr_packed(val: &MyStruct) { // let _field1 = &val.field1; // let _field2 = &val.field2; @@ -1181,10 +1178,8 @@ impl<'a> Context<'a> { // } // ``` // - // Taking a reference to a packed field is unsafe, and applying - // `#[forbid(safe_packed_borrows)]` makes sure that doing this without - // an `unsafe` block (which we deliberately do not generate) - // is a hard error. + // Taking a reference to a packed field is UB, and applying + // `#[forbid(unaligned_references)]` makes sure that doing this is a hard error. // // If the struct ends up having `#[repr(packed)]` applied somehow, // this will generate an (unfriendly) error message. Under all reasonable @@ -1204,6 +1199,22 @@ impl<'a> Context<'a> { // `#[repr(packed)]` in the first place. // // See also https://github.com/taiki-e/pin-project/pull/34. + // + // Note: + // - pin-project v0.4.3 or later (ttps://github.com/taiki-e/pin-project/pull/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 + // + // - Forbid both unaligned_references and safe_packed_borrows lints + // because unaligned_references lint does not exist in older compilers: + // https://github.com/taiki-e/pin-project-lite/pull/55 + // https://github.com/rust-lang/rust/pull/82525 let mut field_refs = vec![]; match fields { Fields::Named(FieldsNamed { named, .. }) => { @@ -1223,7 +1234,7 @@ impl<'a> Context<'a> { let (impl_generics, ty_generics, where_clause) = self.orig.generics.split_for_impl(); let ident = self.orig.ident; Ok(quote! { - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed #impl_generics (this: &#ident #ty_generics) #where_clause { #(let _ = #field_refs;)* } diff --git a/tests/expand/tests/expand/default-struct.expanded.rs b/tests/expand/tests/expand/default-struct.expanded.rs index 8da13a63..903c6186 100644 --- a/tests/expand/tests/expand/default-struct.expanded.rs +++ b/tests/expand/tests/expand/default-struct.expanded.rs @@ -79,7 +79,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/default-tuple_struct.expanded.rs b/tests/expand/tests/expand/default-tuple_struct.expanded.rs index 6809ac18..d81d9e23 100644 --- a/tests/expand/tests/expand/default-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/default-tuple_struct.expanded.rs @@ -64,7 +64,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/multifields-struct.expanded.rs b/tests/expand/tests/expand/multifields-struct.expanded.rs index c52f78dc..89c6fdf2 100644 --- a/tests/expand/tests/expand/multifields-struct.expanded.rs +++ b/tests/expand/tests/expand/multifields-struct.expanded.rs @@ -140,7 +140,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned1; let _ = &this.pinned2; diff --git a/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs b/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs index 8810d0b9..0a82a3fa 100644 --- a/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/multifields-tuple_struct.expanded.rs @@ -116,7 +116,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/naming-struct.expanded.rs b/tests/expand/tests/expand/naming-struct.expanded.rs index e2fcb5ca..630b02d0 100644 --- a/tests/expand/tests/expand/naming-struct.expanded.rs +++ b/tests/expand/tests/expand/naming-struct.expanded.rs @@ -103,7 +103,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/naming-tuple_struct.expanded.rs b/tests/expand/tests/expand/naming-tuple_struct.expanded.rs index 55be4779..a186dbed 100644 --- a/tests/expand/tests/expand/naming-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/naming-tuple_struct.expanded.rs @@ -82,7 +82,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/not_unpin-struct.expanded.rs b/tests/expand/tests/expand/not_unpin-struct.expanded.rs index a3fb6968..a930c004 100644 --- a/tests/expand/tests/expand/not_unpin-struct.expanded.rs +++ b/tests/expand/tests/expand/not_unpin-struct.expanded.rs @@ -70,7 +70,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs b/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs index 96aec956..74579299 100644 --- a/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/not_unpin-tuple_struct.expanded.rs @@ -55,7 +55,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs index f9a08d86..11a8b4f5 100644 --- a/tests/expand/tests/expand/pinned_drop-struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-struct.expanded.rs @@ -81,7 +81,7 @@ const _: () = { } } } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs index 66a532c2..ec7bc2e6 100644 --- a/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/pinned_drop-tuple_struct.expanded.rs @@ -66,7 +66,7 @@ const _: () = { } } } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/project_replace-struct.expanded.rs b/tests/expand/tests/expand/project_replace-struct.expanded.rs index 996af307..607dd84f 100644 --- a/tests/expand/tests/expand/project_replace-struct.expanded.rs +++ b/tests/expand/tests/expand/project_replace-struct.expanded.rs @@ -108,7 +108,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs b/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs index 427c4829..8cec16dc 100644 --- a/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/project_replace-tuple_struct.expanded.rs @@ -90,7 +90,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/pub-struct.expanded.rs b/tests/expand/tests/expand/pub-struct.expanded.rs index 680883a2..c520a318 100644 --- a/tests/expand/tests/expand/pub-struct.expanded.rs +++ b/tests/expand/tests/expand/pub-struct.expanded.rs @@ -79,7 +79,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/pub-tuple_struct.expanded.rs b/tests/expand/tests/expand/pub-tuple_struct.expanded.rs index b3be74f1..01e43050 100644 --- a/tests/expand/tests/expand/pub-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/pub-tuple_struct.expanded.rs @@ -67,7 +67,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs b/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs index 9eff654f..bb2270ff 100644 --- a/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs +++ b/tests/expand/tests/expand/unsafe_unpin-struct.expanded.rs @@ -68,7 +68,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for Struct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &Struct) { let _ = &this.pinned; let _ = &this.unpinned; diff --git a/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs b/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs index 33236711..47655779 100644 --- a/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs +++ b/tests/expand/tests/expand/unsafe_unpin-tuple_struct.expanded.rs @@ -53,7 +53,7 @@ const _: () = { impl ::pin_project::__private::PinnedDrop for TupleStruct { unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} } - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed(this: &TupleStruct) { let _ = &this.0; let _ = &this.1; diff --git a/tests/repr_packed.rs b/tests/repr_packed.rs index fe72675c..599f95dd 100644 --- a/tests/repr_packed.rs +++ b/tests/repr_packed.rs @@ -1,5 +1,8 @@ #![warn(rust_2018_idioms, single_use_lifetimes)] -#![forbid(safe_packed_borrows)] +// unaligned_references did not exist in older compilers and safe_packed_borrows was removed in the latest compilers. +// https://github.com/rust-lang/rust/pull/82525 +#![allow(unknown_lints, renamed_and_removed_lints)] +#![forbid(unaligned_references, safe_packed_borrows)] use std::cell::Cell; diff --git a/tests/ui/pin_project/packed.rs b/tests/ui/pin_project/packed.rs index 86f3ecf3..dd3ebfd6 100644 --- a/tests/ui/pin_project/packed.rs +++ b/tests/ui/pin_project/packed.rs @@ -2,24 +2,32 @@ use pin_project::pin_project; #[pin_project] #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] types -struct A { +struct Packed1 { #[pin] - field: u8, + f: u8, } // Test putting 'repr' before the 'pin_project' attribute #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] types #[pin_project] -struct B { +struct Packed2 { #[pin] - field: u8, + f: u8, } #[pin_project] #[repr(packed(2))] //~ ERROR may not be used on #[repr(packed)] types -struct C { +struct PackedN1 { #[pin] - field: u32, + f: u32, +} + +// Test putting 'repr' before the 'pin_project' attribute +#[repr(packed(2))] //~ ERROR may not be used on #[repr(packed)] types +#[pin_project] +struct PackedN2 { + #[pin] + f: u32, } fn main() {} diff --git a/tests/ui/pin_project/packed.stderr b/tests/ui/pin_project/packed.stderr index 969faea2..e5b9e550 100644 --- a/tests/ui/pin_project/packed.stderr +++ b/tests/ui/pin_project/packed.stderr @@ -15,3 +15,9 @@ error: #[pin_project] attribute may not be used on #[repr(packed)] types | 19 | #[repr(packed(2))] //~ ERROR may not be used on #[repr(packed)] types | ^^^^^^^^^ + +error: #[pin_project] attribute may not be used on #[repr(packed)] types + --> $DIR/packed.rs:26:8 + | +26 | #[repr(packed(2))] //~ ERROR may not be used on #[repr(packed)] types + | ^^^^^^^^^ diff --git a/tests/ui/pin_project/safe_packed_borrows.rs b/tests/ui/pin_project/safe_packed_borrows.rs index ea925dc4..a51b7d12 100644 --- a/tests/ui/pin_project/safe_packed_borrows.rs +++ b/tests/ui/pin_project/safe_packed_borrows.rs @@ -1,21 +1,27 @@ #![forbid(safe_packed_borrows)] +#![allow(unaligned_references)] -// Refs: https://github.com/rust-lang/rust/issues/46043 +// This lint was removed in https://github.com/rust-lang/rust/pull/82525 (nightly-2021-03-28). +// Refs: +// - https://github.com/rust-lang/rust/pull/82525 +// - https://github.com/rust-lang/rust/issues/46043 #[repr(packed)] -struct A { - field: u32, +struct Packed { + f: u32, } #[repr(packed(2))] -struct B { - field: u32, +struct PackedN { + f: u32, } fn main() { - let a = A { field: 1 }; - &a.field; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + let a = Packed { f: 1 }; + &a.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + let _ = &a.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block - let b = B { field: 1 }; - &b.field; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + let b = PackedN { f: 1 }; + &b.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + let _ = &b.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block } diff --git a/tests/ui/pin_project/safe_packed_borrows.stderr b/tests/ui/pin_project/safe_packed_borrows.stderr index f8b736d2..a5a03cb3 100644 --- a/tests/ui/pin_project/safe_packed_borrows.stderr +++ b/tests/ui/pin_project/safe_packed_borrows.stderr @@ -1,8 +1,8 @@ error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) - --> $DIR/safe_packed_borrows.rs:17:5 + --> $DIR/safe_packed_borrows.rs:21:5 | -17 | &a.field; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block - | ^^^^^^^^ +21 | &a.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + | ^^^^ | note: the lint level is defined here --> $DIR/safe_packed_borrows.rs:1:11 @@ -14,10 +14,30 @@ note: the lint level is defined here = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) - --> $DIR/safe_packed_borrows.rs:20:5 + --> $DIR/safe_packed_borrows.rs:22:13 | -20 | &b.field; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block - | ^^^^^^^^ +22 | let _ = &a.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + | ^^^^ + | + = 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 #46043 + = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior + +error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) + --> $DIR/safe_packed_borrows.rs:25:5 + | +25 | &b.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + | ^^^^ + | + = 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 #46043 + = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior + +error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) + --> $DIR/safe_packed_borrows.rs:26:13 + | +26 | let _ = &b.f; //~ ERROR borrow of packed field is unsafe and requires unsafe function or block + | ^^^^ | = 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 #46043 diff --git a/tests/ui/pin_project/unaligned_references.rs b/tests/ui/pin_project/unaligned_references.rs new file mode 100644 index 00000000..99dd4175 --- /dev/null +++ b/tests/ui/pin_project/unaligned_references.rs @@ -0,0 +1,24 @@ +#![forbid(unaligned_references)] +#![allow(safe_packed_borrows)] + +// Refs: https://github.com/rust-lang/rust/issues/82523 + +#[repr(packed)] +struct Packed { + f: u32, +} + +#[repr(packed(2))] +struct PackedN { + f: u32, +} + +fn main() { + let a = Packed { f: 1 }; + &a.f; //~ ERROR reference to packed field is unaligned + let _ = &a.f; //~ ERROR reference to packed field is unaligned + + let b = PackedN { f: 1 }; + &b.f; //~ ERROR reference to packed field is unaligned + let _ = &b.f; //~ ERROR reference to packed field is unaligned +} diff --git a/tests/ui/pin_project/unaligned_references.stderr b/tests/ui/pin_project/unaligned_references.stderr new file mode 100644 index 00000000..efe17dcc --- /dev/null +++ b/tests/ui/pin_project/unaligned_references.stderr @@ -0,0 +1,36 @@ +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:18:5 + | +18 | &a.f; //~ ERROR reference to packed field is unaligned + | ^^^^ + | +note: the lint level is defined here + --> $DIR/unaligned_references.rs:1:11 + | +1 | #![forbid(unaligned_references)] + | ^^^^^^^^^^^^^^^^^^^^ + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:19:13 + | +19 | let _ = &a.f; //~ ERROR reference to packed field is unaligned + | ^^^^ + | + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:22:5 + | +22 | &b.f; //~ ERROR reference to packed field is unaligned + | ^^^^ + | + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:23:13 + | +23 | let _ = &b.f; //~ ERROR reference to packed field is unaligned + | ^^^^ + | + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)