cgen, x.json2: fix anon struct encode (fix #24836) (#24852)

This commit is contained in:
Felipe Pena 2025-07-06 00:41:42 -03:00 committed by GitHub
parent 52ae3f2476
commit 8f15d65168
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -2767,7 +2767,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.expr_with_cast(arg.expr, arg_typ, expected_type)
g.write('.data')
return
} else if arg.expr is ast.Ident && arg_sym.info is ast.Struct && arg_sym.info.is_anon {
} else if arg.expr is ast.Ident && arg_sym.info is ast.Struct && arg_sym.info.is_anon
&& !expected_type.has_flag(.generic) {
// make anon struct struct compatible with another anon struct declaration
g.write('*(${g.cc_type(expected_type, false)}*)&')
}

View File

@ -0,0 +1,24 @@
import x.json2
struct Disk {
dev string
size ?struct {
value u64
}
}
fn test_main() {
disk := Disk{
size: struct {
value: 123
}
}
disk_str := json2.encode[Disk](disk)
assert disk_str == '{"dev":"","size":{"value":123}}'
}
fn test_none() {
disk := Disk{}
disk_str := json2.encode[Disk](disk)
assert disk_str == '{"dev":""}'
}