[ty] Prefer exact matches when solving constrained type variables (#21165)
## Summary The solver is currently order-dependent, and will choose a supertype over the exact type if it appears earlier in the list of constraints. We could be smarter and try to choose the most precise subtype, but I imagine this is something the new constraint solver will fix anyways, and this fixes the issue showing up on https://github.com/astral-sh/ruff/pull/21070.
This commit is contained in:
@@ -1483,6 +1483,14 @@ impl<'db> SpecializationBuilder<'db> {
|
||||
self.add_type_mapping(bound_typevar, ty);
|
||||
}
|
||||
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
|
||||
// Prefer an exact match first.
|
||||
for constraint in constraints.elements(self.db) {
|
||||
if ty == *constraint {
|
||||
self.add_type_mapping(bound_typevar, ty);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
for constraint in constraints.elements(self.db) {
|
||||
if ty
|
||||
.when_assignable_to(self.db, *constraint, self.inferable)
|
||||
|
||||
Reference in New Issue
Block a user