Skip to content

Commit b9b88be

Browse files
y86-devojeda
authored andcommitted
rust: init: wrap type checking struct initializers in a closure
In the implementation of the init macros there is a `if false` statement that type checks the initializer to ensure every field is initialized. Since the next patch has a stack variable to store the struct, the function might allocate too much memory on debug builds. Putting the struct into a closure that is never executed ensures that even in debug builds no stack overflow error is caused. In release builds this was not a problem since the code was optimized away due to the `if false`. Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Gary Guo <[email protected]> Signed-off-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 97de919 commit b9b88be

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

rust/kernel/init/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1040,14 +1040,14 @@ macro_rules! __init_internal {
10401040
// once, this struct initializer will still be type-checked and complain with a
10411041
// very natural error message if a field is forgotten/mentioned more than once.
10421042
#[allow(unreachable_code, clippy::diverging_sub_expression)]
1043-
if false {
1043+
let _ = || {
10441044
$crate::__init_internal!(make_initializer:
10451045
@slot(slot),
10461046
@type_name($t),
10471047
@munch_fields($($fields)*,),
10481048
@acc(),
10491049
);
1050-
}
1050+
};
10511051
}
10521052
Ok(__InitOk)
10531053
}
@@ -1168,8 +1168,8 @@ macro_rules! __init_internal {
11681168
@acc($($acc:tt)*),
11691169
) => {
11701170
// Endpoint, nothing more to munch, create the initializer.
1171-
// Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
1172-
// get the correct type inference here:
1171+
// Since we are in the closure that is never called, this will never get executed.
1172+
// We abuse `slot` to get the correct type inference here:
11731173
unsafe {
11741174
::core::ptr::write($slot, $t {
11751175
$($acc)*

0 commit comments

Comments
 (0)