From 54706dd2294d998c6c6ad91c80a23a3471677c43 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 3 Dec 2020 21:07:01 +0100 Subject: [PATCH 1/2] Allow idx 0 err to be put into error_name_table This way, in the very situation where a function has a return type an error union such as `anyerror!void` but doesn't have any erroneous paths, calling `@errorName` on the unpacked error (which will never be triggered) will not trip up the static analyzer. --- src/stage1/codegen.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 44346b1b1..eb0495c07 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -5178,11 +5178,7 @@ static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutableGen *executable, IrIns static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutableGen *executable, IrInstGenErrName *instruction) { assert(g->generate_error_name_table); - - if (g->errors_by_index.length == 1) { - LLVMBuildUnreachable(g->builder); - return nullptr; - } + assert(g->errors_by_index.length > 0); LLVMValueRef err_val = ir_llvm_value(g, instruction->value); if (ir_want_runtime_safety(g, &instruction->base)) { @@ -7890,7 +7886,7 @@ static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char } static void generate_error_name_table(CodeGen *g) { - if (g->err_name_table != nullptr || !g->generate_error_name_table || g->errors_by_index.length == 1) { + if (g->err_name_table != nullptr || !g->generate_error_name_table) { return; } From e3db2be89943333362210c1a8eb62b81c47bc1d2 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 4 Dec 2020 00:33:04 +0100 Subject: [PATCH 2/2] Add minimal standalone test case --- test/standalone.zig | 1 + test/standalone/issue_7030/build.zig | 14 ++++++++++++++ test/standalone/issue_7030/main.zig | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 test/standalone/issue_7030/build.zig create mode 100644 test/standalone/issue_7030/main.zig diff --git a/test/standalone.zig b/test/standalone.zig index d73587ccd..1295ac329 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -19,6 +19,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/use_alias/build.zig"); cases.addBuildFile("test/standalone/brace_expansion/build.zig"); cases.addBuildFile("test/standalone/empty_env/build.zig"); + cases.addBuildFile("test/standalone/issue_7030/build.zig"); if (std.Target.current.os.tag != .wasi) { cases.addBuildFile("test/standalone/load_dynamic_library/build.zig"); } diff --git a/test/standalone/issue_7030/build.zig b/test/standalone/issue_7030/build.zig new file mode 100644 index 000000000..ab3677370 --- /dev/null +++ b/test/standalone/issue_7030/build.zig @@ -0,0 +1,14 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const exe = b.addExecutable("issue_7030", "main.zig"); + exe.setTarget(.{ + .cpu_arch = .wasm32, + .os_tag = .freestanding, + }); + exe.install(); + b.default_step.dependOn(&exe.step); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&exe.step); +} diff --git a/test/standalone/issue_7030/main.zig b/test/standalone/issue_7030/main.zig new file mode 100644 index 000000000..d29869ff8 --- /dev/null +++ b/test/standalone/issue_7030/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + std.log.info("All your codebase are belong to us.", .{}); +}