1
0
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:
Felipe Pena 2023-05-30 09:23:37 -03:00 committed by GitHub
parent e9fb5b3fcc
commit 0b71cef78a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -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()
}

View 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]
}