cgen: fix generating invalid Option decls for generic interfaces (#10817)

pull/10819/head
spaceface 2021-07-15 21:15:25 +02:00 committed by GitHub
parent aca66d503d
commit ebc34c237b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 4 deletions

View File

@ -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)
}
}
}
}

View File

@ -63,3 +63,36 @@ fn test_extract_basic() {
cc := extract_basic(c)
assert '$aa | $bb | $cc' == '123 | 456 | 789'
}
//////
interface Iterator<T> {
next() ?T
}
struct NumberIterator<T> {
limit T
mut:
val T
}
fn (mut i NumberIterator<T>) next<T>() ?T {
if i.val >= i.limit {
return none
}
i.val++
return i.val
}
fn test_iterator_implementation() {
mut i := Iterator<int>(NumberIterator<int>{
limit: 10
})
for {
if val := i.next() {
println(val)
} else {
println('iterator is done')
break
}
}
}