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

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

Issue 8490019: Capturing for loop variables correctly (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 9 years, 1 month 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
« runtime/vm/parser.cc ('K') | « runtime/vm/parser.cc ('k') | no next file » | 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 0)
+++ tests/language/src/For2Test.dart (revision 0)
@@ -0,0 +1,38 @@
+// Copyright (c) 2011, 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.
+// Dart test program for testing for statement which captures loop variable.
+
+var f;
+
+main() {
+ // Capture the loop variable, ensure we capture the right value.
+ for (int i = 0; i < 10; i++) {
+ if (i == 7) {
+ f = () => "i = $i";
+ }
+ }
+ Expect.equals("i = 7", f());
+
+ // There is only one instance of k. The captured variable continues
+ // to change.
+ int k;
+ for (k = 0; k < 10; k++) {
+ if (k == 7) {
+ f = () => "k = $k";
+ }
+ }
+ Expect.equals("k = 10", f());
+
+ // l gets modified after it's captured. n++ is executed on the
+ // newly introduced instance of n (i.e. the instance of the loop
+ // iteration after the value is captured).
+ for (int n = 0; n < 10; n++) {
+ var l = n;
+ if (l == 7) {
+ f = () => "l = $l, n = 7";
+ }
+ l++;
+ }
+ Expect.equals("l = 8, n = 7", f());
+}
« runtime/vm/parser.cc ('K') | « runtime/vm/parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698