Skip to content

Commit bba7cfe

Browse files
authored
checker: check or-block used on non-option value (#17444)
1 parent 3682a9c commit bba7cfe

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

vlib/v/checker/checker.v

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,8 +3134,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
31343134
info := node.info as ast.IdentVar
31353135
// Got a var with type T, return current generic type
31363136
if node.or_expr.kind != .absent {
3137-
if !info.typ.has_flag(.option) && node.or_expr.kind == .propagate_option {
3138-
c.error('cannot use `?` on non-option variable', node.pos)
3137+
if !info.typ.has_flag(.option) {
3138+
if node.or_expr.kind == .propagate_option {
3139+
c.error('cannot use `?` on non-option variable', node.pos)
3140+
} else if node.or_expr.kind == .block {
3141+
c.error('cannot use `or {}` block on non-option variable', node.pos)
3142+
}
31393143
}
31403144
unwrapped_typ := info.typ.clear_flag(.option).clear_flag(.result)
31413145
c.expected_or_type = unwrapped_typ
@@ -3228,8 +3232,13 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
32283232
if node.or_expr.kind == .absent {
32293233
return typ.clear_flag(.result)
32303234
}
3231-
if !typ.has_flag(.option) && node.or_expr.kind == .propagate_option {
3232-
c.error('cannot use `?` on non-option variable', node.pos)
3235+
if !typ.has_flag(.option) {
3236+
if node.or_expr.kind == .propagate_option {
3237+
c.error('cannot use `?` on non-option variable', node.pos)
3238+
} else if node.or_expr.kind == .block {
3239+
c.error('cannot use `or {}` block on non-option variable',
3240+
node.pos)
3241+
}
32333242
}
32343243
unwrapped_typ := typ.clear_flag(.option).clear_flag(.result)
32353244
c.expected_or_type = unwrapped_typ
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/or_block_non_option_err.vv:3:5: error: cannot use `or {}` block on non-option variable
2+
1 | name := 100
3+
2 |
4+
3 | _ = name or {
5+
| ~~~~
6+
4 | 100
7+
5 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name := 100
2+
3+
_ = name or {
4+
100
5+
}

0 commit comments

Comments
 (0)