checker: add a warning if start value is higher than end value (#12602)

pull/12600/head
Leo Developer 2021-11-28 17:46:52 +01:00 committed by GitHub
parent 5786dd84e6
commit 9c88317ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View File

@ -6365,6 +6365,9 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
&& (cond_type_sym.is_int() || cond_type_sym.info is ast.Enum) { && (cond_type_sym.is_int() || cond_type_sym.info is ast.Enum) {
low = low_expr.val.i64() low = low_expr.val.i64()
high = high_expr.val.i64() high = high_expr.val.i64()
if low > high {
c.error('start value is higher than end value', branch.pos)
}
} else { } else {
c.error('mismatched range types', low_expr.pos) c.error('mismatched range types', low_expr.pos)
} }
@ -6372,6 +6375,9 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
if high_expr is ast.CharLiteral && cond_type_sym.kind in [.byte, .char, .rune] { if high_expr is ast.CharLiteral && cond_type_sym.kind in [.byte, .char, .rune] {
low = low_expr.val[0] low = low_expr.val[0]
high = high_expr.val[0] high = high_expr.val[0]
if low > high {
c.error('start value is higher than end value', branch.pos)
}
} else { } else {
c.error('mismatched range types', low_expr.pos) c.error('mismatched range types', low_expr.pos)
} }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/match_expr_range_low_higher_than_high.vv:5:3: error: start value is higher than end value
3 | value := 10
4 | match value {
5 | 20...10 {}
| ~~~~~~~
6 | else {}
7 | }

View File

@ -0,0 +1,8 @@
fn main() {
value := 10
match value {
20...10 {}
else {}
}
}