Commit 9cff23dbf9

Isaac Hier <isaachier@gmail.com>
2018-07-04 19:27:10
Fix assertion crash on enum switch values
1 parent 9395162
src/ir.cpp
@@ -19149,9 +19149,14 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
             if (!end_val)
                 return ira->codegen->builtin_types.entry_invalid;
 
-            printf("%s\n", buf_ptr(&start_val->type->name));
+            if (start_val->type->id == TypeTableEntryIdEnum)
+                return ira->codegen->builtin_types.entry_invalid;
             assert(start_val->type->id == TypeTableEntryIdInt || start_val->type->id == TypeTableEntryIdComptimeInt);
+
+            if (end_val->type->id == TypeTableEntryIdEnum)
+                return ira->codegen->builtin_types.entry_invalid;
             assert(end_val->type->id == TypeTableEntryIdInt || end_val->type->id == TypeTableEntryIdComptimeInt);
+
             AstNode *prev_node = rangeset_add_range(&rs, &start_val->data.x_bigint, &end_val->data.x_bigint,
                     start_value->source_node);
             if (prev_node != nullptr) {
test/cases/switch_usize_enum_prongs.zig
@@ -1,11 +0,0 @@
-const E = enum(usize) { One, Two };
-
-test "aoeou" {
-    foo(1);
-}
-
-fn foo(x: usize) void {
-    switch (x) {
-        E.One => {},
-    }
-}
test/behavior.zig
@@ -52,7 +52,6 @@ comptime {
     _ = @import("cases/switch.zig");
     _ = @import("cases/switch_prong_err_enum.zig");
     _ = @import("cases/switch_prong_implicit_cast.zig");
-    _ = @import("cases/switch_usize_enum_prongs.zig");
     _ = @import("cases/syntax.zig");
     _ = @import("cases/this.zig");
     _ = @import("cases/try.zig");
test/compile_errors.zig
@@ -358,6 +358,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         ".tmp_source.zig:3:14: note: other value is here",
     );
 
+
+    cases.add(
+        "invalid cast from integral type to enum",
+        \\const E = enum(usize) { One, Two };
+        \\
+        \\export fn entry() void {
+        \\    foo(1);
+        \\}
+        \\
+        \\fn foo(x: usize) void {
+        \\    switch (x) {
+        \\        E.One => {},
+        \\    }
+        \\}
+    ,
+        ".tmp_source.zig:9:10: error: expected type 'usize', found 'E'"
+    );
+
     cases.add(
         "range operator in switch used on error set",
         \\export fn entry() void {