From e6ad9c324d4e632509a084cd1d156218ea025eb3 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 19 Jan 2022 23:11:03 +0100 Subject: [PATCH] This fixes the bug with "case 3 .. 1" #357 --- src/compiler/sema_stmts.c | 9 +++++++++ test/test_suite/statements/switch_error_range.c3 | 12 ++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/test_suite/statements/switch_error_range.c3 diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 88c358c07..082483fa6 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -1919,6 +1919,15 @@ static inline bool sema_check_value_case(SemaContext *context, Type *switch_type if (!*max_ranged && type_is_integer(expr->type) && to_const_expr != const_expr) { + if (int_comp(const_expr->ixx, to_const_expr->ixx, BINARYOP_GT)) + { + sema_error_range((SourceSpan){ expr->span.loc, to_expr->span.end_loc }, + "The range is not valid because the first value (%s) is greater than the second (%s). " + "It would work if you swapped their order.", + int_to_str(const_expr->ixx, 10), + int_to_str(to_const_expr->ixx, 10)); + return false; + } Int128 range = int_sub(to_const_expr->ixx, const_expr->ixx).i; Int128 max_range = { .low = active_target.switchrange_max_size }; if (i128_comp(range, max_range, type_i128) == CMP_GT) diff --git a/test/test_suite/statements/switch_error_range.c3 b/test/test_suite/statements/switch_error_range.c3 new file mode 100644 index 000000000..fe39cce9d --- /dev/null +++ b/test/test_suite/statements/switch_error_range.c3 @@ -0,0 +1,12 @@ +module foo; + +fn void test() +{ + int i; + switch (i) + { + case 15..13: // #error: The range is not valid because the first value (15) is greater than the second (13) + i++; + return; + } +} \ No newline at end of file