@@ -508,7 +508,7 @@ impl<'tcx> InferCtxt<'tcx> {
508508 ) -> InferResult < ' tcx , ( ) > {
509509 let mut obligations = Vec :: new ( ) ;
510510
511- self . insert_hidden_type (
511+ let already_had_hidden_ty = self . insert_hidden_type (
512512 opaque_type_key,
513513 & cause,
514514 param_env,
@@ -517,14 +517,21 @@ impl<'tcx> InferCtxt<'tcx> {
517517 & mut obligations,
518518 ) ?;
519519
520- self . add_item_bounds_for_hidden_type (
521- opaque_type_key. def_id . to_def_id ( ) ,
522- opaque_type_key. args ,
523- cause,
524- param_env,
525- hidden_ty,
526- & mut obligations,
527- ) ;
520+ // If we already had a hidden type, that hidden type will
521+ // have already gone through `add_item_bounds_for_hidden_type`.
522+ // Since all the bounds must already hold for the previous hidden type
523+ // and the new hidden type must be equal to the previous hidden type,
524+ // we can conclude that the bounds must already hold.
525+ if !already_had_hidden_ty {
526+ self . add_item_bounds_for_hidden_type (
527+ opaque_type_key. def_id . to_def_id ( ) ,
528+ opaque_type_key. args ,
529+ cause,
530+ param_env,
531+ hidden_ty,
532+ & mut obligations,
533+ ) ;
534+ }
528535
529536 Ok ( InferOk { value : ( ) , obligations } )
530537 }
@@ -544,7 +551,7 @@ impl<'tcx> InferCtxt<'tcx> {
544551 hidden_ty : Ty < ' tcx > ,
545552 a_is_expected : bool ,
546553 obligations : & mut Vec < PredicateObligation < ' tcx > > ,
547- ) -> Result < ( ) , TypeError < ' tcx > > {
554+ ) -> Result < bool , TypeError < ' tcx > > {
548555 // Ideally, we'd get the span where *this specific `ty` came
549556 // from*, but right now we just use the span from the overall
550557 // value being folded. In simple cases like `-> impl Foo`,
@@ -560,7 +567,8 @@ impl<'tcx> InferCtxt<'tcx> {
560567 cause. clone ( ) ,
561568 param_env,
562569 ty:: PredicateKind :: Ambiguous ,
563- ) )
570+ ) ) ;
571+ Ok ( false )
564572 } else {
565573 let prev = self
566574 . inner
@@ -573,10 +581,11 @@ impl<'tcx> InferCtxt<'tcx> {
573581 . eq_exp ( DefineOpaqueTypes :: Yes , a_is_expected, prev, hidden_ty) ?
574582 . obligations ,
575583 ) ;
584+ Ok ( true )
585+ } else {
586+ Ok ( false )
576587 }
577- } ;
578-
579- Ok ( ( ) )
588+ }
580589 }
581590
582591 pub fn add_item_bounds_for_hidden_type (
0 commit comments