[vala/0.40] codegen: Correctly access captured parameter in precondition of method



commit cd8b832b0cc549838011a62c2c4a9a98bf171bd1
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Mar 13 09:11:01 2020 +0100

    codegen: Correctly access captured parameter in precondition of method
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/144

 codegen/valaccodebasemodule.vala              |  6 +++++
 codegen/valaccodememberaccessmodule.vala      |  2 +-
 codegen/valaccodemethodmodule.vala            |  4 +++
 tests/Makefile.am                             |  1 +
 tests/methods/prepostconditions-captured.vala | 35 +++++++++++++++++++++++++++
 5 files changed, 47 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 7a0a63953..17e1eb4a4 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -43,6 +43,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                public Map<string,string> variable_name_map = new HashMap<string,string> (str_hash, 
str_equal);
                public Map<string,int> closure_variable_count_map = new HashMap<string,int> (str_hash, 
str_equal);
                public Map<LocalVariable,int> closure_variable_clash_map = new HashMap<LocalVariable,int> ();
+               public bool is_in_method_precondition;
 
                public EmitContext (Symbol? symbol = null) {
                        current_symbol = symbol;
@@ -87,6 +88,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                set { emit_context.current_inner_error_id = value; }
        }
 
+       public bool is_in_method_precondition {
+               get { return emit_context.is_in_method_precondition; }
+               set { emit_context.is_in_method_precondition = value; }
+       }
+
        public TypeSymbol? current_type_symbol {
                get {
                        var sym = current_symbol;
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 51708e584..602745bdd 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -463,7 +463,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                } else {
                        string name = param.name;
 
-                       if (param.captured) {
+                       if (param.captured && !is_in_method_precondition) {
                                // captured variables are stored on the heap
                                var block = param.parent_symbol as Block;
                                if (block == null) {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 717fc4433..cfcf9ccbc 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -1170,6 +1170,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
        }
 
        private void create_precondition_statement (Method m, DataType ret_type, Expression precondition) {
+               is_in_method_precondition = true;
+
                var ccheck = new CCodeFunctionCall ();
 
                precondition.emit (this);
@@ -1207,6 +1209,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                }
                
                ccode.add_expression (ccheck);
+
+               is_in_method_precondition = false;
        }
 
        private TypeSymbol? find_parent_type (Symbol sym) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index da814a173..5ae2c3d2f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -108,6 +108,7 @@ TESTS = \
        methods/iterator.vala \
        methods/parameter-ref-array-resize.vala \
        methods/prepostconditions.vala \
+       methods/prepostconditions-captured.vala \
        methods/postconditions.vala \
        methods/same-name.vala \
        methods/symbolresolution.vala \
diff --git a/tests/methods/prepostconditions-captured.vala b/tests/methods/prepostconditions-captured.vala
new file mode 100644
index 000000000..3e7ba95e8
--- /dev/null
+++ b/tests/methods/prepostconditions-captured.vala
@@ -0,0 +1,35 @@
+delegate void Func ();
+
+int bar (int i) requires (i == 23) ensures (i == 42) {
+       Func f = () => {
+               assert (i == 23);
+               i = 42;
+       };
+       f ();
+
+       return i;
+}
+
+void baz (int i) requires (i == 42) ensures (i == 23) {
+       Func f = () => {
+               assert (i == 42);
+               i = 23;
+       };
+       f ();
+}
+
+async int foo (int i) requires (i == 23) ensures (i == 42) {
+       Func f = () => {
+               assert (i == 23);
+               i = 42;
+       };
+       f ();
+
+       return i;
+}
+
+void main () {
+       assert (bar (23) == 42);
+       baz (42);
+       foo.begin (23);
+}


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