Skip to content

Commit 9218cf8

Browse files
BennoLossinojeda
authored andcommitted
rust: init: change the generated name of guard variables
The initializers created by the `[try_][pin_]init!` macros utilize the guard pattern to drop already initialized fields, when initialization fails mid-way. These guards are generated to have the same name as the field that they handle. To prevent namespacing issues [1] when the field name is the same as e.g. a constant name, add `__` as a prefix and `_guard` as the suffix. [ Gary says: "Here's the simplified example: ``` macro_rules! f { () => { let a = 1; let _: u32 = a; } } const a: u64 = 1; fn main() { f!(); } ``` The `a` in `f` have a different hygiene so normally it is scoped to the macro expansion and wouldn't escape. Interestingly a constant is still preferred despite the hygiene so constants escaped into the macro, leading to the error." - Miguel ] Signed-off-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/rust-for-linux/1e8a2a1f-abbf-44ba-8344-705a9cbb1627@proton.me/ [1] Link: https://lore.kernel.org/r/20240403194321.88716-1-benno.lossin@proton.me [ Added Benno's link and Gary's simplified example. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent a0a4e17 commit 9218cf8

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

rust/kernel/init/macros.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
//! // error type is `Infallible`) we will need to drop this field if there
251251
//! // is an error later. This `DropGuard` will drop the field when it gets
252252
//! // dropped and has not yet been forgotten.
253-
//! let t = unsafe {
253+
//! let __t_guard = unsafe {
254254
//! ::pinned_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).t))
255255
//! };
256256
//! // Expansion of `x: 0,`:
@@ -261,14 +261,14 @@
261261
//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).x), x) };
262262
//! }
263263
//! // We again create a `DropGuard`.
264-
//! let x = unsafe {
264+
//! let __x_guard = unsafe {
265265
//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).x))
266266
//! };
267267
//! // Since initialization has successfully completed, we can now forget
268268
//! // the guards. This is not `mem::forget`, since we only have
269269
//! // `&DropGuard`.
270-
//! ::core::mem::forget(x);
271-
//! ::core::mem::forget(t);
270+
//! ::core::mem::forget(__x_guard);
271+
//! ::core::mem::forget(__t_guard);
272272
//! // Here we use the type checker to ensure that every field has been
273273
//! // initialized exactly once, since this is `if false` it will never get
274274
//! // executed, but still type-checked.
@@ -461,16 +461,16 @@
461461
//! {
462462
//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).a), a) };
463463
//! }
464-
//! let a = unsafe {
464+
//! let __a_guard = unsafe {
465465
//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).a))
466466
//! };
467467
//! let init = Bar::new(36);
468468
//! unsafe { data.b(::core::addr_of_mut!((*slot).b), b)? };
469-
//! let b = unsafe {
469+
//! let __b_guard = unsafe {
470470
//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).b))
471471
//! };
472-
//! ::core::mem::forget(b);
473-
//! ::core::mem::forget(a);
472+
//! ::core::mem::forget(__b_guard);
473+
//! ::core::mem::forget(__a_guard);
474474
//! #[allow(unreachable_code, clippy::diverging_sub_expression)]
475475
//! let _ = || {
476476
//! unsafe {
@@ -1209,14 +1209,14 @@ macro_rules! __init_internal {
12091209
// We use `paste!` to create new hygiene for `$field`.
12101210
::kernel::macros::paste! {
12111211
// SAFETY: We forget the guard later when initialization has succeeded.
1212-
let [<$field>] = unsafe {
1212+
let [< __ $field _guard >] = unsafe {
12131213
$crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
12141214
};
12151215

12161216
$crate::__init_internal!(init_slot($use_data):
12171217
@data($data),
12181218
@slot($slot),
1219-
@guards([<$field>], $($guards,)*),
1219+
@guards([< __ $field _guard >], $($guards,)*),
12201220
@munch_fields($($rest)*),
12211221
);
12221222
}
@@ -1240,14 +1240,14 @@ macro_rules! __init_internal {
12401240
// We use `paste!` to create new hygiene for `$field`.
12411241
::kernel::macros::paste! {
12421242
// SAFETY: We forget the guard later when initialization has succeeded.
1243-
let [<$field>] = unsafe {
1243+
let [< __ $field _guard >] = unsafe {
12441244
$crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
12451245
};
12461246

12471247
$crate::__init_internal!(init_slot():
12481248
@data($data),
12491249
@slot($slot),
1250-
@guards([<$field>], $($guards,)*),
1250+
@guards([< __ $field _guard >], $($guards,)*),
12511251
@munch_fields($($rest)*),
12521252
);
12531253
}
@@ -1272,14 +1272,14 @@ macro_rules! __init_internal {
12721272
// We use `paste!` to create new hygiene for `$field`.
12731273
::kernel::macros::paste! {
12741274
// SAFETY: We forget the guard later when initialization has succeeded.
1275-
let [<$field>] = unsafe {
1275+
let [< __ $field _guard >] = unsafe {
12761276
$crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
12771277
};
12781278

12791279
$crate::__init_internal!(init_slot($($use_data)?):
12801280
@data($data),
12811281
@slot($slot),
1282-
@guards([<$field>], $($guards,)*),
1282+
@guards([< __ $field _guard >], $($guards,)*),
12831283
@munch_fields($($rest)*),
12841284
);
12851285
}

0 commit comments

Comments
 (0)