mirror of
https://github.com/vlang/v.git
synced 2025-09-14 06:52:36 +03:00
checker: fix checking for option matching with non-option (#20673)
This commit is contained in:
parent
a327d49e8a
commit
d8b158be2b
2 changed files with 18 additions and 11 deletions
|
@ -55,6 +55,10 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||||
branch.branch_pos)
|
branch.branch_pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !branch.is_else && cond_is_option && branch.exprs[0] !is ast.None {
|
||||||
|
c.error('`match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`',
|
||||||
|
branch.pos)
|
||||||
|
}
|
||||||
// If the last statement is an expression, return its type
|
// If the last statement is an expression, return its type
|
||||||
if branch.stmts.len > 0 {
|
if branch.stmts.len > 0 {
|
||||||
mut stmt := branch.stmts.last()
|
mut stmt := branch.stmts.last()
|
||||||
|
@ -63,10 +67,6 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||||
c.expected_type = node.expected_type
|
c.expected_type = node.expected_type
|
||||||
}
|
}
|
||||||
expr_type := c.expr(mut stmt.expr)
|
expr_type := c.expr(mut stmt.expr)
|
||||||
if !branch.is_else && cond_is_option && branch.exprs[0] !is ast.None {
|
|
||||||
c.error('`match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`',
|
|
||||||
branch.pos)
|
|
||||||
}
|
|
||||||
stmt.typ = expr_type
|
stmt.typ = expr_type
|
||||||
if first_iteration {
|
if first_iteration {
|
||||||
if node.is_expr && (node.expected_type.has_flag(.option)
|
if node.is_expr && (node.expected_type.has_flag(.option)
|
||||||
|
|
|
@ -117,3 +117,10 @@ vlib/v/checker/tests/option_fn_err.vv:69:18: error: type mismatch, `bar` must re
|
||||||
| ~~~~~~~~~
|
| ~~~~~~~~~
|
||||||
70 |
|
70 |
|
||||||
71 | match bar(0) {
|
71 | match bar(0) {
|
||||||
|
vlib/v/checker/tests/option_fn_err.vv:72:3: error: `match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`
|
||||||
|
70 |
|
||||||
|
71 | match bar(0) {
|
||||||
|
72 | 0 {}
|
||||||
|
| ^
|
||||||
|
73 | else {}
|
||||||
|
74 | }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue