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:
parent
2ebd3f0cdb
commit
ed06618498
@ -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 {
|
||||
|
30
vlib/v/tests/fn_call_comptime_array_arg_test.v
Normal file
30
vlib/v/tests/fn_call_comptime_array_arg_test.v
Normal 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']"
|
||||
}
|
Loading…
Reference in New Issue
Block a user