Skip to content

Commit

Permalink
Migrate language_2/variance to NNBD.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
munificent authored and [email protected] committed Jun 26, 2020
1 parent d4154ac commit 48608ce
Show file tree
Hide file tree
Showing 39 changed files with 4,106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

// Tests identifier usage of keywords `out` and `inout`, correct usage of `in`.

import "package:expect/expect.dart";

class A<out> {}

class B<inout> {}

class C<out, inout> {}

F<inout, out>() {}

mixin G<out, inout> {}

typedef H<inout, out> = out Function(inout);

class OutParameter {
var out = 3;
int func(int out) {
return out;
}
}

class inout {
void out(int x) {}
}

var out = 5;

main() {
OutParameter x = new OutParameter();
Expect.equals(2, x.func(2));
Expect.equals(3, x.out);

inout foo = inout();
foo.out(4);

Expect.equals(5, out);

var collection = [0, 1, 2];
for (var x in collection) {
Expect.isTrue(x is int);
}
}
76 changes: 76 additions & 0 deletions tests/language/variance/syntax/variance_disabled_syntax_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.

// Tests with `variance` flag disabled
// Correct variance modifier usage will issue an error.

import 'package:expect/expect.dart';

abstract class A<in X> {
// ^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.
int foo(X bar);
}

class B<out X, in Y, inout Z> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.
// ^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.
// ^^^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

class C<in T> extends A<T> {
// ^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.
@override
int foo(T bar) {
return 2;
}
}

mixin D<out T> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

class E1 {}

mixin E<in T extends E1> {}
// ^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

class F<out T> = Object with D<T>;
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

class G<out out> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

class H<out inout> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED
// [cfe] This requires the 'variance' language feature to be enabled.

main() {
B<int, String, bool> b = B();

C<int> c = C();
Expect.equals(2, c.foo(3));

F<int> f = F();

G<int> g = G();

H<int> h = H();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.

// Tests identifier usage of keywords `out` and `inout`, correct usage of `in`
// with the experimental flag `variance` enabled.

// SharedOptions=--enable-experiment=variance

import "package:expect/expect.dart";

class A<out> {}

class B<inout> {}

class C<out, inout> {}

F<inout, out>() {}

mixin G<out, inout> {}

typedef H<inout, out> = out Function(inout);

class OutParameter {
var out = 3;
int func(int out) {
return out;
}
}

class inout {
void out(int x) {}
}

var out = 5;

main() {
OutParameter x = new OutParameter();
Expect.equals(2, x.func(2));
Expect.equals(3, x.out);

inout foo = inout();
foo.out(4);

Expect.equals(5, out);

var collection = [0, 1, 2];
for (var x in collection) {
Expect.isTrue(x is int);
}
}
45 changes: 45 additions & 0 deletions tests/language/variance/syntax/variance_syntax_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

// SharedOptions=--enable-experiment=variance

import 'package:expect/expect.dart';

abstract class A<in X> {
int foo(X bar);
}

class B<out X, in Y, inout Z> {}

class C<in T> extends A<T> {
@override
int foo(T bar) {
return 2;
}
}

mixin D<out T> {}

class E1 {}

mixin E<in T extends E1> {}

class F<out T> = Object with D<T>;

class G<out out> {}

class H<out inout> {}

main() {
B<int, String, bool> b = B();

C<int> c = C();
Expect.equals(2, c.foo(3));

F<int> f = F();

G<int> g = G();

H<int> h = H();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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.

// Tests erroneous usages of variance in unapplicable type parameters.

// SharedOptions=--enable-experiment=variance

void A(out int foo) {
// ^^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
// [cfe] 'out' isn't a type.
// ^
// [cfe] Type 'out' not found.
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
// [cfe] Expected ')' before this.
List<out String> bar;
// ^
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR
// [cfe] The operator '<' isn't defined for the class 'Type'.
// ^^^
// [analyzer] STATIC_WARNING.UNDEFINED_IDENTIFIER
// [cfe] Expected ';' after this.
// ^^^
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
// [cfe] Getter not found: 'out'.
// ^
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_OPERATOR
// [cfe] The operator '>' isn't defined for the class 'Type'.
// ^^^
// [analyzer] STATIC_WARNING.UNDEFINED_IDENTIFIER
// [cfe] Getter not found: 'bar'.
}

void B(out foo) {}
// ^^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
// [cfe] 'out' isn't a type.
// ^
// [cfe] Type 'out' not found.

class C<in out X, out out Y> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.
// ^^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.

class D<in out inout in out X> {}
// ^^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.
// ^^^^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.
// ^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.
// ^^^
// [analyzer] SYNTACTIC_ERROR.MULTIPLE_VARIANCE_MODIFIERS
// [cfe] Each type parameter can have at most one variance modifier.

typedef E<out T> = T Function(T a);
// ^
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
// [cfe] Expected ',' before this.
53 changes: 53 additions & 0 deletions tests/language/variance/variance_downwards_inference_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

// Tests downwards inference for explicit variance modifiers.

// SharedOptions=--enable-experiment=variance

class A<out T> {
final T _x;
A(T x):_x = x;
T get x => _x;
void set x(Object value) {}
}

class B<in T> {
B(List<T> x);
void set x(T val) {}
}

class C<out T, S> {
final T _x;
C(T x, S y):_x = x;
T get x => _x;
void set x(Object value) {}
void set y(S _value) {}
}

class D<in T> {
D(T x, void Function(T) y) {}
void set x(T val) {}
}

main() {
// int <: T <: Object
// Choose int
A<Object> a = new A(3)..x+=1;

// int <: T
// num <: T
// Choose num
B<int> b = new B(<num>[])..x=2.2;

// int <: T <: Object
// Choose int
// int <: S <: Object
// Choose Object
C<Object, Object> c = new C(3, 3)..x+=1..y="hello";

// int <: T <: num
// Choose num due to contravariant heuristic.
D<int> d = new D(3, (num x) {})..x=2.2;
}
Loading

0 comments on commit 48608ce

Please sign in to comment.