This commit is contained in:
Douglas Creager 2025-12-16 09:13:46 -05:00
parent 18ac8e6a14
commit 5b01dbae2c
2 changed files with 15 additions and 34 deletions

View File

@ -693,11 +693,6 @@ impl<'db> IntersectionBuilder<'db> {
self.add_positive_impl(ty, &mut vec![]) self.add_positive_impl(ty, &mut vec![])
} }
pub(crate) fn add_positive_in_place(&mut self, ty: Type<'db>) {
let this = std::mem::replace(self, Self::empty(self.db));
*self = this.add_positive(ty);
}
pub(crate) fn add_positive_impl( pub(crate) fn add_positive_impl(
mut self, mut self,
ty: Type<'db>, ty: Type<'db>,

View File

@ -19,11 +19,11 @@ use crate::types::variance::VarianceInferable;
use crate::types::visitor::{TypeCollector, TypeVisitor, walk_type_with_recursion_guard}; use crate::types::visitor::{TypeCollector, TypeVisitor, walk_type_with_recursion_guard};
use crate::types::{ use crate::types::{
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarIdentity, BoundTypeVarInstance, ApplyTypeMappingVisitor, BindingContext, BoundTypeVarIdentity, BoundTypeVarInstance,
ClassLiteral, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IntersectionBuilder, ClassLiteral, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IntersectionType,
IsDisjointVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, IsDisjointVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind,
NormalizedVisitor, Type, TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, NormalizedVisitor, Type, TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints,
TypeVarIdentity, TypeVarInstance, TypeVarKind, TypeVarVariance, UnionBuilder, UnionType, TypeVarIdentity, TypeVarInstance, TypeVarKind, TypeVarVariance, UnionType, declaration_type,
declaration_type, walk_type_var_bounds, walk_type_var_bounds,
}; };
use crate::{Db, FxOrderMap, FxOrderSet}; use crate::{Db, FxOrderMap, FxOrderSet};
@ -1584,18 +1584,10 @@ impl<'db> SpecializationBuilder<'db> {
constraints: ConstraintSet<'db>, constraints: ConstraintSet<'db>,
mut f: impl FnMut(TypeVarAssignment<'db>) -> Option<Type<'db>>, mut f: impl FnMut(TypeVarAssignment<'db>) -> Option<Type<'db>>,
) { ) {
#[derive(Default)]
struct Bounds<'db> { struct Bounds<'db> {
lower: UnionBuilder<'db>, lower: Vec<Type<'db>>,
upper: IntersectionBuilder<'db>, upper: Vec<Type<'db>>,
}
impl<'db> Bounds<'db> {
fn new(db: &'db dyn Db) -> Self {
Self {
lower: UnionBuilder::new(db),
upper: IntersectionBuilder::new(db),
}
}
} }
let constraints = constraints.limit_to_valid_specializations(self.db); let constraints = constraints.limit_to_valid_specializations(self.db);
@ -1618,35 +1610,29 @@ impl<'db> SpecializationBuilder<'db> {
let typevar = constraint.typevar(self.db); let typevar = constraint.typevar(self.db);
let lower = constraint.lower(self.db); let lower = constraint.lower(self.db);
let upper = constraint.upper(self.db); let upper = constraint.upper(self.db);
let bounds = mappings let bounds = mappings.entry(typevar).or_default();
.entry(typevar) bounds.lower.push(lower);
.or_insert_with(|| Bounds::new(self.db)); bounds.upper.push(upper);
bounds.lower.add_in_place(lower);
bounds.upper.add_positive_in_place(upper);
if let Type::TypeVar(lower_bound_typevar) = lower { if let Type::TypeVar(lower_bound_typevar) = lower {
let bounds = mappings let bounds = mappings.entry(lower_bound_typevar).or_default();
.entry(lower_bound_typevar) bounds.upper.push(Type::TypeVar(typevar));
.or_insert_with(|| Bounds::new(self.db));
bounds.upper.add_positive_in_place(Type::TypeVar(typevar));
} }
if let Type::TypeVar(upper_bound_typevar) = upper { if let Type::TypeVar(upper_bound_typevar) = upper {
let bounds = mappings let bounds = mappings.entry(upper_bound_typevar).or_default();
.entry(upper_bound_typevar) bounds.lower.push(Type::TypeVar(typevar));
.or_insert_with(|| Bounds::new(self.db));
bounds.lower.add_in_place(Type::TypeVar(typevar));
} }
} }
for (bound_typevar, bounds) in mappings.drain() { for (bound_typevar, bounds) in mappings.drain() {
let variance = formal.variance_of(self.db, bound_typevar); let variance = formal.variance_of(self.db, bound_typevar);
let upper = bounds.upper.build(); let upper = IntersectionType::from_elements(self.db, bounds.upper);
if !upper.is_object() { if !upper.is_object() {
self.add_type_mapping(bound_typevar, upper, variance, &mut f); self.add_type_mapping(bound_typevar, upper, variance, &mut f);
continue; continue;
} }
let lower = bounds.lower.build(); let lower = UnionType::from_elements(self.db, bounds.lower);
if !lower.is_never() { if !lower.is_never() {
self.add_type_mapping(bound_typevar, lower, variance, &mut f); self.add_type_mapping(bound_typevar, lower, variance, &mut f);
} }