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

checker: fix generics in big_struct (fix #9373) (#9383)

This commit is contained in:
yuyi 2021-03-21 00:29:13 +08:00 committed by GitHub
parent a2eb686506
commit 246c09db96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View File

@ -443,6 +443,9 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
param_type_sym := c.table.get_type_symbol(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {
typ = c.table.mktyp(arg.typ)
if arg.expr.is_auto_deref_var() {
typ = typ.deref()
}
break
}
arg_sym := c.table.get_type_symbol(arg.typ)

View File

@ -1018,6 +1018,10 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
g.write('/*af arg*/' + name)
}
} else {
if node.generic_types.len > 0 && arg.expr.is_auto_deref_var() && !arg.is_mut
&& !expected_types[i].is_ptr() {
g.write('*')
}
g.ref_or_deref_arg(arg, expected_types[i], node.language)
}
} else {

View File

@ -0,0 +1,27 @@
struct Product {
pub mut:
id int = 2
sku string = 'ABCD'
ean13 string = 'EFGH'
collections string
ptype string
featured_image string
featured_media string
handle string
variant int
}
pub fn (p Product) save() string {
return do_something(p)
}
fn do_something<T>(p T) string {
return 'whatever'
}
fn test_generics_in_big_struct_method() {
mut p := Product{}
println(p.save())
assert p.save() == 'whatever'
}