Migrate language_2/type_variable to NNBD.

Change-Id: Iee96b1e254f17ee0b1b7b309cc807bf874a1491e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151982
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
Robert Nystrom 2020-06-22 21:20:26 +00:00 committed by commit-bot@chromium.org
parent 03483c1dab
commit 7e756d6f86
33 changed files with 1200 additions and 0 deletions

View file

@ -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 extends dynamic, S extends T> {
T field1;
T field2;
DynamicClass(this.field1, this.field2);
method() => field1 * field2;
}
class NumClass<T extends num, S extends T> {
T field1;
S field2;
NumClass(this.field1, this.field2);
num method1() => field1 * field2;
}
main() {
new DynamicClass<num, int>(0.5, 2).method();
new NumClass<num, double>(2, 0.5).method1();
}

View file

@ -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 extends dynamic, S extends T> {
T field1;
T field2;
DynamicClass(this.field1, this.field2);
method() => field1 * field2;
}
class NumClass<T extends num, S extends T> {
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<num, int>(0.5, 2).method();
new NumClass<num, double>(2, 0.5).method1();
new NumClass<num, double>(2, 0.5).method2();
}

View file

@ -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<T> {}
abstract class I<T extends num> {}
class A</*@compile-error=unspecified*/T> implements I<T>, J<T> {}
main() {
// We are only interested in the instance creation, hence
// the result is assigned to `dynamic`.
dynamic a = /*@compile-error=unspecified*/ new A<String>();
}

View file

@ -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<K extends int> {}
class B<X, Y> {
foo(x) {
}
}
main() {
var b = new B<double, double>();
b.foo(new A());
}

View file

@ -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<K extends int> {}
class B<X, Y> {
foo(x) {
return x is A<X>;
// ^
// [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<double, double>();
b.foo(new A());
}

View file

@ -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<T> implements A<T> {}
class C<
T
> implements B<T> {}
class Class<T> {
newA() {
new A<T>();
}
newB() {
new B<T>();
}
newC() {
new C<T>();
}
}
void test(f()) {
var v = f();
}
void main() {
test(() => new A<int>());
// TODO(eernst): Should it be a compile-time error to create an instance
// of this class in #01?
test(() => new B<int>());
test(() => new C<int>());
test(() => new A<String>());
test(() => new B<String>());
test(() => new C<String>());
dynamic c = new Class<int>();
test(() => c.newA());
test(() => c.newB());
test(() => c.newC());
c = new Class<String>();
test(() => c.newA());
test(() => c.newB());
test(() => c.newC());
}

View file

@ -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<T> implements A<T> {}
// ^
// [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<T> {}
class Class<T> {
newA() {
new A<T>();
// ^
// [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<T>();
}
newC() {
new C<T>();
// ^
// [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<int>());
// TODO(eernst): Should it be a compile-time error to create an instance
// of this class in #01?
test(() => new B<int>());
test(() => new C<int>());
test(() => new A<String>());
// ^
// [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<String>());
test(() => new C<String>());
// ^
// [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<int>();
test(() => c.newA());
test(() => c.newB());
test(() => c.newC());
c = new Class<String>();
test(() => c.newA());
test(() => c.newB());
test(() => c.newC());
}

View file

@ -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<T extends num> {
Foo();
factory Foo.bad() = XFoo; // //# 00: compile-time error
factory Foo.good() = Foo<T>;
factory Foo.IFoo() {
throw "uncalled";
}
}
abstract class IFoo<T extends num> {
factory IFoo() = Foo<T>; //# 11: compile-time error
}
// String is not a subtype of num.
class Baz
extends Foo<String> //# 01: compile-time error
{}
class Biz extends Foo<int> {}
late Foo<int> fi;
// String is not a subtype of num.
late Foo
<String> //# 02: compile-time error
fs;
class Box<T> {
// Box.T is not guaranteed to be a subtype of num.
Foo<T> t; //# 03: compile-time error
makeFoo() {
// Box.T is not guaranteed to be a subtype of num.
return new Foo<T>(); //# 04: compile-time error
}
}
main() {
// String is not a subtype of num.
var v1 = new Foo<String>(); //# 05: compile-time error
// String is not a subtype of num.
Foo<String> v2 = null; //# 06: compile-time error
new Baz();
new Biz();
fi = new Foo();
fs = new Foo();
new Box().makeFoo();
new Box<int>().makeFoo();
new Box<String>().makeFoo();
// Fisk does not exist.
new Box<Fisk>(); //# 07: compile-time error
// Too many type arguments.
new Box<Object, Object>(); //# 08: compile-time error
// Fisk does not exist.
Box<Fisk> box = null; //# 09: compile-time error
// Too many type arguments.
Box<Object, Object> box = null; //# 10: compile-time error
}

View file

@ -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<T> {}
class C<T> {
a() {
return () => new A<T>();
}
list() {
return () => <T>[];
}
map() {
return () => <T, T>{};
}
}
main() {
Expect.isTrue(new C<int>().a()() is A<int>);
Expect.isFalse(new C<int>().a()() is A<String>);
Expect.isTrue(new C<int>().list()() is List<int>);
Expect.isFalse(new C<int>().list()() is List<String>);
Expect.isTrue(new C<int>().map()() is Map<int, int>);
Expect.isFalse(new C<int>().map()() is Map<String, int>);
Expect.isFalse(new C<int>().map()() is Map<int, String>);
}

View file

@ -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<T> {}
class C<T> {
a() {
return () => new A<T>();
}
}
main() {
Expect.isTrue(new C<int>().a()() is A<int>);
Expect.isFalse(new C<int>().a()() is A<String>);
}

View file

@ -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<T> {}
class C<T> {
map() {
return () => <T, T>{};
}
}
main() {
Expect.isTrue(new C<int>().map()() is Map<int, int>);
Expect.isFalse(new C<int>().map()() is Map<String, int>);
Expect.isFalse(new C<int>().map()() is Map<int, String>);
}

View file

@ -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<T> {
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<int>.foo().x(1));
Expect.isFalse(new C<int>.foo().x('1'));
Expect.isFalse(new C<int>.bar().x(1));
Expect.isTrue(new C<int>.bar().x('1'));
Expect.equals(new C<int>.baz().x(1), 1);
Expect.throws(() => new C<int>.baz().x('1'));
}

View file

@ -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<T> {
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() {
}

View file

@ -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<T> {
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();
}

View file

@ -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<T> {
}
class G2<T> {
}
class G3<T> {
}
class G4<T> {
}
class G5<T> {
}
class G6<T> {
}
class G7<T> {
}
class G8<T> {
}
main() {
new G1<int>();
new G2<int>();
new G3<int>();
new G4<int>();
new G5<int>();
new G6<int>();
new G7<int>();
new G8<int>();
}

View 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 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<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
var T;
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G2<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
get T {}
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G3<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
T() {}
//^
// [cfe] Conflicts with type variable 'T'.
}
class G4<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
static var T;
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G5<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
static get T {}
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G6<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
static T() {}
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G7<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
set T(_) {}
// ^
// [cfe] Conflicts with type variable 'T'.
}
class G8<T> {
// ^
// [analyzer] COMPILE_TIME_ERROR.CONFLICTING_TYPE_VARIABLE_AND_MEMBER
static set T(x) {}
// ^
// [cfe] Conflicts with type variable 'T'.
}
main() {
new G1<int>();
new G2<int>();
new G3<int>();
new G4<int>();
new G5<int>();
new G6<int>();
new G7<int>();
new G8<int>();
}

View file

@ -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<T> {
var c = new X<T>();
}
class B<T> extends A<T> {}
class X<T> {}
main() {
Expect.isTrue(new B<int>().c is X<int>);
Expect.isFalse(new B<String>().c is X<int>);
}

View file

@ -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<T> {
var c = (() => new X<T>())();
}
class B<T> extends A<T> {}
class X<T> {}
main() {
Expect.isTrue(new B<int>().c is X<int>);
Expect.isFalse(new B<String>().c is X<int>);
}

View file

@ -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<T> {
var c = (() => <T>[])();
}
class B<T> extends A<T> {}
main() {
Expect.isTrue(new B<int>().c is List<int>);
Expect.isFalse(new B<String>().c is List<int>);
}

View file

@ -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<T> {
var c = <T>[];
}
class B<T> extends A<T> {}
main() {
Expect.isTrue(new B<int>().c is List<int>);
Expect.isFalse(new B<String>().c is List<int>);
}

View file

@ -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<T>();
class Foo<S> {
m(x) => x is Func<S>;
}
class Bar<T> {
f() {
T local() {
throw "uncalled";
}
return local;
}
}
void main() {
dynamic x = new Foo<List<String>>();
if (new DateTime.now().millisecondsSinceEpoch == 42) x = new Foo<int>();
Expect.isFalse(x.m(new Bar<String>().f()));
Expect.isTrue(x.m(new Bar<List<String>>().f()));
}

View file

@ -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<T> {
doFunc() {
T.func(); /*@compile-error=unspecified*/
}
}
main() {
new B<A>().doFunc();
}

View file

@ -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<T> {
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<T, T>();
}
class B<T> extends A<T> {}
main() {
Expect.isTrue(new B<int>().map is Map<int, int>);
}

View file

@ -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<T> {}
class B<T> {
var _copy;
B() {
// We used to not register the dependency between List and B.
_copy = <A<T>>[];
}
}
main() {
var a = new B();
Expect.isFalse(a._copy is List<int>);
Expect.isTrue(a._copy is List<A>);
Expect.isFalse(a._copy is List<A<int>>); //# 01: ok
a = new B<String>();
Expect.isFalse(a._copy is List<String>);
Expect.isTrue(a._copy is List<A>);
Expect.isTrue(a._copy is List<A<String>>);
Expect.isTrue(a._copy is List<A<Object>>);
Expect.isFalse(a._copy is List<A<int>>);
}

View file

@ -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>(T a) {
if (a is num) {
if (a is int) {
a.isEven;
}
}
}

View file

@ -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<T extends A> {
String foo(T x) {
if (x is B) {
var list = [x];
return list.runtimeType.toString();
}
return '';
}
List<T> bar(T x) {
var tlist = <T>[];
if (x is B) {
var list = [x];
tlist = list;
}
return tlist;
}
}
main() {
var foo = new Foo<B>();
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<B> from List<A>.
final expected = (<A>[]).runtimeType.toString().replaceAll('<$A>', '<$B>');
Expect.equals(expected, foo.foo(b));
Expect.listEquals([b], foo.bar(b));
}

View file

@ -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> {
// T is not in scope for a static method.
static Foo<T> m() { /*@compile-error=unspecified*/
return new Foo();
}
}
main() {}

View file

@ -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
>();
}

View file

@ -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
>();
}

View file

@ -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<T> implements I<T> {
Foo() {}
static
m(
f) {
return new Foo<String>();
}
// T is in scope for a factory method.
factory Foo.I(Foo<T> f) {
Foo<T> x = f;
return f;
}
// T is not in scope for a static field.
static
get f {
return new Foo<String>();
}
static void set f(
value) {}
}
abstract class I<T> {
factory I(Foo<T> f) = Foo<T>.I;
}
main() {
Foo.m(new Foo<String>());
new I(new Foo<String>());
var x = Foo.f;
Foo.f = x;
}

View file

@ -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<T> implements I<T> {
Foo() {}
static
Foo<T>
// ^
// [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC
m(
// ^
// [cfe] Can only use type variables in instance methods.
Foo<T>
// ^
// [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC
// [cfe] Type variables can't be used in static members.
f) {
Foo<T> x = new Foo<String>();
// ^
// [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<String>();
// ^^^^^^^^^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.RETURN_OF_INVALID_TYPE
}
// T is in scope for a factory method.
factory Foo.I(Foo<T> f) {
Foo<T> x = f;
return f;
}
// T is not in scope for a static field.
static late Foo<T> 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<T>
// ^
// [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC
get f {
// ^
// [cfe] Can only use type variables in instance methods.
return new Foo<String>();
// ^^^^^^^^^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.RETURN_OF_INVALID_TYPE
}
static void set f(
// ^
// [cfe] Can only use type variables in instance methods.
Foo<T>
// ^
// [analyzer] STATIC_WARNING.TYPE_PARAMETER_REFERENCED_BY_STATIC
// [cfe] Type variables can't be used in static members.
value) {}
}
abstract class I<T> {
factory I(Foo<T> f) = Foo<T>.I;
}
main() {
Foo.m(new Foo<String>());
// ^^^^^^^^^^^^^^^^^
// [analyzer] STATIC_WARNING.ARGUMENT_TYPE_NOT_ASSIGNABLE
new I(new Foo<String>());
Foo.f1 = new Foo<String>();
// ^^^^^^^^^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
// ^
// [cfe] A value of type 'Foo<String>' can't be assigned to a variable of type 'Foo<T>'.
var x = Foo.f;
Foo.f = x;
}

View file

@ -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<T> {
static int method() {
// error, can't reference a type variable in a static context
var foo =
new T(); /*@compile-error=unspecified*/
}
}
main() {
A.method();
}

View file

@ -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 t);
class A<T> {
m() => new B<Foo<T>>();
}
class B<T> {
m(o) => o is T;
}
foo(int i) {}
bar(String s) {}
void main() {
Expect.isTrue(new A<int>().m().m(foo));
Expect.isFalse(new A<int>().m().m(bar));
Expect.isFalse(new A<String>().m().m(foo));
Expect.isTrue(new A<String>().m().m(bar));
Expect.isFalse(new A<double>().m().m(foo));
Expect.isFalse(new A<double>().m().m(bar));
}