dtolnay / no-panic

Attribute macro to require that the compiler prove a function can't ever panic
Apache License 2.0
1k stars 13 forks source link

panic for no panic function #4

Closed leonjia0112 closed 5 years ago

leonjia0112 commented 5 years ago

My rustc version is 1.31.0. I tried the example in the doc but it still panic with the linker error.

Even this code would have a linker error

extern crate no_panic;
no_panic::no_panic;
#[no_panic]
fn main() {
}

Is something wrong with my usage or the crate itself?

dtolnay commented 5 years ago

Try building it in release mode or with:

[profile.dev]
opt-level = 1

Binary crates typically require some optimization to eliminate panic codepaths (while library crates often do not).

leonjia0112 commented 5 years ago

Thanks for your info. Good to know the panic difference between bin and lib. 👍 Are you going to update this crate to compatible with regular cargo build for the binary crate in the future?

dtolnay commented 5 years ago

This can't be changed in this crate. The compiler is what performs the relevant optimization. It's not clear that we want the compiler to perform any additional optimization when building with opt-level = 0. I believe setting opt-level = 1 is the correct solution. Will add a note to the documentation.

leonjia0112 commented 5 years ago

You are right. Setting the optimization level would solve this problem. Thanks.