Skip to content

Commit 7f12eaf

Browse files
authored
cgen: fix missing braces for const init with castexpr from option unwrapping expr (#24276)
1 parent 109e4f3 commit 7f12eaf

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎vlib/v/gen/c/consts_and_globals.v‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
129129
continue
130130
}
131131
}
132-
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
132+
should_surround := field.expr.expr is ast.CallExpr
133+
&& field.expr.expr.or_block.kind != .absent
134+
g.const_decl_init_later(field.mod, name, field.expr, field.typ, should_surround)
133135
} else if field.expr is ast.InfixExpr {
134136
mut has_unwrap_opt_res := false
135137
if field.expr.left is ast.CallExpr {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
_option_bool _t2 = main__t1();
3+
if (_t2.state != 0) {
4+
IError err = _t2.err;
5+
_v_panic(IError_str(err));
6+
VUNREACHABLE();
7+
;
8+
}
9+
10+
_const_main__barz = (((*(bool*)_t2.data)));
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// vtest vflags: -no-skip-unused
2+
fn t1() ?bool {
3+
return true
4+
}
5+
6+
type FooBar = bool
7+
8+
const bar = FooBar(t1() or { panic(err) })
9+
const barz = FooBar(t1() or { panic(err) })

0 commit comments

Comments
 (0)