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

Unified Diff: tests/language/ref_before_declaration_test.dart

Issue 51533003: Compile time error if name is used before variable is declared (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 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
« tests/language/bailout3_test.dart ('K') | « tests/language/prefix9_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/language/ref_before_declaration_test.dart
===================================================================
--- tests/language/ref_before_declaration_test.dart (revision 0)
+++ tests/language/ref_before_declaration_test.dart (working copy)
@@ -0,0 +1,81 @@
+// Copyright (c) 2013, 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.
+
+// Test compile-time errors for illegal variable declarations if the name
+// has been referenced before the variable is declared.
+
+import 'dart:math' as math;
+
+use(value) => value;
+
+var x = 0;
+final y = 0;
+
+class C {
+ var f;
+ C() : f = 'How do you spell PTSD?';
+
+ void test1() {
+ use(f); // Refers to instance field f.
+ var f = 'A shut mouth gathers no foot.'; /// 00: compile-time error
+ }
+
+ void test2() {
+ void localFunc() {
+ use(f); // Refers to instance field f.
+ }
+
+ var f = 'When chemists die, they barium.'; /// 01: compile-time error
+ if (true) {
+ var f = 1; // ok, shadows outer f and instance field f.
+ }
+ }
+
+ void test3() {
+ if (true) {
+ use(x); // Refers to top-level x.
+ use(y); // Refers to top-level y.
+ }
+ final x = "I have not yet begun to procrastinate."; /// 02: compile-time error
+ const y = "Honk if you like peace and quiet!"; /// 03: compile-time error
+ }
+
+ test() {
+ test1();
+ test2();
+ test3();
+ }
+}
+
+void testTypeRef() {
+ String s = 'Can vegetarians eat animal crackers?';
+ var String = "I distinctly remember forgetting that."; /// 04: compile-time error
+}
+
+void testLibPrefix() {
+ var pie = math.PI;
+ final math = 0; /// 05: compile-time error
+}
+
+
+void noErrorsExpected() {
+ use(x);
+ for (var x = 0; x < 10; x++) use(x);
+ for (var i = 0; i < 10; i++) var x = 0;
+ if (true) var x = 0;
+ while (false) var x = 0;
+ try { throw "ball"; } catch (x) { use(x); }
+ switch (x) {
+ case 0: var x = 'Does fuzzy logic tickle?';
+ }
+ var int = 007;
+}
+
+void main() {
+ var c = new C();
+ c.test();
+ testTypeRef();
+ testLibPrefix();
+ noErrorsExpected();
+}
« tests/language/bailout3_test.dart ('K') | « tests/language/prefix9_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698