From ac6289301c6fa23313618456dd5ed89d920a97e0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 30 May 2023 20:24:54 +0800 Subject: [PATCH] ast, cgen: fix generic struct with inconsistent generic types (fix #18254) (#18301) --- vlib/v/gen/c/fn.v | 14 +++++----- ...t_with_inconsistent_generic_types_1_test.v | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/generics_struct_with_inconsistent_generic_types_1_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 802e27a8e3..f4f5e19146 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -222,7 +222,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { eprintln('INFO: compile with `v -live ${g.pref.path} `, if you want to use the [live] function ${node.name} .') } - mut name := g.c_fn_name(node) or { return } + mut name := g.c_fn_name(node) mut type_name := g.typ(g.unwrap_generic(node.return_type)) if node.return_type.has_flag(.generic) && g.table.sym(node.return_type).kind == .array_fixed { @@ -432,17 +432,17 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { } } -fn (mut g Gen) c_fn_name(node &ast.FnDecl) !string { +fn (mut g Gen) c_fn_name(node &ast.FnDecl) string { mut name := node.name if name in ['+', '-', '*', '/', '%', '<', '=='] { name = util.replace_op(name) } if node.is_method { - unwrapped_rec_sym := g.table.sym(g.unwrap_generic(node.receiver.typ)) - if unwrapped_rec_sym.kind == .placeholder { - return error('none') + unwrapped_rec_typ := g.unwrap_generic(node.receiver.typ) + name = g.cc_type(unwrapped_rec_typ, false) + '_' + name + if g.table.sym(unwrapped_rec_typ).kind == .placeholder { + name = name.replace_each(['[', '_T_', ']', '']) } - name = g.cc_type(node.receiver.typ, false) + '_' + name } if node.language == .c { name = util.no_dots(name) @@ -605,7 +605,7 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic typ = g.table.sym(typ).array_info().elem_type.set_flag(.variadic) } param_type_sym := g.table.sym(typ) - mut param_type_name := g.typ(typ) // util.no_dots(param_type_sym.name) + mut param_type_name := g.typ(typ).replace_each(['[', '_T_', ']', '']) if param_type_sym.kind == .function && !typ.has_flag(.option) { info := param_type_sym.info as ast.FnType func := info.func diff --git a/vlib/v/tests/generics_struct_with_inconsistent_generic_types_1_test.v b/vlib/v/tests/generics_struct_with_inconsistent_generic_types_1_test.v new file mode 100644 index 0000000000..d6b9196345 --- /dev/null +++ b/vlib/v/tests/generics_struct_with_inconsistent_generic_types_1_test.v @@ -0,0 +1,26 @@ +pub struct Generic[D] { +pub mut: + field D +} + +pub fn (j Generic[D]) foo() D { + return j.field +} + +pub fn new_generic[T](field T) Generic[T] { + return Generic[T]{ + field: field + } +} + +fn test_generic_struct_with_inconsistent_generic_types() { + my_generic1 := new_generic[int](2) + r1 := my_generic1.foo() + println(r1) + assert r1 == 2 + + my_generic2 := new_generic[string]('hello') + r2 := my_generic2.foo() + println(r2) + assert r2 == 'hello' +}