From 43119d643857efc366bfca527ac2dadfc3f2e906 Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Thu, 25 Aug 2022 02:08:38 +0000
Subject: [PATCH 1/4] Normalize opaques with escaping bound vars

---
 compiler/rustc_trait_selection/src/traits/project.rs         | 2 +-
 compiler/rustc_trait_selection/src/traits/query/normalize.rs | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs
index 6f41cbeb7af..ed83847e880 100644
--- a/compiler/rustc_trait_selection/src/traits/project.rs
+++ b/compiler/rustc_trait_selection/src/traits/project.rs
@@ -499,7 +499,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
             // This is really important. While we *can* handle this, this has
             // severe performance implications for large opaque types with
             // late-bound regions. See `issue-88862` benchmark.
-            ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
+            ty::Opaque(def_id, substs) => {
                 // Only normalize `impl Trait` outside of type inference, usually in codegen.
                 match self.param_env.reveal() {
                     Reveal::UserFacing => ty.super_fold_with(self),
diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs
index 88000d46978..77f83b3f542 100644
--- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs
+++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs
@@ -198,7 +198,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
             // This is really important. While we *can* handle this, this has
             // severe performance implications for large opaque types with
             // late-bound regions. See `issue-88862` benchmark.
-            ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
+            ty::Opaque(def_id, substs) => {
                 // Only normalize `impl Trait` outside of type inference, usually in codegen.
                 match self.param_env.reveal() {
                     Reveal::UserFacing => ty.try_super_fold_with(self),

From d0181447614dfcebcf3a658a6818a4b45cbe90db Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Mon, 29 Aug 2022 07:00:11 +0000
Subject: [PATCH 2/4] Optimize subtyping and equation of GeneratorWitness

---
 compiler/rustc_infer/src/infer/equate.rs      | 18 +++++++++++++++++
 compiler/rustc_infer/src/infer/sub.rs         | 19 ++++++++++++++++++
 .../higher-rank-trait-bounds/issue-95034.rs   | 20 +------------------
 .../issue-95034.stderr                        |  1 -
 4 files changed, 38 insertions(+), 20 deletions(-)
 delete mode 100644 src/test/ui/higher-rank-trait-bounds/issue-95034.stderr

diff --git a/compiler/rustc_infer/src/infer/equate.rs b/compiler/rustc_infer/src/infer/equate.rs
index 3b1798ca737..a962bbb03ad 100644
--- a/compiler/rustc_infer/src/infer/equate.rs
+++ b/compiler/rustc_infer/src/infer/equate.rs
@@ -110,6 +110,24 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
                         .obligations,
                 );
             }
+            (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
+                let a_types = infcx.tcx.anonymize_bound_vars(a_types);
+                let b_types = infcx.tcx.anonymize_bound_vars(b_types);
+                if a_types.bound_vars() == b_types.bound_vars() {
+                    let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
+                        a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
+                    );
+                    for (a, b) in std::iter::zip(a_types, b_types) {
+                        self.relate(a, b)?;
+                    }
+                } else {
+                    self.fields.infcx.super_combine_tys(
+                        self,
+                        infcx.tcx.mk_generator_witness(a_types),
+                        infcx.tcx.mk_generator_witness(b_types),
+                    )?;
+                }
+            }
 
             _ => {
                 self.fields.infcx.super_combine_tys(self, a, b)?;
diff --git a/compiler/rustc_infer/src/infer/sub.rs b/compiler/rustc_infer/src/infer/sub.rs
index b7eab5d4328..2c14a9414f2 100644
--- a/compiler/rustc_infer/src/infer/sub.rs
+++ b/compiler/rustc_infer/src/infer/sub.rs
@@ -164,6 +164,25 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
                 );
                 Ok(ga)
             }
