1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ast: fix for in iterator of generic struct (fix #13579) (#13585)

This commit is contained in:
yuyi 2022-02-24 16:48:52 +08:00 committed by GitHub
parent 9662b79662
commit a28249c119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 4 deletions

View File

@ -1266,10 +1266,16 @@ pub fn (t &TypeSymbol) find_method_with_generic_parent(name string) ?Fn {
Struct, Interface, SumType {
mut method := x
generic_names := parent_sym.info.generic_types.map(table.sym(it).name)
if rt := table.resolve_generic_to_concrete(method.return_type,
generic_names, t.info.concrete_types)
{
method.return_type = rt
return_sym := table.sym(method.return_type)
if return_sym.kind == .struct_ {
method.return_type = table.unwrap_generic_type(method.return_type,
generic_names, t.info.concrete_types)
} else {
if rt := table.resolve_generic_to_concrete(method.return_type,
generic_names, t.info.concrete_types)
{
method.return_type = rt
}
}
method.params = method.params.clone()
for mut param in method.params {

View File

@ -0,0 +1,35 @@
pub struct Item<V> {
pub:
value V
}
pub struct Iter<V> {
arr []V
mut:
ix int
}
pub fn (mut iter Iter<V>) next() ?Item<V> {
if iter.ix >= iter.arr.len {
return none
}
val := iter.arr[iter.ix]
iter.ix += 1
return Item<V>{val}
}
fn iterator<V>(arr []V) Iter<V> {
return Iter<V>{arr, 0}
}
fn test_for_in_iterator_of_generic_struct() {
mut ret := []int{}
mut x := iterator<int>([1, 2, 3, 4, 5])
for item in x {
println(item.value)
ret << item.value
}
println(ret)
assert ret == [1, 2, 3, 4, 5]
}