[ty] Fix assignability checks for invariant generics parameterized by gradual types (#18138)

This commit is contained in:
Alex Waygood 2025-05-16 13:37:07 -04:00 committed by GitHub
parent 28fb802467
commit 0adbb3d600
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 2 deletions

View file

@ -457,10 +457,13 @@ impl<'db> Specialization<'db> {
// corresponding typevar:
// - covariant: verify that self_type <: other_type
// - contravariant: verify that other_type <: self_type
// - invariant: verify that self_type == other_type
// - invariant: verify that self_type <: other_type AND other_type <: self_type
// - bivariant: skip, can't make assignability false
let compatible = match typevar.variance(db) {
TypeVarVariance::Invariant => self_type.is_gradual_equivalent_to(db, *other_type),
TypeVarVariance::Invariant => {
self_type.is_assignable_to(db, *other_type)
&& other_type.is_assignable_to(db, *self_type)
}
TypeVarVariance::Covariant => self_type.is_assignable_to(db, *other_type),
TypeVarVariance::Contravariant => other_type.is_assignable_to(db, *self_type),
TypeVarVariance::Bivariant => true,