mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix generic comptimeselector array resolution (#18296)
This commit is contained in:
parent
e9fb5b3fcc
commit
0b71cef78a
@ -1115,6 +1115,12 @@ fn (mut g Gen) change_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concret
|
||||
}
|
||||
} else if mut call_arg.expr is ast.ComptimeSelector {
|
||||
comptime_args[i] = g.comptime_for_field_type
|
||||
arg_sym := g.table.final_sym(call_arg.typ)
|
||||
param_typ_sym := g.table.sym(param_typ)
|
||||
if arg_sym.kind == .array && param_typ.has_flag(.generic)
|
||||
&& param_typ_sym.kind == .array {
|
||||
comptime_args[i] = g.get_generic_array_element_type(arg_sym.info as ast.Array)
|
||||
}
|
||||
if call_arg.expr.left.is_auto_deref_var() {
|
||||
comptime_args[i] = comptime_args[i].deref()
|
||||
}
|
||||
|
26
vlib/v/tests/generic_comptime_test.v
Normal file
26
vlib/v/tests/generic_comptime_test.v
Normal file
@ -0,0 +1,26 @@
|
||||
struct EmptyStruct {
|
||||
b []int = [1, 2, 3]
|
||||
}
|
||||
|
||||
fn encode_array[T](val []T) []int {
|
||||
return val
|
||||
}
|
||||
|
||||
fn encode_struct[T](val T) []int {
|
||||
$if T is $struct {
|
||||
$for field in T.fields {
|
||||
$if field.is_array {
|
||||
return encode_array(val.$(field.name))
|
||||
}
|
||||
}
|
||||
}
|
||||
return [0]
|
||||
}
|
||||
|
||||
fn encoder() []int {
|
||||
return encode_struct(EmptyStruct{})
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
assert encoder() == [1, 2, 3]
|
||||
}
|
Loading…
Reference in New Issue
Block a user