checker: fix field ?&Type without default value (#19786)

This commit is contained in:
Felipe Pena 2023-11-07 16:25:20 -03:00 committed by GitHub
parent 6fa9a84753
commit 09f3e1e971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 8 deletions

View File

@ -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

View File

@ -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 |

View File

@ -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{}
}