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: check struct reference fields uninitialized (fix #19559) #19607

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5933,7 +5933,7 @@ cause a panic.

```v
struct Node {
a &Node
a &Node = unsafe { nil } // Auto-initialized to nil, use with caution!
b &Node = unsafe { nil } // Auto-initialized to nil, use with caution!
}

Expand Down
15 changes: 8 additions & 7 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if field.name in inited_fields {
continue
}
sym := c.table.sym(field.typ)
sym := c.table.final_sym(field.typ)
if field.name.len > 0 && field.name[0].is_capital() && sym.info is ast.Struct
&& sym.language == .v {
// struct embeds
Expand All @@ -712,6 +712,10 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
}
}
}
if sym.kind == .struct_ {
c.check_ref_fields_initialized(sym, mut checked_types, '${type_sym.name}.${field.name}',
node)
}
continue
}
if field.typ.is_ptr() && !field.typ.has_flag(.shared_f)
Expand All @@ -724,12 +728,6 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if sym.kind == .struct_ {
c.check_ref_fields_initialized(sym, mut checked_types, '${type_sym.name}.${field.name}',
node)
} else if sym.kind == .alias {
parent_sym := c.table.sym((sym.info as ast.Alias).parent_type)
if parent_sym.kind == .struct_ {
c.check_ref_fields_initialized(parent_sym, mut checked_types,
'${type_sym.name}.${field.name}', node)
}
}
// Do not allow empty uninitialized interfaces
mut has_noinit := false
Expand Down Expand Up @@ -858,6 +856,9 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut
} else if sym.kind == .alias {
psym := c.table.sym((sym.info as ast.Alias).parent_type)
if psym.kind == .struct_ {
if field.typ in checked_types {
continue
}
checked_types << field.typ
c.check_ref_fields_initialized(psym, mut checked_types, '${linked_name}.${field.name}',
node)
Expand Down
33 changes: 0 additions & 33 deletions vlib/v/checker/tests/struct_ref_fields_uninitialized_err.out

This file was deleted.

33 changes: 33 additions & 0 deletions vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.vv:25:7: error: reference field `Outer.c1.b` must be initialized (part of struct `ContainsRef`)
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.vv:25:7: error: reference field `ContainsRef.b` must be initialized
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.vv:25:7: error: reference field `Outer.c2.b` must be initialized (part of struct `ContainsRef`)
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.vv:26:7: error: reference field `Struct.ref2` must be initialized
24 | fn main() {
25 | _ := Outer{}
26 | _ := Struct{}
| ~~~~~~~~
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_a.vv:26:7: error: reference field `EmbedStruct.ref2` must be initialized
24 | fn main() {
25 | _ := Outer{}
26 | _ := Struct{}
| ~~~~~~~~
27 | }
26 changes: 26 additions & 0 deletions vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.vv:8:32: warning: unnecessary default value of `none`: struct fields are zeroed by default
6 | pub struct Window {
7 | mut:
8 | desktop_viewport ?&Viewport = none
| ~~~~
9 | phone_viewport ?&Viewport = none
10 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.vv:9:32: warning: unnecessary default value of `none`: struct fields are zeroed by default
7 | mut:
8 | desktop_viewport ?&Viewport = none
9 | phone_viewport ?&Viewport = none
| ~~~~
10 | }
11 |
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.vv:13:8: error: reference field `Window.desktop_viewport.parent` must be initialized (part of struct `Viewport`)
11 |
12 | fn main() {
13 | _ := &Window{}
| ~~~~~~~~
14 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.vv:13:8: error: reference field `Window.phone_viewport.parent` must be initialized (part of struct `Viewport`)
11 |
12 | fn main() {
13 | _ := &Window{}
| ~~~~~~~~
14 | }
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/struct_ref_fields_uninitialized_err_b.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct Viewport {
mut:
parent &Window
}

pub struct Window {
mut:
desktop_viewport ?&Viewport = none
phone_viewport ?&Viewport = none
}

fn main() {
_ := &Window{}
}
Loading