Skip to content

Commit

Permalink
checker: fix generic struct init with update expr (fix vlang#17824) (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored and l1mey112 committed Jun 7, 2023
1 parent cf70fbb commit 587b4db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
}
}
if struct_sym.info.generic_types.len > 0 && struct_sym.info.concrete_types.len == 0
&& !node.is_short_syntax && c.table.cur_concrete_types.len != 0 {
&& !node.is_short_syntax && c.table.cur_concrete_types.len != 0
&& !is_field_zero_struct_init {
if node.generic_types.len == 0 {
c.error('generic struct init must specify type parameter, e.g. Foo[T]',
node.pos)
Expand Down Expand Up @@ -648,8 +649,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
node.pos)
}
}
if !field.has_default_expr && field.name !in inited_fields && !field.typ.is_ptr()
&& !field.typ.has_flag(.option) && c.table.final_sym(field.typ).kind == .struct_ {
if !node.has_update_expr && !field.has_default_expr && field.name !in inited_fields
&& !field.typ.is_ptr() && !field.typ.has_flag(.option)
&& c.table.final_sym(field.typ).kind == .struct_ {
mut zero_struct_init := ast.StructInit{
pos: node.pos
typ: field.typ
Expand Down
39 changes: 39 additions & 0 deletions vlib/v/tests/generic_struct_init_with_update_expr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub struct Time[T] {
pub mut:
start T
end T
}

pub struct Transform[T] {
pub mut:
time Time[T]
before []T
after []T
}

pub fn (t Transform[T]) clone() Transform[T] {
return Transform[T]{
...t
}
}

pub fn (t Transform[T]) default() Transform[T] {
return Transform[T]{}
}

fn test_generic_struct_init_with_update_expr() {
a := Transform[f64]{
before: [0.0, 0.0]
after: [320.0, 240.0]
}

b := a.clone()
println(b)
assert b.before == a.before
assert b.after == a.after

c := a.default()
println(c)
assert c.before.len == 0
assert c.after.len == 0
}

0 comments on commit 587b4db

Please sign in to comment.