@setRuntimeSafety

@setRuntimeSafety

@setRuntimeSafety(safety_on: bool)

Sets whether runtime safety checks are enabled for the scope that contains the function call.

test.zig

test "@setRuntimeSafety" {
    // The builtin applies to the scope that it is called in. So here, integer overflow
    // will not be caught in ReleaseFast and ReleaseSmall modes:
    // var x: u8 = 255;
    // x += 1; // undefined behavior in ReleaseFast/ReleaseSmall modes.
    {
        // However this block has safety enabled, so safety checks happen here,
        // even in ReleaseFast and ReleaseSmall modes.
        @setRuntimeSafety(true);
        var x: u8 = 255;
        x += 1;

        {
            // The value can be overridden at any scope. So here integer overflow
            // would not be caught in any build mode.
            @setRuntimeSafety(false);
            // var x: u8 = 255;
            // x += 1; // undefined behavior in all build modes.
        }
    }
}
``` ```
$ zig test test.zig --release-fast
1/1 test "@setRuntimeSafety"...integer overflow

Tests failed. Use the following command to reproduce the failure:
/home/andy/dev/zig/docgen_tmp/test

Note: it is planned to replace @setRuntimeSafety with @optimizeFor