diff --git a/compiler/rustc_infer/src/infer/equate.rs b/compiler/rustc_infer/src/infer/equate.rs index 3b1798ca73746..59728148a84c4 100644 --- a/compiler/rustc_infer/src/infer/equate.rs +++ b/compiler/rustc_infer/src/infer/equate.rs @@ -110,6 +110,25 @@ 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); + 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 { + return Err(ty::error::TypeError::Sorts(ty::relate::expected_found( + self, a, b, + ))); + } + } _ => { 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 b7eab5d43285b..375dd670fabf4 100644 --- a/compiler/rustc_infer/src/infer/sub.rs +++ b/compiler/rustc_infer/src/infer/sub.rs @@ -164,6 +164,24 @@ 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); + 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)?; + } + Ok(a) + } else { + Err(ty::error::TypeError::Sorts(ty::relate::expected_found(self, a, b))) + } + } _ => { self.fields.infcx.super_combine_tys(self, a, b)?; diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 6f41cbeb7af2a..ed83847e88038 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 88000d469786e..77f83b3f54240 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), diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs index 7ab6d9e2bab12..254a19368bfe2 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}"), } } 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 d8edbe7e56b51..af4946a187f15 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 1d8329142fc5c..0000000000000 --- a/src/test/ui/higher-rank-trait-bounds/issue-95034.stderr +++ /dev/null @@ -1 +0,0 @@ -thread 'rustc' panicked