fix compiler crash switching on global error with no else

This commit is contained in:
Andrew Kelley 2018-02-09 13:49:58 -05:00
parent 1fb308ceee
commit e7bf8f3f04
4 changed files with 38 additions and 19 deletions

View File

@ -15663,6 +15663,11 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
field_prev_uses[start_index] = start_value->source_node;
}
if (!instruction->have_else_prong) {
if (type_is_global_error_set(switch_type)) {
ir_add_error(ira, &instruction->base,
buf_sprintf("else prong required when switching on type 'error'"));
return ira->codegen->builtin_types.entry_invalid;
} else {
for (uint32_t i = 0; i < switch_type->data.error_set.err_count; i += 1) {
ErrorTableEntry *err_entry = switch_type->data.error_set.errors[i];
@ -15673,6 +15678,7 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
}
}
}
}
free(field_prev_uses);
} else if (switch_type->id == TypeTableEntryIdInt) {

View File

@ -499,7 +499,7 @@ pub fn OutStream(comptime Error: type) type {
writeFn: fn(self: &Self, bytes: []const u8) Error!void,
pub fn print(self: &Self, comptime format: []const u8, args: ...) !void {
return std.fmt.format(self, error, self.writeFn, format, args);
return std.fmt.format(self, Error, self.writeFn, format, args);
}
pub fn write(self: &Self, bytes: []const u8) !void {

View File

@ -133,7 +133,6 @@ pub const Parser = struct {
Token.Id.Eof => return Tree {.root_node = root_node},
else => {
self.putBackToken(token);
// TODO shouldn't need this cast
stack.append(State { .TopLevelExtern = null }) catch unreachable;
continue;
},
@ -707,7 +706,7 @@ pub const Parser = struct {
return node;
}
fn parseError(self: &Parser, token: &const Token, comptime fmt: []const u8, args: ...) error {
fn parseError(self: &Parser, token: &const Token, comptime fmt: []const u8, args: ...) (error{ParseError}) {
const loc = self.tokenizer.getTokenLocation(token);
warn("{}:{}:{}: error: " ++ fmt ++ "\n", self.source_file_name, loc.line + 1, loc.column + 1, args);
warn("{}\n", self.tokenizer.buffer[loc.line_start..loc.line_end]);
@ -1082,8 +1081,8 @@ fn testCanonical(source: []const u8) !void {
var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
if (testParse(source, &failing_allocator.allocator)) |_| {
return error.NondeterministicMemoryUsage;
} else |err| {
assert(err == error.OutOfMemory);
} else |err| switch (err) {
error.OutOfMemory => {
// TODO make this pass
//if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {
// warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
@ -1092,6 +1091,8 @@ fn testCanonical(source: []const u8) !void {
// failing_allocator.index, failing_allocator.deallocations);
// return error.MemoryLeakDetected;
//}
},
error.ParseError => @panic("test failed"),
}
}
}

View File

@ -1,6 +1,18 @@
const tests = @import("tests.zig");
pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add("no else prong on switch on global error set",
\\export fn entry() void {
\\ foo(error.A);
\\}
\\fn foo(a: error) void {
\\ switch (a) {
\\ error.A => {},
\\ }
\\}
,
".tmp_source.zig:5:5: error: else prong required when switching on type 'error'");
cases.add("inferred error set with no returned error",
\\export fn entry() void {
\\ foo() catch unreachable;