Skip to content

Commit 4bee8a6

Browse files
BennoLossinhoshinolina
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 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`. Signed-off-by: Benno Lossin <[email protected]>
1 parent c51e1f1 commit 4bee8a6

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

rust/kernel/init.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,16 @@ macro_rules! try_pin_init {
641641
// We use unreachable code to ensure that all fields have been mentioned exactly
642642
// once, this struct initializer will still be type-checked and complain with a
643643
// very natural error message if a field is forgotten/mentioned more than once.
644-
#[allow(unreachable_code, clippy::diverging_sub_expression)]
644+
#[allow(unreachable_code, clippy::diverging_sub_expression, clippy::redundant_closure_call)]
645645
if false {
646-
$crate::try_pin_init!(make_initializer:
647-
@slot(slot),
648-
@type_name($t),
649-
@munch_fields($($fields)*,),
650-
@acc(),
651-
);
646+
(|| {
647+
$crate::try_pin_init!(make_initializer:
648+
@slot(slot),
649+
@type_name($t),
650+
@munch_fields($($fields)*,),
651+
@acc(),
652+
);
653+
})();
652654
}
653655
}
654656
Ok(__InitOk)
@@ -889,14 +891,16 @@ macro_rules! try_init {
889891
// We use unreachable code to ensure that all fields have been mentioned exactly
890892
// once, this struct initializer will still be type-checked and complain with a
891893
// very natural error message if a field is forgotten/mentioned more than once.
892-
#[allow(unreachable_code, clippy::diverging_sub_expression)]
894+
#[allow(unreachable_code, clippy::diverging_sub_expression, clippy::redundant_closure_call)]
893895
if false {
894-
$crate::try_init!(make_initializer:
895-
@slot(slot),
896-
@type_name($t),
897-
@munch_fields($($fields)*,),
898-
@acc(),
899-
);
896+
(|| {
897+
$crate::try_init!(make_initializer:
898+
@slot(slot),
899+
@type_name($t),
900+
@munch_fields($($fields)*,),
901+
@acc(),
902+
);
903+
})();
900904
}
901905
}
902906
Ok(__InitOk)

0 commit comments

Comments
 (0)