Skip to content

Commit 8156382

Browse files
gregkhopsiff
authored andcommitted
Revert "rust: pin-init: add references to previously initialized fields"
This reverts commit c28fc9b0dbc7ae5426689cde4f47c5e32dbf80fc which is commit 42415d1 upstream. It breaks the build at this time. Link: https://lore.kernel.org/r/20260402112712.110869-1-ojeda@kernel.org Reported-by: Miguel Ojeda <ojeda@kernel.org> Cc: Benno Lossin <lossin@kernel.org> Cc: Gary Guo <gary@garyguo.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit 866c39b567bded79b30345a07ef02c4c05473608) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 597b93b commit 8156382

1 file changed

Lines changed: 31 additions & 113 deletions

File tree

rust/kernel/init/macros.rs

Lines changed: 31 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -964,54 +964,35 @@ macro_rules! __pin_data {
964964
@pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
965965
@not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
966966
) => {
967-
$crate::macros::paste! {
968-
// For every field, we create a projection function according to its projection type. If a
969-
// field is structurally pinned, then it must be initialized via `PinInit`, if it is not
970-
// structurally pinned, then it can be initialized via `Init`.
971-
//
972-
// The functions are `unsafe` to prevent accidentally calling them.
973-
#[allow(dead_code, non_snake_case)]
974-
#[expect(clippy::missing_safety_doc)]
975-
impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
976-
where $($whr)*
977-
{
978-
$(
979-
$(#[$($p_attr)*])*
980-
$pvis unsafe fn $p_field<E>(
981-
self,
982-
slot: *mut $p_type,
983-
init: impl $crate::init::PinInit<$p_type, E>,
984-
) -> ::core::result::Result<(), E> {
985-
unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
986-
}
987-
988-
$(#[$($p_attr)*])*
989-
$pvis unsafe fn [<__project_ $p_field>]<'__slot>(
990-
self,
991-
slot: &'__slot mut $p_type,
992-
) -> ::core::pin::Pin<&'__slot mut $p_type> {
993-
unsafe { ::core::pin::Pin::new_unchecked(slot) }
994-
}
995-
)*
996-
$(
997-
$(#[$($attr)*])*
998-
$fvis unsafe fn $field<E>(
999-
self,
1000-
slot: *mut $type,
1001-
init: impl $crate::init::Init<$type, E>,
1002-
) -> ::core::result::Result<(), E> {
1003-
unsafe { $crate::init::Init::__init(init, slot) }
1004-
}
1005-
1006-
$(#[$($attr)*])*
1007-
$fvis unsafe fn [<__project_ $field>]<'__slot>(
1008-
self,
1009-
slot: &'__slot mut $type,
1010-
) -> &'__slot mut $type {
1011-
slot
1012-
}
1013-
)*
1014-
}
967+
// For every field, we create a projection function according to its projection type. If a
968+
// field is structurally pinned, then it must be initialized via `PinInit`, if it is not
969+
// structurally pinned, then it can be initialized via `Init`.
970+
//
971+
// The functions are `unsafe` to prevent accidentally calling them.
972+
#[allow(dead_code)]
973+
impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
974+
where $($whr)*
975+
{
976+
$(
977+
$(#[$($p_attr)*])*
978+
$pvis unsafe fn $p_field<E>(
979+
self,
980+
slot: *mut $p_type,
981+
init: impl $crate::init::PinInit<$p_type, E>,
982+
) -> ::core::result::Result<(), E> {
983+
unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
984+
}
985+
)*
986+
$(
987+
$(#[$($attr)*])*
988+
$fvis unsafe fn $field<E>(
989+
self,
990+
slot: *mut $type,
991+
init: impl $crate::init::Init<$type, E>,
992+
) -> ::core::result::Result<(), E> {
993+
unsafe { $crate::init::Init::__init(init, slot) }
994+
}
995+
)*
1015996
}
1016997
};
1017998
}
@@ -1205,13 +1186,6 @@ macro_rules! __init_internal {
12051186
// return when an error/panic occurs.
12061187
// We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
12071188
unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
1208-
// SAFETY:
1209-
// - the project function does the correct field projection,
1210-
// - the field has been initialized,
1211-
// - the reference is only valid until the end of the initializer.
1212-
#[allow(unused_variables, unused_assignments)]
1213-
let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
1214-
12151189
// Create the drop guard:
12161190
//
12171191
// We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1243,14 +1217,6 @@ macro_rules! __init_internal {
12431217
// SAFETY: `slot` is valid, because we are inside of an initializer closure, we
12441218
// return when an error/panic occurs.
12451219
unsafe { $crate::init::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
1246-
1247-
// SAFETY:
1248-
// - the field is not structurally pinned, since the line above must compile,
1249-
// - the field has been initialized,
1250-
// - the reference is only valid until the end of the initializer.
1251-
#[allow(unused_variables, unused_assignments)]
1252-
let $field = unsafe { &mut (*$slot).$field };
1253-
12541220
// Create the drop guard:
12551221
//
12561222
// We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1269,7 +1235,7 @@ macro_rules! __init_internal {
12691235
);
12701236
}
12711237
};
1272-
(init_slot(): // No `use_data`, so all fields are not structurally pinned
1238+
(init_slot($($use_data:ident)?):
12731239
@data($data:ident),
12741240
@slot($slot:ident),
12751241
@guards($($guards:ident,)*),
@@ -1283,15 +1249,6 @@ macro_rules! __init_internal {
12831249
// SAFETY: The memory at `slot` is uninitialized.
12841250
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
12851251
}
1286-
1287-
#[allow(unused_variables, unused_assignments)]
1288-
// SAFETY:
1289-
// - the field is not structurally pinned, since no `use_data` was required to create this
1290-
// initializer,
1291-
// - the field has been initialized,
1292-
// - the reference is only valid until the end of the initializer.
1293-
let $field = unsafe { &mut (*$slot).$field };
1294-
12951252
// Create the drop guard:
12961253
//
12971254
// We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1302,46 +1259,7 @@ macro_rules! __init_internal {
13021259
$crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
13031260
};
13041261

1305-
$crate::__init_internal!(init_slot():
1306-
@data($data),
1307-
@slot($slot),
1308-
@guards([< __ $field _guard >], $($guards,)*),
1309-
@munch_fields($($rest)*),
1310-
);
1311-
}
1312-
};
1313-
(init_slot($use_data:ident):
1314-
@data($data:ident),
1315-
@slot($slot:ident),
1316-
@guards($($guards:ident,)*),
1317-
// Init by-value.
1318-
@munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
1319-
) => {
1320-
{
1321-
$(let $field = $val;)?
1322-
// Initialize the field.
1323-
//
1324-
// SAFETY: The memory at `slot` is uninitialized.
1325-
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
1326-
}
1327-
// SAFETY:
1328-
// - the project function does the correct field projection,
1329-
// - the field has been initialized,
1330-
// - the reference is only valid until the end of the initializer.
1331-
#[allow(unused_variables, unused_assignments)]
1332-
let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
1333-
1334-
// Create the drop guard:
1335-
//
1336-
// We rely on macro hygiene to make it impossible for users to access this local variable.
1337-
// We use `paste!` to create new hygiene for `$field`.
1338-
$crate::macros::paste! {
1339-
// SAFETY: We forget the guard later when initialization has succeeded.
1340-
let [< __ $field _guard >] = unsafe {
1341-
$crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1342-
};
1343-
1344-
$crate::__init_internal!(init_slot($use_data):
1262+
$crate::__init_internal!(init_slot($($use_data)?):
13451263
@data($data),
13461264
@slot($slot),
13471265
@guards([<$field>], $($guards,)*),

0 commit comments

Comments
 (0)