Files
sdk/tests/language_2/covariance_method_test.dart
T
Paul Berry 5c2fe36b2a Add language_2 tests to verify behavior of covariance flags.
These tests verify that the implementation respects the following flags:
- Field.isCovariant
- Field.isGenericCovariantImpl
- Field.isGenericCovariantInterface
- VariableDeclaration.isCovariant
- VariableDeclaration.isGenericCovariantImpl
- VariableDeclaration.isGenericCovariantInterface
- TypeParameter.isGenericCovariantImpl
- TypeParameter.isGenericCovariantInterface

Forwarding stubs are not yet tested.

Change-Id: I4bcb83e47cdcc3f1250bdc8cfc2994c51ad8334e
Reviewed-on: https://dart-review.googlesource.com/21280
Reviewed-by: Samir Jindel <sjindel@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2017-11-15 22:44:43 +00:00

64 lines
2.0 KiB
Dart

// Copyright (c) 2017, 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";
class A {}
abstract class B<T> {
// x will be marked genericCovariantInterface, since x's type is covariant in
// the type parameter T.
void f2(T x);
// x will be marked genericCovariantInterface, since x's type is covariant in
// the type parameter T.
void f3(T x);
void f4(Object x);
void f5(Object x) {
f4(x);
}
}
class C extends B<A> {
void f1(A x) {}
// x will be marked genericCovariantImpl, since it might be called via
// e.g. B<Object>.
void f2(A x) {}
// x will be marked genericCovariantImpl, since it might be called via
// e.g. B<Object>.
void f3(covariant A x) {}
void f4(covariant A x) {}
}
main() {
// Dynamic method calls should always have their arguments type checked.
dynamic d = new C();
Expect.throwsTypeError(() => d.f1(new Object())); //# 01: ok
// Closure calls should have any arguments marked "genericCovariantImpl" type
// checked.
B<Object> b = new C();
void Function(Object) f = b.f2;
Expect.throwsTypeError(() => f(new Object())); //# 02: ok
// Interface calls should have any arguments marked "genericCovariantImpl"
// type checked provided that the corresponding argument on the interface
// target is marked "genericCovariantInterface".
Expect.throwsTypeError(() => b.f2(new Object())); //# 03: ok
// Interface calls should have any arguments marked "covariant" type checked,
// regardless of whether the corresponding argument on the interface target is
// marked "genericCovariantInterface".
Expect.throwsTypeError(() => b.f3(new Object())); //# 04: ok
Expect.throwsTypeError(() => b.f4(new Object())); //# 05: ok
// This calls should have any arguments marked "covariant" type checked.
Expect.throwsTypeError(() => b.f5(new Object())); //# 06: ok
}