Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Unified Diff: pkg/kernel/testcases/closures/loop2.dart

Issue 3000333002: Fix several bugs in closure conversion. (Closed)
Patch Set: Comments. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/kernel/testcases/closures/loop2.dart
diff --git a/pkg/kernel/testcases/closures/loop2.dart b/pkg/kernel/testcases/closures/loop2.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2069f03edff891f470fd199b8a65aa5556996fec
--- /dev/null
+++ b/pkg/kernel/testcases/closures/loop2.dart
@@ -0,0 +1,30 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+// Check that a variable declared and captured inside a loop is given a separate
+// context for each iteration of the loop, so changes to the variable in
+// subsequent iterations are not visible to closures capturing it in prior
+// iterations.
+
+void doit(int x) {
+ final int max = 10;
+ final double expectedSum = ((max - 1) * max) / 2;
+
+ int counter = 0;
+ var calls = [];
+ while (counter < max) {
+ int pos = counter;
+ calls.add(() => pos + x);
+ counter++;
+ }
+
+ double sum = 0.0;
+ for (var c in calls) sum += c();
+ if (sum != expectedSum)
+ throw new Exception("Unexpected sum = $sum != $expectedSum");
+}
+
+void main() {
+ doit(0);
+}
« no previous file with comments | « pkg/kernel/testcases/closures/for_in_closure.dart.expect ('k') | pkg/kernel/testcases/closures/loop2.dart.expect » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698