limit to valid specializations

This commit is contained in:
Douglas Creager 2025-12-11 17:02:22 -05:00
parent 2950af4fd9
commit 2fd7a7d944
2 changed files with 13 additions and 0 deletions

View File

@ -360,6 +360,18 @@ impl<'db> ConstraintSet<'db> {
self.node.satisfied_by_all_typevars(db, inferable)
}
pub(crate) fn limit_to_valid_specializations(self, db: &'db dyn Db) -> Self {
let mut result = self.node;
let mut seen = FxHashSet::default();
self.node.for_each_constraint(db, &mut |constraint| {
let bound_typevar = constraint.typevar(db);
if seen.insert(bound_typevar) {
result = result.and(db, bound_typevar.valid_specializations(db));
}
});
Self { node: result }
}
/// Updates this constraint set to hold the union of itself and another constraint set.
pub(crate) fn union(&mut self, db: &'db dyn Db, other: Self) -> Self {
self.node = self.node.or(db, other.node);

View File

@ -1569,6 +1569,7 @@ impl<'db> SpecializationBuilder<'db> {
constraints: ConstraintSet<'db>,
mut f: impl FnMut(TypeVarAssignment<'db>) -> Option<Type<'db>>,
) {
let constraints = constraints.limit_to_valid_specializations(self.db);
constraints.for_each_path(self.db, |path| {
for constraint in path.positive_constraints() {
let typevar = constraint.typevar(self.db);