[ty] Do not promote literals in contravariant positions of generic specializations (#21171)
## Summary closes https://github.com/astral-sh/ty/issues/1284 supersedes https://github.com/astral-sh/ruff/pull/20950 by @ibraheemdev ## Test Plan New regression test
This commit is contained in:
@@ -969,10 +969,15 @@ impl<'db> Specialization<'db> {
|
||||
let types: Box<[_]> = self
|
||||
.types(db)
|
||||
.iter()
|
||||
.zip(self.generic_context(db).variables(db))
|
||||
.enumerate()
|
||||
.map(|(i, ty)| {
|
||||
.map(|(i, (ty, typevar))| {
|
||||
let tcx = TypeContext::new(tcx.get(i).copied());
|
||||
ty.apply_type_mapping_impl(db, type_mapping, tcx, visitor)
|
||||
if typevar.variance(db).is_covariant() {
|
||||
ty.apply_type_mapping_impl(db, type_mapping, tcx, visitor)
|
||||
} else {
|
||||
ty.apply_type_mapping_impl(db, &type_mapping.flip(), tcx, visitor)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
@@ -85,6 +85,13 @@ impl TypeVarVariance {
|
||||
TypeVarVariance::Bivariant => TypeVarVariance::Bivariant,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn is_covariant(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
TypeVarVariance::Covariant | TypeVarVariance::Bivariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::FromIterator<Self> for TypeVarVariance {
|
||||
|
||||
Reference in New Issue
Block a user