| author | |
| committer | |
| log | 818a0a26291cf456cfaa955401b1aa8219737d6c |
| tree | 9eac0aacdcc41c718a18c98f3513beffaaeac2d3 |
| parent | 29beb603b752928184f5f5e9f7479412de1e1951 |
* for duplicate integer value
* for missing integer values
* for missing else prong
see #439 files changed, 304 insertions(+), 49 deletions(-)
CMakeLists.txt+1| ... | @@ -56,6 +56,7 @@ set(ZIG_SOURCES | ... | @@ -56,6 +56,7 @@ set(ZIG_SOURCES |
| 56 | "${CMAKE_SOURCE_DIR}/src/main.cpp" | 56 | "${CMAKE_SOURCE_DIR}/src/main.cpp" |
| 57 | "${CMAKE_SOURCE_DIR}/src/os.cpp" | 57 | "${CMAKE_SOURCE_DIR}/src/os.cpp" |
| 58 | "${CMAKE_SOURCE_DIR}/src/parser.cpp" | 58 | "${CMAKE_SOURCE_DIR}/src/parser.cpp" |
| 59 | "${CMAKE_SOURCE_DIR}/src/range_set.cpp" | ||
| 59 | "${CMAKE_SOURCE_DIR}/src/target.cpp" | 60 | "${CMAKE_SOURCE_DIR}/src/target.cpp" |
| 60 | "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp" | 61 | "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp" |
| 61 | "${CMAKE_SOURCE_DIR}/src/util.cpp" | 62 | "${CMAKE_SOURCE_DIR}/src/util.cpp" |
src/analyze.cpp+21-16| ... | @@ -3851,25 +3851,30 @@ int64_t min_signed_val(TypeTableEntry *type_entry) { | ... | @@ -3851,25 +3851,30 @@ int64_t min_signed_val(TypeTableEntry *type_entry) { |
| 3851 | } | 3851 | } |
| 3852 | } | 3852 | } |
| 3853 | 3853 | ||
| 3854 | void eval_min_max_value_int(CodeGen *g, TypeTableEntry *int_type, BigNum *bignum, bool is_max) { | ||
| 3855 | assert(int_type->id == TypeTableEntryIdInt); | ||
| 3856 | if (is_max) { | ||
| 3857 | if (int_type->data.integral.is_signed) { | ||
| 3858 | int64_t val = max_signed_val(int_type); | ||
| 3859 | bignum_init_signed(bignum, val); | ||
| 3860 | } else { | ||
| 3861 | uint64_t val = max_unsigned_val(int_type); | ||
| 3862 | bignum_init_unsigned(bignum, val); | ||
| 3863 | } | ||
| 3864 | } else { | ||
| 3865 | if (int_type->data.integral.is_signed) { | ||
| 3866 | int64_t val = min_signed_val(int_type); | ||
| 3867 | bignum_init_signed(bignum, val); | ||
| 3868 | } else { | ||
| 3869 | bignum_init_unsigned(bignum, 0); | ||
| 3870 | } | ||
| 3871 | } | ||
| 3872 | } | ||
| 3873 | |||
| 3854 | void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max) { | 3874 | void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max) { |
| 3855 | if (type_entry->id == TypeTableEntryIdInt) { | 3875 | if (type_entry->id == TypeTableEntryIdInt) { |
| 3856 | const_val->special = ConstValSpecialStatic; | 3876 | const_val->special = ConstValSpecialStatic; |
| 3857 | if (is_max) { | 3877 | eval_min_max_value_int(g, type_entry, &const_val->data.x_bignum, is_max); |
| 3858 | if (type_entry->data.integral.is_signed) { | ||
| 3859 | int64_t val = max_signed_val(type_entry); | ||
| 3860 | bignum_init_signed(&const_val->data.x_bignum, val); | ||
| 3861 | } else { | ||
| 3862 | uint64_t val = max_unsigned_val(type_entry); | ||
| 3863 | bignum_init_unsigned(&const_val->data.x_bignum, val); | ||
| 3864 | } | ||
| 3865 | } else { | ||
| 3866 | if (type_entry->data.integral.is_signed) { | ||
| 3867 | int64_t val = min_signed_val(type_entry); | ||
| 3868 | bignum_init_signed(&const_val->data.x_bignum, val); | ||
| 3869 | } else { | ||
| 3870 | bignum_init_unsigned(&const_val->data.x_bignum, 0); | ||
| 3871 | } | ||
| 3872 | } | ||
| 3873 | } else if (type_entry->id == TypeTableEntryIdFloat) { | 3878 | } else if (type_entry->id == TypeTableEntryIdFloat) { |
| 3874 | zig_panic("TODO analyze_min_max_value float"); | 3879 | zig_panic("TODO analyze_min_max_value float"); |
| 3875 | } else if (type_entry->id == TypeTableEntryIdBool) { | 3880 | } else if (type_entry->id == TypeTableEntryIdBool) { |
src/analyze.hpp+1| ... | @@ -84,6 +84,7 @@ void complete_enum(CodeGen *g, TypeTableEntry *enum_type); | ... | @@ -84,6 +84,7 @@ void complete_enum(CodeGen *g, TypeTableEntry *enum_type); |
| 84 | bool ir_get_var_is_comptime(VariableTableEntry *var); | 84 | bool ir_get_var_is_comptime(VariableTableEntry *var); |
| 85 | bool const_values_equal(ConstExprValue *a, ConstExprValue *b); | 85 | bool const_values_equal(ConstExprValue *a, ConstExprValue *b); |
| 86 | void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max); | 86 | void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max); |
| 87 | void eval_min_max_value_int(CodeGen *g, TypeTableEntry *int_type, BigNum *bignum, bool is_max); | ||
| 87 | int64_t min_signed_val(TypeTableEntry *type_entry); | 88 | int64_t min_signed_val(TypeTableEntry *type_entry); |
| 88 | uint64_t max_unsigned_val(TypeTableEntry *type_entry); | 89 | uint64_t max_unsigned_val(TypeTableEntry *type_entry); |
| 89 | 90 |
src/ir.cpp+48-6| ... | @@ -12,6 +12,7 @@ | ... | @@ -12,6 +12,7 @@ |
| 12 | #include "ir_print.hpp" | 12 | #include "ir_print.hpp" |
| 13 | #include "os.hpp" | 13 | #include "os.hpp" |
| 14 | #include "parseh.hpp" | 14 | #include "parseh.hpp" |
| 15 | #include "range_set.hpp" | ||
| 15 | 16 | ||
| 16 | struct IrExecContext { | 17 | struct IrExecContext { |
| 17 | ConstExprValue *mem_slot_list; | 18 | ConstExprValue *mem_slot_list; |
| ... | @@ -12981,7 +12982,7 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira | ... | @@ -12981,7 +12982,7 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira |
| 12981 | 12982 | ||
| 12982 | if (switch_type->id == TypeTableEntryIdEnumTag) { | 12983 | if (switch_type->id == TypeTableEntryIdEnumTag) { |
| 12983 | TypeTableEntry *enum_type = switch_type->data.enum_tag.enum_type; | 12984 | TypeTableEntry *enum_type = switch_type->data.enum_tag.enum_type; |
| 12984 | size_t *field_use_counts = allocate<size_t>(enum_type->data.enumeration.src_field_count); | 12985 | AstNode **field_prev_uses = allocate<AstNode *>(enum_type->data.enumeration.src_field_count); |
| 12985 | for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) { | 12986 | for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) { |
| 12986 | IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i]; | 12987 | IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i]; |
| 12987 | 12988 | ||
| ... | @@ -13011,24 +13012,65 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira | ... | @@ -13011,24 +13012,65 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira |
| 13011 | } | 13012 | } |
| 13012 | 13013 | ||
| 13013 | for (size_t field_index = start_index; field_index <= end_index; field_index += 1) { | 13014 | for (size_t field_index = start_index; field_index <= end_index; field_index += 1) { |
| 13014 | field_use_counts[field_index] += 1; | 13015 | AstNode *prev_node = field_prev_uses[field_index]; |
| 13015 | if (field_use_counts[field_index] > 1) { | 13016 | if (prev_node != nullptr) { |
| 13016 | TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_index]; | 13017 | TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_index]; |
| 13017 | ir_add_error(ira, start_value, | 13018 | ErrorMsg *msg = ir_add_error(ira, start_value, |
| 13018 | buf_sprintf("duplicate switch value: '%s.%s'", buf_ptr(&enum_type->name), | 13019 | buf_sprintf("duplicate switch value: '%s.%s'", buf_ptr(&enum_type->name), |
| 13019 | buf_ptr(type_enum_field->name))); | 13020 | buf_ptr(type_enum_field->name))); |
| 13021 | add_error_note(ira->codegen, msg, prev_node, buf_sprintf("other value is here")); | ||
| 13020 | } | 13022 | } |
| 13023 | field_prev_uses[field_index] = start_value->source_node; | ||
| 13021 | } | 13024 | } |
| 13022 | } | 13025 | } |
| 13023 | for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { | 13026 | for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { |
| 13024 | if (field_use_counts[i] == 0) { | 13027 | if (field_prev_uses[i] == nullptr) { |
| 13025 | ir_add_error(ira, &instruction->base, | 13028 | ir_add_error(ira, &instruction->base, |
| 13026 | buf_sprintf("enumeration value '%s.%s' not handled in switch", buf_ptr(&enum_type->name), | 13029 | buf_sprintf("enumeration value '%s.%s' not handled in switch", buf_ptr(&enum_type->name), |
| 13027 | buf_ptr(enum_type->data.enumeration.fields[i].name))); | 13030 | buf_ptr(enum_type->data.enumeration.fields[i].name))); |
| 13028 | } | 13031 | } |
| 13029 | } | 13032 | } |
| 13033 | } else if (switch_type->id == TypeTableEntryIdInt) { | ||
| 13034 | RangeSet rs = {0}; | ||
| 13035 | for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) { | ||
| 13036 | IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i]; | ||
| 13037 | |||
| 13038 | IrInstruction *start_value = range->start->other; | ||
| 13039 | if (type_is_invalid(start_value->value.type)) | ||
| 13040 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13041 | |||
| 13042 | IrInstruction *end_value = range->end->other; | ||
| 13043 | if (type_is_invalid(end_value->value.type)) | ||
| 13044 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13045 | |||
| 13046 | ConstExprValue *start_val = ir_resolve_const(ira, start_value, UndefBad); | ||
| 13047 | if (!start_val) | ||
| 13048 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13049 | |||
| 13050 | ConstExprValue *end_val = ir_resolve_const(ira, end_value, UndefBad); | ||
| 13051 | if (!end_val) | ||
| 13052 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13053 | |||
| 13054 | AstNode *prev_node = rangeset_add_range(&rs, &start_val->data.x_bignum, &end_val->data.x_bignum, | ||
| 13055 | start_value->source_node); | ||
| 13056 | if (prev_node != nullptr) { | ||
| 13057 | ErrorMsg *msg = ir_add_error(ira, start_value, buf_sprintf("duplicate switch value")); | ||
| 13058 | add_error_note(ira->codegen, msg, prev_node, buf_sprintf("previous value is here")); | ||
| 13059 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13060 | } | ||
| 13061 | } | ||
| 13062 | BigNum min_val; | ||
| 13063 | eval_min_max_value_int(ira->codegen, switch_type, &min_val, false); | ||
| 13064 | BigNum max_val; | ||
| 13065 | eval_min_max_value_int(ira->codegen, switch_type, &max_val, true); | ||
| 13066 | if (!rangeset_spans(&rs, &min_val, &max_val)) { | ||
| 13067 | ir_add_error(ira, &instruction->base, buf_sprintf("switch must handle all possibilities")); | ||
| 13068 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13069 | } | ||
| 13030 | } else { | 13070 | } else { |
| 13031 | // TODO check prongs of types other than enumtag | 13071 | ir_add_error(ira, &instruction->base, |
| 13072 | buf_sprintf("else prong required when switching on type '%s'", buf_ptr(&switch_type->name))); | ||
| 13073 | return ira->codegen->builtin_types.entry_invalid; | ||
| 13032 | } | 13074 | } |
| 13033 | ir_build_const_from(ira, &instruction->base); | 13075 | ir_build_const_from(ira, &instruction->base); |
| 13034 | return ira->codegen->builtin_types.entry_void; | 13076 | return ira->codegen->builtin_types.entry_void; |
src/range_set.cpp created+81| ... | @@ -0,0 +1,81 @@ | ||
| 1 | #include "range_set.hpp" | ||
| 2 | |||
| 3 | AstNode *rangeset_add_range(RangeSet *rs, BigNum *first, BigNum *last, AstNode *source_node) { | ||
| 4 | for (size_t i = 0; i < rs->src_range_list.length; i += 1) { | ||
| 5 | RangeWithSrc *range_with_src = &rs->src_range_list.at(i); | ||
| 6 | Range *range = &range_with_src->range; | ||
| 7 | if ((bignum_cmp_gte(first, &range->first) && bignum_cmp_lte(first, &range->last)) || | ||
| 8 | (bignum_cmp_gte(last, &range->first) && bignum_cmp_lte(last, &range->last))) | ||
| 9 | { | ||
| 10 | return range_with_src->source_node; | ||
| 11 | } | ||
| 12 | } | ||
| 13 | rs->src_range_list.append({{*first, *last}, source_node}); | ||
| 14 | |||
| 15 | return nullptr; | ||
| 16 | |||
| 17 | } | ||
| 18 | |||
| 19 | static bool add_range(ZigList<Range> *list, Range *new_range, BigNum *one) { | ||
| 20 | for (size_t i = 0; i < list->length; i += 1) { | ||
| 21 | Range *range = &list->at(i); | ||
| 22 | |||
| 23 | BigNum first_minus_one; | ||
| 24 | if (bignum_sub(&first_minus_one, &range->first, one)) | ||
| 25 | zig_unreachable(); | ||
| 26 | |||
| 27 | if (bignum_cmp_eq(&new_range->last, &first_minus_one)) { | ||
| 28 | range->first = new_range->first; | ||
| 29 | return true; | ||
| 30 | } | ||
| 31 | |||
| 32 | BigNum last_plus_one; | ||
| 33 | if (bignum_add(&last_plus_one, &range->last, one)) | ||
| 34 | zig_unreachable(); | ||
| 35 | |||
| 36 | if (bignum_cmp_eq(&new_range->first, &last_plus_one)) { | ||
| 37 | range->last = new_range->last; | ||
| 38 | return true; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | list->append({new_range->first, new_range->last}); | ||
| 42 | return false; | ||
| 43 | } | ||
| 44 | |||
| 45 | bool rangeset_spans(RangeSet *rs, BigNum *first, BigNum *last) { | ||
| 46 | ZigList<Range> cur_list_value = {0}; | ||
| 47 | ZigList<Range> other_list_value = {0}; | ||
| 48 | ZigList<Range> *cur_list = &cur_list_value; | ||
| 49 | ZigList<Range> *other_list = &other_list_value; | ||
| 50 | |||
| 51 | for (size_t i = 0; i < rs->src_range_list.length; i += 1) { | ||
| 52 | RangeWithSrc *range_with_src = &rs->src_range_list.at(i); | ||
| 53 | Range *range = &range_with_src->range; | ||
| 54 | cur_list->append({range->first, range->last}); | ||
| 55 | } | ||
| 56 | |||
| 57 | BigNum one; | ||
| 58 | bignum_init_unsigned(&one, 1); | ||
| 59 | |||
| 60 | bool changes_made = true; | ||
| 61 | while (changes_made) { | ||
| 62 | changes_made = false; | ||
| 63 | for (size_t cur_i = 0; cur_i < cur_list->length; cur_i += 1) { | ||
| 64 | Range *range = &cur_list->at(cur_i); | ||
| 65 | changes_made = add_range(other_list, range, &one) || changes_made; | ||
| 66 | } | ||
| 67 | ZigList<Range> *tmp = cur_list; | ||
| 68 | cur_list = other_list; | ||
| 69 | other_list = tmp; | ||
| 70 | other_list->resize(0); | ||
| 71 | } | ||
| 72 | |||
| 73 | if (cur_list->length != 1) | ||
| 74 | return false; | ||
| 75 | Range *range = &cur_list->at(0); | ||
| 76 | if (bignum_cmp_neq(&range->first, first)) | ||
| 77 | return false; | ||
| 78 | if (bignum_cmp_neq(&range->last, last)) | ||
| 79 | return false; | ||
| 80 | return true; | ||
| 81 | } | ||
src/range_set.hpp created+30| ... | @@ -0,0 +1,30 @@ | ||
| 1 | /* | ||
| 2 | * Copyright (c) 2017 Andrew Kelley | ||
| 3 | * | ||
| 4 | * This file is part of zig, which is MIT licensed. | ||
| 5 | * See http://opensource.org/licenses/MIT | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef ZIG_RANGE_SET_HPP | ||
| 9 | #define ZIG_RANGE_SET_HPP | ||
| 10 | |||
| 11 | #include "all_types.hpp" | ||
| 12 | |||
| 13 | struct Range { | ||
| 14 | BigNum first; | ||
| 15 | BigNum last; | ||
| 16 | }; | ||
| 17 | |||
| 18 | struct RangeWithSrc { | ||
| 19 | Range range; | ||
| 20 | AstNode *source_node; | ||
| 21 | }; | ||
| 22 | |||
| 23 | struct RangeSet { | ||
| 24 | ZigList<RangeWithSrc> src_range_list; | ||
| 25 | }; | ||
| 26 | |||
| 27 | AstNode *rangeset_add_range(RangeSet *rs, BigNum *first, BigNum *last, AstNode *source_node); | ||
| 28 | bool rangeset_spans(RangeSet *rs, BigNum *first, BigNum *last); | ||
| 29 | |||
| 30 | #endif | ||
test/cases/switch.zig+46-8| ... | @@ -1,6 +1,6 @@ | ... | @@ -1,6 +1,6 @@ |
| 1 | const assert = @import("std").debug.assert; | 1 | const assert = @import("std").debug.assert; |
| 2 | 2 | ||
| 3 | test "switchWithNumbers" { | 3 | test "switch with numbers" { |
| 4 | testSwitchWithNumbers(13); | 4 | testSwitchWithNumbers(13); |
| 5 | } | 5 | } |
| 6 | 6 | ||
| ... | @@ -13,7 +13,7 @@ fn testSwitchWithNumbers(x: u32) { | ... | @@ -13,7 +13,7 @@ fn testSwitchWithNumbers(x: u32) { |
| 13 | assert(result); | 13 | assert(result); |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | test "switchWithAllRanges" { | 16 | test "switch with all ranges" { |
| 17 | assert(testSwitchWithAllRanges(50, 3) == 1); | 17 | assert(testSwitchWithAllRanges(50, 3) == 1); |
| 18 | assert(testSwitchWithAllRanges(101, 0) == 2); | 18 | assert(testSwitchWithAllRanges(101, 0) == 2); |
| 19 | assert(testSwitchWithAllRanges(300, 5) == 3); | 19 | assert(testSwitchWithAllRanges(300, 5) == 3); |
| ... | @@ -29,7 +29,7 @@ fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { | ... | @@ -29,7 +29,7 @@ fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { |
| 29 | } | 29 | } |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | test "implicitComptimeSwitch" { | 32 | test "implicit comptime switch" { |
| 33 | const x = 3 + 4; | 33 | const x = 3 + 4; |
| 34 | const result = switch (x) { | 34 | const result = switch (x) { |
| 35 | 3 => 10, | 35 | 3 => 10, |
| ... | @@ -44,7 +44,7 @@ test "implicitComptimeSwitch" { | ... | @@ -44,7 +44,7 @@ test "implicitComptimeSwitch" { |
| 44 | } | 44 | } |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | test "switchOnEnum" { | 47 | test "switch on enum" { |
| 48 | const fruit = Fruit.Orange; | 48 | const fruit = Fruit.Orange; |
| 49 | nonConstSwitchOnEnum(fruit); | 49 | nonConstSwitchOnEnum(fruit); |
| 50 | } | 50 | } |
| ... | @@ -62,7 +62,7 @@ fn nonConstSwitchOnEnum(fruit: Fruit) { | ... | @@ -62,7 +62,7 @@ fn nonConstSwitchOnEnum(fruit: Fruit) { |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | test "switchStatement" { | 65 | test "switch statement" { |
| 66 | nonConstSwitch(SwitchStatmentFoo.C); | 66 | nonConstSwitch(SwitchStatmentFoo.C); |
| 67 | } | 67 | } |
| 68 | fn nonConstSwitch(foo: SwitchStatmentFoo) { | 68 | fn nonConstSwitch(foo: SwitchStatmentFoo) { |
| ... | @@ -82,7 +82,7 @@ const SwitchStatmentFoo = enum { | ... | @@ -82,7 +82,7 @@ const SwitchStatmentFoo = enum { |
| 82 | }; | 82 | }; |
| 83 | 83 | ||
| 84 | 84 | ||
| 85 | test "switchProngWithVar" { | 85 | test "switch prong with variable" { |
| 86 | switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); | 86 | switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); |
| 87 | switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); | 87 | switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); |
| 88 | switchProngWithVarFn(SwitchProngWithVarEnum.Meh); | 88 | switchProngWithVarFn(SwitchProngWithVarEnum.Meh); |
| ... | @@ -107,7 +107,7 @@ fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) { | ... | @@ -107,7 +107,7 @@ fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) { |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | 109 | ||
| 110 | test "switchWithMultipleExpressions" { | 110 | test "switch with multiple expressions" { |
| 111 | const x = switch (returnsFive()) { | 111 | const x = switch (returnsFive()) { |
| 112 | 1, 2, 3 => 1, | 112 | 1, 2, 3 => 1, |
| 113 | 4, 5, 6 => 2, | 113 | 4, 5, 6 => 2, |
| ... | @@ -135,7 +135,7 @@ fn returnsFalse() -> bool { | ... | @@ -135,7 +135,7 @@ fn returnsFalse() -> bool { |
| 135 | Number.Three => |x| return x > 12.34, | 135 | Number.Three => |x| return x > 12.34, |
| 136 | } | 136 | } |
| 137 | } | 137 | } |
| 138 | test "switchOnConstEnumWithVar" { | 138 | test "switch on const enum with var" { |
| 139 | assert(!returnsFalse()); | 139 | assert(!returnsFalse()); |
| 140 | } | 140 | } |
| 141 | 141 | ||
| ... | @@ -150,3 +150,41 @@ fn trueIfBoolFalseOtherwise(comptime T: type) -> bool { | ... | @@ -150,3 +150,41 @@ fn trueIfBoolFalseOtherwise(comptime T: type) -> bool { |
| 150 | else => false, | 150 | else => false, |
| 151 | } | 151 | } |
| 152 | } | 152 | } |
| 153 | |||
| 154 | test "switch handles all cases of number" { | ||
| 155 | testSwitchHandleAllCases(); | ||
| 156 | comptime testSwitchHandleAllCases(); | ||
| 157 | } | ||
| 158 | |||
| 159 | const u2 = @IntType(false, 2); | ||
| 160 | fn testSwitchHandleAllCases() { | ||
| 161 | assert(testSwitchHandleAllCasesExhaustive(0) == 3); | ||
| 162 | assert(testSwitchHandleAllCasesExhaustive(1) == 2); | ||
| 163 | assert(testSwitchHandleAllCasesExhaustive(2) == 1); | ||
| 164 | assert(testSwitchHandleAllCasesExhaustive(3) == 0); | ||
| 165 | |||
| 166 | assert(testSwitchHandleAllCasesRange(100) == 0); | ||
| 167 | assert(testSwitchHandleAllCasesRange(200) == 1); | ||
| 168 | assert(testSwitchHandleAllCasesRange(201) == 2); | ||
| 169 | assert(testSwitchHandleAllCasesRange(202) == 4); | ||
| 170 | assert(testSwitchHandleAllCasesRange(230) == 3); | ||
| 171 | } | ||
| 172 | |||
| 173 | fn testSwitchHandleAllCasesExhaustive(x: u2) -> u2 { | ||
| 174 | switch (x) { | ||
| 175 | 0 => u2(3), | ||
| 176 | 1 => 2, | ||
| 177 | 2 => 1, | ||
| 178 | 3 => 0, | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | fn testSwitchHandleAllCasesRange(x: u8) -> u8 { | ||
| 183 | switch (x) { | ||
| 184 | 0 ... 100 => u8(0), | ||
| 185 | 101 ... 200 => 1, | ||
| 186 | 201, 203 => 2, | ||
| 187 | 202 => 4, | ||
| 188 | 204 ... 255 => 3, | ||
| 189 | } | ||
| 190 | } |
test/cases/try.zig+1| ... | @@ -12,6 +12,7 @@ fn tryOnErrorUnionImpl() { | ... | @@ -12,6 +12,7 @@ fn tryOnErrorUnionImpl() { |
| 12 | } else |err| switch (err) { | 12 | } else |err| switch (err) { |
| 13 | error.ItBroke, error.NoMem => 1, | 13 | error.ItBroke, error.NoMem => 1, |
| 14 | error.CrappedOut => i32(2), | 14 | error.CrappedOut => i32(2), |
| 15 | else => unreachable, | ||
| 15 | }; | 16 | }; |
| 16 | assert(x == 11); | 17 | assert(x == 11); |
| 17 | } | 18 | } |
test/compile_errors.zig+75-19| ... | @@ -542,7 +542,46 @@ pub fn addCases(cases: &tests.CompileErrorContext) { | ... | @@ -542,7 +542,46 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 542 | ".tmp_source.zig:5:5: error: redefinition of 'Bar'", | 542 | ".tmp_source.zig:5:5: error: redefinition of 'Bar'", |
| 543 | ".tmp_source.zig:2:1: note: previous definition is here"); | 543 | ".tmp_source.zig:2:1: note: previous definition is here"); |
| 544 | 544 | ||
| 545 | cases.add("multiple else prongs in a switch", | 545 | cases.add("switch expression - missing enumeration prong", |
| 546 | \\const Number = enum { | ||
| 547 | \\ One, | ||
| 548 | \\ Two, | ||
| 549 | \\ Three, | ||
| 550 | \\ Four, | ||
| 551 | \\}; | ||
| 552 | \\fn f(n: Number) -> i32 { | ||
| 553 | \\ switch (n) { | ||
| 554 | \\ Number.One => 1, | ||
| 555 | \\ Number.Two => 2, | ||
| 556 | \\ Number.Three => i32(3), | ||
| 557 | \\ } | ||
| 558 | \\} | ||
| 559 | \\ | ||
| 560 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | ||
| 561 | , ".tmp_source.zig:8:5: error: enumeration value 'Number.Four' not handled in switch"); | ||
| 562 | |||
| 563 | cases.add("switch expression - duplicate enumeration prong", | ||
| 564 | \\const Number = enum { | ||
| 565 | \\ One, | ||
| 566 | \\ Two, | ||
| 567 | \\ Three, | ||
| 568 | \\ Four, | ||
| 569 | \\}; | ||
| 570 | \\fn f(n: Number) -> i32 { | ||
| 571 | \\ switch (n) { | ||
| 572 | \\ Number.One => 1, | ||
| 573 | \\ Number.Two => 2, | ||
| 574 | \\ Number.Three => i32(3), | ||
| 575 | \\ Number.Four => 4, | ||
| 576 | \\ Number.Two => 2, | ||
| 577 | \\ } | ||
| 578 | \\} | ||
| 579 | \\ | ||
| 580 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | ||
| 581 | , ".tmp_source.zig:13:15: error: duplicate switch value", | ||
| 582 | ".tmp_source.zig:10:15: note: other value is here"); | ||
| 583 | |||
| 584 | cases.add("switch expression - multiple else prongs", | ||
| 546 | \\fn f(x: u32) { | 585 | \\fn f(x: u32) { |
| 547 | \\ const value: bool = switch (x) { | 586 | \\ const value: bool = switch (x) { |
| 548 | \\ 1234 => false, | 587 | \\ 1234 => false, |
| ... | @@ -555,6 +594,41 @@ pub fn addCases(cases: &tests.CompileErrorContext) { | ... | @@ -555,6 +594,41 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 555 | \\} | 594 | \\} |
| 556 | , ".tmp_source.zig:5:9: error: multiple else prongs in switch expression"); | 595 | , ".tmp_source.zig:5:9: error: multiple else prongs in switch expression"); |
| 557 | 596 | ||
| 597 | cases.add("switch expression - non exhaustive integer prongs", | ||
| 598 | \\fn foo(x: u8) { | ||
| 599 | \\ switch (x) { | ||
| 600 | \\ 0 => {}, | ||
| 601 | \\ } | ||
| 602 | \\} | ||
| 603 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | ||
| 604 | , | ||
| 605 | ".tmp_source.zig:2:5: error: switch must handle all possibilities"); | ||
| 606 | |||
| 607 | cases.add("switch expression - duplicate or overlapping integer value", | ||
| 608 | \\fn foo(x: u8) -> u8 { | ||
| 609 | \\ switch (x) { | ||
| 610 | \\ 0 ... 100 => u8(0), | ||
| 611 | \\ 101 ... 200 => 1, | ||
| 612 | \\ 201, 203 ... 207 => 2, | ||
| 613 | \\ 206 ... 255 => 3, | ||
| 614 | \\ } | ||
| 615 | \\} | ||
| 616 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | ||
| 617 | , | ||
| 618 | ".tmp_source.zig:6:9: error: duplicate switch value", | ||
| 619 | ".tmp_source.zig:5:14: note: previous value is here"); | ||
| 620 | |||
| 621 | cases.add("switch expression - switch on pointer type with no else", | ||
| 622 | \\fn foo(x: &u8) { | ||
| 623 | \\ switch (x) { | ||
| 624 | \\ &y => {}, | ||
| 625 | \\ } | ||
| 626 | \\} | ||
| 627 | \\const y: u8 = 100; | ||
| 628 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | ||
| 629 | , | ||
| 630 | ".tmp_source.zig:2:5: error: else prong required when switching on type '&u8'"); | ||
| 631 | |||
| 558 | cases.add("global variable initializer must be constant expression", | 632 | cases.add("global variable initializer must be constant expression", |
| 559 | \\extern fn foo() -> i32; | 633 | \\extern fn foo() -> i32; |
| 560 | \\const x = foo(); | 634 | \\const x = foo(); |
| ... | @@ -716,24 +790,6 @@ pub fn addCases(cases: &tests.CompileErrorContext) { | ... | @@ -716,24 +790,6 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 716 | ".tmp_source.zig:4:26: error: division by zero is undefined"); | 790 | ".tmp_source.zig:4:26: error: division by zero is undefined"); |
| 717 | 791 | ||
| 718 | 792 | ||
| 719 | cases.add("missing switch prong", | ||
| 720 | \\const Number = enum { | ||
| 721 | \\ One, | ||
| 722 | \\ Two, | ||
| 723 | \\ Three, | ||
| 724 | \\ Four, | ||
| 725 | \\}; | ||
| 726 | \\fn f(n: Number) -> i32 { | ||
| 727 | \\ switch (n) { | ||
| 728 | \\ Number.One => 1, | ||
| 729 | \\ Number.Two => 2, | ||
| 730 | \\ Number.Three => i32(3), | ||
| 731 | \\ } | ||
| 732 | \\} | ||
| 733 | \\ | ||
| 734 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | ||
| 735 | , ".tmp_source.zig:8:5: error: enumeration value 'Number.Four' not handled in switch"); | ||
| 736 | |||
| 737 | cases.add("normal string with newline", | 793 | cases.add("normal string with newline", |
| 738 | \\const foo = "a | 794 | \\const foo = "a |
| 739 | \\b"; | 795 | \\b"; |