From 9eaf517de650b9792345842ffe07ec6f92d35fc3 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 10 Jan 2025 08:23:49 -0300 Subject: [PATCH] fix --- vlib/v/ast/ast.v | 26 ++++++++++---------- vlib/v/gen/c/struct.v | 8 ++++-- vlib/v/parser/struct.v | 1 + vlib/v/tests/c_structs/anon.h | 11 +++++++++ vlib/v/tests/c_structs/cstruct_anon_test.c.v | 19 ++++++++++++++ 5 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 vlib/v/tests/c_structs/anon.h create mode 100644 vlib/v/tests/c_structs/cstruct_anon_test.c.v diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9e41d7fa9cd57a..bc348cda9f20f4 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -424,22 +424,21 @@ pub: generic_types []Type is_pub bool // _pos fields for vfmt - mut_pos int = -1 // mut: - pub_pos int = -1 // pub: - pub_mut_pos int = -1 // pub mut: - global_pos int = -1 // __global: - module_pos int = -1 // module: - language Language - is_union bool - attrs []Attr - pre_comments []Comment - end_comments []Comment - embeds []Embed - + mut_pos int = -1 // mut: + pub_pos int = -1 // pub: + pub_mut_pos int = -1 // pub mut: + global_pos int = -1 // __global: + module_pos int = -1 // module: + is_union bool + attrs []Attr + pre_comments []Comment + end_comments []Comment + embeds []Embed is_implements bool implements_types []TypeNode pub mut: - fields []StructField + language Language + fields []StructField } pub struct Embed { @@ -522,6 +521,7 @@ pub mut: has_update_expr bool // has `...a` init_fields []StructInitField generic_types []Type + language Language } pub enum StructInitKind { diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index afc1e2d8e37b4e..51d26023576ffd 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -78,7 +78,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) { } } if is_anon { - g.writeln('(${styp}){') + if node.language == .v { + g.write('(${styp})') + } + g.writeln('{') } else if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set { mut shared_typ := node.typ.set_flag(.shared_f) shared_styp = g.styp(shared_typ) @@ -433,7 +436,8 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { } if has_option_field || field.anon_struct_decl.fields.len > 0 { default_init := ast.StructInit{ - typ: field.typ + typ: field.typ + language: field.anon_struct_decl.language } g.write('.${field_name} = ') if field.typ.has_flag(.option) { diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index ff88f1dc27357b..baa7c7b5a3a838 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -239,6 +239,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { if p.tok.kind == .key_struct { // Anon structs p.anon_struct_decl = p.struct_decl(true) + p.anon_struct_decl.language = language // Find the registered anon struct type, it was registered above in `p.struct_decl()` typ = p.table.find_type_idx(p.anon_struct_decl.name) } else { diff --git a/vlib/v/tests/c_structs/anon.h b/vlib/v/tests/c_structs/anon.h new file mode 100644 index 00000000000000..63eddd854f9eff --- /dev/null +++ b/vlib/v/tests/c_structs/anon.h @@ -0,0 +1,11 @@ +// anon.h +#ifndef TEST_H +#define TEST_H + +typedef struct { + struct { + int x; + } inner; +} outer; + +#endif \ No newline at end of file diff --git a/vlib/v/tests/c_structs/cstruct_anon_test.c.v b/vlib/v/tests/c_structs/cstruct_anon_test.c.v new file mode 100644 index 00000000000000..5772fddfa65832 --- /dev/null +++ b/vlib/v/tests/c_structs/cstruct_anon_test.c.v @@ -0,0 +1,19 @@ +#insert "@VMODROOT/anon.h" + +@[typedef] +struct C.outer { + inner struct { + x int + } +} + +struct Outer { + inner struct { + val int + } +} + +fn test_main() { + _ = Outer{} + _ = C.outer{} +}