cgen: fix generic return assign to ComptimeSelector (fix #25125) (#25131)

This commit is contained in:
Felipe Pena 2025-08-18 10:38:13 -03:00 committed by GitHub
parent 9f910643ed
commit 5566df56f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View file

@ -442,8 +442,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
if val.typ_key != '' {
val_type = g.type_resolver.get_ct_type_or_default(val.typ_key, var_type)
}
} else if val is ast.CallExpr {
} else if val is ast.CallExpr && val.return_type_generic.has_flag(.generic) {
g.assign_ct_type = g.comptime.comptime_for_field_type
val_type = var_type
}
} else if mut left is ast.IndexExpr && val is ast.ComptimeSelector {
if val.typ_key != '' {

View file

@ -0,0 +1,25 @@
module main
struct AnyStruct[T] {
val T
}
fn decode_struct[T]() T {
mut typ := T{}
$for field in T.fields {
typ.$(field.name) = decode_field(typ.$(field.name))
}
return typ
}
fn decode_field[T](_ T) T {
mut field := T{}
return field
}
type Any = int | string | []Any
fn test_main() {
decode_struct[AnyStruct[Any]]()
decode_struct[AnyStruct[[]Any]]()
}