checker: temporary error for optional struct fields (#11293) (#11534)

pull/11550/head
Rémi 2021-09-18 20:22:24 +00:00 committed by GitHub
parent 0b4e03ad5c
commit e76be4ba37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -1173,6 +1173,10 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
node.fields[i].typ = expr_type
node.fields[i].expected_type = field_info.typ
}
if field_info.typ.has_flag(.optional) {
c.error('field `$field_info.name` is optional, but initialization of optional fields currently unsupported',
field.pos)
}
if expr_type.is_ptr() && expected_type.is_ptr() {
if mut field.expr is ast.Ident {
if mut field.expr.obj is ast.Var {

View File

@ -54,6 +54,13 @@ vlib/v/checker/tests/optional_fn_err.vv:46:10: error: bar() returns an option, s
| ~~~~~~
47 | opt: bar(0),
48 | }
vlib/v/checker/tests/optional_fn_err.vv:47:3: error: field `opt` is optional, but initialization of optional fields currently unsupported
45 | f: fn (_ int) {},
46 | value: bar(0),
47 | opt: bar(0),
| ~~~~~~~~~~~
48 | }
49 | v.add(bar(0)) // call method
vlib/v/checker/tests/optional_fn_err.vv:49:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end
47 | opt: bar(0),
48 | }