From: Gary Guo <gary@garyguo.net>
To: "Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] rust: pin-init: internal: pin_init: use `slot` identifier directly with mixed site
Date: Wed, 16 Sep 2026 13:11:33 +0100 [thread overview]
Message-ID: <20260916-dev-hygiene-v1-2-dfb6ae2588ba@garyguo.net> (raw)
In-Reply-To: <20260916-dev-hygiene-v1-0-dfb6ae2588ba@garyguo.net>
Use `quote_spanned!(Span::mixed_site() => ...)` directly instead of
creating `slot` identifier and interpolate it later. Do the same for
`__data`, too.
This is needed so that we can customize the location of the span without
having the `slot` identifier getting in the way. For example:
quote_spanned!(Span::mixed_site().located_at(loc) => ...)
with `slot` mentioned directly will have the diagnostic pointing to the
desired location, while `#slot` won't.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
rust/pin-init/internal/src/init.rs | 31 ++++++++++++-------------------
1 file changed, 12 insertions(+), 19 deletions(-)
diff --git a/rust/pin-init/internal/src/init.rs b/rust/pin-init/internal/src/init.rs
index 6ade192d27e6..a77ffef9926b 100644
--- a/rust/pin-init/internal/src/init.rs
+++ b/rust/pin-init/internal/src/init.rs
@@ -269,7 +269,6 @@ fn expand(
},
|(_, err)| Box::new(err),
);
- let slot = Ident::new("slot", Span::mixed_site());
let (has_data_trait, get_data, init_from_closure) = if pinned {
(
format_ident!("HasPinData"),
@@ -286,7 +285,7 @@ fn expand(
let init_kind = get_init_kind(rest, dcx);
let zeroable_check = match init_kind {
InitKind::Normal => quote!(),
- InitKind::Zeroing => quote! {
+ InitKind::Zeroing => quote_spanned! { Span::mixed_site() =>
// The user specified `..Zeroable::zeroed()` at the end of the list of fields.
// Therefore we check if the struct implements `Zeroable` and then zero the memory.
// This allows us to also remove the check that all fields are present (since we
@@ -295,9 +294,9 @@ fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
where T: ::pin_init::Zeroable
{}
// Ensure that the struct is indeed `Zeroable`.
- assert_zeroable(#slot);
+ assert_zeroable(slot);
// SAFETY: The type implements `Zeroable` by the check above.
- unsafe { ::core::ptr::write_bytes(#slot, 0, 1) };
+ unsafe { ::core::ptr::write_bytes(slot, 0, 1) };
},
};
let this = match this {
@@ -309,20 +308,19 @@ fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
},
};
// `mixed_site` ensures that the data is not accessible to the user-controlled code.
- let data = Ident::new("__data", Span::mixed_site());
- let init_fields = init_fields(&fields, pinned, &data, &slot);
+ let init_fields = init_fields(&fields, pinned);
let field_check = make_field_check(&fields, init_kind, &path);
Ok(quote_spanned! { Span::mixed_site() => {
// Get the data about fields from the supplied type.
// SAFETY: TODO
- let #data = unsafe {
+ let data = unsafe {
use ::pin_init::__internal::#has_data_trait;
// 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()
};
- // Ensure that `#data` really is of type `#data` and help with type inference:
- let init = #data.__make_closure::<_, #error>(
+ // Ensure that `data` really is of type `data` and help with type inference:
+ let init = data.__make_closure::<_, #error>(
move |slot| {
#zeroable_check
#this
@@ -380,12 +378,7 @@ fn get_init_kind(rest: Option<(Token![..], Expr)>, dcx: &mut DiagCtxt) -> InitKi
}
/// Generate the code that initializes the fields of the struct using the initializers in `field`.
-fn init_fields(
- fields: &Punctuated<InitializerField, Token![,]>,
- pinned: bool,
- data: &Ident,
- slot: &Ident,
-) -> TokenStream {
+fn init_fields(fields: &Punctuated<InitializerField, Token![,]>, pinned: bool) -> TokenStream {
let mut guards = vec![];
let mut guard_attrs = vec![];
let mut res = TokenStream::new();
@@ -413,16 +406,16 @@ fn init_fields(
let ident = member.as_ident();
let slot = if pinned {
- quote! {
+ quote_spanned! { Span::mixed_site() =>
// SAFETY:
// - `slot` is valid and properly aligned.
// - `make_field_check` checks that `&raw mut (*slot).#member` is properly aligned.
// - `make_field_check` prevents `#member` from being used twice, therefore
// `(*slot).#member` is exclusively accessed and has not been initialized.
- (unsafe { #data.#ident(#slot) })
+ (unsafe { data.#ident(slot) })
}
} else {
- quote! {
+ quote_spanned! { Span::mixed_site() =>
// For `init!()` macro, everything is unpinned.
// SAFETY:
// - `&raw mut (*slot).#member` is valid.
@@ -431,7 +424,7 @@ fn init_fields(
// `(*slot).#member` is exclusively accessed and has not been initialized.
(unsafe {
::pin_init::__internal::Slot::<::pin_init::__internal::Unpinned, _>::new(
- &raw mut (*#slot).#member
+ &raw mut (*slot).#member
)
})
}
--
2.54.0
next prev parent reply other threads:[~2026-09-16 12:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-16 12:11 [PATCH 0/3] rust: pin-init: provide better span for diagnostics Gary Guo
2026-09-16 12:11 ` [PATCH 1/3] rust: pin-init: internal: pin_init: emit `slot` using mixed site hygiene Gary Guo
2026-09-16 12:11 ` Gary Guo [this message]
2026-09-16 12:11 ` [PATCH 3/3] rust: pin-init: internal: pin_init: provide span for slot projection Gary Guo
2026-09-16 18:41 ` [PATCH 4/3] rust: pin-init: util: use span of `Index` for generated identifiers Gary Guo
2026-09-18 11:53 ` [PATCH 0/3] rust: pin-init: provide better span for diagnostics Gary Guo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260916-dev-hygiene-v1-2-dfb6ae2588ba@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®