Skip to content

Commit 9b9bf8e

Browse files
committed
Reverts
1 parent d411d9a commit 9b9bf8e

3 files changed

Lines changed: 51 additions & 142 deletions

File tree

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,9 +2569,9 @@ impl<'db> CallableBinding<'db> {
25692569
) -> Self {
25702570
let overloads = overloads
25712571
.into_iter()
2572-
.map(|(original_overload_index, signature)| {
2572+
.map(|(source_overload_index, signature)| {
25732573
Binding::single(signature_type, signature)
2574-
.with_original_overload_index(original_overload_index)
2574+
.with_source_overload_index(source_overload_index)
25752575
})
25762576
.collect();
25772577
Self {
@@ -2615,7 +2615,7 @@ impl<'db> CallableBinding<'db> {
26152615
fn diagnostic_overload_indexes(&self) -> SmallVec<[usize; 1]> {
26162616
self.overloads
26172617
.iter()
2618-
.map(Binding::original_overload_index)
2618+
.map(Binding::source_overload_index)
26192619
.collect()
26202620
}
26212621

@@ -3463,8 +3463,7 @@ impl<'db> CallableBinding<'db> {
34633463
CallableDescription::new(context.db(), self.signature_type);
34643464
let matching_overload =
34653465
function_type_and_kind.map(|(kind, function)| MatchingOverloadLiteral {
3466-
index: self.overloads[matching_overload_index]
3467-
.original_overload_index(),
3466+
index: self.overloads[matching_overload_index].source_overload_index(),
34683467
kind,
34693468
function,
34703469
candidate_indexes: self.diagnostic_overload_indexes(),
@@ -3490,8 +3489,7 @@ impl<'db> CallableBinding<'db> {
34903489
CallableDescription::new(context.db(), self.signature_type);
34913490
let matching_overload =
34923491
function_type_and_kind.map(|(kind, function)| MatchingOverloadLiteral {
3493-
index: self.overloads[matching_overload_index]
3494-
.original_overload_index(),
3492+
index: self.overloads[matching_overload_index].source_overload_index(),
34953493
kind,
34963494
function,
34973495
candidate_indexes: self.diagnostic_overload_indexes(),
@@ -5043,8 +5041,8 @@ pub(crate) struct Binding<'db> {
50435041
///
50445042
/// For example, if a function defines overloads at indexes `0`, `1`, and `2`, and we later
50455043
/// filter a bound method down to only the last two overloads, those bindings still store `1`
5046-
/// and `2` here so diagnostics can point at the original declarations.
5047-
original_overload_index: usize,
5044+
/// and `2` here so diagnostics can point at the source declarations.
5045+
source_overload_index: usize,
50485046

50495047
/// The type that is (hopefully) callable.
50505048
pub(crate) callable_type: Type<'db>,
@@ -5087,7 +5085,7 @@ impl<'db> Binding<'db> {
50875085
let return_ty = signature.return_ty;
50885086
Binding {
50895087
signature,
5090-
original_overload_index: 0,
5088+
source_overload_index: 0,
50915089
callable_type: signature_type,
50925090
signature_type,
50935091
return_ty,
@@ -5101,15 +5099,15 @@ impl<'db> Binding<'db> {
51015099
}
51025100
}
51035101

5104-
/// Records the overload's original definition index for later diagnostics.
5105-
fn with_original_overload_index(mut self, original_overload_index: usize) -> Self {
5106-
self.original_overload_index = original_overload_index;
5102+
/// Records the overload's source definition index for later diagnostics.
5103+
fn with_source_overload_index(mut self, source_overload_index: usize) -> Self {
5104+
self.source_overload_index = source_overload_index;
51075105
self
51085106
}
51095107

5110-
/// Returns the overload's original position in the defining function's overload list.
5111-
fn original_overload_index(&self) -> usize {
5112-
self.original_overload_index
5108+
/// Returns the overload's source position in the defining function's overload list.
5109+
fn source_overload_index(&self) -> usize {
5110+
self.source_overload_index
51135111
}
51145112

51155113
fn replace_callable_type(&mut self, before: Type<'db>, after: Type<'db>) {
@@ -6418,7 +6416,7 @@ impl CompoundDiagnostic for LayeredDiagnostic<'_, '_> {
64186416
struct MatchingOverloadLiteral<'db> {
64196417
/// The position of the matching overload in the list of overloads.
64206418
index: usize,
6421-
/// The original overload positions that remained after any callable-specific filtering.
6419+
/// The source overload positions that remained after any callable-specific filtering.
64226420
candidate_indexes: SmallVec<[usize; 1]>,
64236421
/// The kind of function this overload is for.
64246422
kind: FunctionKind,
@@ -6443,7 +6441,7 @@ impl<'db> MatchingOverloadLiteral<'db> {
64436441
}
64446442

64456443
/// Iterates the overload literals that remain diagnostic candidates, paired with their
6446-
/// original overload-definition index.
6444+
/// source overload-definition index.
64476445
fn candidate_overloads(
64486446
&self,
64496447
db: &'db dyn Db,

crates/ty_python_semantic/src/types/method.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,34 @@ impl<'db> BoundMethodType<'db> {
8181
let function_signature = self.function(db).signature(db);
8282
let typing_self_type = self.typing_self_type(db);
8383

84-
if let Some(applicable_overload_indexes) = self.applicable_overload_indexes(db)
85-
&& !applicable_overload_indexes.is_empty()
86-
{
87-
return CallableSignature::from_overloads(applicable_overload_indexes.into_iter().map(
88-
|index| function_signature.overloads[index].bind_self(db, Some(typing_self_type)),
89-
));
90-
}
91-
92-
CallableSignature::from_overloads(
93-
function_signature
84+
let mut applicable_overloads: SmallVec<[Signature<'db>; 1]> = self
85+
.applicable_overloads(db)
86+
.into_iter()
87+
.map(|signature| signature.bind_self(db, Some(typing_self_type)))
88+
.collect();
89+
90+
// If no overload accepts the bound receiver, keep the full overload set so a later call
91+
// still reports the existing invalid-`self` diagnostic instead of becoming non-callable.
92+
if applicable_overloads.is_empty() {
93+
applicable_overloads = function_signature
9494
.overloads
9595
.iter()
96-
.map(|signature| signature.bind_self(db, Some(typing_self_type))),
97-
)
98-
}
99-
100-
fn applicable_overload_indexes(self, db: &'db dyn Db) -> Option<SmallVec<[usize; 1]>> {
101-
let function_signature = self.function(db).signature(db);
102-
if !function_signature.has_receiver_sensitive_overloads() {
103-
return None;
96+
.map(|signature| signature.bind_self(db, Some(typing_self_type)))
97+
.collect();
10498
}
10599

100+
CallableSignature::from_overloads(applicable_overloads)
101+
}
102+
103+
fn applicable_overloads(self, db: &'db dyn Db) -> SmallVec<[Signature<'db>; 1]> {
106104
let self_instance = self.self_instance(db);
107-
Some(
108-
function_signature
109-
.overloads
110-
.iter()
111-
.enumerate()
112-
.filter(|(_, signature)| signature.can_bind_self_to(db, self_instance))
113-
.map(|(index, _)| index)
114-
.collect(),
115-
)
105+
self.function(db)
106+
.signature(db)
107+
.overloads
108+
.iter()
109+
.filter(|signature| signature.can_bind_self_to(db, self_instance))
110+
.cloned()
111+
.collect()
116112
}
117113

118114
pub(super) fn recursive_type_normalized_impl(

crates/ty_python_semantic/src/types/signatures.rs

Lines changed: 12 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::types::relation::{
2929
use crate::types::{
3030
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, CallableType, ErrorContext,
3131
FindLegacyTypeVarsVisitor, KnownClass, MaterializationKind, ParamSpecAttrKind,
32-
ParameterDescription, SelfBinding, SubclassOfInner, TypeContext, TypeMapping, UnionBuilder,
33-
VarianceInferable, infer_complete_scope_types, todo_type,
32+
ParameterDescription, SelfBinding, TypeContext, TypeMapping, UnionBuilder, VarianceInferable,
33+
infer_complete_scope_types, todo_type,
3434
};
3535
use crate::{Db, FxOrderSet};
3636
use ruff_python_ast::{self as ast, name::Name};
@@ -301,20 +301,6 @@ impl<'db> CallableSignature<'db> {
301301
}
302302
}
303303

304-
/// Returns `true` if binding a concrete receiver could change which overloads remain
305-
/// applicable.
306-
///
307-
/// When every overload has the same receiver annotation (or they are all unannotated),
308-
/// receiver-based pruning cannot affect the overload set, so we can skip the more expensive
309-
/// relation checks during bound-method conversion.
310-
pub(crate) fn has_receiver_sensitive_overloads(&self) -> bool {
311-
let mut receiver_annotations = self.overloads.iter().map(Signature::receiver_annotation);
312-
let Some(first_annotation) = receiver_annotations.next() else {
313-
return false;
314-
};
315-
receiver_annotations.any(|annotation| annotation != first_annotation)
316-
}
317-
318304
/// Replaces any occurrences of `typing.Self` in the parameter and return annotations with the
319305
/// given type. (Does not bind the `self` parameter; to do that, use
320306
/// [`bind_self`][Self::bind_self].)
@@ -745,13 +731,6 @@ impl<'db> Signature<'db> {
745731
}
746732
}
747733

748-
fn receiver_annotation(&self) -> Option<Type<'db>> {
749-
let first_parameter = self.parameters.get(0)?;
750-
first_parameter
751-
.is_positional()
752-
.then(|| first_parameter.annotated_type())
753-
}
754-
755734
/// Returns `true` if this signature's first parameter can accept the bound `self` type.
756735
///
757736
/// This is used to prune impossible overloads when a method is bound to a concrete receiver.
@@ -765,35 +744,23 @@ impl<'db> Signature<'db> {
765744
return true;
766745
}
767746

768-
let annotated_self_ty = first_parameter.annotated_type();
769-
let expected_self_ty = if annotated_self_ty.contains_self(db)
770-
|| annotated_self_ty.has_typevar_or_typevar_instance(db)
771-
{
772-
annotated_self_ty
773-
.bind_self_typevars(db, self_type)
774-
.apply_optional_specialization(db, self_type.class_specialization(db))
775-
} else {
776-
annotated_self_ty
777-
};
747+
let expected_self_ty = first_parameter
748+
.annotated_type()
749+
.bind_self_typevars(db, self_type)
750+
.apply_optional_specialization(db, self_type.class_specialization(db));
778751

779752
if expected_self_ty.is_unknown() {
780753
return true;
781754
}
782755

783-
if let Some(can_bind) =
784-
can_bind_self_to_concrete_receiver_fast_path(db, self_type, expected_self_ty)
785-
{
786-
return can_bind;
787-
}
788-
789756
let constraints = ConstraintSetBuilder::new();
790-
let inferable_typevars = if expected_self_ty.has_typevar_or_typevar_instance(db) {
791-
self.inferable_typevars(db)
792-
} else {
793-
InferableTypeVars::None
794-
};
795757
self_type
796-
.when_assignable_to(db, expected_self_ty, &constraints, inferable_typevars)
758+
.when_assignable_to(
759+
db,
760+
expected_self_ty,
761+
&constraints,
762+
self.inferable_typevars(db),
763+
)
797764
.is_always_satisfied(db)
798765
}
799766

@@ -921,58 +888,6 @@ impl<'db> Signature<'db> {
921888
}
922889
}
923890

924-
fn can_bind_self_to_concrete_receiver_fast_path<'db>(
925-
db: &'db dyn Db,
926-
self_type: Type<'db>,
927-
expected_self_ty: Type<'db>,
928-
) -> Option<bool> {
929-
if self_type.has_dynamic(db)
930-
|| self_type.has_typevar_or_typevar_instance(db)
931-
|| expected_self_ty.has_dynamic(db)
932-
|| expected_self_ty.has_typevar_or_typevar_instance(db)
933-
{
934-
return None;
935-
}
936-
937-
if let (Some(actual_class), Some(expected_class)) = (
938-
self_type.nominal_class(db),
939-
expected_self_ty.nominal_class(db),
940-
) {
941-
return Some(actual_class.is_subclass_of(db, expected_class));
942-
}
943-
944-
let actual_subclass = match self_type {
945-
Type::ClassLiteral(_) | Type::GenericAlias(_) => {
946-
match super::SubclassOfType::try_from_type(db, self_type) {
947-
Some(Type::SubclassOf(subclass_of)) => Some(subclass_of),
948-
_ => None,
949-
}
950-
}
951-
Type::SubclassOf(subclass_of) => Some(subclass_of),
952-
_ => None,
953-
}?;
954-
let expected_subclass = match expected_self_ty {
955-
Type::ClassLiteral(_) | Type::GenericAlias(_) => {
956-
match super::SubclassOfType::try_from_type(db, expected_self_ty) {
957-
Some(Type::SubclassOf(subclass_of)) => Some(subclass_of),
958-
_ => None,
959-
}
960-
}
961-
Type::SubclassOf(subclass_of) => Some(subclass_of),
962-
_ => None,
963-
}?;
964-
965-
match (
966-
actual_subclass.subclass_of(),
967-
expected_subclass.subclass_of(),
968-
) {
969-
(SubclassOfInner::Class(actual_class), SubclassOfInner::Class(expected_class)) => {
970-
Some(actual_class.is_subclass_of(db, expected_class))
971-
}
972-
_ => None,
973-
}
974-
}
975-
976891
impl<'db> VarianceInferable<'db> for &Signature<'db> {
977892
fn variance_of(self, db: &'db dyn Db, typevar: BoundTypeVarInstance<'db>) -> TypeVarVariance {
978893
tracing::trace!(

0 commit comments

Comments
 (0)