Skip to content

Commit 3344d50

Browse files
committed
add ?Sized bounds to traits in #[pin_data] macro
The `#[pin_data]` macro uses some auxiliary traits to ensure that a user does not implement `Drop` for the annotated struct, as that is unsound and can lead to UB. However, if the struct that is annotated is `!Sized`, the current bounds do not work, because `Sized` is an implicit bound for generics. This is *not* a soundness hole of pin-init, as it currently is impossible to construct an unsized struct using pin-init. Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent 8963bc4 commit 3344d50

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

internal/src/pin_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ fn generate_drop_impl(
222222
// if it also implements `Drop`
223223
trait MustNotImplDrop {}
224224
#[expect(drop_bounds)]
225-
impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
225+
impl<T: ::core::ops::Drop + ?::core::marker::Sized> MustNotImplDrop for T {}
226226
impl #impl_generics MustNotImplDrop for #ident #ty_generics
227227
#whr
228228
{}
@@ -231,7 +231,7 @@ fn generate_drop_impl(
231231
// `PinnedDrop` as the parameter to `#[pin_data]`.
232232
#[expect(non_camel_case_types)]
233233
trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
234-
impl<T: ::pin_init::PinnedDrop>
234+
impl<T: ::pin_init::PinnedDrop + ?::core::marker::Sized>
235235
UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
236236
impl #impl_generics
237237
UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for #ident #ty_generics

tests/ui/expand/pin-data.expanded.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ const _: () = {
111111
{}
112112
trait MustNotImplDrop {}
113113
#[expect(drop_bounds)]
114-
impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
114+
impl<T: ::core::ops::Drop + ?::core::marker::Sized> MustNotImplDrop for T {}
115115
impl MustNotImplDrop for Foo {}
116116
#[expect(non_camel_case_types)]
117117
trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
118-
impl<T: ::pin_init::PinnedDrop> UselessPinnedDropImpl_you_need_to_specify_PinnedDrop
119-
for T {}
118+
impl<
119+
T: ::pin_init::PinnedDrop + ?::core::marker::Sized,
120+
> UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
120121
impl UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for Foo {}
121122
};

0 commit comments

Comments
 (0)