Files
sdk/tests/language_2/variance/variance_out_field_test.dart
T
Kallen Tu 550985189c Added errors for variance positions in fields.
When an explicitly defined type variable is used in an incorrect
variance position in a (final/non-final) field, an error is emitted.

Change-Id: Id1af7bdcd9adfe0c94ee76612c58ff63d011a1fb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122881
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
2019-10-31 21:29:39 +00:00

73 lines
1.2 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 various fields for the `out` variance modifier.
// SharedOptions=--enable-experiment=variance
import "package:expect/expect.dart";
typedef Void2Int = int Function();
class A<out T> {
final T a = null;
final T Function() b = () => null;
T get c => null;
A<T> get d => this;
covariant T e;
void set f(covariant T value) => value;
}
mixin BMixin<out T> {
final T a = null;
final T Function() b = () => null;
T get c => null;
BMixin<T> get d => this;
covariant T e;
void set f(covariant T value) => value;
}
class B with BMixin<int> {}
void testClass() {
A<int> a = new A();
Expect.isNull(a.a);
Expect.type<Void2Int>(a.b);
Expect.isNull(a.b());
Expect.isNull(a.c);
Expect.isNull(a.d.a);
a.e = 2;
Expect.equals(2, a.e);
a.f = 2;
}
void testMixin() {
B b = new B();
Expect.isNull(b.a);
Expect.type<Void2Int>(b.b);
Expect.isNull(b.b());
Expect.isNull(b.c);
Expect.isNull(b.d.a);
b.e = 2;
Expect.equals(2, b.e);
b.f = 2;
}
main() {
testClass();
testMixin();
}