cgen: fix missing braces for const init with castexpr from option unwrapping expr (#24276)

This commit is contained in:
Felipe Pena 2025-04-21 12:19:23 -03:00 committed by GitHub
parent 109e4f3a0c
commit 7f12eafbeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View file

@ -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 {

View 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)));
}

View 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) })