cgen: allow alias types to be iterated if parent type has next method (fix #24890) (#24894)

This commit is contained in:
Swastik Baranwal 2025-07-14 12:19:30 +05:30 committed by GitHub
parent feb409071d
commit e2434a8256
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View file

@ -422,7 +422,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
g.writeln('${field_accessor}str[${i}];') g.writeln('${field_accessor}str[${i}];')
} }
} else if node.kind in [.struct, .interface] { } else if node.kind in [.struct, .interface] {
cond_type_sym := g.table.sym(node.cond_type) cond_type_sym := g.table.final_sym(node.cond_type)
next_fn := cond_type_sym.find_method_with_generic_parent('next') or { next_fn := cond_type_sym.find_method_with_generic_parent('next') or {
verror('`next` method not found') verror('`next` method not found')
return return

View file

@ -11,6 +11,8 @@ mut:
idx int idx int
} }
type AliasLineIterator = LineIterator
pub fn (mut line_iterator LineIterator) next() ?string { pub fn (mut line_iterator LineIterator) next() ?string {
if line_iterator.idx >= line_iterator.lines.len { if line_iterator.idx >= line_iterator.lines.len {
return none return none
@ -31,6 +33,12 @@ fn iterator_as_concrete_type() LineIterator {
} }
} }
fn iterator_as_alias_concrete_type() AliasLineIterator {
return AliasLineIterator(LineIterator{
lines: ['alias']
})
}
fn test_main() { fn test_main() {
mut out := []string{} mut out := []string{}
for line in iterator_as_interface() { for line in iterator_as_interface() {
@ -64,4 +72,10 @@ fn test_main() {
assert out[4] == 'LINE: interface' assert out[4] == 'LINE: interface'
assert out[5] == 'LINE: concrete' assert out[5] == 'LINE: concrete'
for _, line in iterator_as_alias_concrete_type() {
out << 'LINE: ${line}'
}
assert out[6] == 'LINE: alias'
} }