@@ -508,7 +508,7 @@ impl<'tcx> InferCtxt<'tcx> {
508
508
) -> InferResult < ' tcx , ( ) > {
509
509
let mut obligations = Vec :: new ( ) ;
510
510
511
- self . insert_hidden_type (
511
+ let already_had_hidden_ty = self . insert_hidden_type (
512
512
opaque_type_key,
513
513
& cause,
514
514
param_env,
@@ -517,14 +517,21 @@ impl<'tcx> InferCtxt<'tcx> {
517
517
& mut obligations,
518
518
) ?;
519
519
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
+ }
528
535
529
536
Ok ( InferOk { value : ( ) , obligations } )
530
537
}
@@ -544,7 +551,7 @@ impl<'tcx> InferCtxt<'tcx> {
544
551
hidden_ty : Ty < ' tcx > ,
545
552
a_is_expected : bool ,
546
553
obligations : & mut Vec < PredicateObligation < ' tcx > > ,
547
- ) -> Result < ( ) , TypeError < ' tcx > > {
554
+ ) -> Result < bool , TypeError < ' tcx > > {
548
555
// Ideally, we'd get the span where *this specific `ty` came
549
556
// from*, but right now we just use the span from the overall
550
557
// value being folded. In simple cases like `-> impl Foo`,
@@ -560,7 +567,8 @@ impl<'tcx> InferCtxt<'tcx> {
560
567
cause. clone ( ) ,
561
568
param_env,
562
569
ty:: PredicateKind :: Ambiguous ,
563
- ) )
570
+ ) ) ;
571
+ Ok ( false )
564
572
} else {
565
573
let prev = self
566
574
. inner
@@ -573,10 +581,11 @@ impl<'tcx> InferCtxt<'tcx> {
573
581
. eq_exp ( DefineOpaqueTypes :: Yes , a_is_expected, prev, hidden_ty) ?
574
582
. obligations ,
575
583
) ;
584
+ Ok ( true )
585
+ } else {
586
+ Ok ( false )
576
587
}
577
- } ;
578
-
579
- Ok ( ( ) )
588
+ }
580
589
}
581
590
582
591
pub fn add_item_bounds_for_hidden_type (
0 commit comments