@@ -53,6 +53,9 @@ pub(crate) struct Bindings<'db> {
5353 /// The type that is (hopefully) callable.
5454 callable_type : Type < ' db > ,
5555
56+ /// The type of the instance being constructed, if this signature is for a constructor.
57+ constructor_instance_type : Option < Type < ' db > > ,
58+
5659 /// By using `SmallVec`, we avoid an extra heap allocation for the common case of a non-union
5760 /// type.
5861 elements : SmallVec < [ CallableBinding < ' db > ; 1 ] > ,
@@ -77,6 +80,7 @@ impl<'db> Bindings<'db> {
7780 callable_type,
7881 elements,
7982 argument_forms : ArgumentForms :: new ( 0 ) ,
83+ constructor_instance_type : None ,
8084 }
8185 }
8286
@@ -89,6 +93,22 @@ impl<'db> Bindings<'db> {
8993 }
9094 }
9195
96+ pub ( crate ) fn with_constructor_instance_type (
97+ mut self ,
98+ constructor_instance_type : Type < ' db > ,
99+ ) -> Self {
100+ self . constructor_instance_type = Some ( constructor_instance_type) ;
101+
102+ for binding in & mut self . elements {
103+ binding. constructor_instance_type = Some ( constructor_instance_type) ;
104+ for binding in & mut binding. overloads {
105+ binding. constructor_instance_type = Some ( constructor_instance_type) ;
106+ }
107+ }
108+
109+ self
110+ }
111+
92112 pub ( crate ) fn set_dunder_call_is_possibly_unbound ( & mut self ) {
93113 for binding in & mut self . elements {
94114 binding. dunder_call_is_possibly_unbound = true ;
@@ -107,6 +127,7 @@ impl<'db> Bindings<'db> {
107127 Self {
108128 callable_type : self . callable_type ,
109129 argument_forms : self . argument_forms ,
130+ constructor_instance_type : self . constructor_instance_type ,
110131 elements : self . elements . into_iter ( ) . map ( f) . collect ( ) ,
111132 }
112133 }
@@ -240,6 +261,10 @@ impl<'db> Bindings<'db> {
240261 self . callable_type
241262 }
242263
264+ pub ( crate ) fn constructor_instance_type ( & self ) -> Option < Type < ' db > > {
265+ self . constructor_instance_type
266+ }
267+
243268 /// Returns the return type of the call. For successful calls, this is the actual return type.
244269 /// For calls with binding errors, this is a type that best approximates the return type. For
245270 /// types that are not callable, returns `Type::Unknown`.
@@ -1357,6 +1382,7 @@ impl<'db> From<CallableBinding<'db>> for Bindings<'db> {
13571382 callable_type : from. callable_type ,
13581383 elements : smallvec_inline ! [ from] ,
13591384 argument_forms : ArgumentForms :: new ( 0 ) ,
1385+ constructor_instance_type : None ,
13601386 }
13611387 }
13621388}
@@ -1370,6 +1396,7 @@ impl<'db> From<Binding<'db>> for Bindings<'db> {
13701396 signature_type,
13711397 dunder_call_is_possibly_unbound : false ,
13721398 bound_type : None ,
1399+ constructor_instance_type : None ,
13731400 overload_call_return_type : None ,
13741401 matching_overload_before_type_checking : None ,
13751402 overloads : smallvec_inline ! [ from] ,
@@ -1378,6 +1405,7 @@ impl<'db> From<Binding<'db>> for Bindings<'db> {
13781405 callable_type,
13791406 elements : smallvec_inline ! [ callable_binding] ,
13801407 argument_forms : ArgumentForms :: new ( 0 ) ,
1408+ constructor_instance_type : None ,
13811409 }
13821410 }
13831411}
@@ -1409,6 +1437,9 @@ pub(crate) struct CallableBinding<'db> {
14091437 /// The type of the bound `self` or `cls` parameter if this signature is for a bound method.
14101438 pub ( crate ) bound_type : Option < Type < ' db > > ,
14111439
1440+ /// The type of the instance being constructed, if this signature is for a constructor.
1441+ pub ( crate ) constructor_instance_type : Option < Type < ' db > > ,
1442+
14121443 /// The return type of this overloaded callable.
14131444 ///
14141445 /// This is [`Some`] only in the following cases:
@@ -1457,6 +1488,7 @@ impl<'db> CallableBinding<'db> {
14571488 signature_type,
14581489 dunder_call_is_possibly_unbound : false ,
14591490 bound_type : None ,
1491+ constructor_instance_type : None ,
14601492 overload_call_return_type : None ,
14611493 matching_overload_before_type_checking : None ,
14621494 overloads,
@@ -1469,6 +1501,7 @@ impl<'db> CallableBinding<'db> {
14691501 signature_type,
14701502 dunder_call_is_possibly_unbound : false ,
14711503 bound_type : None ,
1504+ constructor_instance_type : None ,
14721505 overload_call_return_type : None ,
14731506 matching_overload_before_type_checking : None ,
14741507 overloads : smallvec ! [ ] ,
@@ -2689,7 +2722,7 @@ struct ArgumentTypeChecker<'a, 'db> {
26892722 arguments : & ' a CallArguments < ' a , ' db > ,
26902723 argument_matches : & ' a [ MatchedArgument < ' db > ] ,
26912724 parameter_tys : & ' a mut [ Option < Type < ' db > > ] ,
2692- callable_type : Type < ' db > ,
2725+ constructor_instance_type : Option < Type < ' db > > ,
26932726 call_expression_tcx : TypeContext < ' db > ,
26942727 return_ty : Type < ' db > ,
26952728 errors : & ' a mut Vec < BindingError < ' db > > ,
@@ -2706,7 +2739,7 @@ impl<'a, 'db> ArgumentTypeChecker<'a, 'db> {
27062739 arguments : & ' a CallArguments < ' a , ' db > ,
27072740 argument_matches : & ' a [ MatchedArgument < ' db > ] ,
27082741 parameter_tys : & ' a mut [ Option < Type < ' db > > ] ,
2709- callable_type : Type < ' db > ,
2742+ constructor_instance_type : Option < Type < ' db > > ,
27102743 call_expression_tcx : TypeContext < ' db > ,
27112744 return_ty : Type < ' db > ,
27122745 errors : & ' a mut Vec < BindingError < ' db > > ,
@@ -2717,7 +2750,7 @@ impl<'a, 'db> ArgumentTypeChecker<'a, 'db> {
27172750 arguments,
27182751 argument_matches,
27192752 parameter_tys,
2720- callable_type ,
2753+ constructor_instance_type ,
27212754 call_expression_tcx,
27222755 return_ty,
27232756 errors,
@@ -2759,8 +2792,7 @@ impl<'a, 'db> ArgumentTypeChecker<'a, 'db> {
27592792 } ;
27602793
27612794 let return_with_tcx = self
2762- . callable_type
2763- . synthesized_constructor_return_ty ( self . db )
2795+ . constructor_instance_type
27642796 . or ( self . signature . return_ty )
27652797 . zip ( self . call_expression_tcx . annotation ) ;
27662798
@@ -3109,6 +3141,9 @@ pub(crate) struct Binding<'db> {
31093141 /// it may be a `__call__` method.
31103142 pub ( crate ) signature_type : Type < ' db > ,
31113143
3144+ /// The type of the instance being constructed, if this signature is for a constructor.
3145+ pub ( crate ) constructor_instance_type : Option < Type < ' db > > ,
3146+
31123147 /// Return type of the call.
31133148 return_ty : Type < ' db > ,
31143149
@@ -3140,6 +3175,7 @@ impl<'db> Binding<'db> {
31403175 signature,
31413176 callable_type : signature_type,
31423177 signature_type,
3178+ constructor_instance_type : None ,
31433179 return_ty : Type :: unknown ( ) ,
31443180 inferable_typevars : InferableTypeVars :: None ,
31453181 specialization : None ,
@@ -3204,7 +3240,7 @@ impl<'db> Binding<'db> {
32043240 arguments,
32053241 & self . argument_matches ,
32063242 & mut self . parameter_tys ,
3207- self . callable_type ,
3243+ self . constructor_instance_type ,
32083244 call_expression_tcx,
32093245 self . return_ty ,
32103246 & mut self . errors ,
0 commit comments