diff --git a/tests/language/type_variable/bound_access_runtime_test.dart b/tests/language/type_variable/bound_access_runtime_test.dart new file mode 100644 index 00000000000..0c7a1233d60 --- /dev/null +++ b/tests/language/type_variable/bound_access_runtime_test.dart @@ -0,0 +1,32 @@ +// TODO(multitest): This was automatically migrated from a multitest and may +// contain strange or dead code. + +// 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. + +class DynamicClass { + T field1; + T field2; + + DynamicClass(this.field1, this.field2); + + method() => field1 * field2; +} + +class NumClass { + T field1; + S field2; + + NumClass(this.field1, this.field2); + + num method1() => field1 * field2; + + +} + +main() { + new DynamicClass(0.5, 2).method(); + new NumClass(2, 0.5).method1(); + +} diff --git a/tests/language/type_variable/bound_access_test.dart b/tests/language/type_variable/bound_access_test.dart new file mode 100644 index 00000000000..77f85945ed5 --- /dev/null +++ b/tests/language/type_variable/bound_access_test.dart @@ -0,0 +1,32 @@ +// 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. + +class DynamicClass { + T field1; + T field2; + + DynamicClass(this.field1, this.field2); + + method() => field1 * field2; +} + +class NumClass { + T field1; + S field2; + + NumClass(this.field1, this.field2); + + num method1() => field1 * field2; + + num method2() => field1 + field2.length; + // ^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER + // [cfe] The getter 'length' isn't defined for the class 'num'. +} + +main() { + new DynamicClass(0.5, 2).method(); + new NumClass(2, 0.5).method1(); + new NumClass(2, 0.5).method2(); +} diff --git a/tests/language/type_variable/bounds2_test.dart b/tests/language/type_variable/bounds2_test.dart new file mode 100644 index 00000000000..ecb6c9f62a7 --- /dev/null +++ b/tests/language/type_variable/bounds2_test.dart @@ -0,0 +1,19 @@ +// 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 of parameterized types with invalid bounds. + +abstract class J {} + +abstract class I {} + +class A implements I, J {} + +main() { + // We are only interested in the instance creation, hence + // the result is assigned to `dynamic`. + dynamic a = /*@compile-error=unspecified*/ new A(); +} diff --git a/tests/language/type_variable/bounds3_runtime_test.dart b/tests/language/type_variable/bounds3_runtime_test.dart new file mode 100644 index 00000000000..4dd58432d3b --- /dev/null +++ b/tests/language/type_variable/bounds3_runtime_test.dart @@ -0,0 +1,21 @@ +// 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 of parameterized types with invalid bounds. + +class A {} + +class B { + foo(x) { + + } +} + +main() { + var b = new B(); + b.foo(new A()); +} diff --git a/tests/language/type_variable/bounds3_test.dart b/tests/language/type_variable/bounds3_test.dart new file mode 100644 index 00000000000..471112b2400 --- /dev/null +++ b/tests/language/type_variable/bounds3_test.dart @@ -0,0 +1,22 @@ +// 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 of parameterized types with invalid bounds. + +class A {} + +class B { + foo(x) { + return x is A; + // ^ + // [cfe] Type argument 'X' doesn't conform to the bound 'int' of the type variable 'K' on 'A'. + // ^ + // [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + } +} + +main() { + var b = new B(); + b.foo(new A()); +} diff --git a/tests/language/type_variable/bounds4_runtime_test.dart b/tests/language/type_variable/bounds4_runtime_test.dart new file mode 100644 index 00000000000..eef3b1dcd1a --- /dev/null +++ b/tests/language/type_variable/bounds4_runtime_test.dart @@ -0,0 +1,58 @@ +// 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 instantiation of object with malbounded types. + +class A< + T + + > {} + +class B implements A {} + +class C< + T + + > implements B {} + +class Class { + newA() { + new A(); + } + newB() { + new B(); + } + newC() { + new C(); + } +} + +void test(f()) { + var v = f(); +} + +void main() { + test(() => new A()); + // TODO(eernst): Should it be a compile-time error to create an instance + // of this class in #01? + test(() => new B()); + test(() => new C()); + + test(() => new A()); + test(() => new B()); + test(() => new C()); + + dynamic c = new Class(); + test(() => c.newA()); + test(() => c.newB()); + test(() => c.newC()); + + c = new Class(); + test(() => c.newA()); + test(() => c.newB()); + test(() => c.newC()); +} diff --git a/tests/language/type_variable/bounds4_test.dart b/tests/language/type_variable/bounds4_test.dart new file mode 100644 index 00000000000..9908b9c14ae --- /dev/null +++ b/tests/language/type_variable/bounds4_test.dart @@ -0,0 +1,75 @@ +// 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 instantiation of object with malbounded types. + +class A< + T + extends num + > {} + +class B implements A {} +// ^ +// [cfe] Type argument 'T' doesn't conform to the bound 'num' of the type variable 'T' on 'A' in the supertype 'A' of class 'B'. +// ^ +// [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + +class C< + T + extends num + > implements B {} + +class Class { + newA() { + new A(); + // ^ + // [cfe] Type argument 'T' doesn't conform to the bound 'num' of the type variable 'T' on 'A'. + // ^ + // [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + } + newB() { + new B(); + } + newC() { + new C(); + // ^ + // [cfe] Type argument 'T' doesn't conform to the bound 'num' of the type variable 'T' on 'C'. + // ^ + // [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + } +} + +void test(f()) { + var v = f(); +} + +void main() { + test(() => new A()); + // TODO(eernst): Should it be a compile-time error to create an instance + // of this class in #01? + test(() => new B()); + test(() => new C()); + + test(() => new A()); + // ^ + // [cfe] Type argument 'String' doesn't conform to the bound 'num' of the type variable 'T' on 'A'. + // ^^^^^^ + // [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + test(() => new B()); + test(() => new C()); + // ^ + // [cfe] Type argument 'String' doesn't conform to the bound 'num' of the type variable 'T' on 'C'. + // ^^^^^^ + // [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS + + dynamic c = new Class(); + test(() => c.newA()); + test(() => c.newB()); + test(() => c.newC()); + + c = new Class(); + test(() => c.newA()); + test(() => c.newB()); + test(() => c.newC()); +} diff --git a/tests/language/type_variable/bounds_test.dart b/tests/language/type_variable/bounds_test.dart new file mode 100644 index 00000000000..c23942de8ba --- /dev/null +++ b/tests/language/type_variable/bounds_test.dart @@ -0,0 +1,75 @@ +// 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. + +// Test of parameterized factory methods. + +class Foo { + Foo(); + + factory Foo.bad() = XFoo; // //# 00: compile-time error + + factory Foo.good() = Foo; + + factory Foo.IFoo() { + throw "uncalled"; + } +} + +abstract class IFoo { + factory IFoo() = Foo; //# 11: compile-time error +} + +// String is not a subtype of num. +class Baz + extends Foo //# 01: compile-time error +{} + +class Biz extends Foo {} + +late Foo fi; + +// String is not a subtype of num. +late Foo + //# 02: compile-time error + fs; + +class Box { + // Box.T is not guaranteed to be a subtype of num. + Foo t; //# 03: compile-time error + + makeFoo() { + // Box.T is not guaranteed to be a subtype of num. + return new Foo(); //# 04: compile-time error + } +} + +main() { + // String is not a subtype of num. + var v1 = new Foo(); //# 05: compile-time error + + // String is not a subtype of num. + Foo v2 = null; //# 06: compile-time error + + new Baz(); + new Biz(); + + fi = new Foo(); + fs = new Foo(); + + new Box().makeFoo(); + new Box().makeFoo(); + new Box().makeFoo(); + + // Fisk does not exist. + new Box(); //# 07: compile-time error + + // Too many type arguments. + new Box(); //# 08: compile-time error + + // Fisk does not exist. + Box box = null; //# 09: compile-time error + + // Too many type arguments. + Box box = null; //# 10: compile-time error +} diff --git a/tests/language/type_variable/closure2_test.dart b/tests/language/type_variable/closure2_test.dart new file mode 100644 index 00000000000..1b55ff9aca8 --- /dev/null +++ b/tests/language/type_variable/closure2_test.dart @@ -0,0 +1,31 @@ +// 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. + +import "package:expect/expect.dart"; + +class A {} + +class C { + a() { + return () => new A(); + } + + list() { + return () => []; + } + + map() { + return () => {}; + } +} + +main() { + Expect.isTrue(new C().a()() is A); + Expect.isFalse(new C().a()() is A); + Expect.isTrue(new C().list()() is List); + Expect.isFalse(new C().list()() is List); + Expect.isTrue(new C().map()() is Map); + Expect.isFalse(new C().map()() is Map); + Expect.isFalse(new C().map()() is Map); +} diff --git a/tests/language/type_variable/closure3_test.dart b/tests/language/type_variable/closure3_test.dart new file mode 100644 index 00000000000..ecb1c39e82e --- /dev/null +++ b/tests/language/type_variable/closure3_test.dart @@ -0,0 +1,18 @@ +// 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 A {} + +class C { + a() { + return () => new A(); + } +} + +main() { + Expect.isTrue(new C().a()() is A); + Expect.isFalse(new C().a()() is A); +} diff --git a/tests/language/type_variable/closure4_test.dart b/tests/language/type_variable/closure4_test.dart new file mode 100644 index 00000000000..e75bc085dc1 --- /dev/null +++ b/tests/language/type_variable/closure4_test.dart @@ -0,0 +1,19 @@ +// 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 A {} + +class C { + map() { + return () => {}; + } +} + +main() { + Expect.isTrue(new C().map()() is Map); + Expect.isFalse(new C().map()() is Map); + Expect.isFalse(new C().map()() is Map); +} diff --git a/tests/language/type_variable/closure_test.dart b/tests/language/type_variable/closure_test.dart new file mode 100644 index 00000000000..7fd7381dd3f --- /dev/null +++ b/tests/language/type_variable/closure_test.dart @@ -0,0 +1,27 @@ +// 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. + +import "package:expect/expect.dart"; + +class C { + C.foo() { + x = (a) => a is T; + } + C.bar() { + x = (a) => a is! T; + } + C.baz() { + x = (a) => a as T; + } + var x; +} + +main() { + Expect.isTrue(new C.foo().x(1)); + Expect.isFalse(new C.foo().x('1')); + Expect.isFalse(new C.bar().x(1)); + Expect.isTrue(new C.bar().x('1')); + Expect.equals(new C.baz().x(1), 1); + Expect.throws(() => new C.baz().x('1')); +} diff --git a/tests/language/type_variable/conflict2_runtime_test.dart b/tests/language/type_variable/conflict2_runtime_test.dart new file mode 100644 index 00000000000..275f8dc4f4a --- /dev/null +++ b/tests/language/type_variable/conflict2_runtime_test.dart @@ -0,0 +1,59 @@ +// TODO(multitest): This was automatically migrated from a multitest and may +// contain strange or dead code. + +// Copyright (c) 2014, 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. + +// Regression test for issue 13134. Invocation of a type parameter. + +import "package:expect/expect.dart"; + +class C { + noSuchMethod(Invocation im) { + throw "noSuchMethod shouldn't be called in this test."; + } + + // This is equivalent to (T).call(). See issue 19725 + + + // T is in scope, even in static context. Compile-time error to call this.T(). + + + // X is not in scope. NoSuchMethodError. + + + // Class 'C' has no static method 'T': NoSuchMethodError. + + + // Class '_Type' has no instance method 'call': NoSuchMethodError. + + + // Runtime type T not accessible from static context. Compile-time error. + + + // Class '_Type' has no [] operator: NoSuchMethodError. + + + // Runtime type T not accessible from static context. Compile-time error. + + + // Class '_Type' has no member m: NoSuchMethodError. + + + // Runtime type T not accessible from static context. Compile-time error. + +} + +main() { + + + + + + + + + + +} diff --git a/tests/language/type_variable/conflict2_test.dart b/tests/language/type_variable/conflict2_test.dart new file mode 100644 index 00000000000..4a03e01e4f4 --- /dev/null +++ b/tests/language/type_variable/conflict2_test.dart @@ -0,0 +1,99 @@ +// Copyright (c) 2014, 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. + +// Regression test for issue 13134. Invocation of a type parameter. + +import "package:expect/expect.dart"; + +class C { + noSuchMethod(Invocation im) { + throw "noSuchMethod shouldn't be called in this test."; + } + + // This is equivalent to (T).call(). See issue 19725 + foo() => T(); + // ^ + // [analyzer] STATIC_TYPE_WARNING.INVOCATION_OF_NON_FUNCTION + // [cfe] Method not found: 'T'. + + // T is in scope, even in static context. Compile-time error to call this.T(). + static bar() => T(); + // ^ + // [analyzer] STATIC_TYPE_WARNING.INVOCATION_OF_NON_FUNCTION + // [cfe] Method not found: 'T'. + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + + // X is not in scope. NoSuchMethodError. + static baz() => X(); + // ^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_METHOD + // [cfe] Method not found: 'X'. + + // Class 'C' has no static method 'T': NoSuchMethodError. + static qux() => C.T(); + // ^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_METHOD + // [cfe] Method not found: 'C.T'. + + // Class '_Type' has no instance method 'call': NoSuchMethodError. + quux() => (T)(); + // ^^^ + // [analyzer] STATIC_TYPE_WARNING.INVOCATION_OF_NON_FUNCTION_EXPRESSION + // ^ + // [cfe] The method 'call' isn't defined for the class 'Type'. + + // Runtime type T not accessible from static context. Compile-time error. + static corge() => (T)(); + // ^^^ + // [analyzer] STATIC_TYPE_WARNING.INVOCATION_OF_NON_FUNCTION_EXPRESSION + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + // ^ + // [cfe] The method 'call' isn't defined for the class 'Type'. + + // Class '_Type' has no [] operator: NoSuchMethodError. + grault() => T[0]; + // ^^^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR + // [cfe] The operator '[]' isn't defined for the class 'Type'. + + // Runtime type T not accessible from static context. Compile-time error. + static garply() => T[0]; + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + // ^^^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR + // [cfe] The operator '[]' isn't defined for the class 'Type'. + + // Class '_Type' has no member m: NoSuchMethodError. + waldo() => T.m; + // ^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER + // [cfe] The getter 'm' isn't defined for the class 'Type'. + + // Runtime type T not accessible from static context. Compile-time error. + static fred() => T.m; + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + // ^ + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER + // [cfe] The getter 'm' isn't defined for the class 'Type'. +} + +main() { + Expect.throwsNoSuchMethodError(() => new C().foo()); + C.bar(); + Expect.throwsNoSuchMethodError(() => C.baz()); + Expect.throwsNoSuchMethodError(() => C.qux()); + Expect.throwsNoSuchMethodError(() => new C().quux()); + C.corge(); + Expect.throwsNoSuchMethodError(() => new C().grault()); + C.garply(); + Expect.throwsNoSuchMethodError(() => new C().waldo()); + C.fred(); +} diff --git a/tests/language/type_variable/conflict_runtime_test.dart b/tests/language/type_variable/conflict_runtime_test.dart new file mode 100644 index 00000000000..04623cb6e8a --- /dev/null +++ b/tests/language/type_variable/conflict_runtime_test.dart @@ -0,0 +1,54 @@ +// 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 that we report a compile-time error when a type parameter conflicts +// with an instance or static member with the same name. + +import "package:expect/expect.dart"; + +class G1 { + +} + +class G2 { + +} + +class G3 { + +} + +class G4 { + +} + +class G5 { + +} + +class G6 { + +} + +class G7 { + +} + +class G8 { + +} + +main() { + new G1(); + new G2(); + new G3(); + new G4(); + new G5(); + new G6(); + new G7(); + new G8(); +} diff --git a/tests/language/type_variable/conflict_test.dart b/tests/language/type_variable/conflict_test.dart new file mode 100644 index 00000000000..cc8e4daa984 --- /dev/null +++ b/tests/language/type_variable/conflict_test.dart @@ -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 we report a compile-time error when a type parameter conflicts +// with an instance or static member with the same name. + +import "package:expect/expect.dart"; + +class G1 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + var T; + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G2 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + get T {} + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G3 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + T() {} +//^ +// [cfe] Conflicts with type variable 'T'. +} + +class G4 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + static var T; + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G5 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + static get T {} + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G6 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + static T() {} + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G7 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + set T(_) {} + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +class G8 { +// ^ +// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER + static set T(x) {} + // ^ + // [cfe] Conflicts with type variable 'T'. +} + +main() { + new G1(); + new G2(); + new G3(); + new G4(); + new G5(); + new G6(); + new G7(); + new G8(); +} diff --git a/tests/language/type_variable/field_initializer2_test.dart b/tests/language/type_variable/field_initializer2_test.dart new file mode 100644 index 00000000000..5249bf2a6f4 --- /dev/null +++ b/tests/language/type_variable/field_initializer2_test.dart @@ -0,0 +1,21 @@ +// 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. + +// Check that an inlined field initializer has access to the enclosing +// type variables. + +import "package:expect/expect.dart"; + +class A { + var c = new X(); +} + +class B extends A {} + +class X {} + +main() { + Expect.isTrue(new B().c is X); + Expect.isFalse(new B().c is X); +} diff --git a/tests/language/type_variable/field_initializer_closure2_test.dart b/tests/language/type_variable/field_initializer_closure2_test.dart new file mode 100644 index 00000000000..f3536cab682 --- /dev/null +++ b/tests/language/type_variable/field_initializer_closure2_test.dart @@ -0,0 +1,21 @@ +// 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. + +// Check that an inlined field closure has access to the enclosing +// type variables. + +import "package:expect/expect.dart"; + +class A { + var c = (() => new X())(); +} + +class B extends A {} + +class X {} + +main() { + Expect.isTrue(new B().c is X); + Expect.isFalse(new B().c is X); +} diff --git a/tests/language/type_variable/field_initializer_closure_test.dart b/tests/language/type_variable/field_initializer_closure_test.dart new file mode 100644 index 00000000000..3b277504a10 --- /dev/null +++ b/tests/language/type_variable/field_initializer_closure_test.dart @@ -0,0 +1,19 @@ +// 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. + +// Check that an inlined field closure has access to the enclosing +// type variables. + +import "package:expect/expect.dart"; + +class A { + var c = (() => [])(); +} + +class B extends A {} + +main() { + Expect.isTrue(new B().c is List); + Expect.isFalse(new B().c is List); +} diff --git a/tests/language/type_variable/field_initializer_test.dart b/tests/language/type_variable/field_initializer_test.dart new file mode 100644 index 00000000000..09331c98261 --- /dev/null +++ b/tests/language/type_variable/field_initializer_test.dart @@ -0,0 +1,19 @@ +// 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. + +// Check that an inlined field initializer has access to the enclosing +// type variables. + +import "package:expect/expect.dart"; + +class A { + var c = []; +} + +class B extends A {} + +main() { + Expect.isTrue(new B().c is List); + Expect.isFalse(new B().c is List); +} diff --git a/tests/language/type_variable/function_type_test.dart b/tests/language/type_variable/function_type_test.dart new file mode 100644 index 00000000000..f2b3602ac9b --- /dev/null +++ b/tests/language/type_variable/function_type_test.dart @@ -0,0 +1,27 @@ +// 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. + +import 'package:expect/expect.dart'; + +typedef T Func(); + +class Foo { + m(x) => x is Func; +} + +class Bar { + f() { + T local() { + throw "uncalled"; + } + return local; + } +} + +void main() { + dynamic x = new Foo>(); + if (new DateTime.now().millisecondsSinceEpoch == 42) x = new Foo(); + Expect.isFalse(x.m(new Bar().f())); + Expect.isTrue(x.m(new Bar>().f())); +} diff --git a/tests/language/type_variable/identifier_expression_test.dart b/tests/language/type_variable/identifier_expression_test.dart new file mode 100644 index 00000000000..1827c56a2a7 --- /dev/null +++ b/tests/language/type_variable/identifier_expression_test.dart @@ -0,0 +1,20 @@ +// 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. +// VMOptions=--enable_type_checks + +class A { + static func() { + return "class A"; + } +} + +class B { + doFunc() { + T.func(); /*@compile-error=unspecified*/ + } +} + +main() { + new B().doFunc(); +} diff --git a/tests/language/type_variable/initializer_test.dart b/tests/language/type_variable/initializer_test.dart new file mode 100644 index 00000000000..3d5a85d114b --- /dev/null +++ b/tests/language/type_variable/initializer_test.dart @@ -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. + +import "package:expect/expect.dart"; + +// Regression test for dart2js where the reference to [:this:] in a +// constructor was not propagated to the super initializers. + +class A { + var map; + // Usage of type variables in the initializer makes the SSA builder + // want to access [:this:]. And because the initializers of A are + // inlined in the constructor of B, we have to make sure the + // [:this:] in the A constructor has a corresponding + // SSA instruction. + A() : map = new Map(); +} + +class B extends A {} + +main() { + Expect.isTrue(new B().map is Map); +} diff --git a/tests/language/type_variable/nested_test.dart b/tests/language/type_variable/nested_test.dart new file mode 100644 index 00000000000..423ce6d4e72 --- /dev/null +++ b/tests/language/type_variable/nested_test.dart @@ -0,0 +1,32 @@ +// 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. + +// Regression test for +// http://code.google.com/p/dart/issues/detail?id=9050. + +import 'package:expect/expect.dart'; + +class A {} + +class B { + var _copy; + B() { + // We used to not register the dependency between List and B. + _copy = >[]; + } +} + +main() { + var a = new B(); + Expect.isFalse(a._copy is List); + Expect.isTrue(a._copy is List); + Expect.isFalse(a._copy is List>); //# 01: ok + + a = new B(); + Expect.isFalse(a._copy is List); + Expect.isTrue(a._copy is List); + Expect.isTrue(a._copy is List>); + Expect.isTrue(a._copy is List>); + Expect.isFalse(a._copy is List>); +} diff --git a/tests/language/type_variable/promotion_issue39752_test.dart b/tests/language/type_variable/promotion_issue39752_test.dart new file mode 100644 index 00000000000..731557daaf5 --- /dev/null +++ b/tests/language/type_variable/promotion_issue39752_test.dart @@ -0,0 +1,13 @@ +// 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. + +main() {} + +void f(T a) { + if (a is num) { + if (a is int) { + a.isEven; + } + } +} diff --git a/tests/language/type_variable/promotion_test.dart b/tests/language/type_variable/promotion_test.dart new file mode 100644 index 00000000000..c0a507c4d41 --- /dev/null +++ b/tests/language/type_variable/promotion_test.dart @@ -0,0 +1,40 @@ +// 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. + +import 'package:expect/expect.dart'; + +class A {} + +class B extends A {} + +class Foo { + String foo(T x) { + if (x is B) { + var list = [x]; + return list.runtimeType.toString(); + } + return ''; + } + + List bar(T x) { + var tlist = []; + if (x is B) { + var list = [x]; + tlist = list; + } + return tlist; + } +} + +main() { + var foo = new Foo(); + var b = new B(); + + // List class has many different platform specific implementations + // so we can't rely on the fact that all of them have the same + // user visible name. Instead we build a name for List from List. + final expected = ([]).runtimeType.toString().replaceAll('<$A>', '<$B>'); + Expect.equals(expected, foo.foo(b)); + Expect.listEquals([b], foo.bar(b)); +} diff --git a/tests/language/type_variable/scope2_test.dart b/tests/language/type_variable/scope2_test.dart new file mode 100644 index 00000000000..6893e2d771f --- /dev/null +++ b/tests/language/type_variable/scope2_test.dart @@ -0,0 +1,16 @@ +// 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 malformed type arguments are treated as an error. + +class Foo { + // T is not in scope for a static method. + static Foo m() { /*@compile-error=unspecified*/ + return new Foo(); + } +} + +main() {} diff --git a/tests/language/type_variable/scope3_runtime_test.dart b/tests/language/type_variable/scope3_runtime_test.dart new file mode 100644 index 00000000000..e75b4a03845 --- /dev/null +++ b/tests/language/type_variable/scope3_runtime_test.dart @@ -0,0 +1,20 @@ +// TODO(multitest): This was automatically migrated from a multitest and may +// contain strange or dead code. + +// 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. + +// Test that a type parameter cannot be repeated. + +class Foo< + T + + > {} + +main() { + new Foo< + String + + >(); +} diff --git a/tests/language/type_variable/scope3_test.dart b/tests/language/type_variable/scope3_test.dart new file mode 100644 index 00000000000..9a382001a2b --- /dev/null +++ b/tests/language/type_variable/scope3_test.dart @@ -0,0 +1,20 @@ +// 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. + +// Test that a type parameter cannot be repeated. + +class Foo< + T + , T + //^ + // [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION + // [cfe] A type variable can't have the same name as another. + > {} + +main() { + new Foo< + String + , String + >(); +} diff --git a/tests/language/type_variable/scope_runtime_test.dart b/tests/language/type_variable/scope_runtime_test.dart new file mode 100644 index 00000000000..7212278f56d --- /dev/null +++ b/tests/language/type_variable/scope_runtime_test.dart @@ -0,0 +1,52 @@ +// TODO(multitest): This was automatically migrated from a multitest and may +// contain strange or dead code. + +// 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. + +// Test that type variables referenced from within static members are malformed. + +class Foo implements I { + Foo() {} + + static + + m( + + f) { + + return new Foo(); + } + + // T is in scope for a factory method. + factory Foo.I(Foo f) { + Foo x = f; + return f; + } + + // T is not in scope for a static field. + + + static + + get f { + return new Foo(); + } + + static void set f( + + value) {} +} + +abstract class I { + factory I(Foo f) = Foo.I; +} + +main() { + Foo.m(new Foo()); + new I(new Foo()); + + var x = Foo.f; + Foo.f = x; +} diff --git a/tests/language/type_variable/scope_test.dart b/tests/language/type_variable/scope_test.dart new file mode 100644 index 00000000000..9930d68cdb1 --- /dev/null +++ b/tests/language/type_variable/scope_test.dart @@ -0,0 +1,85 @@ +// 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. + +// Test that type variables referenced from within static members are malformed. + +class Foo implements I { + Foo() {} + + static + Foo + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + m( +// ^ +// [cfe] Can only use type variables in instance methods. + Foo + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + f) { + Foo x = new Foo(); + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + // ^^^^^^^^^^^^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT + return new Foo(); + // ^^^^^^^^^^^^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.RETURN_OF_INVALID_TYPE + } + + // T is in scope for a factory method. + factory Foo.I(Foo f) { + Foo x = f; + return f; + } + + // T is not in scope for a static field. + static late Foo f1; + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + // ^ + // [cfe] Verification of the generated program failed: + + static + Foo + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + get f { + // ^ + // [cfe] Can only use type variables in instance methods. + return new Foo(); + // ^^^^^^^^^^^^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.RETURN_OF_INVALID_TYPE + } + + static void set f( + // ^ + // [cfe] Can only use type variables in instance methods. + Foo + // ^ + // [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC + // [cfe] Type variables can't be used in static members. + value) {} +} + +abstract class I { + factory I(Foo f) = Foo.I; +} + +main() { + Foo.m(new Foo()); + // ^^^^^^^^^^^^^^^^^ + // [analyzer] STATIC_WARNING.ARGUMENT_TYPE_NOT_ASSIGNABLE + new I(new Foo()); + Foo.f1 = new Foo(); + // ^^^^^^^^^^^^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT + // ^ + // [cfe] A value of type 'Foo' can't be assigned to a variable of type 'Foo'. + var x = Foo.f; + Foo.f = x; +} diff --git a/tests/language/type_variable/static_context_test.dart b/tests/language/type_variable/static_context_test.dart new file mode 100644 index 00000000000..e0cd2bc2902 --- /dev/null +++ b/tests/language/type_variable/static_context_test.dart @@ -0,0 +1,17 @@ +// 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. + +// A type variable can't be referenced in a static class + +class A { + static int method() { + // error, can't reference a type variable in a static context + var foo = + new T(); /*@compile-error=unspecified*/ + } +} + +main() { + A.method(); +} diff --git a/tests/language/type_variable/typedef_test.dart b/tests/language/type_variable/typedef_test.dart new file mode 100644 index 00000000000..caa1649f0e3 --- /dev/null +++ b/tests/language/type_variable/typedef_test.dart @@ -0,0 +1,30 @@ +// 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 rti dependency registration takes type variables within typedefs +// into account. + +import 'package:expect/expect.dart'; + +typedef Foo(T t); + +class A { + m() => new B>(); +} + +class B { + m(o) => o is T; +} + +foo(int i) {} +bar(String s) {} + +void main() { + Expect.isTrue(new A().m().m(foo)); + Expect.isFalse(new A().m().m(bar)); + Expect.isFalse(new A().m().m(foo)); + Expect.isTrue(new A().m().m(bar)); + Expect.isFalse(new A().m().m(foo)); + Expect.isFalse(new A().m().m(bar)); +}