@@ -626,11 +626,55 @@ impl ClassField {
626626
627627 fn instantiate_for ( & self , heap : & TypeHeap , instance : & Instance ) -> Self {
628628 self . instantiate_helper ( & mut |ty| {
629- ty. subst_self_type_mut ( & instance. to_type ( heap) ) ;
629+ if let Some ( self_type) = instance. self_return_type_override ( ) {
630+ Self :: subst_callable_return_self_type_mut ( ty, self_type, & instance. to_type ( heap) ) ;
631+ } else {
632+ ty. subst_self_type_mut ( & instance. to_type ( heap) ) ;
633+ }
630634 instance. instantiate_member ( ty)
631635 } )
632636 }
633637
638+ fn subst_callable_return_self_type_mut (
639+ ty : & mut Type ,
640+ self_type : & Type ,
641+ fallback_self_type : & Type ,
642+ ) {
643+ let subst_callable = |callable : & mut Callable | {
644+ callable. ret . subst_self_type_mut ( self_type) ;
645+ callable
646+ . params
647+ . visit_mut ( & mut |ty| ty. subst_self_type_mut ( fallback_self_type) ) ;
648+ } ;
649+ match ty {
650+ Type :: Callable ( callable) => subst_callable ( callable) ,
651+ Type :: Function ( func) => subst_callable ( & mut func. signature ) ,
652+ Type :: Forall ( forall) => match & mut forall. body {
653+ Forallable :: Callable ( callable) => subst_callable ( callable) ,
654+ Forallable :: Function ( func) => subst_callable ( & mut func. signature ) ,
655+ _ => ty. subst_self_type_mut ( fallback_self_type) ,
656+ } ,
657+ Type :: Overload ( overload) => {
658+ for sig in overload. signatures . iter_mut ( ) {
659+ match sig {
660+ OverloadType :: Function ( func) => subst_callable ( & mut func. signature ) ,
661+ OverloadType :: Forall ( forall) => subst_callable ( & mut forall. body . signature ) ,
662+ }
663+ }
664+ }
665+ Type :: Union ( union) => {
666+ for member in union. members . iter_mut ( ) {
667+ Self :: subst_callable_return_self_type_mut (
668+ member,
669+ self_type,
670+ fallback_self_type,
671+ ) ;
672+ }
673+ }
674+ _ => ty. subst_self_type_mut ( fallback_self_type) ,
675+ }
676+ }
677+
634678 fn instantiate_for_class_targs (
635679 & self ,
636680 targs : & TArgs ,
@@ -3822,7 +3866,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
38223866 ) -> Option < ClassAttribute > {
38233867 self . get_class_member ( cls. class_object ( ) , name)
38243868 . map ( |field| {
3825- self . as_instance_attribute ( name, & field, & Instance :: of_protocol ( cls, self_type) )
3869+ self . as_instance_attribute (
3870+ name,
3871+ & field,
3872+ & Instance :: of_class_with_self_type ( cls, self_type) ,
3873+ )
38263874 } )
38273875 }
38283876
0 commit comments