Avoid type mapping

This commit is contained in:
David Peter 2025-09-29 15:18:03 +02:00
parent 0fc7ddb5c9
commit 4b0d423b57
4 changed files with 18 additions and 16 deletions

View file

@ -5359,7 +5359,7 @@ impl<'db> Type<'db> {
// It is important that identity_specialization specializes the class with // It is important that identity_specialization specializes the class with
// _inferable_ typevars, so that our specialization inference logic will // _inferable_ typevars, so that our specialization inference logic will
// try to find a specialization for them. // try to find a specialization for them.
Type::from(class.identity_specialization(db, Type::TypeVar)), Type::from(class.identity_specialization(db, &Type::TypeVar)),
), ),
_ => (None, None, self), _ => (None, None, self),
}, },
@ -5717,8 +5717,15 @@ impl<'db> Type<'db> {
], ],
}); });
}; };
let self_type = typing_self(db, scope_id, typevar_binding_context, class);
Ok(self_type.map(Type::NonInferableTypeVar).unwrap_or(*self)) Ok(typing_self(
db,
scope_id,
typevar_binding_context,
class,
&Type::NonInferableTypeVar,
)
.unwrap_or(*self))
} }
SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)), SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)),
SpecialFormType::TypedDict => Err(InvalidTypeExpressionError { SpecialFormType::TypedDict => Err(InvalidTypeExpressionError {

View file

@ -1532,7 +1532,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn identity_specialization( pub(crate) fn identity_specialization(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
) -> ClassType<'db> { ) -> ClassType<'db> {
self.apply_specialization(db, |generic_context| { self.apply_specialization(db, |generic_context| {
generic_context.identity_specialization(db, typevar_to_type) generic_context.identity_specialization(db, typevar_to_type)

View file

@ -107,7 +107,8 @@ pub(crate) fn typing_self<'db>(
scope_id: ScopeId, scope_id: ScopeId,
typevar_binding_context: Option<Definition<'db>>, typevar_binding_context: Option<Definition<'db>>,
class: ClassLiteral<'db>, class: ClassLiteral<'db>,
) -> Option<BoundTypeVarInstance<'db>> { typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
) -> Option<Type<'db>> {
let module = parsed_module(db, scope_id.file(db)).load(db); let module = parsed_module(db, scope_id.file(db)).load(db);
let index = semantic_index(db, scope_id.file(db)); let index = semantic_index(db, scope_id.file(db));
@ -118,7 +119,7 @@ pub(crate) fn typing_self<'db>(
Some( Some(
TypeVarBoundOrConstraints::UpperBound(Type::instance( TypeVarBoundOrConstraints::UpperBound(Type::instance(
db, db,
class.identity_specialization(db, Type::NonInferableTypeVar), class.identity_specialization(db, typevar_to_type),
)) ))
.into(), .into(),
), ),
@ -138,6 +139,7 @@ pub(crate) fn typing_self<'db>(
typevar_binding_context, typevar_binding_context,
typevar, typevar,
) )
.map(typevar_to_type)
} }
/// A list of formal type variables for a generic function, class, or type alias. /// A list of formal type variables for a generic function, class, or type alias.
@ -333,7 +335,7 @@ impl<'db> GenericContext<'db> {
pub(crate) fn identity_specialization( pub(crate) fn identity_specialization(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>, typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
) -> Specialization<'db> { ) -> Specialization<'db> {
let types = self let types = self
.variables(db) .variables(db)

View file

@ -1255,15 +1255,8 @@ impl<'db> Parameters<'db> {
let class = nearest_enclosing_class(db, index, scope_id).unwrap(); let class = nearest_enclosing_class(db, index, scope_id).unwrap();
Some( Some(
Type::NonInferableTypeVar( typing_self(db, scope_id, typevar_binding_context, class, &Type::TypeVar)
typing_self(db, scope_id, typevar_binding_context, class).unwrap(), .unwrap(),
)
.apply_type_mapping(
db,
&TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition(
definition,
))),
),
) )
} else { } else {
// For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or // For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or