Skip to content

Commit

Permalink
checker: fix field ?&Type without default value (#19786)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 7, 2023
1 parent 6fa9a84 commit 09f3e1e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
18 changes: 10 additions & 8 deletions vlib/v/checker/struct.v
Expand Up @@ -727,14 +727,16 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
node.pos)
continue
}
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)
if !field.typ.has_flag(.option) {
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
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/checker/tests/option_ref_init_err.out
@@ -0,0 +1,21 @@
vlib/v/checker/tests/option_ref_init_err.vv:9:18: warning: unnecessary default value of `none`: struct fields are zeroed by default
7 | mut:
8 | a0 ?&Viewport
9 | a1 ?&Viewport = none
| ~~~~
10 | a2 ?&Viewport = none
11 | a3 ?&Viewport = unsafe { nil }
vlib/v/checker/tests/option_ref_init_err.vv:10:18: warning: unnecessary default value of `none`: struct fields are zeroed by default
8 | a0 ?&Viewport
9 | a1 ?&Viewport = none
10 | a2 ?&Viewport = none
| ~~~~
11 | a3 ?&Viewport = unsafe { nil }
12 | a4 ?&Viewport = nil
vlib/v/checker/tests/option_ref_init_err.vv:12:18: error: `nil` is only allowed in `unsafe` code
10 | a2 ?&Viewport = none
11 | a3 ?&Viewport = unsafe { nil }
12 | a4 ?&Viewport = nil
| ~~~
13 | }
14 |
17 changes: 17 additions & 0 deletions vlib/v/checker/tests/option_ref_init_err.vv
@@ -0,0 +1,17 @@
struct Viewport {
mut:
parent &Window
}

pub struct Window {
mut:
a0 ?&Viewport
a1 ?&Viewport = none
a2 ?&Viewport = none
a3 ?&Viewport = unsafe { nil }
a4 ?&Viewport = nil
}

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

0 comments on commit 09f3e1e

Please sign in to comment.