Skip to content

Commit 36154b8

Browse files
authored
cgen: fix struct init for anon struct field on C structs (fix #23421) (#23422)
1 parent 3b31699 commit 36154b8

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

vlib/v/ast/ast.v

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,22 +424,21 @@ pub:
424424
generic_types []Type
425425
is_pub bool
426426
// _pos fields for vfmt
427-
mut_pos int = -1 // mut:
428-
pub_pos int = -1 // pub:
429-
pub_mut_pos int = -1 // pub mut:
430-
global_pos int = -1 // __global:
431-
module_pos int = -1 // module:
432-
language Language
433-
is_union bool
434-
attrs []Attr
435-
pre_comments []Comment
436-
end_comments []Comment
437-
embeds []Embed
438-
427+
mut_pos int = -1 // mut:
428+
pub_pos int = -1 // pub:
429+
pub_mut_pos int = -1 // pub mut:
430+
global_pos int = -1 // __global:
431+
module_pos int = -1 // module:
432+
is_union bool
433+
attrs []Attr
434+
pre_comments []Comment
435+
end_comments []Comment
436+
embeds []Embed
439437
is_implements bool
440438
implements_types []TypeNode
441439
pub mut:
442-
fields []StructField
440+
language Language
441+
fields []StructField
443442
}
444443

445444
pub struct Embed {
@@ -522,6 +521,7 @@ pub mut:
522521
has_update_expr bool // has `...a`
523522
init_fields []StructInitField
524523
generic_types []Type
524+
language Language
525525
}
526526

527527
pub enum StructInitKind {

vlib/v/gen/c/struct.v

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
7878
}
7979
}
8080
if is_anon {
81-
g.writeln('(${styp}){')
81+
if node.language == .v {
82+
g.write('(${styp})')
83+
}
84+
g.writeln('{')
8285
} else if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set {
8386
mut shared_typ := node.typ.set_flag(.shared_f)
8487
shared_styp = g.styp(shared_typ)
@@ -433,7 +436,8 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
433436
}
434437
if has_option_field || field.anon_struct_decl.fields.len > 0 {
435438
default_init := ast.StructInit{
436-
typ: field.typ
439+
typ: field.typ
440+
language: field.anon_struct_decl.language
437441
}
438442
g.write('.${field_name} = ')
439443
if field.typ.has_flag(.option) {

vlib/v/parser/struct.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
239239
if p.tok.kind == .key_struct {
240240
// Anon structs
241241
p.anon_struct_decl = p.struct_decl(true)
242+
p.anon_struct_decl.language = language
242243
// Find the registered anon struct type, it was registered above in `p.struct_decl()`
243244
typ = p.table.find_type_idx(p.anon_struct_decl.name)
244245
} else {

vlib/v/tests/c_structs/anon.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// anon.h
2+
#ifndef TEST_H
3+
#define TEST_H
4+
5+
typedef struct {
6+
struct {
7+
int x;
8+
} inner;
9+
} outer;
10+
11+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#insert "@VMODROOT/anon.h"
2+
3+
@[typedef]
4+
struct C.outer {
5+
inner struct {
6+
x int
7+
}
8+
}
9+
10+
struct Outer {
11+
inner struct {
12+
val int
13+
}
14+
}
15+
16+
fn test_main() {
17+
_ = Outer{}
18+
_ = C.outer{}
19+
}

0 commit comments

Comments
 (0)