From: Michael James Gratton Date: Mon, 6 Feb 2017 05:47:21 +0000 (+1100) Subject: codegen: Don't return void for non-nullable simple-type structs X-Git-Tag: 0.35.4~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c73d19cc1d9919dcdf992fe0263f108c86dd328a;p=thirdparty%2Fvala.git codegen: Don't return void for non-nullable simple-type structs https://bugzilla.gnome.org/show_bug.cgi?id=778224 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 9a046908b..f979dab46 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -6529,7 +6529,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { } public void return_default_value (DataType return_type) { - ccode.add_return (default_value_for_type (return_type, false)); + var st = return_type.data_type as Struct; + if (st != null && st.is_simple_type () && !return_type.nullable) { + // 0-initialize struct with struct initializer { 0 } + // only allowed as initializer expression in C + var ret_temp_var = get_temp_variable (return_type, true, null, true); + emit_temp_var (ret_temp_var); + ccode.add_return (new CCodeIdentifier (ret_temp_var.name)); + } else { + ccode.add_return (default_value_for_type (return_type, false)); + } } public virtual void generate_dynamic_method_wrapper (DynamicMethod method) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 0db7170a6..8e977db51 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -206,6 +206,7 @@ TESTS = \ errors/bug623049.vala \ errors/bug639589.vala \ errors/bug651145.vala \ + errors/bug778224.vala \ asynchronous/bug595735.vala \ asynchronous/bug595755.vala \ asynchronous/bug596177.vala \ diff --git a/tests/errors/bug778224.vala b/tests/errors/bug778224.vala new file mode 100644 index 000000000..3a61a029b --- /dev/null +++ b/tests/errors/bug778224.vala @@ -0,0 +1,26 @@ +errordomain FooError { + BAR; +} + +[SimpleType] +struct Foo { + int i; +} + +bool @true = true; + +Foo foo () throws FooError { + if (@true) { + throw new FooError.BAR (""); + } + + return { 1 }; +} + +void main () { + try { + foo (); + } catch { + } +} +