mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow nil assignment on non pointer struct fields (#16056)
This commit is contained in:
parent
fb3d093e01
commit
3e33f4a11d
@ -116,6 +116,12 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if field.default_expr is ast.UnsafeExpr {
|
||||
if field.default_expr.expr is ast.Nil && !field.typ.is_ptr()
|
||||
&& c.table.sym(field.typ).kind != .function && !field.typ.is_pointer() {
|
||||
c.error('cannot assign `nil` to a non-pointer field', field.type_pos)
|
||||
}
|
||||
}
|
||||
if field.default_expr is ast.IntegerLiteral {
|
||||
if field.default_expr.val == '0' {
|
||||
c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
|
||||
|
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/struct_non_ptr_field_nil_default_value_err.vv:4:7: error: cannot assign `nil` to a non-pointer field
|
||||
2 |
|
||||
3 | struct Foo {
|
||||
4 | bar SomeInterface = unsafe{ nil }
|
||||
| ~~~~~~~~~~~~~
|
||||
5 | }
|
||||
6 | fn main() {
|
@ -0,0 +1,9 @@
|
||||
interface SomeInterface{}
|
||||
|
||||
struct Foo {
|
||||
bar SomeInterface = unsafe{ nil }
|
||||
}
|
||||
fn main() {
|
||||
foo := Foo{}
|
||||
dump(foo)
|
||||
}
|
Loading…
Reference in New Issue
Block a user