1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check or-block used on non-option value (#17444)

This commit is contained in:
Felipe Pena 2023-02-28 21:08:18 -03:00 committed by GitHub
parent 3682a9cf88
commit bba7cfee0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -3134,8 +3134,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
info := node.info as ast.IdentVar info := node.info as ast.IdentVar
// Got a var with type T, return current generic type // Got a var with type T, return current generic type
if node.or_expr.kind != .absent { if node.or_expr.kind != .absent {
if !info.typ.has_flag(.option) && node.or_expr.kind == .propagate_option { if !info.typ.has_flag(.option) {
c.error('cannot use `?` on non-option variable', node.pos) if node.or_expr.kind == .propagate_option {
c.error('cannot use `?` on non-option variable', node.pos)
} else if node.or_expr.kind == .block {
c.error('cannot use `or {}` block on non-option variable', node.pos)
}
} }
unwrapped_typ := info.typ.clear_flag(.option).clear_flag(.result) unwrapped_typ := info.typ.clear_flag(.option).clear_flag(.result)
c.expected_or_type = unwrapped_typ c.expected_or_type = unwrapped_typ
@ -3228,8 +3232,13 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
if node.or_expr.kind == .absent { if node.or_expr.kind == .absent {
return typ.clear_flag(.result) return typ.clear_flag(.result)
} }
if !typ.has_flag(.option) && node.or_expr.kind == .propagate_option { if !typ.has_flag(.option) {
c.error('cannot use `?` on non-option variable', node.pos) if node.or_expr.kind == .propagate_option {
c.error('cannot use `?` on non-option variable', node.pos)
} else if node.or_expr.kind == .block {
c.error('cannot use `or {}` block on non-option variable',
node.pos)
}
} }
unwrapped_typ := typ.clear_flag(.option).clear_flag(.result) unwrapped_typ := typ.clear_flag(.option).clear_flag(.result)
c.expected_or_type = unwrapped_typ c.expected_or_type = unwrapped_typ

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/or_block_non_option_err.vv:3:5: error: cannot use `or {}` block on non-option variable
1 | name := 100
2 |
3 | _ = name or {
| ~~~~
4 | 100
5 | }

View File

@ -0,0 +1,5 @@
name := 100
_ = name or {
100
}