mirror of
https://github.com/dart-lang/sdk
synced 2024-10-06 14:59:41 +00:00
Migrate language_2/variable to NNBD.
Change-Id: I2c0182203d7566908a574208b7d8f10da08d7a43 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152043 Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com> Commit-Queue: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
parent
52d16dfbba
commit
8eb0b79ac1
23
tests/language/variable/bad_initializer1_runtime_test.dart
Normal file
23
tests/language/variable/bad_initializer1_runtime_test.dart
Normal file
|
@ -0,0 +1,23 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
|
||||
main() {
|
||||
const elems = const [
|
||||
const [
|
||||
1,
|
||||
2.0,
|
||||
true,
|
||||
false,
|
||||
0xffffffffff,
|
||||
|
||||
],
|
||||
"a",
|
||||
"b"
|
||||
];
|
||||
}
|
26
tests/language/variable/bad_initializer1_test.dart
Normal file
26
tests/language/variable/bad_initializer1_test.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
// 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.
|
||||
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
|
||||
main() {
|
||||
const elems = const [
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.RECURSIVE_COMPILE_TIME_CONSTANT
|
||||
// [cfe] Can't declare 'elems' because it was already used in this scope.
|
||||
const [
|
||||
1,
|
||||
2.0,
|
||||
true,
|
||||
false,
|
||||
0xffffffffff,
|
||||
elems
|
||||
// ^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
// [cfe] Getter not found: 'elems'.
|
||||
],
|
||||
"a",
|
||||
"b"
|
||||
];
|
||||
}
|
22
tests/language/variable/bad_initializer2_runtime_test.dart
Normal file
22
tests/language/variable/bad_initializer2_runtime_test.dart
Normal file
|
@ -0,0 +1,22 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
var foo = (int n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1
|
||||
|
||||
;
|
||||
}
|
||||
};
|
||||
foo(1);
|
||||
}
|
26
tests/language/variable/bad_initializer2_test.dart
Normal file
26
tests/language/variable/bad_initializer2_test.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
// 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.
|
||||
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
var foo = (int n) {
|
||||
// ^
|
||||
// [cfe] Can't declare 'foo' because it was already used in this scope.
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1
|
||||
+ foo(n - 1)
|
||||
//^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
// [cfe] Method not found: 'foo'.
|
||||
;
|
||||
}
|
||||
};
|
||||
foo(1);
|
||||
//^
|
||||
// [cfe] Method not found: 'foo'.
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright (c) 2016, 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.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class C<
|
||||
D //# 01: compile-time error
|
||||
E //# none: ok
|
||||
> {
|
||||
void set D(int value) {
|
||||
field = value;
|
||||
}
|
||||
|
||||
int field = -1;
|
||||
}
|
||||
|
||||
main() {
|
||||
C<int> c = new C<int>();
|
||||
c.D = 1;
|
||||
Expect.equals(c.field, 1);
|
||||
}
|
67
tests/language/variable/cyclic_type_variable_test.dart
Normal file
67
tests/language/variable/cyclic_type_variable_test.dart
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright (c) 2012, 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.
|
||||
|
||||
// Tests cyclic reference to type variables in type expressions
|
||||
|
||||
class Base<T> {}
|
||||
|
||||
class Derived extends Base<Derived> {} // legal
|
||||
|
||||
typedef void funcType<
|
||||
T
|
||||
extends T //# 01: compile-time error
|
||||
>(T arg);
|
||||
|
||||
class DerivedFunc extends Base<funcType<DerivedFunc>> {}
|
||||
|
||||
abstract class A<
|
||||
S
|
||||
extends S //# 02: compile-time error
|
||||
> {
|
||||
S? field;
|
||||
}
|
||||
|
||||
abstract class B<U extends Base<U>> {
|
||||
// legal
|
||||
U? field;
|
||||
}
|
||||
|
||||
class C1<
|
||||
V
|
||||
extends V // //# 03: compile-time error
|
||||
> {
|
||||
V? field;
|
||||
}
|
||||
|
||||
class C2<
|
||||
V
|
||||
extends V // //# 04: compile-time error
|
||||
> implements A<V> {
|
||||
V? field;
|
||||
}
|
||||
|
||||
class D1<W extends Base<W>> {
|
||||
// legal
|
||||
W? field;
|
||||
}
|
||||
|
||||
class D2<W extends Base<W>> implements B<W> {
|
||||
// legal
|
||||
W? field;
|
||||
}
|
||||
|
||||
class E<X extends Base<funcType<X>>> {
|
||||
// legal
|
||||
|
||||
X? field;
|
||||
}
|
||||
|
||||
main() {
|
||||
new C1<int>();
|
||||
new C2<int>();
|
||||
new D1<Derived>();
|
||||
new D2<Derived>();
|
||||
new E<DerivedFunc>();
|
||||
funcType<Object>? val = null;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// Copyright (c) 2018, 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.
|
||||
|
||||
class Repeated {
|
||||
var a = '', b = 'Something';
|
||||
|
||||
}
|
||||
|
||||
main() {}
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (c) 2018, 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.
|
||||
|
||||
class Repeated {
|
||||
var a = '', b = 'Something';
|
||||
var b;
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
|
||||
// [cfe] 'b' is already declared in this scope.
|
||||
}
|
||||
|
||||
main() {}
|
83
tests/language/variable/fixed_type_variable2_test.dart
Normal file
83
tests/language/variable/fixed_type_variable2_test.dart
Normal file
|
@ -0,0 +1,83 @@
|
|||
// 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 that type variables are passed on from subtypes that fixed the type
|
||||
// variable in inheritance.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
class A<T> {
|
||||
B<T> createB() => new B<T>();
|
||||
}
|
||||
|
||||
class NumA extends A<num> {}
|
||||
|
||||
class B<T> {
|
||||
T? value;
|
||||
|
||||
void test(var o, bool expect) {
|
||||
Expect.equals(expect, o is T);
|
||||
}
|
||||
}
|
||||
|
||||
class StringB extends B<String> {}
|
||||
|
||||
class C<T> extends A<T> {}
|
||||
|
||||
class IntC extends C<int> {}
|
||||
|
||||
void main() {
|
||||
testA(); //# 01: ok
|
||||
testNumA(); //# 02: ok
|
||||
testB(); //# 03: ok
|
||||
testStringB(); //# 04: ok
|
||||
testC(); //# 05: ok
|
||||
testIntC(); //# 06: ok
|
||||
}
|
||||
|
||||
void testA() {
|
||||
var instanceA = new A<String>();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(0.5, false);
|
||||
instanceB.test(0, false);
|
||||
instanceB.test('', true);
|
||||
}
|
||||
|
||||
void testNumA() {
|
||||
var instanceA = new NumA();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(0.5, true);
|
||||
instanceB.test(0, true);
|
||||
instanceB.test('', false);
|
||||
}
|
||||
|
||||
void testB() {
|
||||
var instanceB = new B<int>();
|
||||
instanceB.test(0.5, false);
|
||||
instanceB.test(0, true);
|
||||
instanceB.test('', false);
|
||||
}
|
||||
|
||||
void testStringB() {
|
||||
var instanceB = new StringB();
|
||||
instanceB.test(0.5, false);
|
||||
instanceB.test(0, false);
|
||||
instanceB.test('', true);
|
||||
}
|
||||
|
||||
void testC() {
|
||||
var instanceA = new C<String>();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(0.5, false);
|
||||
instanceB.test(0, false);
|
||||
instanceB.test('', true);
|
||||
}
|
||||
|
||||
void testIntC() {
|
||||
var instanceA = new IntC();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(0.5, false);
|
||||
instanceB.test(0, true);
|
||||
instanceB.test('', false);
|
||||
}
|
83
tests/language/variable/fixed_type_variable_test.dart
Normal file
83
tests/language/variable/fixed_type_variable_test.dart
Normal file
|
@ -0,0 +1,83 @@
|
|||
// 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 that type variables are passed on from subtypes that fixed the type
|
||||
// variable in inheritance.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
class A<T> {
|
||||
B<T> createB() => new B<T>();
|
||||
}
|
||||
|
||||
class NumA extends A<num> {}
|
||||
|
||||
class B<T> {
|
||||
T? value;
|
||||
|
||||
void test(var type, bool expect) {
|
||||
Expect.equals(expect, T == type);
|
||||
}
|
||||
}
|
||||
|
||||
class StringB extends B<String> {}
|
||||
|
||||
class C<T> extends A<T> {}
|
||||
|
||||
class IntC extends C<int> {}
|
||||
|
||||
void main() {
|
||||
testA(); //# 01: ok
|
||||
testNumA(); //# 02: ok
|
||||
testB(); //# 03: ok
|
||||
testStringB(); //# 04: ok
|
||||
testC(); //# 05: ok
|
||||
testIntC(); //# 06: ok
|
||||
}
|
||||
|
||||
void testA() {
|
||||
var instanceA = new A<String>();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(num, false);
|
||||
instanceB.test(int, false);
|
||||
instanceB.test(String, true);
|
||||
}
|
||||
|
||||
void testNumA() {
|
||||
var instanceA = new NumA();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(num, true);
|
||||
instanceB.test(int, false);
|
||||
instanceB.test(String, false);
|
||||
}
|
||||
|
||||
void testB() {
|
||||
var instanceB = new B<int>();
|
||||
instanceB.test(num, false);
|
||||
instanceB.test(int, true);
|
||||
instanceB.test(String, false);
|
||||
}
|
||||
|
||||
void testStringB() {
|
||||
var instanceB = new StringB();
|
||||
instanceB.test(num, false);
|
||||
instanceB.test(int, false);
|
||||
instanceB.test(String, true);
|
||||
}
|
||||
|
||||
void testC() {
|
||||
var instanceA = new C<String>();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(num, false);
|
||||
instanceB.test(int, false);
|
||||
instanceB.test(String, true);
|
||||
}
|
||||
|
||||
void testIntC() {
|
||||
var instanceA = new IntC();
|
||||
var instanceB = instanceA.createB();
|
||||
instanceB.test(num, false);
|
||||
instanceB.test(int, true);
|
||||
instanceB.test(String, false);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
class A {
|
||||
A();
|
||||
A.foo();
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
B.c1()
|
||||
: super.foo
|
||||
|
||||
()
|
||||
|
||||
;
|
||||
|
||||
B.foo();
|
||||
B.c2()
|
||||
: this.foo
|
||||
|
||||
()
|
||||
|
||||
;
|
||||
|
||||
B.c3()
|
||||
: super
|
||||
|
||||
()
|
||||
|
||||
;
|
||||
|
||||
B();
|
||||
B.c4()
|
||||
: this
|
||||
|
||||
()
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
main() {
|
||||
new B.c1();
|
||||
new B.c2();
|
||||
new B.c3();
|
||||
new B.c4();
|
||||
}
|
60
tests/language/variable/illegal_initializer_test.dart
Normal file
60
tests/language/variable/illegal_initializer_test.dart
Normal file
|
@ -0,0 +1,60 @@
|
|||
// 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.
|
||||
|
||||
class A {
|
||||
A();
|
||||
A.foo();
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
B.c1() : super.foo;
|
||||
// ^
|
||||
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
|
||||
// [cfe] Expected '(' after this.
|
||||
|
||||
B.foo();
|
||||
B.c2() : this.foo;
|
||||
// ^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_REFERENCE_TO_THIS
|
||||
// [cfe] Can't access 'this' in a field initializer.
|
||||
// ^^^^
|
||||
// [analyzer] SYNTACTIC_ERROR.MISSING_ASSIGNMENT_IN_INITIALIZER
|
||||
// [cfe] Expected an assignment after the field name.
|
||||
// ^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INITIALIZER_FOR_NON_EXISTENT_FIELD
|
||||
// ^^^
|
||||
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
|
||||
|
||||
B.c3() : super;
|
||||
// ^
|
||||
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
|
||||
// [cfe] Expected '(' after this.
|
||||
;
|
||||
//^
|
||||
// [analyzer] SYNTACTIC_ERROR.EXPECTED_CLASS_MEMBER
|
||||
// [cfe] Expected a class member, but got ';'.
|
||||
|
||||
B();
|
||||
B.c4() : this;
|
||||
// ^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INITIALIZER_FOR_NON_EXISTENT_FIELD
|
||||
// [cfe] Expected an assignment after the field name.
|
||||
// ^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_REFERENCE_TO_THIS
|
||||
// ^^^^
|
||||
// [analyzer] SYNTACTIC_ERROR.MISSING_ASSIGNMENT_IN_INITIALIZER
|
||||
// [error line 39, column 16, length 0]
|
||||
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
|
||||
// [cfe] Expected '.' before this.
|
||||
// ^
|
||||
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
|
||||
// [cfe] Expected an identifier, but got ''.
|
||||
}
|
||||
|
||||
main() {
|
||||
new B.c1();
|
||||
new B.c2();
|
||||
new B.c3();
|
||||
new B.c4();
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
// 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.
|
||||
|
||||
// Ensure that dart2js's receiver specialization optimization works
|
||||
// with captured variables.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
var list = <dynamic>[new Object(), 31];
|
||||
|
||||
main() {
|
||||
Expect.throwsNoSuchMethodError(() => foo()() + 42);
|
||||
}
|
||||
|
||||
foo() {
|
||||
var a = list[0];
|
||||
var closure = (() => a - 42);
|
||||
return () => a + 54;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// 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.
|
||||
|
||||
// Ensure that dart2js's receiver specialization optimization works
|
||||
// with captured variables.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
var list = <dynamic>[new Object(), 31];
|
||||
|
||||
main() {
|
||||
Expect.throwsNoSuchMethodError(() => foo()() + 42);
|
||||
}
|
||||
|
||||
foo() {
|
||||
var a = list[0];
|
||||
var closure = (() => a + 42);
|
||||
if (list[1] == 0) {
|
||||
a.toInt();
|
||||
return closure;
|
||||
}
|
||||
return closure;
|
||||
}
|
170
tests/language/variable/initializer_super_last_runtime_test.dart
Normal file
170
tests/language/variable/initializer_super_last_runtime_test.dart
Normal file
|
@ -0,0 +1,170 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// Copyright (c) 201, 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.
|
||||
// VMOptions=--enable-asserts
|
||||
//
|
||||
// Dart test program testing assert statements.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class S {
|
||||
const S();
|
||||
const S.named();
|
||||
}
|
||||
|
||||
class C extends S {
|
||||
final int x;
|
||||
C.cc01(int x)
|
||||
: x = x,
|
||||
super();
|
||||
C.cc02(int x)
|
||||
: x = x,
|
||||
super.named();
|
||||
C.cc03(this.x) : super();
|
||||
C.cc04(this.x) : super.named();
|
||||
C.cc05(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super();
|
||||
C.cc06(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super.named();
|
||||
C.cc07(this.x)
|
||||
: assert(x == x),
|
||||
super();
|
||||
C.cc08(this.x)
|
||||
: assert(x == x),
|
||||
super.named();
|
||||
C.cc09(int x)
|
||||
: //
|
||||
|
||||
x = x;
|
||||
C.cc10(int x)
|
||||
: //
|
||||
|
||||
x = x;
|
||||
C.cc11(this.x)
|
||||
: //
|
||||
|
||||
assert(x == x);
|
||||
C.cc12(this.x)
|
||||
: //
|
||||
|
||||
assert(x == x);
|
||||
C.cc13(int x)
|
||||
: //
|
||||
|
||||
x = x,
|
||||
assert(x == x);
|
||||
C.cc14(int x)
|
||||
: //
|
||||
|
||||
x = x,
|
||||
assert(x == x);
|
||||
C.cc15(int x)
|
||||
: x = x,
|
||||
|
||||
assert(x == x);
|
||||
C.cc16(int x)
|
||||
: x = x,
|
||||
|
||||
assert(x == x);
|
||||
|
||||
const C.cc17(int x)
|
||||
: x = x,
|
||||
super();
|
||||
const C.cc18(int x)
|
||||
: x = x,
|
||||
super.named();
|
||||
const C.cc19(this.x) : super();
|
||||
const C.cc20(this.x) : super.named();
|
||||
const C.cc21(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super();
|
||||
const C.cc22(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super.named();
|
||||
const C.cc23(this.x)
|
||||
: assert(x == x),
|
||||
super();
|
||||
const C.cc24(this.x)
|
||||
: assert(x == x),
|
||||
super.named();
|
||||
const C.cc25(int x)
|
||||
: //
|
||||
|
||||
x = x;
|
||||
const C.cc26(int x)
|
||||
: //
|
||||
|
||||
x = x;
|
||||
const C.cc27(this.x)
|
||||
: //
|
||||
|
||||
assert(x == x);
|
||||
const C.cc28(this.x)
|
||||
: //
|
||||
|
||||
assert(x == x);
|
||||
const C.cc29(int x)
|
||||
: //
|
||||
|
||||
x = x,
|
||||
assert(x == x);
|
||||
const C.cc30(int x)
|
||||
: //
|
||||
|
||||
x = x,
|
||||
assert(x == x);
|
||||
const C.cc31(int x)
|
||||
: x = x,
|
||||
|
||||
assert(x == x);
|
||||
const C.cc32(int x)
|
||||
: x = x,
|
||||
|
||||
assert(x == x);
|
||||
}
|
||||
|
||||
main() {
|
||||
// Ensure that erroneous constructors are actually needed.
|
||||
new C.cc01(42);
|
||||
new C.cc02(42);
|
||||
new C.cc03(42);
|
||||
new C.cc04(42);
|
||||
new C.cc05(42);
|
||||
new C.cc06(42);
|
||||
new C.cc07(42);
|
||||
new C.cc08(42);
|
||||
new C.cc09(42);
|
||||
new C.cc10(42);
|
||||
new C.cc11(42);
|
||||
new C.cc12(42);
|
||||
new C.cc13(42);
|
||||
new C.cc14(42);
|
||||
new C.cc15(42);
|
||||
new C.cc16(42);
|
||||
|
||||
const C.cc17(42);
|
||||
const C.cc18(42);
|
||||
const C.cc19(42);
|
||||
const C.cc20(42);
|
||||
const C.cc21(42);
|
||||
const C.cc22(42);
|
||||
const C.cc23(42);
|
||||
const C.cc24(42);
|
||||
const C.cc25(42);
|
||||
const C.cc26(42);
|
||||
const C.cc27(42);
|
||||
const C.cc28(42);
|
||||
const C.cc29(42);
|
||||
const C.cc30(42);
|
||||
const C.cc31(42);
|
||||
const C.cc32(42);
|
||||
}
|
223
tests/language/variable/initializer_super_last_test.dart
Normal file
223
tests/language/variable/initializer_super_last_test.dart
Normal file
|
@ -0,0 +1,223 @@
|
|||
// Copyright (c) 201, 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.
|
||||
// VMOptions=--enable-asserts
|
||||
//
|
||||
// Dart test program testing assert statements.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class S {
|
||||
const S();
|
||||
const S.named();
|
||||
}
|
||||
|
||||
class C extends S {
|
||||
final int x;
|
||||
C.cc01(int x)
|
||||
: x = x,
|
||||
super();
|
||||
C.cc02(int x)
|
||||
: x = x,
|
||||
super.named();
|
||||
C.cc03(this.x) : super();
|
||||
C.cc04(this.x) : super.named();
|
||||
C.cc05(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super();
|
||||
C.cc06(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super.named();
|
||||
C.cc07(this.x)
|
||||
: assert(x == x),
|
||||
super();
|
||||
C.cc08(this.x)
|
||||
: assert(x == x),
|
||||
super.named();
|
||||
C.cc09(int x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x;
|
||||
C.cc10(int x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x;
|
||||
C.cc11(this.x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
C.cc12(this.x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
C.cc13(int x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x,
|
||||
assert(x == x);
|
||||
C.cc14(int x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x,
|
||||
assert(x == x);
|
||||
C.cc15(int x)
|
||||
: x = x,
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
C.cc16(int x)
|
||||
: x = x,
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
|
||||
const C.cc17(int x)
|
||||
: x = x,
|
||||
super();
|
||||
const C.cc18(int x)
|
||||
: x = x,
|
||||
super.named();
|
||||
const C.cc19(this.x) : super();
|
||||
const C.cc20(this.x) : super.named();
|
||||
const C.cc21(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super();
|
||||
const C.cc22(int x)
|
||||
: x = x,
|
||||
assert(x == x),
|
||||
super.named();
|
||||
const C.cc23(this.x)
|
||||
: assert(x == x),
|
||||
super();
|
||||
const C.cc24(this.x)
|
||||
: assert(x == x),
|
||||
super.named();
|
||||
const C.cc25(int x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x;
|
||||
const C.cc26(int x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x;
|
||||
const C.cc27(this.x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
const C.cc28(this.x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
const C.cc29(int x)
|
||||
: //
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x,
|
||||
assert(x == x);
|
||||
const C.cc30(int x)
|
||||
: //
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
x = x,
|
||||
assert(x == x);
|
||||
const C.cc31(int x)
|
||||
: x = x,
|
||||
super(),
|
||||
// ^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
const C.cc32(int x)
|
||||
: x = x,
|
||||
super.named(),
|
||||
// ^^^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.INVALID_SUPER_INVOCATION
|
||||
// ^
|
||||
// [cfe] Can't have initializers after 'super'.
|
||||
assert(x == x);
|
||||
}
|
||||
|
||||
main() {
|
||||
// Ensure that erroneous constructors are actually needed.
|
||||
new C.cc01(42);
|
||||
new C.cc02(42);
|
||||
new C.cc03(42);
|
||||
new C.cc04(42);
|
||||
new C.cc05(42);
|
||||
new C.cc06(42);
|
||||
new C.cc07(42);
|
||||
new C.cc08(42);
|
||||
new C.cc09(42);
|
||||
new C.cc10(42);
|
||||
new C.cc11(42);
|
||||
new C.cc12(42);
|
||||
new C.cc13(42);
|
||||
new C.cc14(42);
|
||||
new C.cc15(42);
|
||||
new C.cc16(42);
|
||||
|
||||
const C.cc17(42);
|
||||
const C.cc18(42);
|
||||
const C.cc19(42);
|
||||
const C.cc20(42);
|
||||
const C.cc21(42);
|
||||
const C.cc22(42);
|
||||
const C.cc23(42);
|
||||
const C.cc24(42);
|
||||
const C.cc25(42);
|
||||
const C.cc26(42);
|
||||
const C.cc27(42);
|
||||
const C.cc28(42);
|
||||
const C.cc29(42);
|
||||
const C.cc30(42);
|
||||
const C.cc31(42);
|
||||
const C.cc32(42);
|
||||
}
|
19
tests/language/variable/inst_field_initializer1_test.dart
Normal file
19
tests/language/variable/inst_field_initializer1_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) 2019, 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.
|
||||
|
||||
/// Catch illegal access to 'this' in initialized instance fields.
|
||||
|
||||
class A {
|
||||
var x = 5;
|
||||
var arr = [x]; // Illegal access to 'this'.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER
|
||||
// [cfe] Can't access 'this' in a field initializer to read 'x'.
|
||||
// ^
|
||||
// [analyzer] STATIC_WARNING.TOP_LEVEL_INSTANCE_GETTER
|
||||
}
|
||||
|
||||
void main() {
|
||||
A();
|
||||
}
|
61
tests/language/variable/inst_field_initializer_test.dart
Normal file
61
tests/language/variable/inst_field_initializer_test.dart
Normal file
|
@ -0,0 +1,61 @@
|
|||
// 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.
|
||||
// Test for instance field initializer expressions.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class Cheese {
|
||||
static const mild = 1;
|
||||
static const stinky = 2;
|
||||
|
||||
// Instance fields with initializer expression.
|
||||
String name = "";
|
||||
var smell = mild;
|
||||
|
||||
Cheese() {
|
||||
Expect.equals("", this.name);
|
||||
Expect.equals(Cheese.mild, this.smell);
|
||||
}
|
||||
|
||||
Cheese.initInBlock(String s) {
|
||||
Expect.equals("", this.name);
|
||||
Expect.equals(Cheese.mild, this.smell);
|
||||
this.name = s;
|
||||
}
|
||||
|
||||
Cheese.initFieldParam(this.name, this.smell) {}
|
||||
|
||||
// Test that static const field Cheese.mild is not shadowed
|
||||
// by the parameter mild when compiling the field initializer
|
||||
// for instance field smell.
|
||||
Cheese.hideAndSeek(var mild) : name = mild {
|
||||
Expect.equals(mild, this.name);
|
||||
Expect.equals(Cheese.mild, this.smell);
|
||||
}
|
||||
}
|
||||
|
||||
class HasNoExplicitConstructor {
|
||||
String s = "Tilsiter";
|
||||
}
|
||||
|
||||
main() {
|
||||
var generic = new Cheese();
|
||||
Expect.equals("", generic.name);
|
||||
Expect.equals(Cheese.mild, generic.smell);
|
||||
|
||||
var gruyere = new Cheese.initInBlock("Gruyere");
|
||||
Expect.equals("Gruyere", gruyere.name);
|
||||
Expect.equals(Cheese.mild, gruyere.smell);
|
||||
|
||||
var munster = new Cheese.initFieldParam("Munster", Cheese.stinky);
|
||||
Expect.equals("Munster", munster.name);
|
||||
Expect.equals(Cheese.stinky, munster.smell);
|
||||
|
||||
var brie = new Cheese.hideAndSeek("Brie");
|
||||
Expect.equals("Brie", brie.name);
|
||||
Expect.equals(Cheese.mild, brie.smell);
|
||||
|
||||
var t = new HasNoExplicitConstructor();
|
||||
Expect.equals("Tilsiter", t.s);
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
// Copyright (c) 2018, 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.
|
||||
|
||||
main() {
|
||||
L:
|
||||
var x, y;
|
||||
x = 42;
|
||||
y = x;
|
||||
}
|
100
tests/language/variable/ref_before_declaration_runtime_test.dart
Normal file
100
tests/language/variable/ref_before_declaration_runtime_test.dart
Normal file
|
@ -0,0 +1,100 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
}
|
||||
|
||||
void test2() {
|
||||
void localFunc() {
|
||||
use(f); // Refers to instance field f.
|
||||
}
|
||||
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void test4() {
|
||||
void Q() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
test() {
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
test4();
|
||||
}
|
||||
}
|
||||
|
||||
void testTypeRef() {
|
||||
String s = 'Can vegetarians eat animal crackers?';
|
||||
|
||||
}
|
||||
|
||||
void testLibPrefix() {
|
||||
var pie = math.pi;
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
132
tests/language/variable/ref_before_declaration_test.dart
Normal file
132
tests/language/variable/ref_before_declaration_test.dart
Normal file
|
@ -0,0 +1,132 @@
|
|||
// 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.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
var f = 'A shut mouth gathers no foot.';
|
||||
// ^
|
||||
// [cfe] Can't declare 'f' because it was already used in this scope.
|
||||
}
|
||||
|
||||
void test2() {
|
||||
void localFunc() {
|
||||
use(f); // Refers to instance field f.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
}
|
||||
|
||||
var f = 'When chemists die, they barium.';
|
||||
// ^
|
||||
// [cfe] Can't declare 'f' because it was already used in this scope.
|
||||
if (true) {
|
||||
var f = 1; // ok, shadows outer f and instance field f.
|
||||
}
|
||||
}
|
||||
|
||||
void test3() {
|
||||
if (true) {
|
||||
use(x); // Refers to top-level x.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
use(y); // Refers to top-level y.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
}
|
||||
final x = "I have not yet begun to procrastinate.";
|
||||
// ^
|
||||
// [cfe] Can't declare 'x' because it was already used in this scope.
|
||||
const y = "Honk if you like peace and quiet!";
|
||||
// ^
|
||||
// [cfe] Can't declare 'y' because it was already used in this scope.
|
||||
}
|
||||
|
||||
void test4() {
|
||||
void Q() {
|
||||
P(); // Refers to non-existing top-level function P
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
// [cfe] The method 'P' isn't defined for the class 'C'.
|
||||
}
|
||||
void P() {
|
||||
// ^
|
||||
// [cfe] Can't declare 'P' because it was already used in this scope.
|
||||
Q();
|
||||
}
|
||||
|
||||
Function f = () {x = f;};
|
||||
// ^
|
||||
// [cfe] Can't declare 'f' because it was already used in this scope.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
}
|
||||
|
||||
test() {
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
test4();
|
||||
}
|
||||
}
|
||||
|
||||
void testTypeRef() {
|
||||
String s = 'Can vegetarians eat animal crackers?';
|
||||
//^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
var String = "I distinctly remember forgetting that.";
|
||||
// ^
|
||||
// [cfe] Can't declare 'String' because it was already used in this scope.
|
||||
}
|
||||
|
||||
void testLibPrefix() {
|
||||
var pie = math.pi;
|
||||
// ^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
final math = 0;
|
||||
// ^
|
||||
// [cfe] Can't declare 'math' because it was already used in this scope.
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
50
tests/language/variable/scope_variable_runtime_test.dart
Normal file
50
tests/language/variable/scope_variable_runtime_test.dart
Normal file
|
@ -0,0 +1,50 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
void testSimpleScope() {
|
||||
{
|
||||
var a = "Test";
|
||||
int b = 1;
|
||||
}
|
||||
{
|
||||
var c;
|
||||
int? d;
|
||||
Expect.isNull(c);
|
||||
Expect.isNull(d);
|
||||
}
|
||||
}
|
||||
|
||||
void testShadowingScope() {
|
||||
var a = "Test";
|
||||
{
|
||||
var a;
|
||||
Expect.isNull(a);
|
||||
a = "a";
|
||||
Expect.equals(a, "a");
|
||||
}
|
||||
Expect.equals(a, "Test");
|
||||
}
|
||||
|
||||
int testShadowingAfterUse() {
|
||||
var a = 1;
|
||||
{
|
||||
var b = 2;
|
||||
var c = a; // Use of 'a' prior to its shadow declaration below.
|
||||
var d = b + c;
|
||||
// Shadow declaration of 'a'.
|
||||
|
||||
return d + a;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
testSimpleScope();
|
||||
testShadowingScope();
|
||||
testShadowingAfterUse();
|
||||
}
|
51
tests/language/variable/scope_variable_test.dart
Normal file
51
tests/language/variable/scope_variable_test.dart
Normal file
|
@ -0,0 +1,51 @@
|
|||
// 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.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
void testSimpleScope() {
|
||||
{
|
||||
var a = "Test";
|
||||
int b = 1;
|
||||
}
|
||||
{
|
||||
var c;
|
||||
int? d;
|
||||
Expect.isNull(c);
|
||||
Expect.isNull(d);
|
||||
}
|
||||
}
|
||||
|
||||
void testShadowingScope() {
|
||||
var a = "Test";
|
||||
{
|
||||
var a;
|
||||
Expect.isNull(a);
|
||||
a = "a";
|
||||
Expect.equals(a, "a");
|
||||
}
|
||||
Expect.equals(a, "Test");
|
||||
}
|
||||
|
||||
num testShadowingAfterUse() {
|
||||
var a = 1;
|
||||
{
|
||||
var b = 2;
|
||||
var c = a; // Use of 'a' prior to its shadow declaration below.
|
||||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.REFERENCED_BEFORE_DECLARATION
|
||||
var d = b + c;
|
||||
// Shadow declaration of 'a'.
|
||||
var a = 5;
|
||||
// ^
|
||||
// [cfe] Can't declare 'a' because it was already used in this scope.
|
||||
return d + a;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
testSimpleScope();
|
||||
testShadowingScope();
|
||||
testShadowingAfterUse();
|
||||
}
|
28
tests/language/variable/scoped_variables_try_catch_test.dart
Normal file
28
tests/language/variable/scoped_variables_try_catch_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2012, 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.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
// Test that try/catch does not shadow a variable at runtime.
|
||||
|
||||
main() {
|
||||
var a = bar();
|
||||
try {
|
||||
a = bar();
|
||||
} catch (e) {}
|
||||
Expect.equals(42, a);
|
||||
|
||||
{
|
||||
var a = foo();
|
||||
try {
|
||||
a = foo();
|
||||
} catch (e) {}
|
||||
Expect.equals(54, a);
|
||||
}
|
||||
|
||||
Expect.equals(42, a);
|
||||
}
|
||||
|
||||
bar() => 42;
|
||||
foo() => 54;
|
|
@ -0,0 +1,86 @@
|
|||
// Copyright (c) 2015, 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.
|
||||
|
||||
// Verify that the individual variable declarations inside a variable
|
||||
// declaration list are not allowed to be annotated with metadata.
|
||||
|
||||
const annotation = null;
|
||||
|
||||
var
|
||||
@annotation //# 01: syntax error
|
||||
v1,
|
||||
@annotation //# 02: syntax error
|
||||
v2;
|
||||
|
||||
int
|
||||
@annotation //# 03: syntax error
|
||||
v3 = -1,
|
||||
@annotation //# 04: syntax error
|
||||
v4 = -1;
|
||||
|
||||
class C {
|
||||
var
|
||||
@annotation //# 05: syntax error
|
||||
f1,
|
||||
@annotation //# 06: syntax error
|
||||
f2;
|
||||
|
||||
int
|
||||
@annotation //# 07: syntax error
|
||||
f3 = -1,
|
||||
@annotation //# 08: syntax error
|
||||
f4 = -1;
|
||||
}
|
||||
|
||||
use(x) => x;
|
||||
|
||||
main() {
|
||||
use(v1);
|
||||
use(v2);
|
||||
use(v3);
|
||||
use(v4);
|
||||
|
||||
C c = new C();
|
||||
use(c.f1);
|
||||
use(c.f2);
|
||||
use(c.f3);
|
||||
use(c.f4);
|
||||
|
||||
var
|
||||
@annotation //# 09: syntax error
|
||||
l1,
|
||||
@annotation //# 10: syntax error
|
||||
l2;
|
||||
|
||||
int
|
||||
@annotation //# 11: syntax error
|
||||
l3 = -1,
|
||||
@annotation //# 12: syntax error
|
||||
l4 = -1;
|
||||
|
||||
use(l1);
|
||||
use(l2);
|
||||
use(l3);
|
||||
use(l4);
|
||||
|
||||
for (var
|
||||
@annotation //# 13: syntax error
|
||||
i1 = 0,
|
||||
@annotation //# 14: syntax error
|
||||
i2 = 0;;) {
|
||||
use(i1);
|
||||
use(i2);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int
|
||||
@annotation //# 15: syntax error
|
||||
i3 = 0,
|
||||
@annotation //# 16: syntax error
|
||||
i4 = 0;;) {
|
||||
use(i3);
|
||||
use(i4);
|
||||
break;
|
||||
}
|
||||
}
|
10
tests/language/variable/variable_named_dart_test.dart
Normal file
10
tests/language/variable/variable_named_dart_test.dart
Normal file
|
@ -0,0 +1,10 @@
|
|||
// Copyright (c) 2016, 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.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
main() {
|
||||
dynamic dart = 123;
|
||||
Expect.equals(dart.toDouble(), 123.0);
|
||||
}
|
Loading…
Reference in a new issue