Commit 46ddd5f5f4

Andrew Kelley <andrew@ziglang.org>
2019-02-09 01:23:46
fix compiler assertion failure when returning value from test
closes #1935
1 parent c2db077
Changed files (3)
src/ir.cpp
@@ -11510,10 +11510,13 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
         return ir_unreach_error(ira);
 
     IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type);
-    if (type_is_invalid(casted_value->value.type) && ira->explicit_return_type_source_node != nullptr) {
-        ErrorMsg *msg = ira->codegen->errors.last();
-        add_error_note(ira->codegen, msg, ira->explicit_return_type_source_node,
-            buf_sprintf("return type declared here"));
+    if (type_is_invalid(casted_value->value.type)) {
+        AstNode *source_node = ira->explicit_return_type_source_node;
+        if (source_node != nullptr) {
+            ErrorMsg *msg = ira->codegen->errors.last();
+            add_error_note(ira->codegen, msg, source_node,
+                buf_sprintf("return type declared here"));
+        }
         return ir_unreach_error(ira);
     }
 
test/compile_errors.zig
@@ -1,6 +1,13 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.addTest(
+        "return invalid type from test",
+        \\test "example" { return 1; }
+    ,
+        ".tmp_source.zig:1:25: error: integer value 1 cannot be implicitly casted to type 'void'",
+    );
+
     cases.add(
         "threadlocal qualifier on const",
         \\threadlocal const x: i32 = 1234;
test/tests.zig
@@ -538,6 +538,7 @@ pub const CompileErrorContext = struct {
         expected_errors: ArrayList([]const u8),
         link_libc: bool,
         is_exe: bool,
+        is_test: bool,
 
         const SourceFile = struct {
             filename: []const u8,
@@ -596,7 +597,13 @@ pub const CompileErrorContext = struct {
             var zig_args = ArrayList([]const u8).init(b.allocator);
             zig_args.append(b.zig_exe) catch unreachable;
 
-            zig_args.append(if (self.case.is_exe) "build-exe" else "build-obj") catch unreachable;
+            if (self.case.is_exe) {
+                try zig_args.append("build-exe");
+            } else if (self.case.is_test) {
+                try zig_args.append("test");
+            } else {
+                try zig_args.append("build-obj");
+            }
             zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
 
             zig_args.append("--name") catch unreachable;
@@ -699,6 +706,7 @@ pub const CompileErrorContext = struct {
             .expected_errors = ArrayList([]const u8).init(self.b.allocator),
             .link_libc = false,
             .is_exe = false,
+            .is_test = false,
         };
 
         tc.addSourceFile(".tmp_source.zig", source);
@@ -726,6 +734,12 @@ pub const CompileErrorContext = struct {
         self.addCase(tc);
     }
 
+    pub fn addTest(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+        const tc = self.create(name, source, expected_lines);
+        tc.is_test = true;
+        self.addCase(tc);
+    }
+
     pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
         const b = self.b;