dart-sdk/tests/language_2/covariance_type_parameter_test.dart
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

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