mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
cgen: fix missing braces for const init with castexpr from option unwrapping expr (#24276)
This commit is contained in:
parent
109e4f3a0c
commit
7f12eafbeb
3 changed files with 23 additions and 1 deletions
|
@ -129,7 +129,9 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||
continue
|
||||
}
|
||||
}
|
||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
||||
should_surround := field.expr.expr is ast.CallExpr
|
||||
&& field.expr.expr.or_block.kind != .absent
|
||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, should_surround)
|
||||
} else if field.expr is ast.InfixExpr {
|
||||
mut has_unwrap_opt_res := false
|
||||
if field.expr.left is ast.CallExpr {
|
||||
|
|
11
vlib/v/gen/c/testdata/const_init_or_block.c.must_have
vendored
Normal file
11
vlib/v/gen/c/testdata/const_init_or_block.c.must_have
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
_option_bool _t2 = main__t1();
|
||||
if (_t2.state != 0) {
|
||||
IError err = _t2.err;
|
||||
_v_panic(IError_str(err));
|
||||
VUNREACHABLE();
|
||||
;
|
||||
}
|
||||
|
||||
_const_main__barz = (((*(bool*)_t2.data)));
|
||||
}
|
9
vlib/v/gen/c/testdata/const_init_or_block.vv
vendored
Normal file
9
vlib/v/gen/c/testdata/const_init_or_block.vv
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
// vtest vflags: -no-skip-unused
|
||||
fn t1() ?bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type FooBar = bool
|
||||
|
||||
const bar = FooBar(t1() or { panic(err) })
|
||||
const barz = FooBar(t1() or { panic(err) })
|
Loading…
Add table
Add a link
Reference in a new issue