Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic_handler isn't just for no_std applications #373

Open
grahamc opened this issue Jul 9, 2022 · 2 comments
Open

panic_handler isn't just for no_std applications #373

grahamc opened this issue Jul 9, 2022 · 2 comments

Comments

@grahamc
Copy link

grahamc commented Jul 9, 2022

The documentation on https://doc.rust-lang.org/nomicon/panic-handler.html suggests that specifying panic handlers is just for no_std applications:

#[panic_handler] is used to define the behavior of panic! in #![no_std] applications.

However it may also be used for other applications too, like this Play link:

https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Apanic%3B%0A%0Apanic%3A%3Aset_hook(Box%3A%3Anew(%7Cpanic_info%7C%20%7B%0A%20%20%20%20if%20let%20Some(s)%20%3D%20panic_info.payload().downcast_ref%3A%3A%3C%26str%3E()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22panic%20occurred%3A%20%7Bs%3A%3F%7D%22)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20println!(%22panic%20occurred%22)%3B%0A%20%20%20%20%7D%0A%7D))%3B%0A%0Apanic!(%22Normal%20panic%22)%3B%0A%7D&edition=2021

This was quite confusing for me because the nomicon was the first resource in DDG for "rust panic handler", which suggested that indeed I was wading into spooky waters:

image

I'm not sure the best way to rephrase it, but I think it shouldn't suggest that it is only for no_std.

@ehuss
Copy link
Contributor

ehuss commented Jul 9, 2022

Maybe I'm a bit confused by where the concern is. Looking at your playground link, I don't see the use of #[panic_handler]. As mentioned, there must be only one #[panic_handler], and there is already one defined in the standard library. Thus, it is only relevant to define a #[panic_handler] in a no_std environment.

Perhaps it could be reworded to say:

The #[panic_handler] attribute is used to define the behavior of panic!. #[panic_handler] must be applied to a function with the signature fn(&PanicInfo) -> !. The core::panic::PanicInfo struct contains information about the location of the panic. There can be only one panic handler in the dependency graph of a binary / dylib / cdylib crate. Defining your own #[panic_handler] is only relevant for no_std development since std defines its own panic handler. If you need to customize panic behavior with a std application, then consider using std::panic::set_hook.

Does that help clarify?

@grahamc
Copy link
Author

grahamc commented Jul 10, 2022

Dang, thanks! Double misunderstanding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants