Migrate language_2/redirecting to NNBD.

Change-Id: Idda1536f02c5c15653b94f1ed7f845cf3c86318d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150464
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
This commit is contained in:
Robert Nystrom 2020-06-08 22:35:37 +00:00 committed by commit-bot@chromium.org
parent 4122ccb514
commit cf5ba0b2aa
11 changed files with 377 additions and 0 deletions

View file

@ -0,0 +1,43 @@
// 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.
import "package:expect/expect.dart";
var string = '';
append(x) {
string += x;
return x;
}
class A {
var x = append('x');
var y;
var z;
// Should append y but not yet x.
A() : this.foo(append('y'));
// Append x and z.
A.foo(this.y) : z = append('z');
}
class B extends A {
var w;
// Call the redirecting constructor using super.
B()
: w = append('w'),
super();
}
main() {
string = '';
new A();
Expect.equals('yxz', string);
string = '';
new B();
Expect.equals('wyxz', string);
}

View file

@ -0,0 +1,50 @@
// 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.
// Bounds checking on redirecting factories.
class Foo<T> {}
class Baz<T> {}
class Foobar<T> implements Foo<T> {}
class Bar<
T // A comment to prevent dartfmt from joining the lines.
extends Foo<T> //# 00: ok
extends Baz<Foo<T>> //# 01: compile-time error
extends Foobar<T> //# 02: compile-time error
> {
Bar.named();
factory Bar() = Qux<T>;
}
class Qux<
T // A comment to prevent dartfmt from joining the lines.
extends Foo<T> //# 00: continued
extends Foo<T> //# 01: continued
extends Foo<T> //# 02: continued
> extends Bar<T> {
Qux() : super.named();
}
class A<T extends int> {
factory A() = B<
T // A comment to prevent dartfmt from joining the lines.
, int //# 03: compile-time error
, String //# 04: ok
>;
}
class B<T extends int
, S extends String //# 03: continued
, S extends String //# 04: continued
> implements A<T> {}
void main() {
new Bar<Never>();
new A<int>();
}

View file

@ -0,0 +1,28 @@
// 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 parameter default values are disallowed in a redirecting factory.
import "package:expect/expect.dart";
class A {
A(this.a, [this.b = 0]);
factory A.f(int a) = A;
int a;
int b;
}
main() {
var x = new A.f(42);
Expect.equals(x.a, 42);
Expect.equals(x.b, 0);
}

View file

@ -0,0 +1,39 @@
// 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 parameter default values are disallowed in a redirecting factory.
import "package:expect/expect.dart";
class A {
A(this.a, [this.b = 0]);
factory A.f(int a) = A;
factory A.g(int a, [int b = 0]) = A;
// ^
// [analyzer] COMPILE_TIME_ERROR.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
// ^
// [cfe] Can't have a default value here because any default values of 'A' would be used instead.
factory A.h(int a, {int b: 0}) = A;
// ^
// [analyzer] COMPILE_TIME_ERROR.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
// ^
// [cfe] Can't have a default value here because any default values of 'A' would be used instead.
// ^
// [analyzer] STATIC_WARNING.REDIRECT_TO_INVALID_FUNCTION_TYPE
// [cfe] The constructor function type 'A Function(int, [int])' isn't a subtype of 'A Function(int, {int b})'.
int a;
int b;
}
main() {
var x = new A.f(42);
Expect.equals(x.a, 42);
Expect.equals(x.b, 0);
var y = new A.f(42, 43);
// ^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.EXTRA_POSITIONAL_ARGUMENTS
// [cfe] Too many positional arguments: 1 allowed, but 2 found.
}

View file

@ -0,0 +1,20 @@
// 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 incompatible signatures in a forwarding factory
// constructor leads to a compile-time error.
import "package:expect/expect.dart";
class A {
A(a, b);
}
main() {
}

View file

