Skip to content

Commit

Permalink
checker: fix struct field init with generic fn variable (fix #20847) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Feb 20, 2024
1 parent 0b792c5 commit 13fbf35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/assign.v
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions 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
}

0 comments on commit 13fbf35

Please sign in to comment.