Skip to content

Commit 48608ce

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/variance to NNBD.
Change-Id: I8e21f82dc241a63f78fb1f6a25669b1aa633788c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152080 Commit-Queue: Bob Nystrom <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent d4154ac commit 48608ce

39 files changed

+4106
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests identifier usage of keywords `out` and `inout`, correct usage of `in`.
6+
7+
import "package:expect/expect.dart";
8+
9+
class A<out> {}
10+
11+
class B<inout> {}
12+
13+
class C<out, inout> {}
14+
15+
F<inout, out>() {}
16+
17+
mixin G<out, inout> {}
18+
19+
typedef H<inout, out> = out Function(inout);
20+
21+
class OutParameter {
22+
var out = 3;
23+
int func(int out) {
24+
return out;
25+
}
26+
}
27+
28+
class inout {
29+
void out(int x) {}
30+
}
31+
32+
var out = 5;
33+
34+
main() {
35+
OutParameter x = new OutParameter();
36+
Expect.equals(2, x.func(2));
37+
Expect.equals(3, x.out);
38+
39+
inout foo = inout();
40+
foo.out(4);
41+
42+
Expect.equals(5, out);
43+
44+
var collection = [0, 1, 2];
45+
for (var x in collection) {
46+
Expect.isTrue(x is int);
47+
}
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests with `variance` flag disabled
6+
// Correct variance modifier usage will issue an error.
7+
8+
import 'package:expect/expect.dart';
9+
10+
abstract class A<in X> {
11+
// ^^
12+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
13+
// [cfe] This requires the 'variance' language feature to be enabled.
14+
int foo(X bar);
15+
}
16+
17+
class B<out X, in Y, inout Z> {}
18+
// ^^^
19+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
20+
// [cfe] This requires the 'variance' language feature to be enabled.
21+
// ^^
22+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
23+
// [cfe] This requires the 'variance' language feature to be enabled.
24+
// ^^^^^
25+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
26+
// [cfe] This requires the 'variance' language feature to be enabled.
27+
28+
class C<in T> extends A<T> {
29+
// ^^
30+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
31+
// [cfe] This requires the 'variance' language feature to be enabled.
32+
@override
33+
int foo(T bar) {
34+
return 2;
35+
}
36+
}
37+
38+
mixin D<out T> {}
39+
// ^^^
40+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
41+
// [cfe] This requires the 'variance' language feature to be enabled.
42+
43+
class E1 {}
44+
45+
mixin E<in T extends E1> {}
46+
// ^^
47+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
48+
// [cfe] This requires the 'variance' language feature to be enabled.
49+
50+
class F<out T> = Object with D<T>;
51+
// ^^^
52+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
53+
// [cfe] This requires the 'variance' language feature to be enabled.
54+
55+
class G<out out> {}
56+
// ^^^
57+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
58+
// [cfe] This requires the 'variance' language feature to be enabled.
59+
60+
class H<out inout> {}
61+
// ^^^
62+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
63+
// [cfe] This requires the 'variance' language feature to be enabled.
64+
65+
main() {
66+
B<int, String, bool> b = B();
67+
68+
C<int> c = C();
69+
Expect.equals(2, c.foo(3));
70+
71+
F<int> f = F();
72+
73+
G<int> g = G();
74+
75+
H<int> h = H();
76+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests identifier usage of keywords `out` and `inout`, correct usage of `in`
6+
// with the experimental flag `variance` enabled.
7+
8+
// SharedOptions=--enable-experiment=variance
9+
10+
import "package:expect/expect.dart";
11+
12+
class A<out> {}
13+
14+
class B<inout> {}
15+
16+
class C<out, inout> {}
17+
18+
F<inout, out>() {}
19+
20+
mixin G<out, inout> {}
21+
22+
typedef H<inout, out> = out Function(inout);
23+
24+
class OutParameter {
25+
var out = 3;
26+
int func(int out) {
27+
return out;
28+
}
29+
}
30+
31+
class inout {
32+
void out(int x) {}
33+
}
34+
35+
var out = 5;
36+
37+
main() {
38+
OutParameter x = new OutParameter();
39+
Expect.equals(2, x.func(2));
40+
Expect.equals(3, x.out);
41+
42+
inout foo = inout();
43+
foo.out(4);
44+
45+
Expect.equals(5, out);
46+
47+
var collection = [0, 1, 2];
48+
for (var x in collection) {
49+
Expect.isTrue(x is int);
50+
}
51+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// SharedOptions=--enable-experiment=variance
6+
7+
import 'package:expect/expect.dart';
8+
9+
abstract class A<in X> {
10+
int foo(X bar);
11+
}
12+
13+
class B<out X, in Y, inout Z> {}
14+
15+
class C<in T> extends A<T> {
16+
@override
17+
int foo(T bar) {
18+
return 2;
19+
}
20+
}
21+
22+
mixin D<out T> {}
23+
24+
class E1 {}
25+
26+
mixin E<in T extends E1> {}
27+
28+
class F<out T> = Object with D<T>;
29+
30+
class G<out out> {}
31+
32+
class H<out inout> {}
33+
34+
main() {
35+
B<int, String, bool> b = B();
36+
37+
C<int> c = C();
38+
Expect.equals(2, c.foo(3));
39+
40+
F<int> f = F();
41+
42+
G<int> g = G();
43+
44+
H<int> h = H();
45+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests erroneous usages of variance in unapplicable type parameters.
6+
7+
// SharedOptions=--enable-experiment=variance
8+
9+
void A(out int foo) {
10+
// ^^^
11+
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
12+
// [cfe] 'out' isn't a type.
13+
// ^
14+
// [cfe] Type 'out' not found.
15+
// ^^^
16+
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
17+
// [cfe] Expected ')' before this.
18+
List<out String> bar;
19+
// ^
20+
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR
21+
// [cfe] The operator '<' isn't defined for the class 'Type'.
22+
// ^^^
23+
// [analyzer] STATIC_WARNING.UNDEFINED_IDENTIFIER
24+
// [cfe] Expected ';' after this.
25+
// ^^^
26+
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
27+
// [cfe] Getter not found: 'out'.
28+
// ^
29+
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR
30+
// [cfe] The operator '>' isn't defined for the class 'Type'.
31+
// ^^^
32+
// [analyzer] STATIC_WARNING.UNDEFINED_IDENTIFIER
33+
// [cfe] Getter not found: 'bar'.
34+
}
35+
36+
void B(out foo) {}
37+
// ^^^
38+
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
39+
// [cfe] 'out' isn't a type.
40+
// ^
41+
// [cfe] Type 'out' not found.
42+
43+
class C<in out X, out out Y> {}
44+
// ^^^
45+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
46+
// [cfe] Each type parameter can have at most one variance modifier.
47+
// ^^^
48+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
49+
// [cfe] Each type parameter can have at most one variance modifier.
50+
51+
class D<in out inout in out X> {}
52+
// ^^^
53+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
54+
// [cfe] Each type parameter can have at most one variance modifier.
55+
// ^^^^^
56+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
57+
// [cfe] Each type parameter can have at most one variance modifier.
58+
// ^^
59+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
60+
// [cfe] Each type parameter can have at most one variance modifier.
61+
// ^^^
62+
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
63+
// [cfe] Each type parameter can have at most one variance modifier.
64+
65+
typedef E<out T> = T Function(T a);
66+
// ^
67+
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
68+
// [cfe] Expected ',' before this.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests downwards inference for explicit variance modifiers.
6+
7+
// SharedOptions=--enable-experiment=variance
8+
9+
class A<out T> {
10+
final T _x;
11+
A(T x):_x = x;
12+
T get x => _x;
13+
void set x(Object value) {}
14+
}
15+
16+
class B<in T> {
17+
B(List<T> x);
18+
void set x(T val) {}
19+
}
20+
21+
class C<out T, S> {
22+
final T _x;
23+
C(T x, S y):_x = x;
24+
T get x => _x;
25+
void set x(Object value) {}
26+
void set y(S _value) {}
27+
}
28+
29+
class D<in T> {
30+
D(T x, void Function(T) y) {}
31+
void set x(T val) {}
32+
}
33+
34+
main() {
35+
// int <: T <: Object
36+
// Choose int
37+
A<Object> a = new A(3)..x+=1;
38+
39+
// int <: T
40+
// num <: T
41+
// Choose num
42+
B<int> b = new B(<num>[])..x=2.2;
43+
44+
// int <: T <: Object
45+
// Choose int
46+
// int <: S <: Object
47+
// Choose Object
48+
C<Object, Object> c = new C(3, 3)..x+=1..y="hello";
49+
50+
// int <: T <: num
51+
// Choose num due to contravariant heuristic.
52+
D<int> d = new D(3, (num x) {})..x=2.2;
53+
}

0 commit comments

Comments
 (0)