diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 4d7e0ce077..c1f1abd6b9 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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)}*)&') } diff --git a/vlib/x/json2/tests/encode_anon_struct_test.v b/vlib/x/json2/tests/encode_anon_struct_test.v new file mode 100644 index 0000000000..3724c49131 --- /dev/null +++ b/vlib/x/json2/tests/encode_anon_struct_test.v @@ -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":""}' +}