]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Don't return void for non-nullable simple-type structs
authorMichael James Gratton <mike@vee.net>
Mon, 6 Feb 2017 05:47:21 +0000 (16:47 +1100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 8 Feb 2017 10:23:28 +0000 (11:23 +0100)
https://bugzilla.gnome.org/show_bug.cgi?id=778224

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/errors/bug778224.vala [new file with mode: 0644]

index 9a046908b5d8869efb6e208e3a9d139dcecf0b3c..f979dab46f01b5b3da04eb2102e4236158981d02 100644 (file)
@@ -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) {
index 0db7170a6ecac14a561b03245c3ff68dfc2a05b1..8e977db5190f2aeefc91866bbcc20bee65f87606 100644 (file)
@@ -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 (file)
index 0000000..3a61a02
--- /dev/null
@@ -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 {
+       }
+}
+