Skip to content

Commit

Permalink
checker: fix embedded structure initialization warnings (#18385)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jun 9, 2023
1 parent dd1d5bc commit 42db392
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/v/checker/struct.v
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/slow_tests/inout/struct_with_embed_field_init.out
@@ -0,0 +1,5 @@
Outer{
Embedded: Embedded{
a: &nil
}
}
20 changes: 20 additions & 0 deletions vlib/v/slow_tests/inout/struct_with_embed_field_init.vv
@@ -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)
}

0 comments on commit 42db392

Please sign in to comment.