Skip to content

Commit 2b86243

Browse files
authored
cgen: fix generation of struct initialisers for structs with option fields, inside ternaries (fix #26476) (#26493)
1 parent 5a39382 commit 2b86243

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,11 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
23742374
g.assign_op = assign_op
23752375
}
23762376
g.assign_op = .unknown
2377-
stmt_str := g.go_before_last_stmt().trim_space()
2377+
stmt_str := if g.inside_ternary > 0 {
2378+
g.go_before_ternary().trim_space()
2379+
} else {
2380+
g.go_before_last_stmt().trim_space()
2381+
}
23782382
mut styp := g.base_type(ret_typ)
23792383
g.empty_line = true
23802384
final_expr_sym := g.table.final_sym(expr_typ)

‎vlib/v/gen/c/struct.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
1414
is_update_tmp_var = true
1515

1616
tmp_update_var = g.new_tmp_var()
17-
s := g.go_before_last_stmt()
17+
s := if g.inside_ternary > 0 { g.go_before_ternary() } else { g.go_before_last_stmt() }
1818
g.empty_line = true
1919

2020
styp := g.styp(node.update_expr_type)
@@ -121,7 +121,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
121121
}
122122
} else if node.typ.has_flag(.option) {
123123
tmp_var := g.new_tmp_var()
124-
s := g.go_before_last_stmt()
124+
s := if g.inside_ternary > 0 { g.go_before_ternary() } else { g.go_before_last_stmt() }
125125
g.empty_line = true
126126

127127
base_styp := g.styp(node.typ.clear_option_and_result())
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pub struct Color {
2+
pub mut:
3+
r u8
4+
g u8
5+
b u8
6+
a u8 = 255
7+
}
8+
9+
pub const color_transparent = Color{0, 0, 0, 0}
10+
11+
pub struct MarkdownStyle {
12+
pub:
13+
table_row_alt ?Color
14+
}
15+
16+
pub struct MarkdownCfg {
17+
pub:
18+
id string
19+
style MarkdownStyle
20+
}
21+
22+
struct AResult {
23+
x int
24+
}
25+
26+
struct App {
27+
mode bool
28+
}
29+
30+
fn (app App) method(params MarkdownCfg) AResult {
31+
return AResult{123}
32+
}
33+
34+
fn another_fn() AResult {
35+
return AResult{456}
36+
}
37+
38+
fn test_main() {
39+
app := App{}
40+
x := if app.mode {
41+
[app.method(id: 'abc')]
42+
} else {
43+
[another_fn()]
44+
}
45+
assert x == [AResult{456}]
46+
}

0 commit comments

Comments
 (0)