Skip to content

Commit

Permalink
Auto merge of #100980 - compiler-errors:normalize-opaque-w-bound-vars…
Browse files Browse the repository at this point in the history
…, r=lcnr

Normalize opaques w/ bound vars

First, we reenable normalization of opaque types with escaping late bound regions to fix rust-lang/miri#2433. This essentially reverts #89285.

Second, we mitigate the perf regression found in #88862 by simplifying the way that we relate (sub and eq) GeneratorWitness types.

This relies on the fact that we construct these GeneratorWitness types somewhat particularly (with all free regions found in the witness types replaced with late bound regions) -- but those bound regions really should be treated as existential regions, not universal ones. Those two facts leads me to believe that we do not need to use the full `higher_ranked_sub` machinery to relate two generator witnesses. I'm pretty confident that this is correct, but I'm glad to discuss this further.
  • Loading branch information
bors committed Sep 22, 2022
2 parents c10f7d7 + 3fc328d commit 8ab71ab
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
19 changes: 19 additions & 0 deletions compiler/rustc_infer/src/infer/equate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_infer/src/infer/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_typeck/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}"),
}
}

Expand Down
20 changes: 1 addition & 19 deletions src/test/ui/higher-rank-trait-bounds/issue-95034.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/higher-rank-trait-bounds/issue-95034.stderr

This file was deleted.

0 comments on commit 8ab71ab

Please sign in to comment.