authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-03 13:28:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-04 17:12:45-07:00
logcac7e5afc7624ef18fe7bc8c6acd743f8b5a7eaf
treeb918d0f139f66b74d95b98ded3fdc66a4c52b661
parent790b8428a26457e7ed9ea20485b9d3085011b989

add std.debug.assertReadable

Useful when trying to figure out whether a slice is valid memory.

1 files changed, 17 insertions(+), 7 deletions(-)

lib/std/debug.zig+17-7
......@@ -398,20 +398,30 @@ pub fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void {
398398 }
399399}
400400
401/// This function invokes undefined behavior when `ok` is `false`.
401/// Invokes detectable illegal behavior when `ok` is `false`.
402///
402403/// In Debug and ReleaseSafe modes, calls to this function are always
403404/// generated, and the `unreachable` statement triggers a panic.
404/// In ReleaseFast and ReleaseSmall modes, calls to this function are
405/// optimized away, and in fact the optimizer is able to use the assertion
406/// in its heuristics.
407/// Inside a test block, it is best to use the `std.testing` module rather
408/// than this function, because this function may not detect a test failure
409/// in ReleaseFast and ReleaseSmall mode. Outside of a test block, this assert
405///
406/// In ReleaseFast and ReleaseSmall modes, calls to this function are optimized
407/// away, and in fact the optimizer is able to use the assertion in its
408/// heuristics.
409///
410/// Inside a test block, it is best to use the `std.testing` module rather than
411/// this function, because this function may not detect a test failure in
412/// ReleaseFast and ReleaseSmall mode. Outside of a test block, this assert
410413/// function is the correct function to use.
411414pub fn assert(ok: bool) void {
412415 if (!ok) unreachable; // assertion failure
413416}
414417
418/// Invokes detectable illegal behavior when the provided slice is not mapped
419/// or lacks read permissions.
420pub fn assertReadable(slice: []const volatile u8) void {
421 if (!runtime_safety) return;
422 for (slice) |*byte| _ = byte.*;
423}
424
415425pub fn panic(comptime format: []const u8, args: anytype) noreturn {
416426 @setCold(true);
417427