authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-22 23:46:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-22 23:46:55-04:00
log8503eff8c12697c35ab5c73d6651c4b996339706
tree13ddf63aa1b81d4f905d80fac129866e2650cc88
parent75328e32045d1275c03f1f37058ee0a3b775c632

add compile error for invalid deref on switch target

closes #945

2 files changed, 19 insertions(+), 1 deletions(-)

src/ir.cpp+4-1
......@@ -14782,7 +14782,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1478214782 return out_val->type;
1478314783 }
1478414784
14785 assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer);
14785 if (target_value_ptr->value.type->id != TypeTableEntryIdPointer) {
14786 ir_add_error(ira, target_value_ptr, buf_sprintf("invalid deref on switch target"));
14787 return ira->codegen->builtin_types.entry_invalid;
14788 }
1478614789
1478714790 TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
1478814791 ConstExprValue *pointee_val = nullptr;
test/compile_errors.zig+15
......@@ -1,6 +1,21 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("invalid deref on switch target",
5 \\comptime {
6 \\ var tile = Tile.Empty;
7 \\ switch (*tile) {
8 \\ Tile.Empty => {},
9 \\ Tile.Filled => {},
10 \\ }
11 \\}
12 \\const Tile = enum {
13 \\ Empty,
14 \\ Filled,
15 \\};
16 ,
17 ".tmp_source.zig:3:13: error: invalid deref on switch target");
18
419 cases.add("invalid field access in comptime",
520 \\comptime { var x = doesnt_exist.whatever; }
621 ,