Skip to content

#[panic_handler] does not work with std. #59222

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

Open
lachlansneff opened this issue Mar 15, 2019 · 6 comments
Open

#[panic_handler] does not work with std. #59222

lachlansneff opened this issue Mar 15, 2019 · 6 comments
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@lachlansneff
Copy link

The #[panic_handler] attribute does not work when using the std library. Thus, there's no way for libraries to register custom panic handlers without resorting to a lazy_static or similar global initializer.

This seems like quite an oversight.

@Mark-Simulacrum
Copy link
Member

cc @japaric -- I presume this is intentional, or at least a known limitation?

@Mark-Simulacrum Mark-Simulacrum added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 15, 2019
@japaric
Copy link
Member

japaric commented Mar 16, 2019

panic_handler was designed for no_std use. Programs that link to std already had an stable API to set a panic handler (e.g. std::panic::set_hook) when panic_handler was designed.

Besides, I'm not sure how you could get panic_handler to work with non-no_std crates. Let's say we add a Cargo feature to std to remove its panic_handler; what happens to the stable std::panic::set_hook API? Should it be cfg-ed away causing compiler errors on any existing crate that uses it? Should it become a no-op changing the semantics of existing crates that use it? Both options are breaking changes. Whether a breaking change like that is OK or not is a question for the libs team.

(Also, you don't want to put panic_handler in general-purpose libraries since you can only link one instance of panic_handler into an application; doing so would make these libraries non-composable, i.e. you'll get a compiler error if you try to use them together. The application top crate is the only place where you should use / select the panic_handler)

@lachlansneff
Copy link
Author

Would crate-specific panic handlers be a possible solution to this?

@Centril
Copy link
Contributor

Centril commented Mar 16, 2019

@japaric

Should it become a no-op changing the semantics of existing crates that use it? Both options are breaking changes. Whether a breaking change like that is OK or not is a question for the libs team.

Any non-#[no_std] crate that annotates a function with #[panic_handler] will get errors about:

error[E0152]: duplicate lang item found: `panic_impl`.

Therefore we get to define the semantics if we want to. A reasonable way to do that would be similar to #[global_allocator], that is, the std crate will add a #[panic_handler] if none is specified by the user.

As for set_hook, it could simply abort if other solutions are not sensible. It's not a breaking change to the actual application that ends up running this since no such applications have hitherto existed.

The application top crate is the only place where you should use / select the panic_handler

I think it would be sensible to have crates for the express purpose of redefining panic_handler and nothing else.

@ghost
Copy link

ghost commented Jun 13, 2019

I would like to see this too.

Using crates like color-backtrace is cumbersome because one has to put color_backtrace::install(); into the main function every time. Setting up tests is especially difficult because there is no main function.

I wish it was enough to just add the crate as a dependency.

@bjorn3
Copy link
Member

bjorn3 commented Dec 23, 2020

The "#[panic_handler]" of libstd does more than just print a panic message and backtrace. It is also responsible for starting unwinding, which can't be done on stable in any other way. Because of this overriding #[panic_handler] is not useful for color-backtrace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants