From fd92bc6021423f144b8d6204a5ec7ac0cceeecc9 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 4 Oct 2023 21:58:38 +0000 Subject: [PATCH] Handle ReErased in responses in new solver --- .../src/solve/canonicalize.rs | 12 ++++++++++-- .../ui/impl-trait/erased-regions-in-hidden-ty.rs | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/ui/impl-trait/erased-regions-in-hidden-ty.rs diff --git a/compiler/rustc_trait_selection/src/solve/canonicalize.rs b/compiler/rustc_trait_selection/src/solve/canonicalize.rs index 19f0c9fe826..377ae1b4e85 100644 --- a/compiler/rustc_trait_selection/src/solve/canonicalize.rs +++ b/compiler/rustc_trait_selection/src/solve/canonicalize.rs @@ -224,12 +224,20 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { let kind = match *r { ty::ReLateBound(..) => return r, - ty::ReStatic => match self.canonicalize_mode { + // We may encounter `ReStatic` in item signatures or the hidden type + // of an opaque. `ReErased` should only be encountered in the hidden + // type of an opaque for regions that are ignored for the purposes of + // captures. + // + // FIXME: We should investigate the perf implications of not uniquifying + // `ReErased`. We may be able to short-circuit registering region + // obligations if we encounter a `ReErased` on one side, for example. + ty::ReStatic | ty::ReErased => match self.canonicalize_mode { CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT), CanonicalizeMode::Response { .. } => return r, }, - ty::ReErased | ty::ReFree(_) | ty::ReEarlyBound(_) => match self.canonicalize_mode { + ty::ReFree(_) | ty::ReEarlyBound(_) => match self.canonicalize_mode { CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT), CanonicalizeMode::Response { .. } => bug!("unexpected region in response: {r:?}"), }, diff --git a/tests/ui/impl-trait/erased-regions-in-hidden-ty.rs b/tests/ui/impl-trait/erased-regions-in-hidden-ty.rs new file mode 100644 index 00000000000..794dabe08b8 --- /dev/null +++ b/tests/ui/impl-trait/erased-regions-in-hidden-ty.rs @@ -0,0 +1,15 @@ +// revisions: current next +//[next] compile-flags: -Ztrait-solver=next +// check-pass + +// Make sure that the compiler can handle `ReErased` in the hidden type of an opaque. + +fn foo<'a: 'a>(x: &'a Vec) -> impl Fn() + 'static { + || () +} + +fn bar() -> impl Fn() + 'static { + foo(&vec![]) +} + +fn main() {}