@@ -1593,9 +1593,16 @@ impl<'db> Type<'db> {
15931593 } )
15941594 }
15951595
1596+ ( Type :: TypeVar ( _) , _) if relation. is_assignability ( ) => {
1597+ // The implicit lower bound of a typevar is `Never`, which means
1598+ // that it is always assignable to any other type.
1599+
1600+ // TODO: record the unification constraints
1601+
1602+ ConstraintSet :: from ( true )
1603+ }
1604+
15961605 // `Never` is the bottom type, the empty set.
1597- // Other than one unlikely edge case (TypeVars bound to `Never`),
1598- // no other type is a subtype of or assignable to `Never`.
15991606 ( _, Type :: Never ) => ConstraintSet :: from ( false ) ,
16001607
16011608 ( Type :: Union ( union) , _) => union. elements ( db) . iter ( ) . when_all ( db, |& elem_ty| {
@@ -1632,6 +1639,22 @@ impl<'db> Type<'db> {
16321639 // be specialized to `Never`.)
16331640 ( _, Type :: NonInferableTypeVar ( _) ) => ConstraintSet :: from ( false ) ,
16341641
1642+ ( _, Type :: TypeVar ( typevar) )
1643+ if relation. is_assignability ( )
1644+ && typevar. typevar ( db) . upper_bound ( db) . is_none_or ( |bound| {
1645+ !self
1646+ . has_relation_to_impl ( db, bound, relation, visitor)
1647+ . is_never_satisfied ( )
1648+ } ) =>
1649+ {
1650+ // TODO: record the unification constraints
1651+
1652+ typevar
1653+ . typevar ( db)
1654+ . upper_bound ( db)
1655+ . when_none_or ( |bound| self . has_relation_to_impl ( db, bound, relation, visitor) )
1656+ }
1657+
16351658 // TODO: Infer specializations here
16361659 ( Type :: TypeVar ( _) , _) | ( _, Type :: TypeVar ( _) ) => ConstraintSet :: from ( false ) ,
16371660
@@ -5662,13 +5685,25 @@ impl<'db> Type<'db> {
56625685 ] ,
56635686 } ) ;
56645687 } ;
5665- let instance = Type :: instance ( db, class. unknown_specialization ( db) ) ;
5688+
5689+ let upper_bound = Type :: instance (
5690+ db,
5691+ class. apply_specialization ( db, |generic_context| {
5692+ let types = generic_context
5693+ . variables ( db)
5694+ . iter ( )
5695+ . map ( |typevar| Type :: NonInferableTypeVar ( * typevar) ) ;
5696+
5697+ generic_context. specialize ( db, types. collect ( ) )
5698+ } ) ,
5699+ ) ;
5700+
56665701 let class_definition = class. definition ( db) ;
56675702 let typevar = TypeVarInstance :: new (
56685703 db,
56695704 ast:: name:: Name :: new_static ( "Self" ) ,
56705705 Some ( class_definition) ,
5671- Some ( TypeVarBoundOrConstraints :: UpperBound ( instance ) . into ( ) ) ,
5706+ Some ( TypeVarBoundOrConstraints :: UpperBound ( upper_bound ) . into ( ) ) ,
56725707 // According to the [spec], we can consider `Self`
56735708 // equivalent to an invariant type variable
56745709 // [spec]: https://typing.python.org/en/latest/spec/generics.html#self
@@ -6010,8 +6045,8 @@ impl<'db> Type<'db> {
60106045 partial. get ( db, bound_typevar) . unwrap_or ( self )
60116046 }
60126047 TypeMapping :: MarkTypeVarsInferable ( binding_context) => {
6013- if bound_typevar . binding_context ( db ) == * binding_context {
6014- Type :: TypeVar ( bound_typevar)
6048+ if binding_context. is_none_or ( |context| context == bound_typevar . binding_context ( db ) ) {
6049+ Type :: TypeVar ( bound_typevar. mark_typevars_inferable ( db , visitor ) )
60156050 } else {
60166051 self
60176052 }
@@ -6695,8 +6730,17 @@ pub enum TypeMapping<'a, 'db> {
66956730 BindSelf ( Type < ' db > ) ,
66966731 /// Replaces occurrences of `typing.Self` with a new `Self` type variable with the given upper bound.
66976732 ReplaceSelf { new_upper_bound : Type < ' db > } ,
6698- /// Marks the typevars that are bound by a generic class or function as inferable.
6699- MarkTypeVarsInferable ( BindingContext < ' db > ) ,
6733+ /// Marks type variables as inferable.
6734+ ///
6735+ /// When we create the signature for a generic function, we mark its type variables as inferable. Since
6736+ /// the generic function might reference type variables from enclosing generic scopes, we include the
6737+ /// function's binding context in order to only mark those type variables as inferable that are actually
6738+ /// bound by that function.
6739+ ///
6740+ /// When the parameter is set to `None`, *all* type variables will be marked as inferable. We use this
6741+ /// variant when descending into the bounds and/or constraints, and the default value of a type variable,
6742+ /// which may include nested type variables (`Self` has a bound of `C[T]` for a generic class `C[T]`).
6743+ MarkTypeVarsInferable ( Option < BindingContext < ' db > > ) ,
67006744 /// Create the top or bottom materialization of a type.
67016745 Materialize ( MaterializationKind ) ,
67026746}
@@ -7637,6 +7681,43 @@ impl<'db> TypeVarInstance<'db> {
76377681 )
76387682 }
76397683
7684+ fn mark_typevars_inferable (
7685+ self ,
7686+ db : & ' db dyn Db ,
7687+ visitor : & ApplyTypeMappingVisitor < ' db > ,
7688+ ) -> Self {
7689+ // Type variables can have nested type variables in their bounds, constraints, or default value.
7690+ // When we mark a type variable as inferable, we also mark all of these nested type variables as
7691+ // inferable, so we set the parameter to `None` here.
7692+ let type_mapping = & TypeMapping :: MarkTypeVarsInferable ( None ) ;
7693+
7694+ Self :: new (
7695+ db,
7696+ self . name ( db) ,
7697+ self . definition ( db) ,
7698+ self . _bound_or_constraints ( db)
7699+ . map ( |bound_or_constraints| match bound_or_constraints {
7700+ TypeVarBoundOrConstraintsEvaluation :: Eager ( bound_or_constraints) => {
7701+ bound_or_constraints
7702+ . mark_typevars_inferable ( db, visitor)
7703+ . into ( )
7704+ }
7705+ TypeVarBoundOrConstraintsEvaluation :: LazyUpperBound
7706+ | TypeVarBoundOrConstraintsEvaluation :: LazyConstraints => bound_or_constraints,
7707+ } ) ,
7708+ self . explicit_variance ( db) ,
7709+ self . _default ( db) . and_then ( |default| match default {
7710+ TypeVarDefaultEvaluation :: Eager ( ty) => {
7711+ Some ( ty. apply_type_mapping_impl ( db, type_mapping, visitor) . into ( ) )
7712+ }
7713+ TypeVarDefaultEvaluation :: Lazy => self
7714+ . lazy_default ( db)
7715+ . map ( |ty| ty. apply_type_mapping_impl ( db, type_mapping, visitor) . into ( ) ) ,
7716+ } ) ,
7717+ self . kind ( db) ,
7718+ )
7719+ }
7720+
76407721 fn to_instance ( self , db : & ' db dyn Db ) -> Option < Self > {
76417722 let bound_or_constraints = match self . bound_or_constraints ( db) ? {
76427723 TypeVarBoundOrConstraints :: UpperBound ( upper_bound) => {
@@ -7867,6 +7948,18 @@ impl<'db> BoundTypeVarInstance<'db> {
78677948 )
78687949 }
78697950
7951+ fn mark_typevars_inferable (
7952+ self ,
7953+ db : & ' db dyn Db ,
7954+ visitor : & ApplyTypeMappingVisitor < ' db > ,
7955+ ) -> Self {
7956+ Self :: new (
7957+ db,
7958+ self . typevar ( db) . mark_typevars_inferable ( db, visitor) ,
7959+ self . binding_context ( db) ,
7960+ )
7961+ }
7962+
78707963 fn to_instance ( self , db : & ' db dyn Db ) -> Option < Self > {
78717964 Some ( Self :: new (
78727965 db,
@@ -7972,6 +8065,31 @@ impl<'db> TypeVarBoundOrConstraints<'db> {
79728065 }
79738066 }
79748067 }
8068+
8069+ fn mark_typevars_inferable (
8070+ self ,
8071+ db : & ' db dyn Db ,
8072+ visitor : & ApplyTypeMappingVisitor < ' db > ,
8073+ ) -> Self {
8074+ let type_mapping = & TypeMapping :: MarkTypeVarsInferable ( None ) ;
8075+
8076+ match self {
8077+ TypeVarBoundOrConstraints :: UpperBound ( bound) => TypeVarBoundOrConstraints :: UpperBound (
8078+ bound. apply_type_mapping_impl ( db, type_mapping, visitor) ,
8079+ ) ,
8080+ TypeVarBoundOrConstraints :: Constraints ( constraints) => {
8081+ TypeVarBoundOrConstraints :: Constraints ( UnionType :: new (
8082+ db,
8083+ constraints
8084+ . elements ( db)
8085+ . iter ( )
8086+ . map ( |ty| ty. apply_type_mapping_impl ( db, type_mapping, visitor) )
8087+ . collect :: < Vec < _ > > ( )
8088+ . into_boxed_slice ( ) ,
8089+ ) )
8090+ }
8091+ }
8092+ }
79758093}
79768094
79778095/// Error returned if a type is not awaitable.
0 commit comments