[ty] Avoid narrowing on non-generic calls (#22102)

## Summary

Resolves https://github.com/astral-sh/ty/issues/2026.
This commit is contained in:
Ibraheem Ahmed 2025-12-19 23:18:07 -05:00 committed by GitHub
parent 674d3902c6
commit 2a959ef3f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 1 deletions

View File

@ -7007,12 +7007,20 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
) -> Result<(), CallErrorKind> {
let db = self.db();
let has_generic_context = bindings
.iter()
.flat_map(CallableBinding::overloads)
.any(|overload| overload.signature.generic_context.is_some());
// If the type context is a union, attempt to narrow to a specific element.
let narrow_targets: &[_] = match call_expression_tcx.annotation {
// TODO: We could theoretically attempt to narrow to every element of
// the power set of this union. However, this leads to an exponential
// explosion of inference attempts, and is rarely needed in practice.
Some(Type::Union(union)) => union.elements(db),
//
// We only need to attempt narrowing on generic calls, otherwise the type
// context has no effect.
Some(Type::Union(union)) if has_generic_context => union.elements(db),
_ => &[],
};