summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Guo <gary@garyguo.net>2026-09-23 19:20:16 +0100
committerGary Guo <gary@garyguo.net>2026-09-25 21:38:44 +0100
commitc5155147cdf0053694f8bcdfcc616d254ae6842e (patch)
tree67df211729148cfd62c3bdce11ed2337e9fb19e7
parentc0243b93d0be35a4e8687b658991e3e6b2f1e9e7 (diff)
downloadlinux-next-c5155147cdf0053694f8bcdfcc616d254ae6842e.tar.gz
linux-next-c5155147cdf0053694f8bcdfcc616d254ae6842e.zip
rust: pin-init: internal: use `HasInitData` to provide inference help only
Currently, type inference of the initialized type is done by use the function calling syntax `PATH::__pin_data` or `PATH::__init_data` to obtain the required init data type, without needing the user to explicitly mention all generics. Switch to always use `HasInitData::__init_data` to provide that inference, which will not produce method-not-found errors as it is always available. Change `__pin_data` to take `InitData<Self>` as the receiver, so for types that require `HasPinData`, `HasPinData::__pin_data(data)` can be used to obtain the needed type without having to mention the user-provided path again. The expanded code also uses the span of the type, so diagnostics will hint to user that the type is causing the issue. With this change, the error message when `HasPinData` is not implemented changes from 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` to 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 { | ^^^^^^^^^^ Link: https://patch.msgid.link/20260923-dev-pin-data-diag-v1-3-e4d7193569e5@garyguo.net Signed-off-by: Gary Guo <gary@garyguo.net>
-rw-r--r--rust/pin-init/internal/src/init.rs22
-rw-r--r--rust/pin-init/internal/src/pin_data.rs2
-rw-r--r--rust/pin-init/src/__internal.rs2
-rw-r--r--rust/pin-init/src/lib.rs3
4 files changed, 16 insertions, 13 deletions
diff --git a/rust/pin-init/internal/src/init.rs b/rust/pin-init/internal/src/init.rs
index 1957be83a196..e98182c65447 100644
--- a/rust/pin-init/internal/src/init.rs
+++ b/rust/pin-init/internal/src/init.rs
@@ -269,18 +269,17 @@ fn expand(
},
|(_, err)| Box::new(err),
);
- let (has_data_trait, get_data, init_from_closure) = if pinned {
+ let (get_pin_data, init_from_closure) = if pinned {
(
- format_ident!("HasPinData"),
- format_ident!("__pin_data"),
+ Some(
+ quote_spanned! { path.span().resolved_at(Span::mixed_site()) =>
+ let data = ::pin_init::__internal::HasPinData::__pin_data(data);
+ },
+ ),
format_ident!("pin_init_from_closure"),
)
} else {
- (
- format_ident!("HasInitData"),
- format_ident!("__init_data"),
- format_ident!("init_from_closure"),
- )
+ (None, format_ident!("init_from_closure"))
};
let init_kind = get_init_kind(rest, dcx);
let zeroable_check = match init_kind {
@@ -313,11 +312,14 @@ fn expand(
Ok(quote_spanned! { Span::mixed_site() => {
// Get the data about fields from the supplied type.
let data = {
- use ::pin_init::__internal::#has_data_trait;
+ use ::pin_init::__internal::HasInitData;
// Can't use `<#path as #has_data_trait>::#get_data`, since the user is able to omit
// generics (which need to be present with that syntax).
- #path::#get_data()
+ #path::__init_data()
};
+
+ #get_pin_data
+
// Ensure that `data` really is of type `data` and help with type inference:
let init = data.__make_closure::<_, #error>(
move |slot| {
diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs
index 9fdce1593c7f..982a38ceea93 100644
--- a/rust/pin-init/internal/src/pin_data.rs
+++ b/rust/pin-init/internal/src/pin_data.rs
@@ -527,7 +527,7 @@ fn generate_the_pin_data(
type PinData = __ThePinData #ty_generics;
#[inline]
- fn __pin_data() -> Self::PinData {
+ fn __pin_data(_: ::pin_init::__internal::InitData<Self>) -> Self::PinData {
__ThePinData { __phantom: ::pin_init::__internal::PhantomInvariant::new() }
}
}
diff --git a/rust/pin-init/src/__internal.rs b/rust/pin-init/src/__internal.rs
index e5f75432fe87..914c4d1a06eb 100644
--- a/rust/pin-init/src/__internal.rs
+++ b/rust/pin-init/src/__internal.rs
@@ -70,7 +70,7 @@ impl InitOk {
pub unsafe trait HasPinData {
type PinData;
- fn __pin_data() -> Self::PinData;
+ fn __pin_data(_: InitData<Self>) -> Self::PinData;
}
/// This trait is automatically implemented for every type.
diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
index 4110590348c6..319485dc18ce 100644
--- a/rust/pin-init/src/lib.rs
+++ b/rust/pin-init/src/lib.rs
@@ -920,7 +920,8 @@ macro_rules! assert_pinned {
($ty:ty, $field:ident, $field_ty:ty, inline) => {
// SAFETY: This code is unreachable.
let _ = move |ptr: *mut $ty| unsafe {
- let data = <$ty as $crate::__internal::HasPinData>::__pin_data();
+ let data = <$ty as $crate::__internal::HasInitData>::__init_data();
+ let data = $crate::__internal::HasPinData::__pin_data(data);
_ = data
.$field(ptr)
.init($crate::__internal::AlwaysFail::<$field_ty>::new());