From 34409b10f9ed1dd9cbdc110f8297a8efc34613f3 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Tue, 26 Nov 2019 11:06:45 +0100 Subject: 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 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); } -- cgit v1.2.1