authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-08 19:23:46-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-08 19:23:46-05:00
log46ddd5f5f4db7977010f78129fade7dfa5b9d8d3
tree1247e46cfd016c69e23620f9370670fe02caad4b
parentc2db077574be841da586fa62d67619c901dd535d
signaturelock-open Commit is signed but in an unrecognized format.

fix compiler assertion failure when returning value from test

closes #1935

3 files changed, 29 insertions(+), 5 deletions(-)

src/ir.cpp+7-4
...@@ -11510,10 +11510,13 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio...@@ -11510,10 +11510,13 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
11510 return ir_unreach_error(ira);11510 return ir_unreach_error(ira);
1151111511
11512 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type);11512 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type);
11513 if (type_is_invalid(casted_value->value.type) && ira->explicit_return_type_source_node != nullptr) {11513 if (type_is_invalid(casted_value->value.type)) {
11514 ErrorMsg *msg = ira->codegen->errors.last();11514 AstNode *source_node = ira->explicit_return_type_source_node;
11515 add_error_note(ira->codegen, msg, ira->explicit_return_type_source_node,11515 if (source_node != nullptr) {
11516 buf_sprintf("return type declared here"));11516 ErrorMsg *msg = ira->codegen->errors.last();
11517 add_error_note(ira->codegen, msg, source_node,
11518 buf_sprintf("return type declared here"));
11519 }
11517 return ir_unreach_error(ira);11520 return ir_unreach_error(ira);
11518 }11521 }
1151911522
test/compile_errors.zig+7
...@@ -1,6 +1,13 @@...@@ -1,6 +1,13 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.addTest(
5 "return invalid type from test",
6 \\test "example" { return 1; }
7 ,
8 ".tmp_source.zig:1:25: error: integer value 1 cannot be implicitly casted to type 'void'",
9 );
10
4 cases.add(11 cases.add(
5 "threadlocal qualifier on const",12 "threadlocal qualifier on const",
6 \\threadlocal const x: i32 = 1234;13 \\threadlocal const x: i32 = 1234;
test/tests.zig+15-1
...@@ -538,6 +538,7 @@ pub const CompileErrorContext = struct {...@@ -538,6 +538,7 @@ pub const CompileErrorContext = struct {
538 expected_errors: ArrayList([]const u8),538 expected_errors: ArrayList([]const u8),
539 link_libc: bool,539 link_libc: bool,
540 is_exe: bool,540 is_exe: bool,
541 is_test: bool,
541542
542 const SourceFile = struct {543 const SourceFile = struct {
543 filename: []const u8,544 filename: []const u8,
...@@ -596,7 +597,13 @@ pub const CompileErrorContext = struct {...@@ -596,7 +597,13 @@ pub const CompileErrorContext = struct {
596 var zig_args = ArrayList([]const u8).init(b.allocator);597 var zig_args = ArrayList([]const u8).init(b.allocator);
597 zig_args.append(b.zig_exe) catch unreachable;598 zig_args.append(b.zig_exe) catch unreachable;
598599
599 zig_args.append(if (self.case.is_exe) "build-exe" else "build-obj") catch unreachable;600 if (self.case.is_exe) {
601 try zig_args.append("build-exe");
602 } else if (self.case.is_test) {
603 try zig_args.append("test");
604 } else {
605 try zig_args.append("build-obj");
606 }
600 zig_args.append(b.pathFromRoot(root_src)) catch unreachable;607 zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
601608
602 zig_args.append("--name") catch unreachable;609 zig_args.append("--name") catch unreachable;
...@@ -699,6 +706,7 @@ pub const CompileErrorContext = struct {...@@ -699,6 +706,7 @@ pub const CompileErrorContext = struct {
699 .expected_errors = ArrayList([]const u8).init(self.b.allocator),706 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
700 .link_libc = false,707 .link_libc = false,
701 .is_exe = false,708 .is_exe = false,
709 .is_test = false,
702 };710 };
703711
704 tc.addSourceFile(".tmp_source.zig", source);712 tc.addSourceFile(".tmp_source.zig", source);
...@@ -726,6 +734,12 @@ pub const CompileErrorContext = struct {...@@ -726,6 +734,12 @@ pub const CompileErrorContext = struct {
726 self.addCase(tc);734 self.addCase(tc);
727 }735 }
728736
737 pub fn addTest(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
738 const tc = self.create(name, source, expected_lines);
739 tc.is_test = true;
740 self.addCase(tc);
741 }
742
729 pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {743 pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
730 const b = self.b;744 const b = self.b;
731745