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
1151011510 return ir_unreach_error(ira);
1151111511
1151211512 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) {
11514 ErrorMsg *msg = ira->codegen->errors.last();
11515 add_error_note(ira->codegen, msg, ira->explicit_return_type_source_node,
11516 buf_sprintf("return type declared here"));
11513 if (type_is_invalid(casted_value->value.type)) {
11514 AstNode *source_node = ira->explicit_return_type_source_node;
11515 if (source_node != nullptr) {
11516 ErrorMsg *msg = ira->codegen->errors.last();
11517 add_error_note(ira->codegen, msg, source_node,
11518 buf_sprintf("return type declared here"));
11519 }
1151711520 return ir_unreach_error(ira);
1151811521 }
1151911522
test/compile_errors.zig+7
......@@ -1,6 +1,13 @@
11const tests = @import("tests.zig");
22
33pub 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
411 cases.add(
512 "threadlocal qualifier on const",
613 \\threadlocal const x: i32 = 1234;
test/tests.zig+15-1
......@@ -538,6 +538,7 @@ pub const CompileErrorContext = struct {
538538 expected_errors: ArrayList([]const u8),
539539 link_libc: bool,
540540 is_exe: bool,
541 is_test: bool,
541542
542543 const SourceFile = struct {
543544 filename: []const u8,
......@@ -596,7 +597,13 @@ pub const CompileErrorContext = struct {
596597 var zig_args = ArrayList([]const u8).init(b.allocator);
597598 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 }
600607 zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
601608
602609 zig_args.append("--name") catch unreachable;
......@@ -699,6 +706,7 @@ pub const CompileErrorContext = struct {
699706 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
700707 .link_libc = false,
701708 .is_exe = false,
709 .is_test = false,
702710 };
703711
704712 tc.addSourceFile(".tmp_source.zig", source);
......@@ -726,6 +734,12 @@ pub const CompileErrorContext = struct {
726734 self.addCase(tc);
727735 }
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
729743 pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
730744 const b = self.b;
731745