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

ast: fix for in iterator with generic structs (#18344)

This commit is contained in:
yuyi 2023-06-05 00:15:15 +08:00 committed by GitHub
parent 02ea6028af
commit 325f64bc60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -1905,7 +1905,7 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
mut info := ts.info
info.is_generic = false
info.concrete_types = final_concrete_types
info.parent_type = typ
info.parent_type = typ.set_flag(.generic)
info.fields = fields
new_idx := t.register_sym(
kind: .struct_
@ -1940,7 +1940,7 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
mut info := ts.info
info.is_generic = false
info.concrete_types = final_concrete_types
info.parent_type = typ
info.parent_type = typ.set_flag(.generic)
info.fields = fields
info.variants = variants
new_idx := t.register_sym(
@ -1984,7 +1984,7 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr
mut info := ts.info
info.is_generic = false
info.concrete_types = final_concrete_types
info.parent_type = typ
info.parent_type = typ.set_flag(.generic)
info.fields = fields
info.methods = imethods
new_idx := t.register_sym(

View File

@ -0,0 +1,28 @@
struct MyGenericArray[T] {
arr []T
mut:
idx int
}
fn (mut self MyGenericArray[T]) next() ?T {
if self.arr.len <= self.idx {
return none
} else {
defer {
self.idx++
}
return self.arr[self.idx]
}
}
fn test_for_in_iterator_of_generic_struct() {
mut a := MyGenericArray{[4, 5, 6], 1}
mut ret := []int{}
for i in a {
println(i)
ret << i
}
assert ret.len == 2
assert ret[0] == 5
assert ret[1] == 6
}