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

checker: fix generics with pointer index (fix #15810) (#15815)

This commit is contained in:
yuyi 2022-09-20 03:02:49 +08:00 committed by GitHub
parent a0d647d1e3
commit e6e3751980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 2 deletions

View File

@ -3485,8 +3485,12 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
node.is_farray = true
}
.any {
typ = c.unwrap_generic(typ)
typ_sym = c.table.final_sym(typ)
unwrapped_typ := c.unwrap_generic(typ)
unwrapped_sym := c.table.final_sym(unwrapped_typ)
if unwrapped_sym.kind in [.map, .array, .array_fixed] {
typ = unwrapped_typ
typ_sym = unwrapped_sym
}
}
else {}
}

View File

@ -0,0 +1,62 @@
module main
pub struct Vec<T> {
mut:
data &T
cap int
len int
}
pub struct Iter<T> {
mut:
v &Vec<T>
pos int
}
pub fn with_cap<T>(cap int) Vec<T> {
new_data := unsafe { C.malloc(cap * int(sizeof(T))) }
unsafe { C.memset(new_data, 0, cap * int(sizeof(T))) }
return Vec<T>{
data: new_data
cap: cap
len: 0
}
}
pub fn (ar &Vec<T>) iter() Iter<T> {
return Iter<T>{
v: unsafe { ar }
}
}
pub fn (mut iter Iter<T>) next() ?&T {
if iter.pos >= iter.v.len {
return none
}
res := unsafe { &iter.v.data[iter.pos] }
iter.pos++
return res
}
pub fn (mut ar Vec<T>) push(elm T) {
unsafe {
ar.data[ar.len - 1] = elm
}
}
struct Product {
price f64
}
fn test_generic_with_pointer_index() {
vec1 := with_cap<Product>(5)
println(vec1)
assert vec1.len == 0
assert vec1.cap == 5
vec2 := with_cap<int>(5)
println(vec2)
assert vec2.len == 0
assert vec2.cap == 5
}