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

cgen: fix generic func arg type when passing array compile-time field (#16798)

This commit is contained in:
Felipe Pena 2022-12-29 06:23:57 -03:00 committed by GitHub
parent 2ebd3f0cdb
commit ed06618498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -1386,8 +1386,13 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
if g.comptime_for_field_type != 0 && g.inside_comptime_for_field
&& has_comptime_field {
mut concrete_types := node.concrete_types.map(g.unwrap_generic(it))
arg_sym := g.table.sym(g.comptime_for_field_type)
for k in comptime_args {
concrete_types[k] = g.comptime_for_field_type
if arg_sym.kind == .array {
concrete_types[k] = (arg_sym.info as ast.Array).elem_type
} else {
concrete_types[k] = g.comptime_for_field_type
}
}
name = g.generic_fn_name(concrete_types, name)
} else {

View File

@ -0,0 +1,30 @@
struct IntArray {
a []int
}
fn encode_array[U](val []U) []string {
mut out := []string{}
$if U is int {
out << 'is int'
} $else $if U is $Struct {
out << 'is struct'
}
return out
}
fn encode_struct[U](val U) ?[]string {
$for field in U.fields {
if field.is_array {
value := val.$(field.name)
return encode_array(value)
}
}
return none
}
fn test_main() {
int_array := IntArray{
a: [1, 2, 3]
}
assert encode_struct(int_array)?.str() == "['is int']"
}