From b94c5c2a9c4fba381b711126b52c83a92f31e758 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 4 Jan 2022 22:02:53 +0800 Subject: [PATCH] ast: fix fn_type_signature for anon functions with alias arguments (#13024) --- vlib/v/ast/table.v | 15 +++++++-- vlib/v/tests/anon_fn_with_alias_args_test.v | 36 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/anon_fn_with_alias_args_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 758f3d4e71..7117f3062d 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -200,8 +200,13 @@ pub fn (t &Table) fn_type_signature(f &Fn) string { // TODO: for now ignore mut/pts in sig for now typ := arg.typ.set_nr_muls(0) arg_type_sym := t.sym(typ) - sig += arg_type_sym.str().to_lower().replace_each(['.', '__', '&', '', '[', 'arr_', 'chan ', - 'chan_', 'map[', 'map_of_', ']', '_to_', '<', '_T_', ',', '_', ' ', '', '>', '']) + if arg_type_sym.kind == .alias { + sig += arg_type_sym.cname + } else { + sig += arg_type_sym.str().to_lower().replace_each(['.', '__', '&', '', '[', 'arr_', + 'chan ', 'chan_', 'map[', 'map_of_', ']', '_to_', '<', '_T_', ',', '_', ' ', '', + '>', '']) + } if i < f.params.len - 1 { sig += '_' } @@ -209,7 +214,11 @@ pub fn (t &Table) fn_type_signature(f &Fn) string { if f.return_type != 0 && f.return_type != void_type { sym := t.sym(f.return_type) opt := if f.return_type.has_flag(.optional) { 'option_' } else { '' } - sig += '__$opt$sym.kind' + if sym.kind == .alias { + sig += '__$opt$sym.cname' + } else { + sig += '__$opt$sym.kind' + } } return sig } diff --git a/vlib/v/tests/anon_fn_with_alias_args_test.v b/vlib/v/tests/anon_fn_with_alias_args_test.v new file mode 100644 index 0000000000..98b1b264a2 --- /dev/null +++ b/vlib/v/tests/anon_fn_with_alias_args_test.v @@ -0,0 +1,36 @@ +type MyString = string +type MyInt = int + +struct S1 { + x fn (a int) MyString +} + +struct S2 { + y fn (a int) MyInt +} + +fn get_string(a int) MyString { + return '$a' +} + +fn get_int(a int) MyInt { + return a +} + +fn test_anon_fn_with_alias_args() { + s1 := S1{ + x: get_string + } + println(s1.x) + ret1 := s1.x(22) + println(ret1) + assert ret1 == '22' + + s2 := S2{ + y: get_int + } + println(s2.y) + ret2 := s2.y(22) + println(ret2) + assert ret2 == 22 +}