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

Unified Diff: tests/language/src/For2Test.dart

Issue 9921030: Fix capturing of for-loop variables (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 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
« no previous file with comments | « tests/language/language.status ('k') | tests/language/src/FunctionTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/language/src/For2Test.dart
===================================================================
--- tests/language/src/For2Test.dart (revision 6004)
+++ tests/language/src/For2Test.dart (working copy)
@@ -30,9 +30,40 @@
for (int n = 0; n < 10; n++) {
var l = n;
if (l == 7) {
- f = () => "l = $l, n = 7";
+ f = () => "l = $l, n = $n";
}
l++;
}
Expect.equals("l = 8, n = 7", f());
+
+ // Loop variable is incremented in loop body instead of the increment.
+ // expression. Thus, the captured value is incremented by one before
+ // a new loop variable instance is created.
+ for (int i = 0; i < 10; /*nothing*/) {
+ if (i == 7) {
+ f = () => "i = $i";
+ }
+ i++;
+ }
+ Expect.equals("i = 8", f());
+
+ // Make sure continue still ensures the loop variable is captured correctly.
+ for (int i = 0; i < 10; i++) {
+ if (i == 7) {
+ f = () => "i = $i";
+ }
+ continue;
+ i++;
+ }
+ Expect.equals("i = 7", f());
+
+ // Nested loops with captured variables.
+ for (int k = 0; k < 5; k++) {
+ for (int i = 0; i < 10; i++) {
+ if (k == 3 && i == 7) {
+ f = () => "k = $k, i = $i";
+ }
+ }
+ }
+ Expect.equals("k = 3, i = 7", f());
}
« no previous file with comments | « tests/language/language.status ('k') | tests/language/src/FunctionTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698