dart-sdk/tests/language/variance/variance_out_inference_error_test.dart
Konstantin Shcheglov 8de8a1c226 Move codes from StaticTypeWarningCode into CompileTimeErrorCode.
They are all errors now.

Change-Id: If48d38e38e845fd5b5a950dd5514bf1cbbce03d8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155880
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-07-27 16:44:15 +00:00

61 lines
2.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 local inference errors for the `out` variance modifier.
// SharedOptions=--enable-experiment=variance
class Covariant<out T> {}
class Exactly<inout T> {}
class Upper {}
class Middle extends Upper {}
class Lower extends Middle {}
class CovBound<out T> {
CovBound(T x, void Function(T) y) {}
}
Exactly<T> inferCovCov<T>(Covariant<T> x, Covariant<T> y) => new Exactly<T>();
Exactly<T> inferCovBound<T>(CovBound<T> x) => new Exactly<T>();
main() {
Exactly<Upper> upper;
Exactly<Middle> middle;
Exactly<Lower> lower;
// Lower <: T <: Middle.
// We choose Middle.
var inferredMiddle = inferCovCov(Covariant<Lower>(), Covariant<Middle>());
lower = inferredMiddle;
// ^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
// [cfe] A value of type 'Exactly<Middle>' can't be assigned to a variable of type 'Exactly<Lower>'.
// Lower <: T <: Upper.
// We choose Upper.
var inferredUpper = inferCovCov(Covariant<Lower>(), Covariant<Upper>());
lower = inferredUpper;
// ^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
// [cfe] A value of type 'Exactly<Upper>' can't be assigned to a variable of type 'Exactly<Lower>'.
// Inference for Covbound(...) produces Lower <: T <: Upper.
// Since T is covariant, we choose Lower as the solution.
var inferredCovLower = inferCovBound(CovBound(Lower(), (Upper x) {}));
upper = inferredCovLower;
// ^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
// [cfe] A value of type 'Exactly<Lower>' can't be assigned to a variable of type 'Exactly<Upper>'.
// Inference for Covbound(...) produces Lower <: T <: Middle.
// Since T is covariant, we choose Lower as the solution.
var inferredCovLower2 = inferCovBound(CovBound(Lower(), (Middle x) {}));
middle = inferredCovLower2;
// ^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
// [cfe] A value of type 'Exactly<Lower>' can't be assigned to a variable of type 'Exactly<Middle>'.
}