[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::{ use crate::types::{
ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor, ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor,
Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarVariance, Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind,
UnionType, binding_type, declaration_type, TypeVarVariance, UnionType, binding_type, declaration_type,
}; };
use crate::{Db, FxOrderSet}; use crate::{Db, FxOrderSet};
@ -401,6 +401,17 @@ impl<'db> GenericContext<'db> {
fn heap_size((variables,): &(FxOrderSet<BoundTypeVarInstance<'db>>,)) -> usize { fn heap_size((variables,): &(FxOrderSet<BoundTypeVarInstance<'db>>,)) -> usize {
ruff_memory_usage::order_set_heap_size(variables) 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)] #[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))); return_ty.map(|ty| ty.apply_type_mapping(db, &TypeMapping::BindSelf(self_type)));
} }
Self { Self {
generic_context: self.generic_context, generic_context: self.generic_context.map(|context| context.bind_self(db)),
inherited_generic_context: self.inherited_generic_context, inherited_generic_context: self.inherited_generic_context,
definition: self.definition, definition: self.definition,
parameters, parameters,