[PATCH 4/5] rust: pin-init: internal: add custom diagnostic when `#[pin_data]` is not implemented

From: Gary Guo

Date: Wed Sep 23 2026 - 14:26:20 EST


Produce a more helpful error message when `#[pin_data]` is omitted. The
error message changes from

error[E0277]: the trait bound `Foo: pin_init::__internal::HasPinData` is not satisfied
--> tests/ui/compile-fail/init/missing_pin_data.rs:9:19
|
9 | pin_init!(Self { a: 42 })
| ^^^^ unsatisfied trait bound
|
help: the trait `pin_init::__internal::HasPinData` is not implemented for `Foo`
--> tests/ui/compile-fail/init/missing_pin_data.rs:3:1
|
3 | struct Foo {
| ^^^^^^^^^^

to

error[E0277]: `Foo` cannot be used with `pin_init!` macro
--> tests/ui/compile-fail/init/missing_pin_data.rs:9:19
|
9 | pin_init!(Self { a: 42 })
| ^^^^ unsatisfied trait bound
|
help: the trait `pin_init::__internal::HasPinData` is not implemented for `Foo`
--> tests/ui/compile-fail/init/missing_pin_data.rs:3:1
|
3 | struct Foo {
| ^^^^^^^^^^
= note: did you forget to add `#[pin_data]` attribute to the struct?

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/pin-init/src/__internal.rs | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/rust/pin-init/src/__internal.rs b/rust/pin-init/src/__internal.rs
index f236db47d18b..cb7a0c3066b6 100644
--- a/rust/pin-init/src/__internal.rs
+++ b/rust/pin-init/src/__internal.rs
@@ -83,6 +83,10 @@ pub unsafe fn new() -> Self {
///
/// `pin-init` relies on the correctness of the helper functions defined on `PinData`.
/// Thus, only the `#[pin_data]` can implement this trait.
+#[diagnostic::on_unimplemented(
+ message = "`{Self}` cannot be used with `pin_init!` macro",
+ note = "did you forget to add `#[pin_data]` attribute to the struct?"
+)]
pub unsafe trait HasPinData {
type PinData;


--
2.54.0