[vala/0.46] codegen: Fix precondition in creation method of structs



commit 17b30b83dd7536bf64c4bdb0aad8f484384e12f0
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Nov 26 11:06:45 2019 +0100

    codegen: Fix precondition in creation method of structs

 codegen/valaccodemethodmodule.vala   | 15 ++++++++++-----
 tests/methods/prepostconditions.vala | 27 +++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index d596568df..57f62d64a 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -1141,7 +1141,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                }
        }
 
-       private void create_precondition_statement (CodeNode method_node, DataType ret_type, Expression 
precondition) {
+       private void create_precondition_statement (Method m, DataType ret_type, Expression precondition) {
                var ccheck = new CCodeFunctionCall ();
 
                precondition.emit (this);
@@ -1152,10 +1152,15 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                ccheck.add_argument (new CCodeConstant ("\"%s\"".printf (message.replace ("\n", " ").escape 
(""))));
                requires_assert = true;
 
-               if (method_node is CreationMethod) {
-                       ccheck.call = new CCodeIdentifier ("_vala_return_val_if_fail");
-                       ccheck.add_argument (new CCodeConstant ("NULL"));
-               } else if (method_node is Method && ((Method) method_node).coroutine) {
+               if (m is CreationMethod) {
+                       if (m.parent_symbol is Class) {
+                               ccheck.call = new CCodeIdentifier ("_vala_return_val_if_fail");
+                               ccheck.add_argument (new CCodeConstant ("NULL"));
+                       } else {
+                               // creation method of struct
+                               ccheck.call = new CCodeIdentifier ("_vala_return_if_fail");
+                       }
+               } else if (m.coroutine) {
                        // _co function
                        ccheck.call = new CCodeIdentifier ("_vala_return_val_if_fail");
                        ccheck.add_argument (new CCodeConstant ("FALSE"));
diff --git a/tests/methods/prepostconditions.vala b/tests/methods/prepostconditions.vala
index d7474df75..d8cd4d3f0 100644
--- a/tests/methods/prepostconditions.vala
+++ b/tests/methods/prepostconditions.vala
@@ -61,6 +61,24 @@ class Foo {
        }
 }
 
+struct Bar {
+       public bool ensured;
+       public bool required;
+
+       public Bar () requires (required = true) {
+       }
+
+       public Bar.post () ensures (ensured = true) {
+       }
+
+       public void bar () ensures (ensured = true) {
+       }
+
+       public string foo () ensures (result.length >= 3) {
+               return "foo";
+       }
+}
+
 void main () {
        var foo = new Foo();
        assert(foo.required);
@@ -76,4 +94,13 @@ void main () {
 
        var foo2 = new Foo.post ();
        assert (foo2.ensured);
+
+       var bar = new Bar ();
+       assert (bar.required);
+       bar.bar ();
+       assert (bar.ensured);
+       assert (bar.foo () == "foo");
+
+       var bar2 = new Bar.post ();
+       assert (bar2.ensured);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]