Add fast path for object vs unbounded inferable TypeVar

This handles the common case where we're checking if `object` (the
implicit positive element of a pure negation like `~str`) is assignable
to a generic type parameter like `_T` in `Iterator[_T]`.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Charlie Marsh
2026-01-13 20:57:49 -05:00
parent 0eb28df0b0
commit 70859e4ba7

View File

@@ -672,6 +672,21 @@ impl<'db> Type<'db> {
ConstraintSet::from(false)
}
// Fast path: `object` is assignable to any inferable type variable with no upper bound
// (or with `object` as its upper bound), which is the common case for generic
// type parameters like `_T` in `Iterator[_T]`.
(Type::NominalInstance(source), Type::TypeVar(typevar))
if source.is_object()
&& typevar.is_inferable(db, inferable)
&& relation.is_assignability()
&& typevar
.typevar(db)
.upper_bound(db)
.is_none_or(|bound| bound.is_object()) =>
{
ConstraintSet::from(true)
}
// Fast path: `object` is not a subtype of any callable type, since not all objects
// are callable.
(Type::NominalInstance(source), Type::Callable(_)) if source.is_object() => {