add compile error for invalid deref on switch target

closes #945
This commit is contained in:
Andrew Kelley 2018-04-22 23:46:55 -04:00
parent 75328e3204
commit 8503eff8c1
2 changed files with 19 additions and 1 deletions

View File

@ -14782,7 +14782,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
return out_val->type;
}
assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer);
if (target_value_ptr->value.type->id != TypeTableEntryIdPointer) {
ir_add_error(ira, target_value_ptr, buf_sprintf("invalid deref on switch target"));
return ira->codegen->builtin_types.entry_invalid;
}
TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
ConstExprValue *pointee_val = nullptr;

View File

@ -1,6 +1,21 @@
const tests = @import("tests.zig");
pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add("invalid deref on switch target",
\\comptime {
\\ var tile = Tile.Empty;
\\ switch (*tile) {
\\ Tile.Empty => {},
\\ Tile.Filled => {},
\\ }
\\}
\\const Tile = enum {
\\ Empty,
\\ Filled,
\\};
,
".tmp_source.zig:3:13: error: invalid deref on switch target");
cases.add("invalid field access in comptime",
\\comptime { var x = doesnt_exist.whatever; }
,