I’m working on small program to learn the language, but I’m running into build errors I don’t understand. When I uncomment afor
loop, my program fails to build with an error in fmt.zig
, and I don’t see the connection between the error and my code. What’s especially mystifying is the compiler is complaining about the readUntilDelimiter
call, but this program builds and runs as expected while the for
loop is commented out.
This is the part of my main
function that’s breaking:
while (running) {
try stdout.print("> ", .{});
_ = try stdin.readUntilDelimiter(&input_buffer, '\n');
const input: []u8 = std.mem.sliceTo(&input_buffer, '\n');
// Uncommenting this loop breaks the build
// for (commands) |command| {
// _ = command;
// }
try stdout.print("Invalid command: '{s}'\n", .{input});
}
The definition for the struct and array in question is here:
const Command = struct {
action: *const fn () anyerror!void,
name: []const u8,
description: []const u8,
};
const commands = [_]Command{
.{ .action = &help, .name = "help", .description = "description for help" },
.{ .action = &quit, .name = "quit", .description = "description for quit" },
};
Finally, the build error is here:
/opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/fmt.zig:273:17: error: expected . or }, found ' '
@compileError("expected . or }, found '" ++ unicode.utf8EncodeComptime(ch) ++ "'");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/fmt.zig:152:55: note: called from here
const placeholder = comptime Placeholder.parse(fmt[fmt_begin..fmt_end].*);
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
print__anon_3701: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/Writer.zig:23:26
print: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io.zig:324:47
dumpCurrentStackTrace: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/debug.zig:189:19
panicImpl: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/debug.zig:482:17
default_panic: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/builtin.zig:845:22
typeErasedWriteFn: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io.zig:355:41
any: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io.zig:350:28
bufPrint__anon_6172: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/fmt.zig:1768:24
panicExtra__anon_2950: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/debug.zig:428:33
panicOutOfBounds: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/builtin.zig:868:25
readByte: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/Reader.zig:232:5
streamUntilDelimiter__anon_3698: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/Reader.zig:210:38
readUntilDelimiter: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/Reader.zig:137:34
readUntilDelimiter: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io.zig:169:41
main: src/main.zig:153:22
callMain: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/start.zig:511:32
callMainWithArgs: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/start.zig:469:12
main: /opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/start.zig:484:12