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

checker: check struct embed required field (#17595)

This commit is contained in:
yuyi 2023-03-11 17:12:26 +08:00 committed by GitHub
parent 0370116562
commit b691e1e24a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 0 deletions

View File

@ -656,6 +656,13 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
c.struct_init(mut zero_struct_init, true)
}
}
for embed in info.embeds {
mut zero_struct_init := ast.StructInit{
pos: node.pos
typ: embed
}
c.struct_init(mut zero_struct_init, true)
}
// println('>> checked_types.len: $checked_types.len | checked_types: $checked_types | type_sym: $type_sym.name ')
}
else {}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_embed_required_field_err.vv:11:7: error: field `Foo.foo` must be initialized
9 |
10 | fn main() {
11 | b := Bar {
| ~~~~~
12 | foo: 1
13 | }

View File

@ -0,0 +1,15 @@
struct Foo {
foo u8 [required]
}
struct Bar {
Foo
foo u8 [required]
}
fn main() {
b := Bar {
foo: 1
}
println(b)
}

View File

@ -25,3 +25,9 @@ vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:26:7: warning: refer
26 | _ := Struct{}
| ~~~~~~~~
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:26:7: warning: reference field `EmbedStruct.ref2` must be initialized
24 | fn main() {
25 | _ := Outer{}
26 | _ := Struct{}
| ~~~~~~~~
27 | }