Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

no-panic

> 编程语言
Open source

Attribute macro to require that the compiler prove a function can't ever panic

1.2K stars0 likes1 views
WebsiteGitHub

About

Attribute macro to require that the compiler prove a function can't ever panic

#[no_panic]

A Rust attribute macro to require that the compiler prove a function can't ever panic.

toml
[dependencies]
no-panic = "0.1"
rust
use no_panic::no_panic;

#[no_panic]
fn demo(s: &str) -> &str {
    &s[1..]
}

fn main() {
    println!("{}", demo("input string"));
}

If the function does panic (or the compiler fails to prove that the function cannot panic), the program fails to compile with a linker error that identifies the function name. Let's trigger that by passing a string that cannot be sliced at the first byte:

rust
fn main() {
    println!("{}", demo("\\u{1f980}input string"));
}
…

The error is not stellar but notice the ERROR[no-panic] part at the end that provides the name of the offending function.

Caveats

  • Functions that require some amount of optimization to prove that they do not panic may no longer compile in debug mode after being marked #[no_panic].

  • Panic detection happens at link time across the entire dependency graph, so any Cargo commands that do not invoke a linker will not trigger panic detection. This includes cargo build of library crates and cargo check of binary and library crates.

  • The attribute is useless in code built with panic = "abort". Code must be built with panic = "unwind" (the default) in order for any panics to be detected. After confirming absence of panics, you can of course still ship your software as a panic = "abort" build.

  • Const functions are not supported. The attribute will fail to compile if placed on a const fn.

If you find that code requires optimization to pass #[no_panic], either make no-panic an optional dependency that you only enable in release builds, or add a section like the following to your Cargo.toml or .cargo/config.toml to enable very basic optimization in debug builds.

toml
[profile.dev]
opt-level = 1

If the code that you need to prove isn't panicking makes function calls to non-generic non-inline functions from a different crate, you may need thin LTO enabled for the linker to deduce those do not panic.

toml
[profile.release]
lto = "thin"

If thin LTO isn't cutting it, the next thing to try would be fat LTO with a single codegen unit:

toml
[profile.release]
lto = "fat"
codegen-units = 1

If you want no_panic to just assume that some function you call doesn't panic, and get Undefined Behavior if it does at runtime, see dtolnay/no-panic#16; try wrapping that call in an unsafe extern "C" wrapper.

Acknowledgments

The linker error technique is based on Kixunil's crate dont_panic. Check out that crate for other convenient ways to require absence of panics.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言