[ty] Remove `Self` from generic context when binding `Self`

This commit is contained in:
David Peter 2025-09-10 17:08:45 +02:00
parent 65982a1e14
commit b1e64a0da4
2 changed files with 14 additions and 3 deletions

View File

@ -17,8 +17,8 @@ use crate::types::tuple::{TupleSpec, TupleType, walk_tuple_type};
use crate::types::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor,
Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarVariance,
UnionType, binding_type, declaration_type,
Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind,
TypeVarVariance, UnionType, binding_type, declaration_type,
};
use crate::{Db, FxOrderSet};
@ -401,6 +401,17 @@ impl<'db> GenericContext<'db> {
fn heap_size((variables,): &(FxOrderSet<BoundTypeVarInstance<'db>>,)) -> usize {
ruff_memory_usage::order_set_heap_size(variables)
}
/// Returns a version of this generic context with the `Self` typevar removed.
pub(crate) fn bind_self(self, db: &'db dyn Db) -> Self {
Self::from_typevar_instances(
db,
self.variables(db)
.iter()
.filter(|typevar| typevar.typevar(db).kind(db) != TypeVarKind::TypingSelf)
.copied(),
)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]

View File

@ -522,7 +522,7 @@ impl<'db> Signature<'db> {
return_ty.map(|ty| ty.apply_type_mapping(db, &TypeMapping::BindSelf(self_type)));
}
Self {
generic_context: self.generic_context,
generic_context: self.generic_context.map(|context| context.bind_self(db)),
inherited_generic_context: self.inherited_generic_context,
definition: self.definition,
parameters,