authorgravatar for isaachier@gmail.comIsaac Hier <isaachier@gmail.com> 2018-07-04 13:27:10-04:00
committergravatar for isaachier@gmail.comIsaac Hier <isaachier@gmail.com> 2018-07-04 13:27:10-04:00
log9cff23dbf9ff3da716a1c4397f9411eba09f6cac
treeebde3411341a755db5a13b13b9324dd99e8e0a51
parent9395162a7c41689bcd1c0c48f9eabffc1485fc74

Fix assertion crash on enum switch values


4 files changed, 24 insertions(+), 13 deletions(-)

src/ir.cpp+6-1
...@@ -19149,9 +19149,14 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira...@@ -19149,9 +19149,14 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
19149 if (!end_val)19149 if (!end_val)
19150 return ira->codegen->builtin_types.entry_invalid;19150 return ira->codegen->builtin_types.entry_invalid;
1915119151
19152 printf("%s\n", buf_ptr(&start_val->type->name));19152 if (start_val->type->id == TypeTableEntryIdEnum)
19153 return ira->codegen->builtin_types.entry_invalid;
19153 assert(start_val->type->id == TypeTableEntryIdInt || start_val->type->id == TypeTableEntryIdComptimeInt);19154 assert(start_val->type->id == TypeTableEntryIdInt || start_val->type->id == TypeTableEntryIdComptimeInt);
19155
19156 if (end_val->type->id == TypeTableEntryIdEnum)
19157 return ira->codegen->builtin_types.entry_invalid;
19154 assert(end_val->type->id == TypeTableEntryIdInt || end_val->type->id == TypeTableEntryIdComptimeInt);19158 assert(end_val->type->id == TypeTableEntryIdInt || end_val->type->id == TypeTableEntryIdComptimeInt);
19159
19155 AstNode *prev_node = rangeset_add_range(&rs, &start_val->data.x_bigint, &end_val->data.x_bigint,19160 AstNode *prev_node = rangeset_add_range(&rs, &start_val->data.x_bigint, &end_val->data.x_bigint,
19156 start_value->source_node);19161 start_value->source_node);
19157 if (prev_node != nullptr) {19162 if (prev_node != nullptr) {
test/behavior.zig-1
...@@ -52,7 +52,6 @@ comptime {...@@ -52,7 +52,6 @@ comptime {
52 _ = @import("cases/switch.zig");52 _ = @import("cases/switch.zig");
53 _ = @import("cases/switch_prong_err_enum.zig");53 _ = @import("cases/switch_prong_err_enum.zig");
54 _ = @import("cases/switch_prong_implicit_cast.zig");54 _ = @import("cases/switch_prong_implicit_cast.zig");
55 _ = @import("cases/switch_usize_enum_prongs.zig");
56 _ = @import("cases/syntax.zig");55 _ = @import("cases/syntax.zig");
57 _ = @import("cases/this.zig");56 _ = @import("cases/this.zig");
58 _ = @import("cases/try.zig");57 _ = @import("cases/try.zig");
test/cases/switch_usize_enum_prongs.zig deleted-11
...@@ -1,11 +0,0 @@
1const E = enum(usize) { One, Two };
2
3test "aoeou" {
4 foo(1);
5}
6
7fn foo(x: usize) void {
8 switch (x) {
9 E.One => {},
10 }
11}
test/compile_errors.zig+18
...@@ -358,6 +358,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -358,6 +358,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
358 ".tmp_source.zig:3:14: note: other value is here",358 ".tmp_source.zig:3:14: note: other value is here",
359 );359 );
360360
361
362 cases.add(
363 "invalid cast from integral type to enum",
364 \\const E = enum(usize) { One, Two };
365 \\
366 \\export fn entry() void {
367 \\ foo(1);
368 \\}
369 \\
370 \\fn foo(x: usize) void {
371 \\ switch (x) {
372 \\ E.One => {},
373 \\ }
374 \\}
375 ,
376 ".tmp_source.zig:9:10: error: expected type 'usize', found 'E'"
377 );
378
361 cases.add(379 cases.add(
362 "range operator in switch used on error set",380 "range operator in switch used on error set",
363 \\export fn entry() void {381 \\export fn entry() void {