parser: force `...` rather than `..` in matches for inclusive ranges (#5852)
parent
fa03f390b3
commit
3583302ad4
|
@ -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 {
|
||||
|
|
|
@ -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') }
|
||||
|
|
|
@ -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') }
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,6 @@
|
|||
fn test_match() {
|
||||
match 5 {
|
||||
0..10 { '0-9' }
|
||||
else { 'other' }
|
||||
}
|
||||
}
|
|
@ -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'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue