@@ -595,7 +595,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
595595 self . evaluate_predicates_recursively_in_new_solver ( predicates)
596596 } else {
597597 let mut result = EvaluatedToOk ;
598- for obligation in predicates {
598+ for mut obligation in predicates {
599+ obligation. set_depth_from_parent ( stack. depth ( ) ) ;
599600 let eval = self . evaluate_predicate_recursively ( stack, obligation. clone ( ) ) ?;
600601 if let EvaluatedToErr = eval {
601602 // fast-path - EvaluatedToErr is the top of the lattice,
@@ -661,12 +662,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
661662 let p = bound_predicate. rebind ( p) ;
662663 // Does this code ever run?
663664 match self . infcx . subtype_predicate ( & obligation. cause , obligation. param_env , p) {
664- Ok ( Ok ( InferOk { mut obligations, .. } ) ) => {
665- self . add_depth ( obligations. iter_mut ( ) , obligation. recursion_depth ) ;
666- self . evaluate_predicates_recursively (
667- previous_stack,
668- obligations. into_iter ( ) ,
669- )
665+ Ok ( Ok ( InferOk { obligations, .. } ) ) => {
666+ self . evaluate_predicates_recursively ( previous_stack, obligations)
670667 }
671668 Ok ( Err ( _) ) => Ok ( EvaluatedToErr ) ,
672669 Err ( ..) => Ok ( EvaluatedToAmbig ) ,
@@ -677,12 +674,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
677674 let p = bound_predicate. rebind ( p) ;
678675 // Does this code ever run?
679676 match self . infcx . coerce_predicate ( & obligation. cause , obligation. param_env , p) {
680- Ok ( Ok ( InferOk { mut obligations, .. } ) ) => {
681- self . add_depth ( obligations. iter_mut ( ) , obligation. recursion_depth ) ;
682- self . evaluate_predicates_recursively (
683- previous_stack,
684- obligations. into_iter ( ) ,
685- )
677+ Ok ( Ok ( InferOk { obligations, .. } ) ) => {
678+ self . evaluate_predicates_recursively ( previous_stack, obligations)
686679 }
687680 Ok ( Err ( _) ) => Ok ( EvaluatedToErr ) ,
688681 Err ( ..) => Ok ( EvaluatedToAmbig ) ,
@@ -755,9 +748,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
755748 arg,
756749 obligation. cause . span ,
757750 ) {
758- Some ( mut obligations) => {
759- self . add_depth ( obligations. iter_mut ( ) , obligation. recursion_depth ) ;
760-
751+ Some ( obligations) => {
761752 cache. wf_args . borrow_mut ( ) . push ( ( arg, previous_stack. depth ( ) ) ) ;
762753 let result =
763754 self . evaluate_predicates_recursively ( previous_stack, obligations) ;
@@ -826,10 +817,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
826817 }
827818 }
828819
829- self . add_depth (
830- subobligations. iter_mut ( ) ,
831- obligation. recursion_depth ,
832- ) ;
820+ // Need to explicitly set the depth of nested goals here as
821+ // projection obligations can cycle by themselves and in
822+ // `evaluate_predicates_recursively` we only add the depth
823+ // for parent trait goals because only these get added to the
824+ // `TraitObligationStackList`.
825+ for subobligation in subobligations. iter_mut ( ) {
826+ subobligation. set_depth_from_parent ( obligation. recursion_depth ) ;
827+ }
833828 let res = self . evaluate_predicates_recursively (
834829 previous_stack,
835830 subobligations,
@@ -909,38 +904,28 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
909904 if a. def . did == b. def . did
910905 && tcx. def_kind ( a. def . did ) == DefKind :: AssocConst =>
911906 {
912- if let Ok ( new_obligations ) = self
907+ if let Ok ( InferOk { obligations , value : ( ) } ) = self
913908 . infcx
914909 . at ( & obligation. cause , obligation. param_env )
915910 . trace ( c1, c2)
916911 . eq ( DefineOpaqueTypes :: No , a. substs , b. substs )
917912 {
918- let mut obligations = new_obligations. obligations ;
919- self . add_depth (
920- obligations. iter_mut ( ) ,
921- obligation. recursion_depth ,
922- ) ;
923913 return self . evaluate_predicates_recursively (
924914 previous_stack,
925- obligations. into_iter ( ) ,
915+ obligations,
926916 ) ;
927917 }
928918 }
929919 ( _, Unevaluated ( _) ) | ( Unevaluated ( _) , _) => ( ) ,
930920 ( _, _) => {
931- if let Ok ( new_obligations ) = self
921+ if let Ok ( InferOk { obligations , value : ( ) } ) = self
932922 . infcx
933923 . at ( & obligation. cause , obligation. param_env )
934924 . eq ( DefineOpaqueTypes :: No , c1, c2)
935925 {
936- let mut obligations = new_obligations. obligations ;
937- self . add_depth (
938- obligations. iter_mut ( ) ,
939- obligation. recursion_depth ,
940- ) ;
941926 return self . evaluate_predicates_recursively (
942927 previous_stack,
943- obligations. into_iter ( ) ,
928+ obligations,
944929 ) ;
945930 }
946931 }
@@ -1366,24 +1351,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13661351 self . infcx . evaluation_cache . insert ( ( param_env, trait_pred) , dep_node, result) ;
13671352 }
13681353
1369- /// For various reasons, it's possible for a subobligation
1370- /// to have a *lower* recursion_depth than the obligation used to create it.
1371- /// Projection sub-obligations may be returned from the projection cache,
1372- /// which results in obligations with an 'old' `recursion_depth`.
1373- /// Additionally, methods like `InferCtxt.subtype_predicate` produce
1374- /// subobligations without taking in a 'parent' depth, causing the
1375- /// generated subobligations to have a `recursion_depth` of `0`.
1376- ///
1377- /// To ensure that obligation_depth never decreases, we force all subobligations
1378- /// to have at least the depth of the original obligation.
1379- fn add_depth < T : ' cx , I : Iterator < Item = & ' cx mut Obligation < ' tcx , T > > > (
1380- & self ,
1381- it : I ,
1382- min_depth : usize ,
1383- ) {
1384- it. for_each ( |o| o. recursion_depth = cmp:: max ( min_depth, o. recursion_depth ) + 1 ) ;
1385- }
1386-
13871354 fn check_recursion_depth < T > (
13881355 & self ,
13891356 depth : usize ,
0 commit comments