From 6688c0f3d718c508a8f150992d95c0103049a5cd Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 19 Jan 2023 00:59:02 +0800 Subject: [PATCH] checker: fix generic fn infering error with alias argument (#17026) --- vlib/v/checker/check_types.v | 4 ++-- .../inout/generic_fn_with_alias_arg.out | 12 ++++++++++ .../inout/generic_fn_with_alias_arg.vv | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 vlib/v/slow_tests/inout/generic_fn_with_alias_arg.out create mode 100644 vlib/v/slow_tests/inout/generic_fn_with_alias_arg.vv diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index c2363644ad..41a025f659 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -865,7 +865,7 @@ fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) { if param.typ.has_flag(.generic) && param_type_sym.name == gt_name { typ = ast.mktyp(arg.typ) - sym := c.table.sym(arg.typ) + sym := c.table.final_sym(arg.typ) if sym.info is ast.FnType { mut func_ := sym.info.func func_.name = '' @@ -898,7 +898,7 @@ fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) { typ = typ.set_nr_muls(0) } } else if param.typ.has_flag(.generic) { - arg_sym := c.table.sym(arg.typ) + arg_sym := c.table.final_sym(arg.typ) if param.typ.has_flag(.variadic) { typ = ast.mktyp(arg.typ) } else if arg_sym.kind == .array && param_type_sym.kind == .array { diff --git a/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.out b/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.out new file mode 100644 index 0000000000..e82bd32020 --- /dev/null +++ b/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.out @@ -0,0 +1,12 @@ +MyStruct[int]{ + a: 0 +} +MyStructAlias(MyStruct[int]{ + a: 0 +}) +MyStruct[int]{ + a: 0 +} +MyStructAlias(MyStruct[int]{ + a: 0 +}) diff --git a/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.vv b/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.vv new file mode 100644 index 0000000000..9db6449fbd --- /dev/null +++ b/vlib/v/slow_tests/inout/generic_fn_with_alias_arg.vv @@ -0,0 +1,23 @@ +struct MyStruct[T] { + a T +} + +fn mprint[T](s MyStruct[T]) { + println(s) +} + +fn mprint_with_alias(s MyStructAlias) { + println(s) +} + +type MyStructAlias = MyStruct[int] + +fn main() { + a := MyStruct[int]{} + mprint(a) // works + mprint_with_alias(a) // works + + b := MyStructAlias{} + mprint(b) // does not work, requires mprint[int](b) + mprint_with_alias(b) // works +}