@@ -887,8 +887,13 @@ where
887887 }
888888 }
889889
890- let mut folder =
891- ReplaceProjectionWith { ecx, param_env, mapping : replace_projection_with, nested : vec ! [ ] } ;
890+ let mut folder = ReplaceProjectionWith {
891+ ecx,
892+ param_env,
893+ mapping : replace_projection_with,
894+ self_ty : trait_ref. self_ty ( ) ,
895+ nested : vec ! [ ] ,
896+ } ;
892897 let folded_requirements = requirements. fold_with ( & mut folder) ;
893898
894899 folder
@@ -900,6 +905,7 @@ where
900905
901906struct ReplaceProjectionWith < ' a , D : SolverDelegate < Interner = I > , I : Interner > {
902907 ecx : & ' a EvalCtxt < ' a , D > ,
908+ self_ty : I :: Ty ,
903909 param_env : I :: ParamEnv ,
904910 mapping : HashMap < I :: DefId , ty:: Binder < I , ty:: ProjectionPredicate < I > > > ,
905911 nested : Vec < Goal < I , I :: Predicate > > ,
@@ -914,29 +920,28 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I>
914920
915921 fn fold_ty ( & mut self , ty : I :: Ty ) -> I :: Ty {
916922 if let ty:: Alias ( ty:: Projection , alias_ty) = ty. kind ( ) {
917- if let Some ( replacement ) = self . mapping . get ( & alias_ty . def_id ) {
918- // We may have a case where our object type's projection bound is higher-ranked,
919- // but the where clauses we instantiated are not. We can solve this by instantiating
920- // the binder at the usage site.
921- let proj = self . ecx . instantiate_binder_with_infer ( * replacement ) ;
922- // FIXME: Technically this equate could be fallible...
923- self . nested . extend (
924- self . ecx
925- . eq_and_get_goals (
926- self . param_env ,
927- alias_ty ,
928- proj . projection_term . expect_ty ( self . ecx . cx ( ) ) ,
929- )
930- . expect (
931- "expected to be able to unify goal projection with dyn's projection" ,
932- ) ,
933- ) ;
934- proj . term . expect_ty ( )
935- } else {
936- ty . super_fold_with ( self )
923+ if alias_ty . self_ty ( ) == self . self_ty {
924+ if let Some ( replacement ) = self . mapping . get ( & alias_ty . def_id ) {
925+ // We may have a case where our object type's projection bound is higher-ranked,
926+ // but the where clauses we instantiated are not. We can solve this by instantiating
927+ // the binder at the usage site.
928+ let proj = self . ecx . instantiate_binder_with_infer ( * replacement ) ;
929+ // FIXME: Technically this equate could be fallible...
930+ self . nested . extend (
931+ self . ecx
932+ . eq_and_get_goals (
933+ self . param_env ,
934+ alias_ty ,
935+ proj . projection_term . expect_ty ( self . ecx . cx ( ) ) ,
936+ )
937+ . expect (
938+ "expected to be able to unify goal projection with dyn's projection" ,
939+ ) ,
940+ ) ;
941+ return proj . term . expect_ty ( ) ;
942+ }
937943 }
938- } else {
939- ty. super_fold_with ( self )
940944 }
945+ ty. super_fold_with ( self )
941946 }
942947}
0 commit comments