Code
I tried this code:
#![no_std]
// Yes, this is `no_std` + `extern crate std`, which happens a lot in
// crates that are `cfg(not(something), no_std)`
extern crate std;
extern crate alloc;
use alloc::boxed::Box;
fn main() {
std::panic::set_hook(Box::new(panic_handler));
}
fn panic_handler(info: &core::panic::PanicInfo) {
}
playground
I expected to see this happen: It compiles just fine
Instead, this happened: I get the following error:
error[E0631]: type mismatch in function arguments
--> src/main.rs:9:26
|
9 | std::panic::set_hook(Box::new(panic_handler));
| ^^^^^^^^^^^^^^^^^^^^^^^ expected due to this
...
12 | fn panic_handler(info: &core::panic::PanicInfo) {
| ----------------------------------------------- found signature defined here
|
= note: expected function signature `for<'a, 'b> fn(&'a PanicHookInfo<'b>) -> _`
found function signature `fn(&PanicInfo<'_>) -> _`
= note: required for the cast from `Box<for<'a, 'b> fn(&'a PanicInfo<'b>) {panic_handler}>` to `Box<(dyn for<'a, 'b> Fn(&'a PanicHookInfo<'b>) + Send + Sync + 'static)>`
On nightly PanicInfo is a type alias for PanicHookInfo, however the alias only exists in the std crate. If you reference PanicInfo from core, that is now a different type that does not unify with PanicHookInfo.
It is relatively common in crates that support no_std to always use the lowest possible crate to reference for any reexported type, which is why our code was referencing core::panic even though this particular snippet of code needs std to work overall.
Version it worked on
It most recently worked on: Rust 1.79, Rust 1.80-beta3
Version with regression
rustc --version --verbose:
1.81.0-nightly (2024-06-19 d8a38b00024cd7156dea)
Code
I tried this code:
playground
I expected to see this happen: It compiles just fine
Instead, this happened: I get the following error:
On nightly
PanicInfois a type alias forPanicHookInfo, however the alias only exists in thestdcrate. If you referencePanicInfofromcore, that is now a different type that does not unify withPanicHookInfo.It is relatively common in crates that support
no_stdto always use the lowest possible crate to reference for any reexported type, which is why our code was referencingcore::paniceven though this particular snippet of code needsstdto work overall.Version it worked on
It most recently worked on: Rust 1.79, Rust 1.80-beta3
Version with regression
rustc --version --verbose: