Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix embedded structure initialization warnings (fix #18376) #18385

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
}