1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix generic struct init with update expr (fix #17824) (#17827)

This commit is contained in:
yuyi 2023-03-31 15:59:52 +08:00 committed by GitHub
parent ff0adba8a9
commit 6ff1c0a0b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -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 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 { if node.generic_types.len == 0 {
c.error('generic struct init must specify type parameter, e.g. Foo[T]', c.error('generic struct init must specify type parameter, e.g. Foo[T]',
node.pos) node.pos)
@ -648,8 +649,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
node.pos) node.pos)
} }
} }
if !field.has_default_expr && field.name !in inited_fields && !field.typ.is_ptr() if !node.has_update_expr && !field.has_default_expr && field.name !in inited_fields
&& !field.typ.has_flag(.option) && c.table.final_sym(field.typ).kind == .struct_ { && !field.typ.is_ptr() && !field.typ.has_flag(.option)
&& c.table.final_sym(field.typ).kind == .struct_ {
mut zero_struct_init := ast.StructInit{ mut zero_struct_init := ast.StructInit{
pos: node.pos pos: node.pos
typ: field.typ typ: field.typ

View File

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