summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Guo <gary@garyguo.net>2026-10-06 17:29:31 +0200
committerGary Guo <gary@garyguo.net>2026-10-07 12:52:27 +0200
commit70ff1ccded06372e80acb5a67b710c3e979e3f4b (patch)
tree44e992e8ec3e15278948e963a65166808ffede8d
parent499b8f8a5d0401a6ab83b346a307f923640a024a (diff)
rust: pin-init: internal: init: forget guards in reverse order
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. Acked-by: Benno Lossin <lossin@kernel.org> Link: https://patch.msgid.link/20261006-dev-selfref-dep-v1-1-da716f740d6d@garyguo.net Signed-off-by: Gary Guo <gary@garyguo.net>
-rw-r--r--rust/pin-init/internal/src/init.rs17
1 files 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)*
}
}