- Second value in switch range not checked properly, causing an error on non-const values. #2777

This commit is contained in:
Christoffer Lerno
2026-01-20 17:12:41 +01:00
parent 3fe55b5e51
commit 9c435352b9
3 changed files with 14 additions and 3 deletions

View File

@@ -21,6 +21,7 @@
- Create optional with `~` instead of `?`. `return io::EOF?;` becomes `return io::EOF~`.
- Deprecated use of `?` to create optional.
- Vectors not converted to arrays when passed as raw vaargs. #2776
- Second value in switch range not checked properly, causing an error on non-const values. #2777
### Fixes
- Regression with npot vector in struct triggering an assert #2219.

View File

@@ -2432,10 +2432,10 @@ static inline bool sema_check_value_case(SemaContext *context, Type *switch_type
*if_chained = true;
return true;
}
if (is_range && !first_is_const)
if (is_range)
{
sema_error_at(context, extend_span_with_token(expr->span, to_expr->span), "Ranges must be constant integers.");
return false;
bool second_is_const = sema_cast_const(to_expr) && (expr_is_const_int(to_expr) || expr_is_const_enum(to_expr));
if (!first_is_const || !second_is_const) RETURN_SEMA_ERROR(first_is_const ? to_expr : expr, "Ranges must be constant integers or enum values, but this is not an integer / enum constant.");
}
bool is_enum = expr_is_const_enum(expr);
ExprConst *const_expr = &expr->const_expr;

View File

@@ -0,0 +1,10 @@
fn void main()
{
for (int i = 0; ; )
{
switch (i)
{
case 4 .. i: // #error: Ranges must be constant integers or enum values, but this is not an integer
}
}
}