mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: "Benno Lossin" <lossin@kernel.org>,
	"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,
	 Gary Guo <gary@garyguo.net>
Subject: [PATCH 1/8] rust: pin-init: internal: init: forget guards in reverse order
Date: Tue, 06 Oct 2026 17:29:31 +0200	[thread overview]
Message-ID: <20261006-dev-selfref-dep-v1-1-da716f740d6d@garyguo.net> (raw)
In-Reply-To: <20261006-dev-selfref-dep-v1-0-da716f740d6d@garyguo.net>

Currently, the forget order is the same as initialization order, this is
the wrong way around compared to their drop order when going out-of-scope.
The wrong order will cause borrow checker errors if there are lifetime
dependencies between the guards. This does not matter for now because there
cannot be any lifetime dependency; it would matter for self-referential
borrows.

Reverse the order to be consistent with their out-of-scope drop order.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/pin-init/internal/src/init.rs | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/rust/pin-init/internal/src/init.rs b/rust/pin-init/internal/src/init.rs
index 6605e04d49df..f0b23d0311ac 100644
--- a/rust/pin-init/internal/src/init.rs
+++ b/rust/pin-init/internal/src/init.rs
@@ -361,8 +361,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) -> TokenStream {
-    let mut guards = vec![];
-    let mut guard_attrs = vec![];
+    let mut forget_guards = vec![];
     let mut res = TokenStream::new();
     for InitializerField { attrs, kind } in fields {
         let cfgs = {
@@ -474,17 +473,19 @@ fn init_fields(fields: &Punctuated<InitializerField, Token![,]>, pinned: bool) -
             #binding
         });
 
-        guards.push(guard);
-        guard_attrs.push(cfgs);
+        forget_guards.push(quote_spanned! { span =>
+            #(#cfgs)*
+            ::core::mem::forget(#guard);
+        });
     }
+
+    forget_guards.reverse();
+
     quote! {
         #res
         // If execution reaches this point, all fields have been initialized. Therefore we can now
         // dismiss the guards by forgetting them.
-        #(
-            #(#guard_attrs)*
-            ::core::mem::forget(#guards);
-        )*
+        #(#forget_guards)*
     }
 }
 

-- 
2.54.0


  reply	other threads:[~2026-10-06 15:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-06 15:29 [PATCH 0/8] rust: pin-init: various cleanups to prepare for self-reference Gary Guo
2026-10-06 15:29 ` Gary Guo [this message]
2026-10-06 15:29 ` [PATCH 2/8] rust: pin-init: internal: init: set span location of guards Gary Guo
2026-10-06 15:29 ` [PATCH 3/8] rust: pin-init: internal: add utility to handle generics merging Gary Guo
2026-10-06 15:29 ` [PATCH 4/8] rust: pin-init: internal: add utility for extract a single attribute Gary Guo
2026-10-06 15:29 ` [PATCH 5/8] rust: pin-init: internal: pin_data: move projection struct into `const _: ()` block Gary Guo
2026-10-06 15:29 ` [PATCH 6/8] rust: pin-init: internal: pin_data: extract `#[cfg]` processing to its own function Gary Guo
2026-10-06 15:29 ` [PATCH 7/8] rust: pin-init: internal: pin_data: rename `'__pin` lifetime to `'__this` Gary Guo
2026-10-06 15:29 ` [PATCH 8/8] rust: pin-init: internal: pin_data: parse into `StructInfo` before generating 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=20261006-dev-selfref-dep-v1-1-da716f740d6d@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®