Why does this fail?
const timestamp = comptime std.time.timestamp();
Here’s the message it gives me:
$ zig build run
run
└─ run comptime_cross_compile
└─ install
└─ install comptime_cross_compile
└─ zig build-exe comptime_cross_compile Debug native-native 1 errors
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/os/linux.zig:1507:21: error: unable to evaluate comptime expression
const ptr = @atomicLoad(?VdsoClockGettime, &vdso_clock_gettime, .unordered);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/os/linux.zig:1507:52: note: operation is runtime due to this operand
const ptr = @atomicLoad(?VdsoClockGettime, &vdso_clock_gettime, .unordered);
^~~~~~~~~~~~~~~~~~~
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/posix.zig:5721:39: note: called at comptime from here
switch (errno(system.clock_gettime(clock_id, &tp))) {
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/time.zig:68:43: note: called at comptime from here
const ts = posix.clock_gettime(.REALTIME) catch |err| switch (err) {
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/time.zig:29:53: note: called at comptime from here
return @as(i64, @intCast(@divFloor(nanoTimestamp(), ns_per_ms)));
~~~~~~~~~~~~~^~
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/std/time.zig:20:36: note: called at comptime from here
return @divFloor(milliTimestamp(), ms_per_s);
~~~~~~~~~~~~~~^~
src/main.zig:18:43: note: called at comptime from here
const timestamp = comptime std.time.timestamp();
~~~~~~~~~~~~~~~~~~^~
src/main.zig:18:16: note: 'comptime' keyword forces comptime evaluation
const timestamp = comptime std.time.timestamp();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: the following command failed with 1 compilation errors:
/gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/bin/zig build-exe -ODebug -target native-native -mcpu baseline --dep comptime_cross_compile_lib -Mroot=/mnt/store/Projects/Zig language/comptime-cross-compile/src/main.zig -ODebug -target native-native -mcpu baseline -Mcomptime_cross_compile_lib=/mnt/store/Projects/Zig language/comptime-cross-compile/src/root.zig --cache-dir /mnt/store/Projects/Zig language/comptime-cross-compile/.zig-cache --global-cache-dir /home/dan/.cache/zig --name comptime_cross_compile --zig-lib-dir /gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig/ --listen=-
Build Summary: 2/7 steps succeeded; 1 failed
run transitive failure
└─ run comptime_cross_compile transitive failure
├─ zig build-exe comptime_cross_compile Debug native-native 1 errors
└─ install transitive failure
└─ install comptime_cross_compile transitive failure
└─ zig build-exe comptime_cross_compile Debug native-native (reused)
error: the following build command failed with exit code 1:
/mnt/store/Projects/Zig language/comptime-cross-compile/.zig-cache/o/4b7643883ee70484ee4b79392b35c6d9/build /gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/bin/zig /gnu/store/76f996h4854np3g8i0n97hhi5nl1dp67-zig-0.14.0/lib/zig /mnt/store/Projects/Zig language/comptime-cross-compile /mnt/store/Projects/Zig language/comptime-cross-compile/.zig-cache /home/dan/.cache/zig --seed 0x894cdc02 -Z5e045d9c41cd3489 run
in the documentation it says:
It doesn’t make sense that a program could call
exit()
(or any other external function) at compile-time, …
But why not? Why not allow the call to an OS agnostic library call like std.time.timestamp()
?
For context, I ran into this issue because I wanted to add a debug log message that included the time and date of the last compilation. in C++ I would use the __DATE__
and __TIME__
predefined macros