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

checker: fix embedded structure initialization warnings (#18385)

This commit is contained in:
yuyi 2023-06-09 22:44:15 +08:00 committed by GitHub
parent dd1d5bca1b
commit 42db392e76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -586,6 +586,16 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
field.expr.pos)
}
}
// all the fields of initialized embedded struct are ignored, they are considered initialized
sym := c.table.sym(field.typ)
if field.name.len > 0 && field.name[0].is_capital() && sym.kind == .struct_
&& sym.language == .v {
struct_fields := c.table.struct_fields(sym)
for struct_field in struct_fields {
inited_fields << struct_field.name
}
}
}
// Check uninitialized refs/sum types
// The variable `fields` contains two parts, the first part is the same as info.fields,

View File

@ -0,0 +1,5 @@
Outer{
Embedded: Embedded{
a: &nil
}
}

View File

@ -0,0 +1,20 @@
struct Embedded {
a &int
}
fn new_embedded() Embedded {
return Embedded{
a: unsafe { nil }
}
}
struct Outer {
Embedded
}
fn main() {
o := Outer{
Embedded: new_embedded()
}
println(o)
}