summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael James Gratton <mike@vee.net>2017-02-06 16:47:21 +1100
committerRico Tzschichholz <ricotz@ubuntu.com>2017-02-08 11:23:28 +0100
commitc73d19cc1d9919dcdf992fe0263f108c86dd328a (patch)
tree69746eebb0a66ab6b7a75474fcd0cca5d1072c2e
parent6b275cf3adcdae1b91054586435d10c99d13c0ef (diff)
downloadvala-c73d19cc1d9919dcdf992fe0263f108c86dd328a.tar.gz
codegen: Don't return void for non-nullable simple-type structs
https://bugzilla.gnome.org/show_bug.cgi?id=778224
-rw-r--r--codegen/valaccodebasemodule.vala11
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/errors/bug778224.vala26
3 files changed, 37 insertions, 1 deletions
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 {
+ }
+}
+