dart-sdk/tests/language/variance/variance_downwards_inference_test.dart
Robert Nystrom 122f07c7bb Fix mistakes in variance_downwards_inference_test.
Fix #43422.

Change-Id: Ic315148284e45a26cfe88ac6177c16c671a318eb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162961
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2020-09-17 00:15:19 +00:00

54 lines
1.1 KiB
Dart

// 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;
}