zig icon indicating copy to clipboard operation
zig copied to clipboard

Add a `@compileError` hint when trying to print from comptime

Open tau-dev opened this issue 1 year ago • 4 comments

Fixes #14238.

While we can't cover every I/O function like this, lockStdErr covers std.debug.print and std.log.*, probably the most important places to leave a useful hint for newcomers to Zig.

tau-dev avatar Jul 23 '24 20:07 tau-dev

can you add a test?

nektro avatar Jul 23 '24 23:07 nektro

@nektro, there is no way to test compile errors in the standard library...

mlugg avatar Jul 23 '24 23:07 mlugg

@nektro, there is no way to test compile errors in the standard library...

Out of curiosity, what's wrong with the following test file?

test/cases/compile_errors/io_in_comptime.zig:

const std = @import("std");

export fn a() void {
    comptime std.debug.print("Printing in comptime isn't allowed!", .{});
}

// error
// backend=stage2
// target=native
//
// :?:?: error: I/O cannot be used from comptime. Consider using @compileError() or @compileLog().
// :?:?: note: called from here
// :?:?: note: called from here
// :4:29: note: called from here

Asking because I took a similar approach in the tests I wrote for #20748.

WillLillis avatar Jul 24 '24 01:07 WillLillis

The tests in test/ exist to test the compiler implementation, not the standard library. So, you should remove those tests from #20748 -- I appreciate the effort, but that's not what that test suite is there for!

mlugg avatar Jul 24 '24 01:07 mlugg

@andrewrk #14238 is still open, how else would that be solved?

tau-dev avatar Feb 25 '25 11:02 tau-dev