#panic #static-assert #static-check #linker

no-std dont_panic

panic!()-like macro that causes linking error instead of panicking. May be used to statically ensure some code won't panic.

1 unstable release

Uses old Rust 2015

0.1.0 Jul 6, 2017

#6 in #static-assert


Used in dont_panic_slice

MITNFA license

4KB
69 lines

Don't panic!()

Ensure that code can't panic at compile time.

Example

This code will compile and (not) run just fine:

let should_panic = false;
if should_panic {
    dont_panic!("This will never execute.");
}

However, this code will cause linking error:

let should_panic = true;
if should_panic {
    dont_panic!("This will never execute.");
}

Caveats

  • This works only when appropriate opt_level is specified - it may require release build.
  • The error message is weird link error. You don't get line number, etc.
  • There may be situations in which you know that the code is unreachable but compiler can't prove it.

lib.rs:

This crate provides macros that look just like panic!() but instead of panicking, they cause linking error if their calls are not optimized-out. This can be used to ensure the compiler optimizes away some code.

Example

#[macro_use]
extern crate dont_panic;

fn main() {
/*
    let x = 6 * 9;
    if x == 42 {
        dont_panic!("6 * 9 == 42");
    }
*/
let x = false;
if x {
    dont_panic!("42");
}
}

Compile with --release or --features=panic

No runtime deps

Features