[vala/staging] codegen: Reset outdated array _size_ variable after use as ref parameter



commit 5490b0b21a4089d803d51c91311430d60d266f7b
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Mar 12 19:19:19 2020 +0100

    codegen: Reset outdated array _size_ variable after use as ref parameter
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/929

 codegen/valaccodemethodcallmodule.vala        | 14 ++++++++++++++
 tests/Makefile.am                             |  1 +
 tests/methods/parameter-ref-array-resize.vala | 11 +++++++++++
 3 files changed, 26 insertions(+)
---
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index e43ff8be7..17df1cda4 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -896,6 +896,20 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        }
 
                        var unary = arg as UnaryExpression;
+
+                       // update possible stale _*_size_ variable
+                       if (unary != null && unary.operator == UnaryOperator.REF) {
+                               if (get_ccode_array_length (param) && param.variable_type is ArrayType
+                                   && !((ArrayType) param.variable_type).fixed_length && ((ArrayType) 
param.variable_type).rank == 1) {
+                                       unowned Symbol? array_var = unary.inner.symbol_reference;
+                                       unowned LocalVariable? array_local = array_var as LocalVariable;
+                                       if (array_var != null && array_var.is_internal_symbol ()
+                                           && ((array_local != null && !array_local.captured) || array_var 
is Field)) {
+                                               ccode.add_assignment (get_array_size_cvalue 
(unary.inner.target_value), get_array_length_cvalue (unary.inner.target_value, 1));
+                                       }
+                               }
+                       }
+
                        if (unary == null || unary.operator != UnaryOperator.OUT) {
                                continue;
                        }
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6c74a3186..dcda2cbe0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -118,6 +118,7 @@ TESTS = \
        methods/contains.vala \
        methods/extern.vala \
        methods/iterator.vala \
+       methods/parameter-ref-array-resize.vala \
        methods/prepostconditions.vala \
        methods/same-name.vala \
        methods/symbolresolution.vala \
diff --git a/tests/methods/parameter-ref-array-resize.vala b/tests/methods/parameter-ref-array-resize.vala
new file mode 100644
index 000000000..ffe27d35b
--- /dev/null
+++ b/tests/methods/parameter-ref-array-resize.vala
@@ -0,0 +1,11 @@
+void foo (ref int[] a) {
+       a = new int[1];
+}
+
+void main() {
+       int[] a = new int[10 * 1024 * 1024];
+       foo (ref a);
+       for (int i = 1; i < 10 * 1024 * 1024; i++) {
+               a += 4711;
+       }
+}


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