+            (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
+                let a_types = infcx.tcx.anonymize_bound_vars(a_types);
+                let b_types = infcx.tcx.anonymize_bound_vars(b_types);
+                if a_types.bound_vars() == b_types.bound_vars() {
+                    let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
+                        a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
+                    );
+                    for (a, b) in std::iter::zip(a_types, b_types) {
+                        self.relate(a, b)?;
+                    }
+                } else {
+                    self.fields.infcx.super_combine_tys(
+                        self,
+                        infcx.tcx.mk_generator_witness(a_types),
+                        infcx.tcx.mk_generator_witness(b_types),
+                    )?;
+                }
+                Ok(a)
+            }
 
             _ => {
                 self.fields.infcx.super_combine_tys(self, a, b)?;
diff --git a/src/test/ui/higher-rank-trait-bounds/issue-95034.rs b/src/test/ui/higher-rank-trait-bounds/issue-95034.rs
index d8edbe7e56b..af4946a187f 100644
--- a/src/test/ui/higher-rank-trait-bounds/issue-95034.rs
+++ b/src/test/ui/higher-rank-trait-bounds/issue-95034.rs
@@ -1,23 +1,5 @@
-// known-bug: #95034
-// failure-status: 101
+// check-pass
 // compile-flags: --edition=2021 --crate-type=lib
-// rustc-env:RUST_BACKTRACE=0
-
-// normalize-stderr-test "thread 'rustc' panicked.*" -> "thread 'rustc' panicked"
-// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
-// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
-// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""
-// normalize-stderr-test "note: we would appreciate a bug report.*\n\n" -> ""
-// normalize-stderr-test "note: compiler flags.*\n\n" -> ""
-// normalize-stderr-test "note: rustc.*running on.*\n\n" -> ""
-// normalize-stderr-test "query stack during panic:\n" -> ""
-// normalize-stderr-test "we're just showing a limited slice of the query stack\n" -> ""
-// normalize-stderr-test "end of query stack\n" -> ""
-// normalize-stderr-test "#.*\n" -> ""
-
-// This should not ICE.
-
-// Refer to the issue for more minimized versions.
 
 use std::{
     future::Future,
diff --git a/src/test/ui/higher-rank-trait-bounds/issue-95034.stderr b/src/test/ui/higher-rank-trait-bounds/issue-95034.stderr
deleted file mode 100644
index 1d8329142fc..00000000000
--- a/src/test/ui/higher-rank-trait-bounds/issue-95034.stderr
+++ /dev/null
@@ -1 +0,0 @@
-thread 'rustc' panicked

From 02ad984d74b6e238b16a50c61505d76acc0e648c Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Mon, 29 Aug 2022 18:23:45 +0000
Subject: [PATCH 3/4] Comment, and bail early if bound vars list differs

---
 compiler/rustc_infer/src/infer/equate.rs | 11 ++++++-----
 compiler/rustc_infer/src/infer/sub.rs    | 11 +++++------
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/compiler/rustc_infer/src/infer/equate.rs b/compiler/rustc_infer/src/infer/equate.rs
index a962bbb03ad..59728148a84 100644
--- a/compiler/rustc_infer/src/infer/equate.rs
+++ b/compiler/rustc_infer/src/infer/equate.rs
@@ -110,6 +110,9 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
                         .obligations,
                 );
             }
+            // Optimization of GeneratorWitness relation since we know that all
+            // free regions are replaced with bound regions during construction.
+            // This greatly speeds up equating of GeneratorWitness.
             (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
                 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
                 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
@@ -121,11 +124,9 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
                         self.relate(a, b)?;
                     }
                 } else {
-                    self.fields.infcx.super_combine_tys(
-                        self,
-                        infcx.tcx.mk_generator_witness(a_types),
-                        infcx.tcx.mk_generator_witness(b_types),
-                    )?;
+                    return Err(ty::error::TypeError::Sorts(ty::relate::expected_found(
+                        self, a, b,
+                    )));
                 }
             }
 
diff --git a/compiler/rustc_infer/src/infer/sub.rs b/compiler/rustc_infer/src/infer/sub.rs
index 2c14a9414f2..375dd670fab 100644
--- a/compiler/rustc_infer/src/infer/sub.rs
+++ b/compiler/rustc_infer/src/infer/sub.rs
@@ -164,6 +164,9 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
                 );
                 Ok(ga)
             }
+            // Optimization of GeneratorWitness relation since we know that all
+            // free regions are replaced with bound regions during construction.
+            // This greatly speeds up subtyping of GeneratorWitness.
             (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
                 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
                 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
@@ -174,14 +177,10 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
                     for (a, b) in std::iter::zip(a_types, b_types) {
                         self.relate(a, b)?;
                     }
+                    Ok(a)
                 } else {
-                    self.fields.infcx.super_combine_tys(
-                        self,
-                        infcx.tcx.mk_generator_witness(a_types),
-                        infcx.tcx.mk_generator_witness(b_types),
-                    )?;
+                    Err(ty::error::TypeError::Sorts(ty::relate::expected_found(self, a, b)))
                 }
-                Ok(a)
             }
 
             _ => {

From 3fc328dfd3c7699a9be3c889de55c354d5a5c006 Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Mon, 29 Aug 2022 20:09:45 +0000
Subject: [PATCH 4/4] Normalize generator interior type before erasing

---
 compiler/rustc_typeck/src/check/generator_interior.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs
index 7ab6d9e2bab..254a19368bf 100644
--- a/compiler/rustc_typeck/src/check/generator_interior.rs
+++ b/compiler/rustc_typeck/src/check/generator_interior.rs
@@ -218,7 +218,8 @@ pub fn resolve_interior<'a, 'tcx>(
         .filter_map(|mut cause| {
             // Erase regions and canonicalize late-bound regions to deduplicate as many types as we
             // can.
-            let erased = fcx.tcx.erase_regions(cause.ty);
+            let ty = fcx.normalize_associated_types_in(cause.span, cause.ty);
+            let erased = fcx.tcx.erase_regions(ty);
             if captured_tys.insert(erased) {
                 // Replace all regions inside the generator interior with late bound regions.
                 // Note that each region slot in the types gets a new fresh late bound region,
@@ -263,7 +264,7 @@ pub fn resolve_interior<'a, 'tcx>(
     // Unify the type variable inside the generator with the new witness
     match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
         Ok(ok) => fcx.register_infer_ok_obligations(ok),
-        _ => bug!(),
+        _ => bug!("failed to relate {interior} and {witness}"),
     }
 }