diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2019-11-26 11:06:45 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2019-11-26 13:03:31 +0100 |
commit | 34409b10f9ed1dd9cbdc110f8297a8efc34613f3 (patch) | |
tree | a30280952c15d41b513daff7145cce1d06612695 | |
parent | aa628acffcb170cfb1634bc65d17b8b04a10446d (diff) | |
download | vala-34409b10f9ed1dd9cbdc110f8297a8efc34613f3.tar.gz |
codegen: Fix precondition in creation method of structs
-rw-r--r-- | codegen/valaccodemethodmodule.vala | 15 | ||||
-rw-r--r-- | tests/methods/prepostconditions.vala | 27 |
2 files changed, 37 insertions, 5 deletions
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala index 9eb868015..fe73655c2 100644 --- a/codegen/valaccodemethodmodule.vala +++ b/codegen/valaccodemethodmodule.vala @@ -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")); 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); } |