liveBook · Manning

No-panic

As explained in Write Powerful Rust Macros

no-panic is a Rust procedural macro library that statically verifies that annotated functions cannot panic.

no-panic is a Rust procedural attribute macro that makes the compiler prove an annotated function cannot panic. It targets code where panic freedom matters more than ordinary runtime testing. section 4.8

Turning possible panics into build failures

The technique relies on Rust’s cleanup rules. A generated sentinel type implements Drop, calls a deliberately missing C symbol from drop, and is then forgotten. If the annotated function might panic, unwinding can invoke Drop, preserving the reference and producing a linker error. If panic is impossible, the compiler can eliminate the unused Drop path. section 4.8

How Write Powerful Rust Macros covers it

How deep it goes
Explains the Drop, forget, optimization, and linking mechanism without detailing the complete expansion.
Examples
Uses a Dropper that panics before mem::forget and a generated sentinel calling a nonexistent C function.
What it assumes
Assumes Rust knowledge of procedural macros, Drop, panic unwinding, mem::forget, and linking.
What you can do after
Apply it via the no-panic procedural macro to detect possible panics during builds.
How it connects
Connects procedural macros with panic unwinding, the Drop trait, compiler optimization, and linker errors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.