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 <yakoyoku@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20230814084602.25699-6-benno.lossin@proton.me Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
Родитель
97de919d57
Коммит
b9b88be046
|
@ -1040,14 +1040,14 @@ macro_rules! __init_internal {
|
||||||
// once, this struct initializer will still be type-checked and complain with a
|
// once, this struct initializer will still be type-checked and complain with a
|
||||||
// very natural error message if a field is forgotten/mentioned more than once.
|
// very natural error message if a field is forgotten/mentioned more than once.
|
||||||
#[allow(unreachable_code, clippy::diverging_sub_expression)]
|
#[allow(unreachable_code, clippy::diverging_sub_expression)]
|
||||||
if false {
|
let _ = || {
|
||||||
$crate::__init_internal!(make_initializer:
|
$crate::__init_internal!(make_initializer:
|
||||||
@slot(slot),
|
@slot(slot),
|
||||||
@type_name($t),
|
@type_name($t),
|
||||||
@munch_fields($($fields)*,),
|
@munch_fields($($fields)*,),
|
||||||
@acc(),
|
@acc(),
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Ok(__InitOk)
|
Ok(__InitOk)
|
||||||
}
|
}
|
||||||
|
@ -1168,8 +1168,8 @@ macro_rules! __init_internal {
|
||||||
@acc($($acc:tt)*),
|
@acc($($acc:tt)*),
|
||||||
) => {
|
) => {
|
||||||
// Endpoint, nothing more to munch, create the initializer.
|
// Endpoint, nothing more to munch, create the initializer.
|
||||||
// Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
|
// Since we are in the closure that is never called, this will never get executed.
|
||||||
// get the correct type inference here:
|
// We abuse `slot` to get the correct type inference here:
|
||||||
unsafe {
|
unsafe {
|
||||||
::core::ptr::write($slot, $t {
|
::core::ptr::write($slot, $t {
|
||||||
$($acc)*
|
$($acc)*
|
||||||
|
|
Загрузка…
Ссылка в новой задаче