From 76d4b1b823a6fbca20e24b37c4be97a5541eebf4 Mon Sep 17 00:00:00 2001 From: Aaron Sikes Date: Sat, 23 Oct 2021 14:23:58 -0400 Subject: [PATCH] Better erroring for unsupported build option types --- lib/std/build/OptionsStep.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/std/build/OptionsStep.zig b/lib/std/build/OptionsStep.zig index dcb6eb5671..dfe512adec 100644 --- a/lib/std/build/OptionsStep.zig +++ b/lib/std/build/OptionsStep.zig @@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value: out.writeAll(";\n") catch unreachable; } +// TODO: non-recursive? fn printLiteral(out: anytype, val: anytype, indent: u8) !void { const T = @TypeOf(val); switch (@typeInfo(T)) { .Array => { - // TODO: non-recursive? try out.print("{s} {{\n", .{@typeName(T)}); for (val) |item| { try out.writeByteNTimes(' ', indent + 4); @@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void { try out.writeByteNTimes(' ', indent); try out.writeAll("}"); }, - else => try out.print("{any}", .{val}), + .Optional => { + if (val) |inner| { + return printLiteral(out, inner, indent); + } else { + return out.writeAll("null"); + } + }, + .Void, + .Bool, + .Int, + .Float, + .Null, + => try out.print("{any}", .{val}), + else => @compileError(comptime std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})), } } @@ -283,6 +296,7 @@ test "OptionsStep" { options.addOption(usize, "option1", 1); options.addOption(?usize, "option2", null); + options.addOption(?usize, "option3", 3); options.addOption([]const u8, "string", "zigisthebest"); options.addOption(?[]const u8, "optional_string", null); options.addOption([2][2]u16, "nested_array", nested_array); @@ -294,6 +308,7 @@ test "OptionsStep" { try std.testing.expectEqualStrings( \\pub const option1: usize = 1; \\pub const option2: ?usize = null; + \\pub const option3: ?usize = 3; \\pub const string: []const u8 = "zigisthebest"; \\pub const optional_string: ?[]const u8 = null; \\pub const nested_array: [2][2]u16 = [2][2]u16 {