cgen: fix generating invalid Option decls for generic interfaces (#10817)
parent
aca66d503d
commit
ebc34c237b
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue