Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,15 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.result),
expr)
}
} else if expr.left is ast.SelectorExpr && expr.left_type.has_option_or_result() {
with_modifier_kind := if expr.left_type.has_flag(.option) {
'an Option'
} else {
'a Result'
}
with_modifier := if expr.left_type.has_flag(.option) { '?' } else { '!' }
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',
expr.left.pos)
}
}
ast.CastExpr {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/option_map_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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
20 | }
21 | }
22 | assert op2.opmap['1'] == 1.0
| ~~~~~
23 | }
23 changes: 23 additions & 0 deletions vlib/v/checker/tests/option_map_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct OptionalMap {
opmap ?map[string]f64
}

fn main() {
op := OptionalMap{
opmap: {
'1': 0.0
}
}
assert op.opmap or {
{
'1': 0.0
}
}['1'] == 0.0

op2 := OptionalMap{
opmap: {
'1': 1.0
}
}
assert op2.opmap['1'] == 1.0
}