Skip to content

rust: pin-init: improve diagnostics of missing `#[pin_data]` and `#[pin]`

Gary Guo lkml rust-for-linux 1 version Applied

Status from this message: Applied to pin-init-next, with typo pointed out by Sashiko fixed.

Get series with b4
b4 am [email protected] change-id 20260923-dev-pin-data-diag-65209ab751fc

Versions newest first

  1. v1 Latest5 patches 1 replyApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/5rust: pin-init: internal: make `__init_data` and `__pin_data` saferust/pin-init/internal/src/init.rs rust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+7 -90 replies
2/5rust: pin-init: internal: remove associated type of `HasInitData`rust/pin-init/src/__internal.rs+14 -230 replies
3/5rust: pin-init: internal: use `HasInitData` to provide inference help onlyrust/pin-init/internal/src/init.rs rust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs +1 more+16 -130 replies
4/5rust: pin-init: internal: add custom diagnostic when `#[pin_data]` is not implementedrust/pin-init/src/__internal.rs+4 -00 replies
5/5rust: pin-init: add diagnostic attribute for `PinInit` and `Init`rust/pin-init/src/lib.rs+8 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/5] rust: pin-init: improve diagnostics of missing `#[pin_data]` and `#[pin]`

Improve the diagnostics of missing #[pin_data] and #[pin], by adding diagnostics attributes. For #[pin_data], we have to change the expansion to retrieve PinData in a two-step process, so we change the error message from "method exists but trait bound not satisfied" to "trait not implemented".

This is the new error message for missing #[pin_data]:

    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`

where it was previously

    error[E0599]: no associated function or constant named `__pin_data` found for struct `Foo` in the current scope
     --> tests/ui/compile-fail/init/missing_pin_data.rs:9:9
      |
    3 | struct Foo {
      | ---------- associated function or constant `__pin_data` not found for this struct
    ...
    9 |         pin_init!(Self { a: 42 })
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^ associated function or constant not found in `Foo`
      |
      = help: items from traits can only be used if the trait is implemented and in scope
      = note: the following trait defines an item `__pin_data`, perhaps you need to implement it:
              candidate #1: `pin_init::__internal::HasPinData`

and this is the new error message for missing #[pin]:

error[E0277]: impl PinInit<usize> cannot be used to movably initialize usize with error _
--> tests/ui/compile-fail/pin_data/missing_pin.rs:12:18
|
12 | a <- a,
| -----^
| | |
| | the trait Init<usize, _> is not implemented for impl PinInit<usize>
| required by a bound introduced by this call
|
= note: if your type implements PinInit but not Init, you might be forgetting a #[pin] annotation on fields

where it was previously

error[E0277]: the trait bound impl pin_init::PinInit<Bar>: Init<Bar, _> is not satisfied
--> tests/ui/compile-fail/init/invalid_init.rs:19:16
|
19 | bar <- Bar::new(),
| -------^^^^^^^^^^
| | |
| | the trait Init<Bar, _> is not implemented for impl pin_init::PinInit<Bar>
| required by a bound introduced by this call

---
Gary Guo (5):
rust: pin-init: internal: make __init_data and __pin_data safe
rust: pin-init: internal: remove associated type of HasInitData
rust: pin-init: internal: use HasInitData to provide inference help only
rust: pin-init: internal: add custom diagnostic when #[pin_data] is not implemented
rust: pin-init: add diagnostic attribute for PinInit and Init

 rust/pin-init/internal/src/init.rs     | 25 +++++++++---------
 rust/pin-init/internal/src/pin_data.rs |  2 +-
 rust/pin-init/src/__internal.rs        | 48 +++++++++++++++-------------------
 rust/pin-init/src/lib.rs               | 11 +++++++-
 4 files changed, 45 insertions(+), 41 deletions(-)

---
base-commit: dfb6a037fd586f1ffcba3b143dab58f5c88294d1
change-id: 20260923-dev-pin-data-diag-65209ab751fc

Best regards,

Signature · 1 line
Gary Guo <[email protected]>