diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 28b3241ec4..09a8e1b1cd 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -189,8 +189,8 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { if is_decl || is_shared_re_assign { // check generic struct init and return unwrap generic struct type if mut right is ast.StructInit { + c.expr(mut right) if right.typ.has_flag(.generic) { - c.expr(mut right) right_type = right.typ } } else if mut right is ast.PrefixExpr { diff --git a/vlib/v/tests/struct_field_init_with_generic_fn_test.v b/vlib/v/tests/struct_field_init_with_generic_fn_test.v new file mode 100644 index 0000000000..405a962b41 --- /dev/null +++ b/vlib/v/tests/struct_field_init_with_generic_fn_test.v @@ -0,0 +1,25 @@ +struct ClassInfo { + func fn () = unsafe { nil } +} + +pub fn func2[T]() { +} + +pub fn func1[T]() { + ci := ClassInfo{ + func: func2[T] + } + ci.func() +} + +struct Struct1 {} + +struct Struct2 {} + +fn test_struct_field_init_with_generic_fn() { + func1[i32]() + func1[bool]() + func1[Struct1]() + func1[Struct2]() + assert true +}