]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Avoid taking extra reference of foreach collection for index iteration
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 9 Nov 2020 14:38:38 +0000 (15:38 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 10 Nov 2020 07:40:13 +0000 (08:40 +0100)
vala/valaforeachstatement.vala

index da80b24aa142803f608074cddd142d2f020d256a..258a81dae1f87d6509ee71a0f49a8ad9702b84e1 100644 (file)
@@ -205,7 +205,11 @@ public class Vala.ForeachStatement : Block {
                        return false;
                }
 
-               add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_list".printf (variable_name), collection, source_reference), source_reference));
+               var list_type = collection_type.copy ();
+               if (collection.symbol_reference is Variable) {
+                       list_type.value_owned = false;
+               }
+               add_statement (new DeclarationStatement (new LocalVariable (list_type, "_%s_list".printf (variable_name), collection, source_reference), source_reference));
                add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_size".printf (variable_name), new MemberAccess (new MemberAccess.simple ("_%s_list".printf (variable_name), source_reference), "size", source_reference), source_reference), source_reference));
                add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_index".printf (variable_name), new UnaryExpression (UnaryOperator.MINUS, new IntegerLiteral ("1", source_reference), source_reference), source_reference), source_reference));
                var next = new UnaryExpression (UnaryOperator.INCREMENT, new MemberAccess.simple ("_%s_index".printf (variable_name), source_reference), source_reference);