Skip to content

Commit ddd5254

Browse files
authored
checker: fix missing checker for option map indexing (fix #22490) (#22495)
1 parent ab862c4 commit ddd5254

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

vlib/v/checker/checker.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,15 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
12781278
c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.result),
12791279
expr)
12801280
}
1281+
} else if expr.left is ast.SelectorExpr && expr.left_type.has_option_or_result() {
1282+
with_modifier_kind := if expr.left_type.has_flag(.option) {
1283+
'an Option'
1284+
} else {
1285+
'a Result'
1286+
}
1287+
with_modifier := if expr.left_type.has_flag(.option) { '?' } else { '!' }
1288+
c.error('field `${expr.left.field_name}` is ${with_modifier_kind}, so it should have either an `or {}` block, or `${with_modifier}` at the end',
1289+
expr.left.pos)
12811290
}
12821291
}
12831292
ast.CastExpr {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/checker/tests/option_map_err.vv:22:13: error: field `opmap` is an Option, so it should have either an `or {}` block, or `?` at the end
2+
20 | }
3+
21 | }
4+
22 | assert op2.opmap['1'] == 1.0
5+
| ~~~~~
6+
23 | }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct OptionalMap {
2+
opmap ?map[string]f64
3+
}
4+
5+
fn main() {
6+
op := OptionalMap{
7+
opmap: {
8+
'1': 0.0
9+
}
10+
}
11+
assert op.opmap or {
12+
{
13+
'1': 0.0
14+
}
15+
}['1'] == 0.0
16+
17+
op2 := OptionalMap{
18+
opmap: {
19+
'1': 1.0
20+
}
21+
}
22+
assert op2.opmap['1'] == 1.0
23+
}

0 commit comments

Comments
 (0)