From 7f12eafbeb1a63e1e90b0bdde26f1af2fb3bc813 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 21 Apr 2025 12:19:23 -0300 Subject: [PATCH] cgen: fix missing braces for const init with castexpr from option unwrapping expr (#24276) --- vlib/v/gen/c/consts_and_globals.v | 4 +++- vlib/v/gen/c/testdata/const_init_or_block.c.must_have | 11 +++++++++++ vlib/v/gen/c/testdata/const_init_or_block.vv | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/gen/c/testdata/const_init_or_block.c.must_have create mode 100644 vlib/v/gen/c/testdata/const_init_or_block.vv diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index 0a4ec91b9..11aeb43c1 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -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 { diff --git a/vlib/v/gen/c/testdata/const_init_or_block.c.must_have b/vlib/v/gen/c/testdata/const_init_or_block.c.must_have new file mode 100644 index 000000000..cd1f5f927 --- /dev/null +++ b/vlib/v/gen/c/testdata/const_init_or_block.c.must_have @@ -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))); +} \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/const_init_or_block.vv b/vlib/v/gen/c/testdata/const_init_or_block.vv new file mode 100644 index 000000000..b2f4aad2e --- /dev/null +++ b/vlib/v/gen/c/testdata/const_init_or_block.vv @@ -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) }) -- 2.39.5