This fixes the bug with "case 3 .. 1" #357

This commit is contained in:
Christoffer Lerno
2022-01-19 23:11:03 +01:00
parent 3450016978
commit e6ad9c324d
2 changed files with 21 additions and 0 deletions

View File

@@ -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)

View File

@@ -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;
}
}