dart-sdk/tests/compiler/dartdevc_native/variance_test.dart
Nicholas Shahan 8064c1c342 [ddc] Add typeRep<T>() to inline an unwrapped type directly
Also add `legacyTypeRep<T>()` that is similar but performs
a shallow conversion to a legacy type.

Using this in compiler/dartdevc_native/ tests gives a better usability
instead of constantly wrapping and unwrapping types. It allows us to
avoid stripping off the legacy from a type when calling `wrapType()`.

Issue: https://github.com/dart-lang/sdk/issues/40266
Change-Id: I07225f18c253222b31203b0b110233a3e018a7d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142547
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2020-04-08 00:03:38 +00:00

53 lines
1.4 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.
// @dart = 2.6
// SharedOptions=--enable-experiment=variance
// Tests the emission of explicit variance modifiers.
import 'dart:_runtime'
show wrapType, unwrapType, getGenericArgVariances, Variance, typeRep;
import 'package:expect/expect.dart';
class A<in T> {}
class B<out T> {}
class C<inout T> {}
class D<T> {}
class E<inout X, out Y, in Z> {}
mixin F<in T> {}
class G<inout T> = Object with F<T>;
List getVariances(Object t) {
// TODO(nshahan) Update to handle legacy wrapper when we unfork dart:_runtime.
var type = unwrapType(wrapType(t));
return getGenericArgVariances(type);
}
main() {
Expect.listEquals([Variance.contravariant], getVariances(typeRep<A>()));
Expect.listEquals([Variance.covariant], getVariances(typeRep<B>()));
Expect.listEquals([Variance.invariant], getVariances(typeRep<C>()));
// Implicit variance is not emitted into the generated code.
Expect.isNull(getVariances(typeRep<D>()));
Expect.listEquals(
[Variance.invariant, Variance.covariant, Variance.contravariant],
getVariances(typeRep<E>()));
Expect.listEquals([Variance.contravariant], getVariances(typeRep<F>()));
Expect.listEquals([Variance.invariant], getVariances(typeRep<G>()));
}