cgen: fix generating invalid Option decls for generic interfaces (#10817)
parent
aca66d503d
commit
ebc34c237b
|
@ -1375,6 +1375,8 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
|||
ast.Import {}
|
||||
ast.InterfaceDecl {
|
||||
// definitions are sorted and added in write_types
|
||||
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
|
||||
|
@ -1382,6 +1384,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast.Module {
|
||||
// g.is_builtin_mod = node.name == 'builtin'
|
||||
g.is_builtin_mod = node.name in ['builtin', 'os', 'strconv', 'strings', 'gg']
|
||||
|
|
|
@ -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