Detecting overflow-checks

Heh, a kind of hacky way to do this, but which in practice ought to work quite often, is to try and perform an overflow operation within a build.rs script, and see if it panics. Emit a cfg based on that:

//! build.rs
fn main ()
{
    if ::std::panic::catch_unwind(|| {
        #[allow(arithmetic_overflow)]
        let _ = 255_u8 + 1;
    }).is_err()
    {
        // `cfg(overflow_checks)`
        println!("cargo:rustc-cfg=overflow_checks");
    } // else `not(cfg(overflow_checks))`
}
3 Likes