Commit 0f633167c5

Andrew Kelley <superjoe30@gmail.com>
2017-04-23 09:14:22
fix crash when unwrapping error with no error decls
closes #339
1 parent ad90404
Changed files (4)
src/codegen.cpp
@@ -668,6 +668,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
 
     g->generate_error_name_table = true;
     generate_error_name_table(g);
+    assert(g->err_name_table != nullptr);
 
     size_t unwrap_err_msg_text_len = strlen(unwrap_err_msg_text);
     size_t err_buf_len = strlen(unwrap_err_msg_text) + g->largest_err_name_len;
@@ -755,7 +756,6 @@ static void gen_debug_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val) {
     LLVMValueRef safety_crash_err_fn = get_safety_crash_err_fn(g);
     LLVMBuildCall(g->builder, safety_crash_err_fn, &err_val, 1, "");
     LLVMBuildUnreachable(g->builder);
-
 }
 
 static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
@@ -2649,7 +2649,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
     LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);
     LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, is_volatile);
 
-    if (ir_want_debug_safety(g, &instruction->base) && instruction->safety_check_on) {
+    if (ir_want_debug_safety(g, &instruction->base) && instruction->safety_check_on && g->error_decls.length > 1) {
         LLVMValueRef err_val;
         if (type_has_bits(child_type)) {
             LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
test/standalone/issue_339/build.zig
@@ -0,0 +1,8 @@
+const Builder = @import("std").build.Builder;
+
+pub fn build(b: &Builder) {
+    const obj = b.addObject("test", "test.zig");
+
+    const test_step = b.step("test", "Test the program");
+    test_step.dependOn(&obj.step);
+}
test/standalone/issue_339/test.zig
@@ -0,0 +1,7 @@
+pub fn panic(msg: []const u8) -> noreturn { @breakpoint(); while (true) {} }
+
+fn bar() -> %void {}
+
+export fn foo() {
+    %%bar();
+}
test/build_examples.zig
@@ -7,4 +7,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {
     cases.add("example/guess_number/main.zig");
     cases.addBuildFile("example/shared_library/build.zig");
     cases.addBuildFile("example/mix_o_files/build.zig");
+    cases.addBuildFile("test/standalone/issue_339/build.zig");
 }