@ -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.
// Test that incompatible signatures in a forwarding factory
// constructor leads to a compile-time error.
import "package:expect/expect.dart";
class A {
A(a, b);
factory A.f() = A;
// ^
// [analyzer] STATIC_WARNING.REDIRECT_TO_INVALID_FUNCTION_TYPE
// [cfe] The constructor function type 'A Function(dynamic, dynamic)' isn't a subtype of 'A Function()'.
}
main() {
new A.f();
}

View file

@ -0,0 +1,25 @@
// 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.
// From Dart Language Specification, 0.12 M1, "7.6.2 Factories": It is
// a compile-time error if a redirecting factory constructor does not
// redirect to a non-redirecting factory constructor or to a
// generative constructor in a finite number of steps.
// TODO(ahe): The above specification will probably change to
// something like: "It is a compile-time error if a redirecting
// factory constructor redirects to itself, either directly or
// indirectly via a sequence of redirections."
class Foo extends Bar {
factory Foo() = Bar; //# 01: compile-time error
}
class Bar {
factory Bar() = Foo; //# 02: compile-time error
}
main() {
new Foo();
}

View file

@ -0,0 +1,81 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test long substitution of long redirection chains.
import 'package:expect/expect.dart';
class C {
const factory C() = D<int>;
}
class D<T> implements C {
const factory D() = E<double, T>;
}
class E<S, T> implements D<T> {
final field = 42;
const E();
}
class X<T> {
const factory X() = Y<T>;
}
class X1<T1> {
const factory X1() = Y<T1>;
}
class X2 {
const factory X2() = Y<int>;
}
class Y<S> implements X<S>, X1<S>, X2 {
const factory Y() = Y1<S>;
}
class Y1<U> implements Y<U> {
const factory Y1() = Z<U>;
}
class Z<V> implements Y1<V> {
final field = 87;
const Z();
}
void main() {
testC(new C());
testC(const C());
testZ(new X<int>());
testZ(new X1<int>());
testZ(new X2());
testZ(const X<int>());
testZ(const X1<int>());
testZ(const X2());
}
void testC(var c) {
Expect.isTrue(c is C);
Expect.isTrue(c is D<int>);
Expect.isTrue(c is! D<String>);
Expect.isTrue(c is E<double, int>);
Expect.isTrue(c is! E<String, int>);
Expect.isTrue(c is! E<double, String>);
Expect.equals(42, c.field);
}
void testZ(var z) {
Expect.isTrue(z is X<int>);
Expect.isTrue(z is! X<String>);
Expect.isTrue(z is X1<int>);
Expect.isTrue(z is! X1<String>);
Expect.isTrue(z is X2);
Expect.isTrue(z is Y<int>);
Expect.isTrue(z is! Y<String>);
Expect.isTrue(z is Y1<int>);
Expect.isTrue(z is! Y1<String>);
Expect.isTrue(z is Z<int>);
Expect.isTrue(z is! Z<String>);
Expect.equals(87, z.field);
}

View file

@ -0,0 +1,26 @@
// 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 Foo<T> {
factory Foo() = Bar<T>;
Foo.create() {}
}
class Bar<
T
> extends Foo<T> {
factory Bar() {
return new Bar<T>.create();
}
Bar.create() : super.create() {}
}
main() {
new Foo<String>();
}

View file

@ -0,0 +1,29 @@
// 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 Foo<T> {
factory Foo() = Bar<T>;
// ^
// [cfe] The type 'T' doesn't extend 'num'.
// ^
// [analyzer] COMPILE_TIME_ERROR.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS
Foo.create() {}
}
class Bar<
T
extends num
> extends Foo<T> {
factory Bar() {
return new Bar<T>.create();
}
Bar.create() : super.create() {}
}
main() {
new Foo<String>();
// ^
// [cfe] Type argument 'String' doesn't conform to the bound 'num' of the type variable 'T' on 'Bar'.
}

View file

@ -0,0 +1,16 @@
// 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 A implements B {
var x;
A(Object this.x);
}
class B {
factory B(String s) = A;
}
main() {
B(42 as dynamic); //# 01: runtime error
}