From ebc34c237b505ecfd03c557c3af72189cfb6815a Mon Sep 17 00:00:00 2001 From: spaceface Date: Thu, 15 Jul 2021 21:15:25 +0200 Subject: [PATCH] cgen: fix generating invalid Option decls for generic interfaces (#10817) --- vlib/v/gen/c/cgen.v | 11 +++++---- vlib/v/tests/generic_interface_test.v | 33 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1b4fc09dde..771202193e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1375,10 +1375,13 @@ fn (mut g Gen) stmt(node ast.Stmt) { ast.Import {} ast.InterfaceDecl { // definitions are sorted and added in write_types - for method in node.methods { - if method.return_type.has_flag(.optional) { - // Register an optional if it's not registered yet - g.register_optional(method.return_type) + ts := g.table.get_type_symbol(node.typ) + if !(ts.info as ast.Interface).is_generic { + for method in node.methods { + if method.return_type.has_flag(.optional) { + // Register an optional if it's not registered yet + g.register_optional(method.return_type) + } } } } diff --git a/vlib/v/tests/generic_interface_test.v b/vlib/v/tests/generic_interface_test.v index 662855b5b3..059b1af7c7 100644 --- a/vlib/v/tests/generic_interface_test.v +++ b/vlib/v/tests/generic_interface_test.v @@ -63,3 +63,36 @@ fn test_extract_basic() { cc := extract_basic(c) assert '$aa | $bb | $cc' == '123 | 456 | 789' } + +////// +interface Iterator { + next() ?T +} + +struct NumberIterator { + limit T +mut: + val T +} + +fn (mut i NumberIterator) next() ?T { + if i.val >= i.limit { + return none + } + i.val++ + return i.val +} + +fn test_iterator_implementation() { + mut i := Iterator(NumberIterator{ + limit: 10 + }) + for { + if val := i.next() { + println(val) + } else { + println('iterator is done') + break + } + } +}