I’m trying to print a buffer at comptime. In my use case, the user does not need file or calling information, they just need to see the provided text at comptime.
compileError adds noise but at least prints the buffer (unfortunately with an offset) the way I provide it.
file:0:0: error:
Invalid action. Expected one of the following:
- option 1
- option 2
@compileError(msg);
^~~~~~~~~~~~~~~~~~
file:0:0: note: called from here
print.message("\nInvalid action. Expected one of the following::\n. - option 1\n - option 2\n");
compileLog also adds noise and doesn’t print the buffer the way I provide it.
file.zig:0:0: error: found compile log statement
@compileLog(msg);
^~~~~~~~~~~~~~~~
Compile Log Output:
@as([]const u8, "\nInvalid action. Expected one of the following:\n. - option 1\n - option 2\n")
std.io.getStdErr().writer().write calls externs which fail in comptime
zig/std/os.zig:1135:32: error: comptime call of extern function
const rc = system.write(fd, bytes.ptr, adjusted_len);
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zig/std/fs/file.zig:1163:28: note: called from here
return os.write(self.handle, bytes);
~~~~~~~~^~~~~~~~~~~~~~~~~~~~
zig/std/io/writer.zig:17:27: note: called from here
return writeFn(self.context, bytes);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
file:0:0: note: called from here
index += stderr.write(msg[index..]) catch return;
~~~~~~~~~~~~^~~~~~~~~~~~~~
file:0:0: note: called from here
print.message("\nInvalid action. Expected one of the following:\n. - option 1\n - option 2\n");
Is there anyway, at comptime, to just print to console the buffer I provide with nothing else printed? Is this possible to do when:
- Wanting to also stop compilation
- Wanting to continue compilation
Thanks!
Note: also the provided caller information isn’t helpful to end users anyway. It only shows where the compilerLog and compilerError is called from (which is my handler function)
As far as I know @compileLog
and @compileError
are the only functions that allow printing stuff to the terminal at compile-time.
Your only other option would be to do something within the build system, but that would make the whole thing pretty complicated.
May I ask why you want this? Especially the case were you don’t want to stop compilation seems weird to me. Couldn’t you just do a runtime print instead?
2 Likes
I hadn’t, but it looks like it handles the formatting during comptime then provides the buffer which I then still have to get into the console. I’ve already handled all the formatting and am having trouble getting my formatted buffer printed to console
pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
comptime {
var buf: [count(fmt, args):0]u8 = undefined;
_ = bufPrint(&buf, fmt, args) catch unreachable;
buf[buf.len] = 0;
return &buf;
}
}
Definitely a good resource though for anyone needing to format a string at comptime, I didn’t know this existed
2 Likes
That’s unfortunately what I thought
Yeah I might do that at a later date. Do you have any idea on where to edit the compileLog functionality? It’s almost what I need so I might branch and edit it
Compile-time syntax analysis of an inlined query language with configurable hints/warnings/errors. Not stopping compilation would be the hints/warnings. I could do a runtime print and then immediately abort for errors but when I already have information at comptime I’d very much prefer not affect runtime code at all and just fail to compile. In Rust I was accomplishing this with macros. I can’t show it off just yet but it would make more sense why I’m doing it this way if I could. I’ll be showcasing it along with the rest of the software on here once we launch our public beta here soon.
Edit: Also I personally hate the workflow of let’s fix this and see what breaks next. If I can provide multiple errors at once then stop compilation after providing all the necessary information I’d much prefer it
3 Likes
I ended up branching zig and implemented a fairly hacky change to compileLog’s behavior to actually print the provided buffer and remove the file info, error info etc.
This
file.zig:0:0: error: found compile log statement
@compileLog(msg);
^~~~~~~~~~~~~~~~
Compile Log Output:
@as([]const u8, "\nInvalid action. Expected one of the following:\n - option 1\n - option 2\n")
is now:
Invalid action. Expected one of the following:
- option 1
- option 2
This is obviously not a long term plan as I don’t expect end users to use a non-official branch of Zig, but it gives me an opportunity to see if this is as valuable as I think it is.
Does anyone know if there’s been any discussion around something like a “@compilePrint” that effectively mimics std.debug.print but at compilation?
My understanding is that Zig very intentionally does not have warnings, so something that would allow user code to emit warnings during compilation would likely be considered a misfeature.
The most relevant issue is probably Don't stop compilation if compileLog is used · Issue #5469 · ziglang/zig · GitHub
1 Like
Ah that’s unfortunate. I definitely understand Zig itself not utilizing warnings, but I feel like forcing the entire ecosystem to be unable to do so would be a bit of an overstep and unnecessarily limiting. This feature certainly could be used to generate warnings but I’m sure it would have many other use-cases as well
Thank you for sharing the discussion link!
1 Like