From 5a4a1997e744bb8e1b860287df36b9ef432b35ab Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 4 Jul 2021 01:11:56 +0800 Subject: [PATCH] cgen: fix empty struct initilization (#10651) --- vlib/v/gen/c/cgen.v | 8 ++++---- vlib/v/tests/anon_fn_with_optional_test.v | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 83baf73f41..1832bd604d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -694,7 +694,7 @@ fn (mut g Gen) register_chan_pop_optional_call(opt_el_type string, styp string) static inline $opt_el_type __Option_${styp}_popval($styp ch) { $opt_el_type _tmp = {0}; if (sync__Channel_try_pop_priv(ch, _tmp.data, false)) { - return ($opt_el_type){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {0} }; + return ($opt_el_type){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; } return _tmp; }') @@ -708,7 +708,7 @@ fn (mut g Gen) register_chan_push_optional_call(el_type string, styp string) { g.channel_definitions.writeln(' static inline Option_void __Option_${styp}_pushval($styp ch, $el_type e) { if (sync__Channel_try_push_priv(ch, &e, false)) { - return (Option_void){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {0} }; + return (Option_void){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; } return (Option_void){0}; }') @@ -4482,7 +4482,7 @@ fn (mut g Gen) gen_optional_error(target_type ast.Type, expr ast.Expr) { styp := g.typ(target_type) g.write('($styp){ .state=2, .err=') g.expr(expr) - g.write(', .data={0} }') + g.write(', .data={EMPTY_STRUCT_INITIALIZATION} }') } fn (mut g Gen) return_stmt(node ast.Return) { @@ -5054,7 +5054,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { } if field.typ.has_flag(.optional) { field_name := c_name(field.name) - g.write('.$field_name = {0},') + g.write('.$field_name = {EMPTY_STRUCT_INITIALIZATION},') initialized = true continue } diff --git a/vlib/v/tests/anon_fn_with_optional_test.v b/vlib/v/tests/anon_fn_with_optional_test.v index 8e4740d42b..c4dae93ebb 100644 --- a/vlib/v/tests/anon_fn_with_optional_test.v +++ b/vlib/v/tests/anon_fn_with_optional_test.v @@ -1,6 +1,4 @@ -struct Response { - ret int -} +struct Response {} pub struct Route { handler fn (mut App) Response