Skip to content

Commit 8216b7f

Browse files
committed
Make some region folders a little stricter.
Because certain regions cannot occur in them.
1 parent 458d4da commit 8216b7f

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1576,17 +1576,10 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
15761576
&& let hir::OpaqueTyOrigin::FnReturn(source) | hir::OpaqueTyOrigin::AsyncFn(source) = opaque.origin
15771577
&& source == self.fn_def_id
15781578
{
1579-
let opaque_ty = tcx.fold_regions(unshifted_opaque_ty, |re, depth| {
1580-
if let ty::ReLateBound(index, bv) = re.kind() {
1581-
if depth != ty::INNERMOST {
1582-
return tcx.mk_re_error_with_message(
1583-
DUMMY_SP,
1584-
"we shouldn't walk non-predicate binders with `impl Trait`...",
1585-
);
1586-
}
1587-
tcx.mk_re_late_bound(index.shifted_out_to_binder(self.depth), bv)
1588-
} else {
1589-
re
1579+
let opaque_ty = tcx.fold_regions(unshifted_opaque_ty, |re, _depth| {
1580+
match re.kind() {
1581+
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReError(_) => re,
1582+
r => bug!("unexpected region: {r:?}"),
15901583
}
15911584
});
15921585
for (bound, bound_span) in tcx

compiler/rustc_hir_analysis/src/collect.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
386386

387387
fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
388388
let ty = self.tcx.fold_regions(ty, |r, _| match *r {
389-
ty::ReErased => self.tcx.lifetimes.re_static,
390-
_ => r,
389+
// This is never reached in practice. If it ever is reached,
390+
// `ReErased` should be changed to `ReStatic`, and any other region
391+
// left alone.
392+
r => bug!("unexpected region: {r:?}"),
391393
});
392394
self.tcx().const_error_with_message(ty, span, "bad placeholder constant")
393395
}

compiler/rustc_middle/src/ty/subst.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,13 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for SubstFolder<'a, 'tcx> {
829829
None => region_param_out_of_range(data, self.substs),
830830
}
831831
}
832-
_ => r,
832+
ty::ReLateBound(..)
833+
| ty::ReFree(_)
834+
| ty::ReStatic
835+
| ty::RePlaceholder(_)
836+
| ty::ReErased
837+
| ty::ReError(_) => r,
838+
ty::ReVar(_) => bug!("unexpected region: {r:?}"),
833839
}
834840
}
835841

compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ pub(in crate::solve) fn replace_erased_lifetimes_with_bound_vars<'tcx>(
9191
) -> ty::Binder<'tcx, Ty<'tcx>> {
9292
debug_assert!(!ty.has_late_bound_regions());
9393
let mut counter = 0;
94-
let ty = tcx.fold_regions(ty, |mut r, current_depth| {
95-
if let ty::ReErased = r.kind() {
94+
let ty = tcx.fold_regions(ty, |r, current_depth| match r.kind() {
95+
ty::ReErased => {
9696
let br =
9797
ty::BoundRegion { var: ty::BoundVar::from_u32(counter), kind: ty::BrAnon(None) };
9898
counter += 1;
99-
r = tcx.mk_re_late_bound(current_depth, br);
99+
tcx.mk_re_late_bound(current_depth, br)
100100
}
101-
r
101+
// All free regions should be erased here.
102+
r => bug!("unexpected region: {r:?}"),
102103
});
103104
let bound_vars = tcx.mk_bound_variable_kinds_from_iter(
104105
(0..counter).map(|_| ty::BoundVariableKind::Region(ty::BrAnon(None))),

compiler/rustc_trait_selection/src/traits/select/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3006,16 +3006,16 @@ fn bind_generator_hidden_types_above<'tcx>(
30063006

30073007
// Only remap erased regions if we use them.
30083008
if considering_regions {
3009-
ty = tcx.fold_regions(ty, |mut r, current_depth| {
3010-
if let ty::ReErased = r.kind() {
3009+
ty = tcx.fold_regions(ty, |r, current_depth| match r.kind() {
3010+
ty::ReErased => {
30113011
let br = ty::BoundRegion {
30123012
var: ty::BoundVar::from_u32(counter),
30133013
kind: ty::BrAnon(None),
30143014
};
30153015
counter += 1;
3016-
r = tcx.mk_re_late_bound(current_depth, br);
3016+
tcx.mk_re_late_bound(current_depth, br)
30173017
}
3018-
r
3018+
r => bug!("unexpected region: {r:?}"),
30193019
})
30203020
}
30213021

src/librustdoc/clean/auto_trait.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,10 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for RegionReplacer<'a, 'tcx> {
741741

742742
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
743743
match *r {
744+
// These are the regions that can be seen in the AST.
744745
ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned().unwrap_or(r),
745-
_ => r,
746+
ty::ReEarlyBound(_) | ty::ReStatic | ty::ReLateBound(..) | ty::ReError(_) => r,
747+
r => bug!("unexpected region: {r:?}"),
746748
}
747749
}
748750
}

0 commit comments

Comments
 (0)