// 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 `in` variance modifier. // SharedOptions=--enable-experiment=variance class Covariant {} class Contravariant {} class Exactly {} class Upper {} class Middle extends Upper {} class Lower extends Middle {} class ContraBound { ContraBound(T x, void Function(T) y) {} } Exactly inferCovContra(Covariant x, Contravariant y) => new Exactly(); Exactly inferContraContra(Contravariant x, Contravariant y) => new Exactly(); Exactly inferContraBound(ContraBound x) => new Exactly(); main() { Exactly upper; Exactly lower; // T <: Upper and T <: Middle. // We choose Middle. var inferredMiddle = inferContraContra(Contravariant(), Contravariant()); upper = inferredMiddle; // ^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT // [cfe] A value of type 'Exactly' can't be assigned to a variable of type 'Exactly'. // T <: Upper and T <: Lower. // We choose Lower. var inferredLower = inferContraContra(Contravariant(), Contravariant()); upper = inferredLower; // ^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT // [cfe] A value of type 'Exactly' can't be assigned to a variable of type 'Exactly'. // int <: T <: String is not a valid constraint. inferCovContra(Covariant(), Contravariant()); //^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.COULD_NOT_INFER // [cfe] unspecified // String <: T <: int is not a valid constraint. inferCovContra(Covariant(), Contravariant()); //^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.COULD_NOT_INFER // [cfe] unspecified // Middle <: T <: Lower is not a valid constraint inferCovContra(Covariant(), Contravariant()); //^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.COULD_NOT_INFER // [cfe] unspecified // Upper <: T <: Lower is not a valid constraint inferCovContra(Covariant(), Contravariant()); //^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.COULD_NOT_INFER // [cfe] unspecified // Upper <: T <: Middle is not a valid constraint inferCovContra(Covariant(), Contravariant()); //^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.COULD_NOT_INFER // [cfe] unspecified // Inference for Contrabound(...) produces Lower <: T <: Upper. // Since T is contravariant, we choose Upper as the solution. var inferredContraUpper = inferContraBound(ContraBound(Lower(), (Upper x) {})); lower = inferredContraUpper; // ^^^^^^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT // [cfe] A value of type 'Exactly' can't be assigned to a variable of type 'Exactly'. // Inference for Contrabound(...) produces Lower <: T <: Middle. // Since T is contravariant, we choose Middle as the solution. var inferredContraMiddle = inferContraBound(ContraBound(Lower(), (Middle x) {})); lower = inferredContraMiddle; // ^^^^^^^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT // [cfe] A value of type 'Exactly' can't be assigned to a variable of type 'Exactly'. }