From ab20b8c53a37b644fd9a8cdf4e3f42bbe9f88054 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 25 Jul 2021 20:54:22 +0800 Subject: [PATCH] cgen: fix generics interface with multi generic structs (#10950) --- vlib/v/gen/c/fn.v | 4 ++ ...nterface_with_multi_generic_structs_test.v | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 vlib/v/tests/generics_interface_with_multi_generic_structs_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index d217734769..781742e600 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -180,6 +180,10 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { name = util.replace_op(name) } if node.is_method { + unwrapped_rec_sym := g.table.get_type_symbol(g.unwrap_generic(node.receiver.typ)) + if unwrapped_rec_sym.kind == .placeholder { + return + } name = g.cc_type(node.receiver.typ, false) + '_' + name // name = g.table.get_type_symbol(node.receiver.typ).name + '_' + name } diff --git a/vlib/v/tests/generics_interface_with_multi_generic_structs_test.v b/vlib/v/tests/generics_interface_with_multi_generic_structs_test.v new file mode 100644 index 0000000000..24f27e17bd --- /dev/null +++ b/vlib/v/tests/generics_interface_with_multi_generic_structs_test.v @@ -0,0 +1,58 @@ +interface Iter { + next() ?T +} + +fn (it Iter) skip(n int) Iter { + return SkipIter{ + n: n + } +} + +struct ArrIter { + data []T +mut: + index int +} + +fn (mut it ArrIter) next() ?T { + if it.index >= it.data.len { + return none + } + defer { + it.index++ + } + return it.data[it.index] +} + +struct SkipIter { +mut: + n int + iter Iter +} + +fn (mut it SkipIter) next() ?T { + for it.n > 0 { + it.iter.next() ? + it.n-- + } + return it.iter.next() +} + +fn test_generics_interface_with_multi_generic_structs() { + x := Iter(ArrIter{ + data: [1, 2, 3] + }) + println(x) + + mut ret := x.next() or { 0 } + println(ret) + assert ret == 1 + + ret = x.next() or { 0 } + println(ret) + assert ret == 2 + + ret = x.next() or { 0 } + println(ret) + assert ret == 3 +}