1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix missing check for option value on non-optional struct field assignment (#17785)

This commit is contained in:
Heptalon 2023-03-27 04:11:56 +02:00 committed by GitHub
parent f08b88223d
commit 6b4fb0fc3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -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_

View File

@ -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
@ -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) {