From a1aca4c5788114213171bea3a308474b534a7748 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 1 Aug 2023 12:20:33 +0800 Subject: [PATCH] checker: fix generic struct field init recursively (related #19014) (#19025) --- vlib/v/checker/struct.v | 12 +++++++++--- ...generics_struct_field_with_default_fn_type_test.v | 1 - 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 3b9204d005..65561da3f5 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -318,6 +318,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini } } struct_sym := c.table.sym(node.typ) + mut old_inside_generic_struct_init := false + mut old_cur_struct_generic_types := []ast.Type{} + mut old_cur_struct_concrete_types := []ast.Type{} if struct_sym.info is ast.Struct { // check if the generic param types have been defined for ct in struct_sym.info.concrete_types { @@ -357,13 +360,16 @@ 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.generic_types.len == struct_sym.info.concrete_types.len { + old_inside_generic_struct_init = c.inside_generic_struct_init + old_cur_struct_generic_types = c.cur_struct_generic_types.clone() + old_cur_struct_concrete_types = c.cur_struct_concrete_types.clone() c.inside_generic_struct_init = true c.cur_struct_generic_types = struct_sym.info.generic_types.clone() c.cur_struct_concrete_types = struct_sym.info.concrete_types.clone() defer { - c.inside_generic_struct_init = false - c.cur_struct_generic_types = [] - c.cur_struct_concrete_types = [] + c.inside_generic_struct_init = old_inside_generic_struct_init + c.cur_struct_generic_types = old_cur_struct_generic_types + c.cur_struct_concrete_types = old_cur_struct_concrete_types } } } else if struct_sym.info is ast.Alias { diff --git a/vlib/v/tests/generics_struct_field_with_default_fn_type_test.v b/vlib/v/tests/generics_struct_field_with_default_fn_type_test.v index 16d3547bf7..30b5a0cd14 100644 --- a/vlib/v/tests/generics_struct_field_with_default_fn_type_test.v +++ b/vlib/v/tests/generics_struct_field_with_default_fn_type_test.v @@ -7,7 +7,6 @@ struct BloomFilter1[T] { [heap] struct BloomFilter2[T] { - // TODO V bug hash_func fn (T) u32 = default_cb[T] // hash function, input [T] , output u32 table_size int // every entry is one-bit, packed into `table` num_functions int // 1~16