zig

fork of https://codeberg.org/ziglang/zig
Log | Tree | Refs | README | LICENSE

commit c459edac18a53854dbfdebe50a370d169e715145 (tree)
parent 72899da44bb95ebd90f5fcc5b0d3212491f94e9a
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Thu,  9 May 2019 13:18:13 -0400

compile error for attempt to cast enum literal to error

closes #2203

Diffstat:
Msrc/ir.cpp | 10++++++++--
Mtest/compile_errors.zig | 11+++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/ir.cpp b/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; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -3,6 +3,17 @@ 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 { \\ _ = @sizeOf(@typeOf(null));