diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 709601287e..5046ac9f5a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2616,7 +2616,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol typ := c.table.type_to_str(c.expr(expr.low)) c.error('cannot use type `$typ` in match range', branch.pos) } - for i in low .. high { + for i in low..high + 1 { key = i.str() val := if key in branch_exprs { branch_exprs[key] } else { 0 } if val == 1 { diff --git a/vlib/v/checker/tests/match_duplicate_branch.out b/vlib/v/checker/tests/match_duplicate_branch.out index e7866f5296..657def0ea2 100644 --- a/vlib/v/checker/tests/match_duplicate_branch.out +++ b/vlib/v/checker/tests/match_duplicate_branch.out @@ -35,7 +35,7 @@ vlib/v/checker/tests/match_duplicate_branch.v:43:3: error: match case `2` is han 45 | } vlib/v/checker/tests/match_duplicate_branch.v:51:3: error: match case `3` is handled more than once 49 | match i { - 50 | 1..5 { println('1 to 4') } + 50 | 1...5 { println('1 to 5') } 51 | 3 { println('3') } | ~~~ 52 | else { println('else') } diff --git a/vlib/v/checker/tests/match_duplicate_branch.vv b/vlib/v/checker/tests/match_duplicate_branch.vv index 81df309dfc..dd5a92c907 100644 --- a/vlib/v/checker/tests/match_duplicate_branch.vv +++ b/vlib/v/checker/tests/match_duplicate_branch.vv @@ -47,7 +47,7 @@ fn test_int(i int) { fn test_range(i int) { match i { - 1..5 { println('1 to 4') } + 1...5 { println('1 to 5') } 3 { println('3') } else { println('else') } } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 1296ba5824..e2b9082f76 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2242,7 +2242,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { g.expr(expr.low) g.write(' && ') g.expr(node.cond) - g.write(' < ') + g.write(' <= ') g.expr(expr.high) g.write(')') } else { diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index da277e97ae..698484d537 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -188,6 +188,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr { expr := p.expr(0) p.inside_match_case = false if p.tok.kind == .dotdot { + p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)', p.tok.position()) + } else if p.tok.kind == .ellipsis { p.next() expr2 := p.expr(0) exprs << ast.RangeExpr{ diff --git a/vlib/v/parser/tests/match_range_dotdot_err.out b/vlib/v/parser/tests/match_range_dotdot_err.out new file mode 100644 index 0000000000..d473b54c60 --- /dev/null +++ b/vlib/v/parser/tests/match_range_dotdot_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/match_range_dotdot_err.v:3:4: error: match only supports inclusive (`...`) ranges, not exclusive (`..`) + 1 | fn test_match() { + 2 | match 5 { + 3 | 0..10 { '0-9' } + | ~~ + 4 | else { 'other' } + 5 | } diff --git a/vlib/v/parser/tests/match_range_dotdot_err.vv b/vlib/v/parser/tests/match_range_dotdot_err.vv new file mode 100644 index 0000000000..0c7f1125c6 --- /dev/null +++ b/vlib/v/parser/tests/match_range_dotdot_err.vv @@ -0,0 +1,6 @@ +fn test_match() { + match 5 { + 0..10 { '0-9' } + else { 'other' } + } +} diff --git a/vlib/v/tests/match_test.v b/vlib/v/tests/match_test.v index 7cbf220d14..268ee9229c 100644 --- a/vlib/v/tests/match_test.v +++ b/vlib/v/tests/match_test.v @@ -63,19 +63,19 @@ fn test_match_integers() { } fn test_match_multiple() { - assert match 5 { + assert match 9 { 1, 2, 3 { '1-3' } 4, 5 { '4-5' } - 6..9, 9 { '6-9' } + 6...9 { '6-9' } else { 'other' } - } == '4-5' + } == '6-9' } fn test_match_range() { assert match `f` { - `0`..`9` { 'digit' } - `A`..`Z` { 'uppercase' } - `a`..`z` { 'lowercase' } + `0`...`9` { 'digit' } + `A`...`Z` { 'uppercase' } + `a`...`z` { 'lowercase' } else { 'other' } } == 'lowercase' }