From 246c09db962b739157642e492e177cfc799003b9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 21 Mar 2021 00:29:13 +0800 Subject: [PATCH] checker: fix generics in big_struct (fix #9373) (#9383) --- vlib/v/checker/check_types.v | 3 +++ vlib/v/gen/c/fn.v | 4 +++ .../generics_in_big_struct_method_test.v | 27 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 vlib/v/tests/generics_in_big_struct_method_test.v diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 3e36792a12..89d7c3208c 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -443,6 +443,9 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) { param_type_sym := c.table.get_type_symbol(param.typ) if param.typ.has_flag(.generic) && param_type_sym.name == gt_name { typ = c.table.mktyp(arg.typ) + if arg.expr.is_auto_deref_var() { + typ = typ.deref() + } break } arg_sym := c.table.get_type_symbol(arg.typ) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index e35fbb779a..e82abcac46 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1018,6 +1018,10 @@ fn (mut g Gen) call_args(node ast.CallExpr) { g.write('/*af arg*/' + name) } } else { + if node.generic_types.len > 0 && arg.expr.is_auto_deref_var() && !arg.is_mut + && !expected_types[i].is_ptr() { + g.write('*') + } g.ref_or_deref_arg(arg, expected_types[i], node.language) } } else { diff --git a/vlib/v/tests/generics_in_big_struct_method_test.v b/vlib/v/tests/generics_in_big_struct_method_test.v new file mode 100644 index 0000000000..c86a596efc --- /dev/null +++ b/vlib/v/tests/generics_in_big_struct_method_test.v @@ -0,0 +1,27 @@ +struct Product { +pub mut: + id int = 2 + sku string = 'ABCD' + ean13 string = 'EFGH' + + collections string + ptype string + featured_image string + featured_media string + handle string + variant int +} + +pub fn (p Product) save() string { + return do_something(p) +} + +fn do_something(p T) string { + return 'whatever' +} + +fn test_generics_in_big_struct_method() { + mut p := Product{} + println(p.save()) + assert p.save() == 'whatever' +}