Skip to content

internal: use loop {} to produce never value#151

Open
nbdd0121 wants to merge 1 commit intomainfrom
dev/panic
Open

internal: use loop {} to produce never value#151
nbdd0121 wants to merge 1 commit intomainfrom
dev/panic

Conversation

@nbdd0121
Copy link
Copy Markdown
Member

@nbdd0121 nbdd0121 commented May 8, 2026

In the init!/pin_init! macros, we rely on a trick that assigns never
(!) values to all mentioned fields in never-executed code to let the
compiler check that all fields have been initialized.

Currently we use ::core::panic!() to produce this value, but before Rust
1.91.0, it creates outlined panic_cold_explicit functions which do not
get removed by the optimizer, thus leaving dead code behind in the binary.
This has been fixed by [1], which lands in Rust 1.91.0+, higher than the
kernel minimum version 1.85.0.

This causes ~200 dead panic_cold_explicit instances being included in the
binary, with ~90 of them from nova-core's usage of pin-init.

Work around the issue by using loop {} which creates the never value
without macro expansion or function call at all. All instances of
panic_cold_explicit outside libcore are removed by this change in my
kernel build.

In the `init!`/`pin_init!` macros, we rely on a trick that assigns never
(`!`) values to all mentioned fields in never-executed code to let the
compiler check that all fields have been initialized.

Currently we use `::core::panic!()` to produce this value, but before Rust
1.91.0, it creates outlined `panic_cold_explicit` functions which do not
get removed by the optimizer, thus leaving dead code behind in the binary.
This has been fixed by [1], which lands in Rust 1.91.0+, higher than the
kernel minimum version 1.85.0.

This causes ~200 dead `panic_cold_explicit` instances being included in the
binary, with ~90 of them from nova-core's usage of pin-init.

Work around the issue by using `loop {}` which creates the never value
without macro expansion or function call at all. All instances of
`panic_cold_explicit` outside libcore are removed by this change in my
kernel build.

Link: rust-lang/rust#145304 [1]
Signed-off-by: Gary Guo <gary@garyguo.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant