From 1a4f46ae7d185e14bc3ff76f078fc27cfff58e8f Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 11 Apr 2020 01:01:37 -0500 Subject: [PATCH 1/3] add note about `@tagName` for non-exhaustive enums --- doc/langref.html.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index cd1905ebfb..6531323532 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -8167,7 +8167,7 @@ test "vector @splat" { {#header_open|@tagName#}
{#syntax#}@tagName(value: var) []const u8{#endsyntax#}

- Converts an enum value or union value to a slice of bytes representing the name. + Converts an enum value or union value to a slice of bytes representing the name. Not valid for unamed fields in non-exhaustive enums.

{#header_close#} From de08d283da8f8f4da8dfaee61c0bdd85c813c938 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 11 Apr 2020 15:18:54 -0500 Subject: [PATCH 2/3] fix compilation under mingw --- CMakeLists.txt | 3 +++ src-self-hosted/stage2.zig | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 279156c93c..092cdb7a70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -454,6 +454,9 @@ if("${ZIG_TARGET_TRIPLE}" STREQUAL "native") WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) set(ZIG_EXECUTABLE "${zig_BINARY_DIR}/zig") + if (WIN32) + set(ZIG_EXECUTABLE "${ZIG_EXECUTABLE}.exe") + endif() else() add_custom_target(zig_build_libstage2 ALL COMMAND "${ZIG_EXECUTABLE}" ${BUILD_LIBSTAGE2_ARGS} diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index 9d8c8446d5..aba4706f09 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -897,6 +897,22 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file: return .None; } +fn enumToString(value: var, type_name: []const u8) ![]const u8 { + switch (@typeInfo(@TypeOf(value))) { + .Enum => |e| { + if (e.is_exhaustive) { + return std.fmt.allocPrint(std.heap.c_allocator, ".{}", .{@tagName(value)}); + } else { + return std.fmt.allocPrint(std.heap.c_allocator, + "@intToEnum({}, {})", + .{type_name, @enumToInt(value)} + ); + } + }, + else => unreachable + } +} + // ABI warning const Stage2Target = extern struct { arch: c_int, @@ -1114,13 +1130,13 @@ const Stage2Target = extern struct { .windows => try os_builtin_str_buffer.outStream().print( \\ .windows = .{{ - \\ .min = .{}, - \\ .max = .{}, + \\ .min = {}, + \\ .max = {}, \\ }}}}, \\ , .{ - @tagName(target.os.version_range.windows.min), - @tagName(target.os.version_range.windows.max), + try enumToString(target.os.version_range.windows.min, "Target.Os.WindowsVersion"), + try enumToString(target.os.version_range.windows.max, "Target.Os.WindowsVersion") }), } try os_builtin_str_buffer.appendSlice("};\n"); From eefe6956fde0adc2b0b56fdb6d22e06e2543219e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 11 Apr 2020 16:43:19 -0400 Subject: [PATCH 3/3] clarify what "not valid" means --- doc/langref.html.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 6531323532..bb824076d6 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -8167,7 +8167,7 @@ test "vector @splat" { {#header_open|@tagName#}
{#syntax#}@tagName(value: var) []const u8{#endsyntax#}

- Converts an enum value or union value to a slice of bytes representing the name. Not valid for unamed fields in non-exhaustive enums. + Converts an enum value or union value to a slice of bytes representing the name.

If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}.

{#header_close#}