]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Fix precondition in creation method of structs
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 26 Nov 2019 10:06:45 +0000 (11:06 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 26 Nov 2019 12:03:31 +0000 (13:03 +0100)
codegen/valaccodemethodmodule.vala
tests/methods/prepostconditions.vala

index 9eb868015fd01500e800ef547bb963f89e8f4c13..fe73655c2ce3096f88b66a7b470cce5490300c70 100644 (file)
@@ -1120,7 +1120,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);
@@ -1131,10 +1131,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"));
index d7474df757f384f07c18f7d193c8d305724974a4..d8cd4d3f05eb50b1130aa8fd60d1c7f89d46d930 100644 (file)
@@ -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);
 }