[PATCH v3 08/15] rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro

Benno Lossin posted 15 patches 3 weeks, 4 days ago
There is a newer version of this series
[PATCH v3 08/15] rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro
Posted by Benno Lossin 3 weeks, 4 days ago
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.

Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Signed-off-by: Benno Lossin <lossin@kernel.org>
---
Changes in v3: none
Changes in v2: none
---
 rust/pin-init/internal/src/pin_data.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs
index fb4b8507c01d..9a43085f0864 100644
--- a/rust/pin-init/internal/src/pin_data.rs
+++ b/rust/pin-init/internal/src/pin_data.rs
@@ -231,7 +231,7 @@ fn drop(&mut self) {
             // if it also implements `Drop`
             trait MustNotImplDrop {}
             #[expect(drop_bounds)]
-            impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
+            impl<T: ::core::ops::Drop + ?::core::marker::Sized> MustNotImplDrop for T {}
             impl #impl_generics MustNotImplDrop for #ident #ty_generics
                 #whr
             {}
@@ -240,7 +240,7 @@ impl #impl_generics MustNotImplDrop for #ident #ty_generics
             // `PinnedDrop` as the parameter to `#[pin_data]`.
             #[expect(non_camel_case_types)]
             trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
-            impl<T: ::pin_init::PinnedDrop>
+            impl<T: ::pin_init::PinnedDrop + ?::core::marker::Sized>
                 UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
             impl #impl_generics
                 UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for #ident #ty_generics
-- 
2.52.0
Re: [PATCH v3 08/15] rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro
Posted by Gary Guo 3 weeks, 4 days ago
On Wed Jan 14, 2026 at 6:18 PM GMT, Benno Lossin wrote:
> 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.
> 
> Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
> Signed-off-by: Benno Lossin <lossin@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
> Changes in v3: none
> Changes in v2: none
> ---
>  rust/pin-init/internal/src/pin_data.rs | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)