diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 2e5a8d513a..01c634c4e2 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -163,6 +163,10 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) { field.default_expr.pos) } } + if field.typ.has_flag(.option) && field.default_expr is ast.None { + c.warn('unnecessary default value of `none`: struct fields are zeroed by default', + field.default_expr.pos) + } continue } if field.typ in ast.unsigned_integer_type_idxs { diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index e903adfb5a..72004521e6 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -346,7 +346,11 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { return true } - if field.typ.has_flag(.option) { + if field.default_expr is ast.None { + tmp_var := g.new_tmp_var() + g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var) + return true + } else if field.typ.has_flag(.option) { tmp_var := g.new_tmp_var() g.expr_with_tmp_var(field.default_expr, field.default_expr_typ, field.typ, tmp_var) diff --git a/vlib/v/tests/generic_struct_recursive_test.v b/vlib/v/tests/generic_struct_recursive_test.v new file mode 100644 index 0000000000..e58d2e3fe9 --- /dev/null +++ b/vlib/v/tests/generic_struct_recursive_test.v @@ -0,0 +1,20 @@ +struct Node[T] { +mut: + value T + next ?&Node[T] = none +} + +fn test_main() { + mut n := Node[int]{ + value: 1 + } + mut m := Node[int]{ + value: 2 + } + n.next = &m + a := n.next or { return } + dump(a) + dump(m) + dump(n) + assert a.value == 2 +}