From 621574c12a0b9ae5e02767d4d27ff158cf2dcfd1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 7 May 2022 05:51:04 +0800 Subject: [PATCH] checker: fix error for generic method on nested struct (#14322) --- vlib/v/checker/fn.v | 3 +- .../generics_method_on_nested_struct2_test.v | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics_method_on_nested_struct2_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index abf4025a07..42eea161b3 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1223,7 +1223,8 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { rec_sym := c.table.sym(node.left_type) rec_is_generic := left_type.has_flag(.generic) if rec_sym.info is ast.Struct { - if rec_is_generic && node.concrete_types.len == 0 { + if rec_is_generic && node.concrete_types.len == 0 + && method.generic_names.len == rec_sym.info.generic_types.len { node.concrete_types = rec_sym.info.generic_types } else if !rec_is_generic && rec_sym.info.concrete_types.len > 0 && node.concrete_types.len > 0 diff --git a/vlib/v/tests/generics_method_on_nested_struct2_test.v b/vlib/v/tests/generics_method_on_nested_struct2_test.v new file mode 100644 index 0000000000..cdd9080e78 --- /dev/null +++ b/vlib/v/tests/generics_method_on_nested_struct2_test.v @@ -0,0 +1,38 @@ +struct Outer { +mut: + inner Inner +} + +struct Inner { + val T +} + +fn (mut i Inner) next(input S) f64 { + $if S is f32 { + return 32 + } $else { + panic('"$S.name" is not supported') + return 0 + } +} + +fn (mut o Outer) next(input S) f64 { + $if S is f32 { + return o.inner.next(input) + } $else { + panic('"$S.name" is not supported') + return 0 + } +} + +fn test_generics_method_on_nested_struct() { + mut outer := Outer{ + inner: Inner{ + val: 1.1 + } + } + + res := outer.next(f32(99.0)) + println(res) + assert res == 32.0 +}