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

checker: fix generics that return reference generics struct (fix #6218) (#9869)

This commit is contained in:
yuyi 2021-04-27 01:01:01 +08:00 committed by GitHub
parent 7ddf569de6
commit 21d1f86ead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -1457,7 +1457,7 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e
idx := c.table.type_idxs[nrt]
if idx != 0 {
c.ensure_type_exists(idx, call_expr.pos) or {}
call_expr.return_type = ast.new_type(idx).derive(return_type)
call_expr.return_type = ast.new_type(idx).derive(return_type).clear_flag(.generic)
} else {
mut fields := rts.info.fields.clone()
if rts.info.generic_types.len == concrete_types.len {
@ -1481,7 +1481,7 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e
mod: c.mod
info: info
})
call_expr.return_type = ast.new_type(stru_idx)
call_expr.return_type = ast.new_type(stru_idx).derive(return_type).clear_flag(.generic)
}
}
}

View File

@ -0,0 +1,15 @@
struct Foo<T> {
data []T
}
fn new_foo<T>(len int) &Foo<T> {
return &Foo{
data: []T{len: len}
}
}
fn test_generics_return_reference_generics_struct() {
f := new_foo<int>(4)
println(f)
assert f.data == [0, 0, 0, 0]
}