1\#\[no\_panic\] 2=============== 3 4[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/no--panic-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/no-panic) 5[<img alt="crates.io" src="https://img.shields.io/crates/v/no-panic.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/no-panic) 6[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-no--panic-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/no-panic) 7[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/no-panic/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/no-panic/actions?query=branch%3Amaster) 8 9A Rust attribute macro to require that the compiler prove a function can't ever 10panic. 11 12```toml 13[dependencies] 14no-panic = "0.1" 15``` 16 17```rust 18use no_panic::no_panic; 19 20#[no_panic] 21fn demo(s: &str) -> &str { 22 &s[1..] 23} 24 25fn main() { 26 println!("{}", demo("input string")); 27} 28``` 29 30If the function does panic (or the compiler fails to prove that the function 31cannot panic), the program fails to compile with a linker error that identifies 32the function name. Let's trigger that by passing a string that cannot be sliced 33at the first byte: 34 35```rust 36fn main() { 37 println!("{}", demo("\u{1f980}input string")); 38} 39``` 40 41```console 42 Compiling no-panic-demo v0.0.1 43error: linking with `cc` failed: exit code: 1 44 | 45 = note: /no-panic-demo/target/release/deps/no_panic_demo-7170785b672ae322.no_p 46anic_demo1-cba7f4b666ccdbcbbf02b7348e5df1b2.rs.rcgu.o: In function `_$LT$no_pani 47c_demo..demo..__NoPanic$u20$as$u20$core..ops..drop..Drop$GT$::drop::h72f8f423002 48b8d9f': 49 no_panic_demo1-cba7f4b666ccdbcbbf02b7348e5df1b2.rs:(.text._ZN72_$LT$no 50_panic_demo..demo..__NoPanic$u20$as$u20$core..ops..drop..Drop$GT$4drop17h72f8f42 513002b8d9fE+0x2): undefined reference to ` 52 53 ERROR[no-panic]: detected panic in function `demo` 54 ' 55 collect2: error: ld returned 1 exit status 56``` 57 58The error is not stellar but notice the ERROR\[no-panic\] part at the end that 59provides the name of the offending function. 60 61<br> 62 63### Caveats 64 65- Functions that require some amount of optimization to prove that they do not 66 panic may no longer compile in debug mode after being marked `#[no_panic]`. 67 68- Panic detection happens at link time across the entire dependency graph, so 69 any Cargo commands that do not invoke a linker will not trigger panic 70 detection. This includes `cargo build` of library crates and `cargo check` of 71 binary and library crates. 72 73- The attribute is useless in code built with `panic = "abort"`. 74 75If you find that code requires optimization to pass `#[no_panic]`, either make 76no-panic an optional dependency that you only enable in release builds, or add a 77section like the following to Cargo.toml to enable very basic optimization in 78debug builds. 79 80```toml 81[profile.dev] 82opt-level = 1 83``` 84 85If the code that you need to prove isn't panicking makes function calls to 86non-generic non-inline functions from a different crate, you may need thin LTO 87enabled for the linker to deduce those do not panic. 88 89```toml 90[profile.release] 91lto = "thin" 92``` 93 94If you want no\_panic to just assume that some function you call doesn't panic, 95and get Undefined Behavior if it does at runtime, see [dtolnay/no-panic#16]; try 96wrapping that call in an `unsafe extern "C"` wrapper. 97 98[dtolnay/no-panic#16]: https://github.com/dtolnay/no-panic/issues/16 99 100<br> 101 102### Acknowledgments 103 104The linker error technique is based on [Kixunil]'s crate [`dont_panic`]. Check 105out that crate for other convenient ways to require absence of panics. 106 107[Kixunil]: https://github.com/Kixunil 108[`dont_panic`]: https://github.com/Kixunil/dont_panic 109 110<br> 111 112#### License 113 114<sup> 115Licensed under either of <a href="LICENSE-APACHE">Apache License, Version 1162.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. 117</sup> 118 119<br> 120 121<sub> 122Unless you explicitly state otherwise, any contribution intentionally submitted 123for inclusion in this crate by you, as defined in the Apache-2.0 license, shall 124be dual licensed as above, without any additional terms or conditions. 125</sub> 126