Skip to content

Commit 580cc37

Browse files
BennoLossinojeda
authored andcommitted
rust: pin-init: internal: init: document load-bearing fact of field accessors
The functions `[Pin]Init::__[pinned_]init` and `ptr::write` called from the `init!` macro require the passed pointer to be aligned. This fact is ensured by the creation of field accessors to previously initialized fields. Since we missed this very important fact from the beginning [1], document it in the code. Link: https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/initialized.20field.20accessor.20detection/with/576210658 [1] Fixes: 90e53c5 ("rust: add pin-init API core") Cc: <stable@vger.kernel.org> # 6.6.y, 6.12.y: 42415d1: rust: pin-init: add references to previously initialized fields Cc: <stable@vger.kernel.org> # 6.6.y, 6.12.y, 6.18.y, 6.19.y Signed-off-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260302140424.4097655-2-lossin@kernel.org [ Updated Cc: stable@ tags as discussed. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent a075082 commit 580cc37

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

rust/pin-init/internal/src/init.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ fn init_fields(
251251
});
252252
// Again span for better diagnostics
253253
let write = quote_spanned!(ident.span()=> ::core::ptr::write);
254+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
255+
// Unaligned fields will cause the compiler to emit E0793. We do not support
256+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
257+
// `ptr::write` below has the same requirement.
254258
let accessor = if pinned {
255259
let project_ident = format_ident!("__project_{ident}");
256260
quote! {
@@ -278,6 +282,10 @@ fn init_fields(
278282
InitializerKind::Init { ident, value, .. } => {
279283
// Again span for better diagnostics
280284
let init = format_ident!("init", span = value.span());
285+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
286+
// Unaligned fields will cause the compiler to emit E0793. We do not support
287+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
288+
// `ptr::write` below has the same requirement.
281289
let (value_init, accessor) = if pinned {
282290
let project_ident = format_ident!("__project_{ident}");
283291
(

0 commit comments

Comments
 (0)