Skip to content

Commit

Permalink
checker: fix missing check for option value on non-optional struct fi…
Browse files Browse the repository at this point in the history
…eld assignment (#17785)
  • Loading branch information
Heptalon committed Mar 27, 2023
1 parent f08b882 commit 6b4fb0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
}
if !field_info.typ.has_flag(.option) && !field.typ.has_flag(.result) {
expr_type = c.check_expr_opt_call(field.expr, expr_type)
if expr_type.has_flag(.option) {
c.error('cannot assign an Option value to a non-option struct field',
field.pos)
}
}
expr_type_sym := c.table.sym(expr_type)
if field_type_sym.kind == .voidptr && expr_type_sym.kind == .struct_
Expand Down
13 changes: 10 additions & 3 deletions vlib/v/checker/tests/option_fn_err.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
vlib/v/checker/tests/option_fn_err.vv:40:9: error: assert can be used only with `bool` expressions, but found `bool` instead
38 |
38 |
39 | // assert
40 | assert bar(true)
| ~~~~~~~~~
41 |
41 |
42 | // struct
vlib/v/checker/tests/option_fn_err.vv:45:3: error: cannot assign an Option value to a non-option struct field
43 | mut v := Data{
44 | f: fn (_ int) {}
45 | value: bar(0)
| ~~~~~~~~~~~~~
46 | opt: bar(0)
47 | }
vlib/v/checker/tests/option_fn_err.vv:60:13: error: cannot use Option or Result as index (array type `[]int`)
58 | _ := [1]int{init: bar(0)}
59 | // index
Expand All @@ -31,5 +38,5 @@ vlib/v/checker/tests/option_fn_err.vv:69:18: error: type mismatch, `bar` must re
68 | println(arr.any(bar(true)))
69 | println(arr.all(bar(true)))
| ~~~~~~~~~
70 |
70 |
71 | match bar(0) {

0 comments on commit 6b4fb0f

Please sign in to comment.