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

checker: fix wrong struct warn about inited fields (#17678)

This commit is contained in:
Felipe Pena 2023-03-16 16:42:00 -03:00 committed by GitHub
parent 6709b2de0f
commit 658b116d07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 11 deletions

View File

@ -2687,7 +2687,8 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
if node.unresolved {
return c.expr(c.table.resolve_init(node, c.unwrap_generic(node.typ)))
}
return c.struct_init(mut node, false)
mut inited_fields := []string{}
return c.struct_init(mut node, false, mut inited_fields)
}
ast.TypeNode {
if !c.inside_x_is_type && node.typ.has_flag(.generic) && unsafe { c.table.cur_fn != 0 }

View File

@ -269,7 +269,7 @@ fn minify_sort_fn(a &ast.StructField, b &ast.StructField) int {
}
}
fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_init bool) ast.Type {
fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_init bool, mut inited_fields []string) ast.Type {
util.timing_start(@METHOD)
defer {
util.timing_measure_cumulative(@METHOD)
@ -450,7 +450,6 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
info_fields_sorted = info.fields.clone()
info_fields_sorted.sort(a.i < b.i)
}
mut inited_fields := []string{}
for i, mut field in node.fields {
mut field_info := ast.StructField{}
mut field_name := ''
@ -567,6 +566,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
// need to modify here accordingly.
fields := c.table.struct_fields(type_sym)
mut checked_types := []ast.Type{}
for i, field in fields {
if field.name in inited_fields {
continue
@ -653,7 +653,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
pos: node.pos
typ: field.typ
}
c.struct_init(mut zero_struct_init, true)
c.struct_init(mut zero_struct_init, true, mut inited_fields)
}
}
for embed in info.embeds {
@ -661,7 +661,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
pos: node.pos
typ: embed
}
c.struct_init(mut zero_struct_init, true)
c.struct_init(mut zero_struct_init, true, mut inited_fields)
}
// println('>> checked_types.len: $checked_types.len | checked_types: $checked_types | type_sym: $type_sym.name ')
}

View File

@ -1,7 +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 {
vlib/v/checker/tests/struct_embed_required_field_err.vv:12:7: error: field `Bar.bar` must be initialized
10 |
11 | fn main() {
12 | b := Bar {
| ~~~~~
12 | foo: 1
13 | }
13 | foo: 1
14 | }

View File

@ -1,5 +1,6 @@
struct Foo {
foo u8 [required]
bar u8 [required]
}
struct Bar {

View File

@ -0,0 +1,21 @@
struct Inner {
ptr &u8
}
struct Outter {
Inner
mut:
size int
}
fn new_outter() &Outter {
return &Outter{
ptr: vcalloc(0)
size: 1
}
}
fn main () {
outter := new_outter()
println(outter)
}