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



commit ec797a6f8594e50921f218ff46ecd88ea3177ac8
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 1474efe44..717fc4433 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -1169,7 +1169,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);
@@ -1180,10 +1180,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]