aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobin Voetter <robin@voetter.nl>2023-10-08 17:02:59 +0200
committerRobin Voetter <robin@voetter.nl>2023-10-15 14:00:08 +0200
commitdc44baf763f38ba3735d45fc4f633cac13949b0e (patch)
tree94017201182ca6009fd1d43a89ba711767f3eee6 /lib
parent839a93a101804bb41cb519a965dc62e3eb7deed0 (diff)
downloadzig-dc44baf763f38ba3735d45fc4f633cac13949b0e.tar.gz
zig-dc44baf763f38ba3735d45fc4f633cac13949b0e.zip
std.testing: allow print() at comptime
This allows functions like expectEqual to be performed at comptime. If an error is detected, the result is logged via a compile error.
Diffstat (limited to 'lib')
-rw-r--r--lib/std/testing.zig10
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 4dda8a0d0d..d4d162721d 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -22,10 +22,14 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
pub var log_level = std.log.Level.warn;
fn print(comptime fmt: []const u8, args: anytype) void {
- // Disable printing in tests for simple backends.
- if (builtin.zig_backend == .stage2_spirv64) return;
+ if (@inComptime()) {
+ @compileError(std.fmt.comptimePrint(fmt, args));
+ } else {
+ // Disable printing in tests for simple backends.
+ if (builtin.zig_backend == .stage2_spirv64) return;
- std.debug.print(fmt, args);
+ std.debug.print(fmt, args);
+ }
}
/// This function is intended to be used only in tests. It prints diagnostics to stderr