From 13fbf35f66b75e7035994fefc19e18d7cafa033d Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 20 Feb 2024 23:34:55 +0800 Subject: [PATCH] checker: fix struct field init with generic fn variable (fix #20847) (#20878) --- vlib/v/checker/assign.v | 2 +- .../struct_field_init_with_generic_fn_test.v | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/struct_field_init_with_generic_fn_test.v 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 +}