Commit c459edac18

Andrew Kelley <andrew@ziglang.org>
2019-05-09 19:18:13
compile error for attempt to cast enum literal to error
closes #2203
1 parent 72899da
Changed files (2)
src/ir.cpp
@@ -21213,11 +21213,17 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
         for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
             IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];
 
-            IrInstruction *start_value = range->start->child;
+            IrInstruction *start_value_uncasted = range->start->child;
+            if (type_is_invalid(start_value_uncasted->value.type))
+                return ira->codegen->invalid_instruction;
+            IrInstruction *start_value = ir_implicit_cast(ira, start_value_uncasted, switch_type);
             if (type_is_invalid(start_value->value.type))
                 return ira->codegen->invalid_instruction;
 
-            IrInstruction *end_value = range->end->child;
+            IrInstruction *end_value_uncasted = range->end->child;
+            if (type_is_invalid(end_value_uncasted->value.type))
+                return ira->codegen->invalid_instruction;
+            IrInstruction *end_value = ir_implicit_cast(ira, end_value_uncasted, switch_type);
             if (type_is_invalid(end_value->value.type))
                 return ira->codegen->invalid_instruction;
 
test/compile_errors.zig
@@ -2,6 +2,17 @@ const tests = @import("tests.zig");
 const builtin = @import("builtin");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "attempt to cast enum literal to error",
+        \\export fn entry() void {
+        \\    switch (error.Hi) {
+        \\        .Hi => {},
+        \\    }
+        \\}
+    ,
+        "tmp.zig:3:9: error: expected type 'error{Hi}', found '(enum literal)'",
+    );
+
     cases.add(
         "@sizeOf bad type",
         \\export fn entry() void {