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:
parent
3682a9cf88
commit
bba7cfee0b
@ -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
|
||||||
|
7
vlib/v/checker/tests/or_block_non_option_err.out
Normal file
7
vlib/v/checker/tests/or_block_non_option_err.out
Normal 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 | }
|
5
vlib/v/checker/tests/or_block_non_option_err.vv
Normal file
5
vlib/v/checker/tests/or_block_non_option_err.vv
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
name := 100
|
||||||
|
|
||||||
|
_ = name or {
|
||||||
|
100
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user