From 66314e2c8e18aba1ec5ede54f05b2d9e0cab1952 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 14 Apr 2023 14:14:42 +1000 Subject: [PATCH 1/2] Rewrite `RegionReplacer::fold_region`. --- src/librustdoc/clean/auto_trait.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 9479b3ee0369a..f6088f4e64a41 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -1,7 +1,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_hir::lang_items::LangItem; -use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable, TypeSuperFoldable}; +use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable}; use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult}; use thin_vec::ThinVec; @@ -740,10 +740,9 @@ impl<'a, 'tcx> TypeFolder> for RegionReplacer<'a, 'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - (match *r { - ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned(), - _ => None, - }) - .unwrap_or_else(|| r.super_fold_with(self)) + match *r { + ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned().unwrap_or(r), + _ => r, + } } } From 043c1dee8e4cfd42c5c5743157d045c355ed6a30 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 13 Apr 2023 10:38:57 +1000 Subject: [PATCH 2/2] Make all region folders strict about which variants they accept. --- .../src/diagnostics/bound_region_errors.rs | 6 ++- .../rustc_borrowck/src/diagnostics/mod.rs | 20 +++----- .../src/diagnostics/region_name.rs | 6 +-- .../rustc_borrowck/src/region_infer/mod.rs | 50 +++++++++++-------- .../src/region_infer/opaque_types.rs | 21 ++++---- compiler/rustc_borrowck/src/renumber.rs | 7 ++- .../src/type_check/constraint_conversion.rs | 3 +- .../rustc_borrowck/src/universal_regions.rs | 19 ++++--- .../rustc_hir_analysis/src/astconv/mod.rs | 5 +- .../rustc_hir_analysis/src/check/check.rs | 3 +- .../src/check/compare_impl_item.rs | 20 +++++--- .../rustc_hir_analysis/src/check/wfcheck.rs | 15 ++---- compiler/rustc_hir_analysis/src/collect.rs | 8 +-- .../rustc_hir_analysis/src/collect/type_of.rs | 2 +- .../rustc_hir_analysis/src/hir_wf_check.rs | 6 ++- compiler/rustc_hir_typeck/src/demand.rs | 5 +- .../src/generator_interior/mod.rs | 9 ++-- compiler/rustc_hir_typeck/src/writeback.rs | 22 ++++++-- .../src/infer/canonical/canonicalizer.rs | 1 + compiler/rustc_infer/src/infer/combine.rs | 35 ++++++------- .../error_reporting/nice_region_error/util.rs | 15 +++--- compiler/rustc_infer/src/infer/freshen.rs | 7 +-- compiler/rustc_infer/src/infer/fudge.rs | 17 +++++-- .../src/infer/lexical_region_resolve/mod.rs | 13 ++++- .../rustc_infer/src/infer/opaque_types.rs | 19 ++++++- compiler/rustc_infer/src/infer/resolve.rs | 16 +++++- .../src/opaque_hidden_inferred_bound.rs | 8 ++- compiler/rustc_middle/src/mir/query.rs | 2 +- compiler/rustc_middle/src/ty/erase_regions.rs | 11 +++- compiler/rustc_middle/src/ty/fold.rs | 23 +++++++-- compiler/rustc_middle/src/ty/opaque_types.rs | 10 ++-- compiler/rustc_middle/src/ty/print/pretty.rs | 9 +++- compiler/rustc_middle/src/ty/subst.rs | 3 +- .../src/solve/assembly/structural_traits.rs | 10 ++-- .../src/solve/canonicalize.rs | 2 +- .../src/traits/project.rs | 14 +++++- .../src/traits/select/mod.rs | 16 ++++-- compiler/rustc_traits/src/chalk/lowering.rs | 8 +-- compiler/rustc_ty_utils/src/ty.rs | 20 ++++---- src/librustdoc/clean/auto_trait.rs | 3 +- 40 files changed, 313 insertions(+), 176 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs index 84f75caa6928a..f8ab2b0797032 100644 --- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs @@ -428,7 +428,7 @@ fn try_extract_error_from_region_constraints<'tcx>( placeholder_region, vec![], ), - (Some(error_region), _) => { + (Some(error_region), ty::RePlaceholder(_)) => { RegionResolutionError::ConcreteFailure(cause.clone(), error_region, placeholder_region) } // Note universe here is wrong... @@ -439,9 +439,11 @@ fn try_extract_error_from_region_constraints<'tcx>( cause.clone(), placeholder_region, ), - (None, _) => { + (None, ty::ReStatic) => { RegionResolutionError::ConcreteFailure(cause.clone(), sub_region, placeholder_region) } + (Some(_), r) => bug!("unexpected region with `Some`: {r:?}"), + (None, r) => bug!("unexpected region with `None`: {r:?}"), }; NiceRegionError::new(&infcx.err_ctxt(), error).try_report_from_nll().or_else(|| { if let SubregionOrigin::Subtype(trace) = cause { diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 110354a20d839..95a4ce78e0247 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -466,12 +466,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { // lifetimes without names with the value `'0`. if let ty::Ref(region, ..) = ty.kind() { match **region { - ty::ReLateBound(_, ty::BoundRegion { kind: br, .. }) - | ty::RePlaceholder(ty::PlaceholderRegion { - bound: ty::BoundRegion { kind: br, .. }, - .. - }) => printer.region_highlight_mode.highlighting_bound_region(br, counter), - _ => {} + ty::ReLateBound(_, ty::BoundRegion { kind: br, .. }) => { + printer.region_highlight_mode.highlighting_bound_region(br, counter) + } + r => bug!("unexpected region: {r:?}"), } } @@ -485,12 +483,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let region = if let ty::Ref(region, ..) = ty.kind() { match **region { - ty::ReLateBound(_, ty::BoundRegion { kind: br, .. }) - | ty::RePlaceholder(ty::PlaceholderRegion { - bound: ty::BoundRegion { kind: br, .. }, - .. - }) => printer.region_highlight_mode.highlighting_bound_region(br, counter), - _ => {} + ty::ReLateBound(_, ty::BoundRegion { kind: br, .. }) => { + printer.region_highlight_mode.highlighting_bound_region(br, counter) + } + r => bug!("unexpected region: {r:?}"), } region } else { diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index f69c4829ae299..d0b1c8c00e4dc 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -357,11 +357,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { ty::BoundRegionKind::BrAnon(..) => None, }, - ty::ReLateBound(..) - | ty::ReVar(..) - | ty::RePlaceholder(..) - | ty::ReErased - | ty::ReError(_) => None, + r => bug!("unexpected region: {r:?}"), } } diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 729f3dbff3b46..3ce86c57d1cd5 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1127,22 +1127,27 @@ impl<'tcx> RegionInferenceContext<'tcx> { let ty = ty.fold_with(&mut OpaqueFolder { tcx }); let ty = tcx.fold_regions(ty, |r, _depth| { - let r_vid = self.to_region_vid(r); - let r_scc = self.constraint_sccs.scc(r_vid); - - // The challenge is this. We have some region variable `r` - // whose value is a set of CFG points and universal - // regions. We want to find if that set is *equivalent* to - // any of the named regions found in the closure. - // To do so, we simply check every candidate `u_r` for equality. - self.scc_values - .universal_regions_outlived_by(r_scc) - .filter(|&u_r| !self.universal_regions.is_local_free_region(u_r)) - .find(|&u_r| self.eval_equal(u_r, r_vid)) - .map(|u_r| tcx.mk_re_var(u_r)) - // In the case of a failure, use `ReErased`. We will eventually - // return `None` in this case. - .unwrap_or(tcx.lifetimes.re_erased) + match r.kind() { + ty::ReStatic | ty::ReVar(_) => { + let r_vid = self.to_region_vid(r); + let r_scc = self.constraint_sccs.scc(r_vid); + + // The challenge is this. We have some region variable `r` + // whose value is a set of CFG points and universal + // regions. We want to find if that set is *equivalent* to + // any of the named regions found in the closure. + // To do so, we simply check every candidate `u_r` for equality. + self.scc_values + .universal_regions_outlived_by(r_scc) + .filter(|&u_r| !self.universal_regions.is_local_free_region(u_r)) + .find(|&u_r| self.eval_equal(u_r, r_vid)) + .map(|u_r| tcx.mk_re_var(u_r)) + // In the case of a failure, use `ReErased`. We will eventually + // return `None` in this case. + .unwrap_or(tcx.lifetimes.re_erased) + } + r => bug!("unexpected region: {r:?}"), + } }); debug!("try_promote_type_test_subject: folded ty = {:?}", ty); @@ -1332,11 +1337,14 @@ impl<'tcx> RegionInferenceContext<'tcx> { where T: TypeFoldable>, { - tcx.fold_regions(value, |r, _db| { - let vid = self.to_region_vid(r); - let scc = self.constraint_sccs.scc(vid); - let repr = self.scc_representatives[scc]; - tcx.mk_re_var(repr) + tcx.fold_regions(value, |r, _db| match r.kind() { + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReVar(_) => { + let vid = self.to_region_vid(r); + let scc = self.constraint_sccs.scc(vid); + let repr = self.scc_representatives[scc]; + tcx.mk_re_var(repr) + } + r => bug!("unexpected region: {r:?}"), }) } diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index 2b16655cf7d5a..ecf3c2784fa8c 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -110,14 +110,14 @@ impl<'tcx> RegionInferenceContext<'tcx> { // Next, insert universal regions from substs, so we can translate regions that appear // in them but are not subject to member constraints, for instance closure substs. - let universal_substs = infcx.tcx.fold_regions(substs, |region, _| { - if let ty::RePlaceholder(..) = region.kind() { - // Higher kinded regions don't need remapping, they don't refer to anything outside of this the substs. - return region; - } - let vid = self.to_region_vid(region); - to_universal_region(vid, &mut subst_regions) - }); + let universal_substs = + infcx.tcx.fold_regions(substs, |region, _| match region.kind() { + ty::ReVar(_) => { + let vid = self.to_region_vid(region); + to_universal_region(vid, &mut subst_regions) + } + r => bug!("unexpected region: {r:?}"), + }); debug!(?universal_substs); debug!(?subst_regions); @@ -132,7 +132,8 @@ impl<'tcx> RegionInferenceContext<'tcx> { .find(|ur_vid| self.eval_equal(vid, **ur_vid)) .and_then(|ur_vid| self.definitions[*ur_vid].external_name) .unwrap_or(infcx.tcx.lifetimes.re_erased), - _ => region, + ty::ReEarlyBound(_) | ty::ReStatic => region, + r => bug!("unexpected region: {r:?}"), }); debug!(?universal_concrete_type); @@ -201,7 +202,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { } } } - _ => region, + r => bug!("unexpected region: {r:?}"), }) } } diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index 94ce29dfe5197..bd8d4e8a0557e 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -75,8 +75,11 @@ impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> { F: Fn() -> RegionCtxt, { let origin = NllRegionVariableOrigin::Existential { from_forall: false }; - self.infcx.tcx.fold_regions(value, |_region, _depth| { - self.infcx.next_nll_region_var(origin, || region_ctxt_fn()) + self.infcx.tcx.fold_regions(value, |region, _depth| match region.kind() { + ty::ReErased | ty::ReError(_) => { + self.infcx.next_nll_region_var(origin, || region_ctxt_fn()) + } + r => bug!("unexpected region: {r:?}"), }) } } diff --git a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs index 71eae7b27d1db..eed070b60c9a4 100644 --- a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs +++ b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs @@ -178,7 +178,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> { ty::RePlaceholder(placeholder) => { self.constraints.placeholder_region(self.infcx, placeholder) } - _ => r, + ty::ReStatic | ty::ReVar(_) => r, + r => bug!("unexpected region: {r:?}"), }) } else { value diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index 70fddb1057c09..7311f37753305 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -757,11 +757,13 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { where T: TypeFoldable>, { - self.infcx.tcx.fold_regions(value, |region, _depth| { - let name = region.get_name_or_anon(); - debug!(?region, ?name); - - self.next_nll_region_var(origin, || RegionCtxt::Free(name)) + self.infcx.tcx.fold_regions(value, |region, _depth| match region.kind() { + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReErased | ty::ReError(_) => { + let name = region.get_name_or_anon(); + debug!(?region, ?name); + self.next_nll_region_var(origin, || RegionCtxt::Free(name)) + } + r => bug!("unexpected region: {r:?}"), }) } @@ -889,7 +891,12 @@ impl<'tcx> UniversalRegionIndices<'tcx> { where T: TypeFoldable>, { - tcx.fold_regions(value, |region, _| tcx.mk_re_var(self.to_region_vid(region))) + tcx.fold_regions(value, |region, _| match region.kind() { + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReError(_) => { + tcx.mk_re_var(self.to_region_vid(region)) + } + r => bug!("unexpected region: {r:?}"), + }) } } diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 8d1156c1771e9..4141f268796a4 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -3226,8 +3226,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } hir::TyKind::Typeof(e) => { let ty_erased = tcx.type_of(e.def_id).subst_identity(); - let ty = tcx.fold_regions(ty_erased, |r, _| { - if r.is_erased() { tcx.lifetimes.re_static } else { r } + let ty = tcx.fold_regions(ty_erased, |r, _| match r.kind() { + ty::ReErased => tcx.lifetimes.re_static, + r => bug!("unexpected region: {r:?}"), }); let span = ast_ty.span; let (ty, opt_sugg) = if let Some(ty) = ty.make_suggestable(tcx, false) { diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 0bb98fdf2a23e..644d7d990fe20 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -419,7 +419,8 @@ fn check_opaque_meets_bounds<'tcx>( let hidden_ty = tcx.type_of(def_id.to_def_id()).subst(tcx, substs); let hidden_ty = tcx.fold_regions(hidden_ty, |re, _dbi| match re.kind() { ty::ReErased => infcx.next_region_var(RegionVariableOrigin::MiscVariable(span)), - _ => re, + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReError(_) => re, + r => bug!("unexpected region: {r:?}"), }); let misc_cause = traits::ObligationCause::misc(span, def_id); diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 5d119a7737a4c..36f7f391e0f9e 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -458,13 +458,13 @@ impl<'tcx> TypeFolder> for RemapLateBound<'_, 'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - if let ty::ReFree(fr) = *r { - self.tcx.mk_re_free( + match r.kind() { + ty::ReFree(fr) => self.tcx.mk_re_free( fr.scope, self.mapping.get(&fr.bound_region).copied().unwrap_or(fr.bound_region), - ) - } else { - r + ), + ty::ReEarlyBound(_) | ty::ReStatic => r, + r => bug!("unexpected region: {r:?}"), } } } @@ -765,11 +765,15 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( let num_impl_substs = tcx.generics_of(impl_m.container_id(tcx)).params.len(); let ty = tcx.fold_regions(ty, |region, _| { match region.kind() { - // Remap all free regions, which correspond to late-bound regions in the function. + // Remap all free regions, which correspond to late-bound regions in the + // function. ty::ReFree(_) => {} - // Remap early-bound regions as long as they don't come from the `impl` itself. + // Remap early-bound regions as long as they don't come from the `impl` + // itself. ty::ReEarlyBound(ebr) if tcx.parent(ebr.def_id) != impl_m.container_id(tcx) => {} - _ => return region, + ty::ReEarlyBound(_) | + ty::ReStatic => return region, + r => bug!("unexpected region: {r:?}"), } let Some(ty::ReEarlyBound(e)) = map.get(®ion.into()).map(|r| r.expect_region().kind()) else { diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 53197bc849106..aa096460c2478 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1574,17 +1574,10 @@ impl<'tcx> TypeVisitor> for ImplTraitInTraitFinder<'_, 'tcx> { && let hir::OpaqueTyOrigin::FnReturn(source) | hir::OpaqueTyOrigin::AsyncFn(source) = opaque.origin && source == self.fn_def_id { - let opaque_ty = tcx.fold_regions(unshifted_opaque_ty, |re, depth| { - if let ty::ReLateBound(index, bv) = re.kind() { - if depth != ty::INNERMOST { - return tcx.mk_re_error_with_message( - DUMMY_SP, - "we shouldn't walk non-predicate binders with `impl Trait`...", - ); - } - tcx.mk_re_late_bound(index.shifted_out_to_binder(self.depth), bv) - } else { - re + let opaque_ty = tcx.fold_regions(unshifted_opaque_ty, |re, _depth| { + match re.kind() { + ty::ReEarlyBound(_) | ty::ReFree(_) => re, + r => bug!("unexpected region: {r:?}"), } }); for (bound, bound_span) in tcx diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index cbbaf8f857dac..91b6aec912163 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -387,8 +387,10 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> { fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> { let ty = self.tcx.fold_regions(ty, |r, _| match *r { - ty::ReErased => self.tcx.lifetimes.re_static, - _ => r, + // This is never reached in practice. If it ever is reached, + // `ReErased` should be changed to `ReStatic`, and any other region + // left alone. + r => bug!("unexpected region: {r:?}"), }); self.tcx().const_error_with_message(ty, span, "bad placeholder constant") } @@ -1142,7 +1144,7 @@ fn infer_return_ty_for_fn_sig<'tcx>( // Typeck doesn't expect erased regions to be returned from `type_of`. let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r { ty::ReErased => tcx.lifetimes.re_static, - _ => r, + r => bug!("unexpected region: {r:?}"), }); let mut visitor = HirPlaceholderCollector::default(); diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index c173bd913a84c..5e537c981b660 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -933,7 +933,7 @@ fn infer_placeholder_type<'a>( // Typeck doesn't expect erased regions to be returned from `type_of`. tcx.fold_regions(ty, |r, _| match *r { ty::ReErased => tcx.lifetimes.re_static, - _ => r, + r => bug!("unexpected region: {r:?}"), }) } diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index 8269a6ddea5f4..0cb43f7f45999 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -194,6 +194,10 @@ impl<'tcx> TypeFolder> for EraseAllBoundRegions<'tcx> { self.tcx } fn fold_region(&mut self, r: Region<'tcx>) -> Region<'tcx> { - if r.is_late_bound() { self.tcx.lifetimes.re_erased } else { r } + match r.kind() { + ty::ReLateBound(..) => self.tcx.lifetimes.re_erased, + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReError(_) => r, + r => bug!("unexpected region: {r:?}"), + } } } diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index a4c3be1d17745..4e401d3f4d216 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -270,7 +270,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Hack to make equality checks on types with inference variables and regions useful. let mut eraser = BottomUpFolder { tcx: self.tcx, - lt_op: |_| self.tcx.lifetimes.re_erased, + lt_op: |r| match r.kind() { + ty::ReStatic | ty::ReVar(_) | ty::ReErased => self.tcx.lifetimes.re_erased, + r => bug!("unexpected region: {r:?}"), + }, ct_op: |c| c, ty_op: |t| match *t.kind() { ty::Infer(ty::TyVar(_)) => self.tcx.mk_ty_var(ty::TyVid::from_u32(0)), diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index 5faa6ab13dd7d..7bf26bb590dd0 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -257,19 +257,20 @@ pub fn resolve_interior<'a, 'tcx>( _ => mk_bound_region(None), } } - // FIXME: these should use `BrNamed` + // // FIXME: these should use `BrNamed` ty::ReEarlyBound(region) => { mk_bound_region(Some(fcx.tcx.def_span(region.def_id))) } - ty::ReLateBound(_, ty::BoundRegion { kind, .. }) - | ty::ReFree(ty::FreeRegion { bound_region: kind, .. }) => match kind { + // ty::ReLateBound(_, ty::BoundRegion { kind, .. }) + ty::ReFree(ty::FreeRegion { bound_region: kind, .. }) => match kind { ty::BoundRegionKind::BrAnon(span) => mk_bound_region(span), ty::BoundRegionKind::BrNamed(def_id, _) => { mk_bound_region(Some(fcx.tcx.def_span(def_id))) } ty::BoundRegionKind::BrEnv => mk_bound_region(None), }, - _ => mk_bound_region(None), + ty::ReStatic | ty::ReErased => mk_bound_region(None), + r => bug!("unexpected region: {r:?}"), }; let r = fcx.tcx.mk_re_late_bound(current_depth, br); r diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index e876fa27593d4..885c348fb8f02 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -774,7 +774,16 @@ impl<'tcx> TypeFolder> for EraseEarlyRegions<'tcx> { } } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - if r.is_late_bound() { r } else { self.tcx.lifetimes.re_erased } + match r.kind() { + ty::ReLateBound(..) => r, + ty::ReEarlyBound(_) + | ty::ReFree(..) + | ty::ReStatic + | ty::RePlaceholder(_) + | ty::ReErased + | ty::ReError(_) => self.tcx.lifetimes.re_erased, + ty::ReVar(_) => bug!("unexpected region: {r:?}"), + } } } @@ -802,8 +811,15 @@ impl<'cx, 'tcx> TypeFolder> for Resolver<'cx, 'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - debug_assert!(!r.is_late_bound(), "Should not be resolving bound region."); - self.tcx.lifetimes.re_erased + match r.kind() { + ty::ReEarlyBound(_) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReError(_) => self.tcx.lifetimes.re_erased, + r => bug!("unexpected region: {r:?}"), + } } fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index e808911a38b12..4c93d6390148d 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -343,6 +343,7 @@ impl<'cx, 'tcx> TypeFolder> for Canonicalizer<'cx, 'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { + // All region variants occur in this match. ty::ReLateBound(index, ..) => { if index >= self.binder_index { bug!("escaping late-bound region during canonicalization"); diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index fe45b5ebe61b1..51fbc86edc210 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -939,30 +939,25 @@ impl<'tcx> FallibleTypeFolder> for ConstInferUnifier<'_, 'tcx> { ) -> Result, TypeError<'tcx>> { debug!("ConstInferUnifier: r={:?}", r); - match *r { + Ok(match *r { // Never make variables for regions bound within the type itself, // nor for erased regions. - ty::ReLateBound(..) | ty::ReErased | ty::ReError(_) => { - return Ok(r); - } + ty::ReLateBound(..) | ty::ReErased | ty::ReError(_) => r, - ty::RePlaceholder(..) - | ty::ReVar(..) - | ty::ReStatic - | ty::ReEarlyBound(..) - | ty::ReFree(..) => { - // see common code below + // ty::RePlaceholder(..) + ty::ReVar(..) | ty::ReStatic | ty::ReEarlyBound(..) | ty::ReFree(..) => { + let r_universe = self.infcx.universe_of_region(r); + if self.for_universe.can_name(r_universe) { + r + } else { + // FIXME: This is non-ideal because we don't give a + // very descriptive origin for this region variable. + self.infcx + .next_region_var_in_universe(MiscVariable(self.span), self.for_universe) + } } - } - - let r_universe = self.infcx.universe_of_region(r); - if self.for_universe.can_name(r_universe) { - return Ok(r); - } else { - // FIXME: This is non-ideal because we don't give a - // very descriptive origin for this region variable. - Ok(self.infcx.next_region_var_in_universe(MiscVariable(self.span), self.for_universe)) - } + r => bug!("unexpected region: {r:?}"), + }) } #[instrument(level = "debug", skip(self), ret)] diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs index c5ef48fe3da25..b4b089279f6dc 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs @@ -82,13 +82,16 @@ pub fn find_param_with_region<'tcx>( // May return None; sometimes the tables are not yet populated. let ty = fn_sig.inputs()[index]; let mut found_anon_region = false; - let new_param_ty = tcx.fold_regions(ty, |r, _| { - if r == anon_region { - found_anon_region = true; - replace_region - } else { - r + let new_param_ty = tcx.fold_regions(ty, |r, _| match r.kind() { + ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => { + if r == anon_region { + found_anon_region = true; + replace_region + } else { + r + } } + r => bug!("unexpected region: {r:?}"), }); found_anon_region.then(|| { let ty_hir_id = fn_decl.inputs[index].hir_id; diff --git a/compiler/rustc_infer/src/infer/freshen.rs b/compiler/rustc_infer/src/infer/freshen.rs index d89f63e5c53e9..6b16932797046 100644 --- a/compiler/rustc_infer/src/infer/freshen.rs +++ b/compiler/rustc_infer/src/infer/freshen.rs @@ -110,11 +110,8 @@ impl<'a, 'tcx> TypeFolder> for TypeFreshener<'a, 'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { - ty::ReLateBound(..) => { - // leave bound regions alone - r - } - + // All region variants occur in this match. + ty::ReLateBound(..) => r, // leave bound regions alone ty::ReEarlyBound(..) | ty::ReFree(_) | ty::ReVar(_) diff --git a/compiler/rustc_infer/src/infer/fudge.rs b/compiler/rustc_infer/src/infer/fudge.rs index 86c2c2be4a805..c2a244e1381f4 100644 --- a/compiler/rustc_infer/src/infer/fudge.rs +++ b/compiler/rustc_infer/src/infer/fudge.rs @@ -220,12 +220,19 @@ impl<'a, 'tcx> TypeFolder> for InferenceFudger<'a, 'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - if let ty::ReVar(vid) = *r && self.region_vars.0.contains(&vid) { - let idx = vid.index() - self.region_vars.0.start.index(); - let origin = self.region_vars.1[idx]; - return self.infcx.next_region_var(origin); + match r.kind() { + ty::ReVar(vid) if self.region_vars.0.contains(&vid) => { + let idx = vid.index() - self.region_vars.0.start.index(); + let origin = self.region_vars.1[idx]; + return self.infcx.next_region_var(origin); + } + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) => r, + r => bug!("unexpected region: {r:?}"), } - r } fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs index f298b95ca35b1..c48fd1ec31092 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs @@ -989,7 +989,18 @@ impl<'tcx> LexicalRegionResolutions<'tcx> { where T: TypeFoldable>, { - tcx.fold_regions(value, |r, _db| self.resolve_region(tcx, r)) + tcx.fold_regions(value, |r, _db| { + match r.kind() { + ty::ReEarlyBound(_) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReError(_) => {} + r => bug!("unexpected region: {r:?}"), + } + self.resolve_region(tcx, r) + }) } fn value(&self, rid: RegionVid) -> &VarValue<'tcx> { diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index 3a0a0494a7ed3..7fe6333687247 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -57,7 +57,19 @@ impl<'tcx> InferCtxt<'tcx> { }; let value = value.fold_with(&mut BottomUpFolder { tcx: self.tcx, - lt_op: |lt| lt, + lt_op: |lt| { + // All region variants occur in this match. + match lt.kind() { + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReErased + | ty::ReError(_) => lt, + } + }, ct_op: |ct| ct, ty_op: |ty| match *ty.kind() { ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) @@ -580,7 +592,10 @@ impl<'tcx> InferCtxt<'tcx> { ) if def_id.to_def_id() == def_id2 && substs == substs2 => hidden_ty, _ => ty, }, - lt_op: |lt| lt, + lt_op: |lt| { + // All region variants occur in this function. + lt + }, ct_op: |ct| ct, }); diff --git a/compiler/rustc_infer/src/infer/resolve.rs b/compiler/rustc_infer/src/infer/resolve.rs index 4f49f4165074f..4674f89f15874 100644 --- a/compiler/rustc_infer/src/infer/resolve.rs +++ b/compiler/rustc_infer/src/infer/resolve.rs @@ -91,7 +91,13 @@ impl<'a, 'tcx> TypeFolder> for OpportunisticRegionResolver<'a, 'tcx .borrow_mut() .unwrap_region_constraints() .opportunistic_resolve_var(TypeFolder::interner(self), vid), - _ => r, + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::RePlaceholder(_) + | ty::ReErased => r, + r => bug!("unexpected region: {r:?}"), } } @@ -238,7 +244,13 @@ impl<'a, 'tcx> FallibleTypeFolder> for FullTypeResolver<'a, 'tcx> { .as_ref() .expect("region resolution not performed") .resolve_region(self.infcx.tcx, r)), - _ => Ok(r), + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::RePlaceholder(_) + | ty::ReError(_) => Ok(r), + r => bug!("unexpected region: {r:?}"), } } diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs index f9d43fe220036..99efe9d7a44cd 100644 --- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs +++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs @@ -104,7 +104,13 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound { let proj_replacer = &mut BottomUpFolder { tcx: cx.tcx, ty_op: |ty| if ty == proj_ty { proj_term } else { ty }, - lt_op: |lt| lt, + lt_op: |lt| { + match lt.kind() { + ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {} + r => bug!("unexpected region: {r:?}"), + } + lt + }, ct_op: |ct| ct, }; // For example, in `impl Trait`, for all of the bounds on `Assoc`, diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index cfdf1dcf5c029..6b1cb473702db 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -415,7 +415,7 @@ impl<'tcx> ClosureOutlivesSubjectTy<'tcx> { ty::BoundRegion { var: ty::BoundVar::new(vid.index()), kind: ty::BrAnon(None) }; tcx.mk_re_late_bound(depth, br) } - _ => bug!("unexpected region in ClosureOutlivesSubjectTy: {r:?}"), + _ => bug!("unexpected region: {r:?}"), }); Self { inner } diff --git a/compiler/rustc_middle/src/ty/erase_regions.rs b/compiler/rustc_middle/src/ty/erase_regions.rs index 3837732483244..2b45444fae802 100644 --- a/compiler/rustc_middle/src/ty/erase_regions.rs +++ b/compiler/rustc_middle/src/ty/erase_regions.rs @@ -52,7 +52,7 @@ impl<'tcx> TypeFolder> for RegionEraserVisitor<'tcx> { } fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { - // because late-bound regions affect subtyping, we can't + // Because late-bound regions affect subtyping, we can't // erase the bound/free distinction, but we can replace // all free regions with 'erased. // @@ -61,8 +61,15 @@ impl<'tcx> TypeFolder> for RegionEraserVisitor<'tcx> { // away. In codegen, they will always be erased to 'erased // whenever a substitution occurs. match *r { + // All region variants occur in this match. ty::ReLateBound(..) => r, - _ => self.tcx.lifetimes.re_erased, + ty::ReEarlyBound(_) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReErased + | ty::ReError(_) => self.tcx.lifetimes.re_erased, } } } diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 203e16bea27fe..9bc235e7f0d14 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -116,11 +116,19 @@ impl<'a, 'tcx> TypeFolder> for RegionFolder<'a, 'tcx> { #[instrument(skip(self), level = "debug", ret)] fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { + // All region variants occur in this match. ty::ReLateBound(debruijn, _) if debruijn < self.current_index => { debug!(?self.current_index, "skipped bound region"); r } - _ => { + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReErased + | ty::ReError(_) => { debug!(?self.current_index, "folding free region"); (self.fold_region_fn)(r, self.current_index) } @@ -204,6 +212,7 @@ where fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { + // All region variants occur in this match. ty::ReLateBound(debruijn, br) if debruijn == self.current_index => { let region = self.delegate.replace_region(br); if let ty::ReLateBound(debruijn1, br) = *region { @@ -217,7 +226,14 @@ where region } } - _ => r, + ty::ReEarlyBound(..) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::RePlaceholder(_) + | ty::ReErased + | ty::ReError(_) => r, } } @@ -452,7 +468,8 @@ impl<'tcx> TypeFolder> for Shifter<'tcx> { let debruijn = debruijn.shifted_in(self.amount); self.tcx.mk_re_late_bound(debruijn, br) } - _ => r, + ty::ReEarlyBound(_) | ty::ReLateBound(..) | ty::ReStatic => r, + r => bug!("unexpected region: {r:?}"), } } diff --git a/compiler/rustc_middle/src/ty/opaque_types.rs b/compiler/rustc_middle/src/ty/opaque_types.rs index 751f3066c9cc6..b33c9c65e0bcc 100644 --- a/compiler/rustc_middle/src/ty/opaque_types.rs +++ b/compiler/rustc_middle/src/ty/opaque_types.rs @@ -114,12 +114,10 @@ impl<'tcx> TypeFolder> for ReverseMapper<'tcx> { // The regions that we expect from borrow checking. ty::ReEarlyBound(_) | ty::ReFree(_) => {} - ty::RePlaceholder(_) | ty::ReVar(_) => { - // All of the regions in the type should either have been - // erased by writeback, or mapped back to named regions by - // borrow checking. - bug!("unexpected region kind in opaque type: {:?}", r); - } + // All of the regions in the type should either have been + // erased by writeback, or mapped back to named regions by + // borrow checking. + ty::RePlaceholder(_) | ty::ReVar(_) => bug!("unexpected region: {r:?}"), } match self.map.get(&r.into()).map(|k| k.unpack()) { diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 72caadaf66199..95d42d6b3fd47 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2258,6 +2258,7 @@ impl<'a, 'tcx> ty::TypeFolder> for RegionFolder<'a, 'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { let name = &mut self.name; let region = match *r { + // All region variants occur in this match. ty::ReLateBound(db, br) if db >= self.current_index => { *self.region_map.entry(br).or_insert_with(|| name(Some(db), self.current_index, br)) } @@ -2279,7 +2280,13 @@ impl<'a, 'tcx> ty::TypeFolder> for RegionFolder<'a, 'tcx> { } } } - _ => return r, + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::ReVar(_) + | ty::ReErased + | ty::ReError(_) => return r, }; if let ty::ReLateBound(debruijn1, br) = *region { assert_eq!(debruijn1, ty::INNERMOST); diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index f05b873432d29..d8ce8e69c1b89 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -840,7 +840,8 @@ impl<'a, 'tcx> TypeFolder> for SubstFolder<'a, 'tcx> { None => region_param_out_of_range(data, self.substs), } } - _ => r, + ty::ReLateBound(..) | ty::ReFree(_) | ty::ReStatic | ty::ReErased | ty::ReError(_) => r, + r => bug!("unexpected region: {r:?}"), } } diff --git a/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs b/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs index 1a566e87dc8e3..1a2f95d0934db 100644 --- a/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs @@ -85,20 +85,20 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_auto_trait<'tcx>( } } -pub(in crate::solve) fn replace_erased_lifetimes_with_bound_vars<'tcx>( +fn replace_erased_lifetimes_with_bound_vars<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, ) -> ty::Binder<'tcx, Ty<'tcx>> { debug_assert!(!ty.has_late_bound_regions()); let mut counter = 0; - let ty = tcx.fold_regions(ty, |mut r, current_depth| { - if let ty::ReErased = r.kind() { + let ty = tcx.fold_regions(ty, |r, current_depth| match r.kind() { + ty::ReErased => { let br = ty::BoundRegion { var: ty::BoundVar::from_u32(counter), kind: ty::BrAnon(None) }; counter += 1; - r = tcx.mk_re_late_bound(current_depth, br); + tcx.mk_re_late_bound(current_depth, br) } - r + r => bug!("unexpected region: {r:?}"), }); let bound_vars = tcx.mk_bound_variable_kinds_from_iter( (0..counter).map(|_| ty::BoundVariableKind::Region(ty::BrAnon(None))), diff --git a/compiler/rustc_trait_selection/src/solve/canonicalize.rs b/compiler/rustc_trait_selection/src/solve/canonicalize.rs index 25e7439ece791..38f43c74d9ea3 100644 --- a/compiler/rustc_trait_selection/src/solve/canonicalize.rs +++ b/compiler/rustc_trait_selection/src/solve/canonicalize.rs @@ -242,7 +242,7 @@ impl<'tcx> TypeFolder> for Canonicalizer<'_, 'tcx> { } }, - ty::ReError(_) => return r, + r => bug!("unexpected region: {r:?}"), }; let existing_bound_var = match self.canonicalize_mode { diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 826fc63ca0653..263fc90a54f9c 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -776,7 +776,12 @@ impl<'tcx> TypeFolder> for BoundVarReplacer<'_, 'tcx> { self.mapped_regions.insert(p, br); self.infcx.tcx.mk_re_placeholder(p) } - _ => r, + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReFree(_) + | ty::ReStatic + | ty::RePlaceholder(_) => r, + r => bug!("unexpected region: {r:?}"), } } @@ -879,7 +884,12 @@ impl<'tcx> TypeFolder> for PlaceholderReplacer<'_, 'tcx> { .borrow_mut() .unwrap_region_constraints() .opportunistic_resolve_var(self.infcx.tcx, vid), - _ => r0, + ty::ReEarlyBound(_) + | ty::ReLateBound(..) + | ty::ReStatic + | ty::RePlaceholder(_) + | ty::ReErased => r0, + r => bug!("unexpected region: {r:?}"), }; let r2 = match *r1 { diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 6bb53418beabb..93db15e1276ad 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2459,7 +2459,13 @@ impl<'tcx> SelectionContext<'_, 'tcx> { let value = value.fold_with(&mut BottomUpFolder { tcx: self.tcx(), ty_op: |_| err, - lt_op: |l| l, + lt_op: |lt| { + match lt { + // This is never reached in practice. If it ever is reached, + // it should just return `ReErased`. + r => bug!("unexpected region: {r:?}"), + } + }, ct_op: |c| c, }); Normalized { value, obligations: vec![] } @@ -3007,16 +3013,16 @@ fn bind_generator_hidden_types_above<'tcx>( // Only remap erased regions if we use them. if considering_regions { - ty = tcx.fold_regions(ty, |mut r, current_depth| { - if let ty::ReErased = r.kind() { + ty = tcx.fold_regions(ty, |r, current_depth| match r.kind() { + ty::ReErased => { let br = ty::BoundRegion { var: ty::BoundVar::from_u32(counter), kind: ty::BrAnon(None), }; counter += 1; - r = tcx.mk_re_late_bound(current_depth, br); + tcx.mk_re_late_bound(current_depth, br) } - r + r => bug!("unexpected region: {r:?}"), }) } diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index 2be72879b7b13..cf40963e52986 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -1045,7 +1045,8 @@ impl<'a, 'tcx> TypeFolder> for NamedBoundVarSubstitutor<'a, 'tcx> { ty::BrEnv => unimplemented!(), ty::BrAnon(..) => {} }, - _ => (), + ty::ReLateBound(..) | ty::ReStatic => (), + r => bug!("unexpected region: {r:?}"), }; r.super_fold_with(self) @@ -1141,8 +1142,9 @@ impl<'tcx> TypeFolder> for ParamsSubstitutor<'tcx> { self.tcx.mk_re_late_bound(self.binder_index, br) } }, - - _ => r.super_fold_with(self), + // Other region kinds might need the same treatment here. + ty::ReLateBound(..) => r.super_fold_with(self), + r => bug!("unexpected region: {r:?}"), } } } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index cb06c7acff02a..1a9698eb0d38d 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -287,16 +287,18 @@ impl<'tcx> TypeVisitor> for ImplTraitInTraitFinder<'_, 'tcx> { // bounds of the RPITIT. Shift these binders back out when // constructing the top-level projection predicate. let shifted_alias_ty = self.tcx.fold_regions(unshifted_alias_ty, |re, depth| { - if let ty::ReLateBound(index, bv) = re.kind() { - if depth != ty::INNERMOST { - return self.tcx.mk_re_error_with_message( - DUMMY_SP, - "we shouldn't walk non-predicate binders with `impl Trait`...", - ); + match re.kind() { + ty::ReEarlyBound(_) => re, + ty::ReLateBound(index, bv) => { + if depth != ty::INNERMOST { + return self.tcx.mk_re_error_with_message( + DUMMY_SP, + "we shouldn't walk non-predicate binders with `impl Trait`...", + ); + } + self.tcx.mk_re_late_bound(index.shifted_out_to_binder(self.depth), bv) } - self.tcx.mk_re_late_bound(index.shifted_out_to_binder(self.depth), bv) - } else { - re + r => bug!("unexpected region: {r:?}"), } }); diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index f6088f4e64a41..c74b9803c18c9 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -742,7 +742,8 @@ impl<'a, 'tcx> TypeFolder> for RegionReplacer<'a, 'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned().unwrap_or(r), - _ => r, + ty::ReEarlyBound(_) | ty::ReStatic | ty::ReLateBound(..) => r, + r => bug!("unexpected region: {r:?}"), } } }