Skip to content

Commit 9509348

Browse files
authored
Rollup merge of #98576 - lcnr:region-stuff-cool-beans, r=jackh726
small regions refactoring these commits should be fairly self-contained r? rust-lang/types
2 parents 4f61fe2 + 28fafc4 commit 9509348

File tree

21 files changed

+38
-59
lines changed

21 files changed

+38
-59
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
879879
}
880880

881881
let mut found = false;
882-
tcx.fold_regions(tcx.type_of(body_parent_did), &mut true, |r: ty::Region<'tcx>, _| {
882+
tcx.fold_regions(tcx.type_of(body_parent_did), |r: ty::Region<'tcx>, _| {
883883
if *r == ty::ReEarlyBound(region) {
884884
found = true;
885885
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10091009

10101010
debug!("try_promote_type_test_subject(ty = {:?})", ty);
10111011

1012-
let ty = tcx.fold_regions(ty, &mut false, |r, _depth| {
1012+
let ty = tcx.fold_regions(ty, |r, _depth| {
10131013
let region_vid = self.to_region_vid(r);
10141014

10151015
// The challenge if this. We have some region variable `r`
@@ -1289,7 +1289,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
12891289
where
12901290
T: TypeFoldable<'tcx>,
12911291
{
1292-
tcx.fold_regions(value, &mut false, |r, _db| {
1292+
tcx.fold_regions(value, |r, _db| {
12931293
let vid = self.to_region_vid(r);
12941294
let scc = self.constraint_sccs.scc(vid);
12951295
let repr = self.scc_representatives[scc];

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
5959
debug!(?concrete_type, ?substs);
6060

6161
let mut subst_regions = vec![self.universal_regions.fr_static];
62-
let universal_substs = infcx.tcx.fold_regions(substs, &mut false, |region, _| {
62+
let universal_substs = infcx.tcx.fold_regions(substs, |region, _| {
6363
if let ty::RePlaceholder(..) = region.kind() {
6464
// Higher kinded regions don't need remapping, they don't refer to anything outside of this the substs.
6565
return region;
@@ -91,7 +91,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9191
subst_regions.dedup();
9292

9393
let universal_concrete_type =
94-
infcx.tcx.fold_regions(concrete_type, &mut false, |region, _| match *region {
94+
infcx.tcx.fold_regions(concrete_type, |region, _| match *region {
9595
ty::ReVar(vid) => subst_regions
9696
.iter()
9797
.find(|ur_vid| self.eval_equal(vid, **ur_vid))
@@ -146,7 +146,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
146146
where
147147
T: TypeFoldable<'tcx>,
148148
{
149-
tcx.fold_regions(ty, &mut false, |region, _| match *region {
149+
tcx.fold_regions(ty, |region, _| match *region {
150150
ty::ReVar(vid) => {
151151
// Find something that we can name
152152
let upper_bound = self.approx_universal_upper_bound(vid);

compiler/rustc_borrowck/src/renumber.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
3131
where
3232
T: TypeFoldable<'tcx>,
3333
{
34-
infcx.tcx.fold_regions(value, &mut false, |_region, _depth| {
34+
infcx.tcx.fold_regions(value, |_region, _depth| {
3535
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
3636
infcx.next_nll_region_var(origin)
3737
})

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
2323
tcx: TyCtxt<'tcx>,
2424
universal_regions: &'a UniversalRegions<'tcx>,
2525
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
26-
implicit_region_bound: Option<ty::Region<'tcx>>,
26+
implicit_region_bound: ty::Region<'tcx>,
2727
param_env: ty::ParamEnv<'tcx>,
2828
locations: Locations,
2929
span: Span,
@@ -36,7 +36,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
3636
infcx: &'a InferCtxt<'a, 'tcx>,
3737
universal_regions: &'a UniversalRegions<'tcx>,
3838
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
39-
implicit_region_bound: Option<ty::Region<'tcx>>,
39+
implicit_region_bound: ty::Region<'tcx>,
4040
param_env: ty::ParamEnv<'tcx>,
4141
locations: Locations,
4242
span: Span,
@@ -108,7 +108,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
108108
// create new region variables, which can't be done later when
109109
// verifying these bounds.
110110
if t1.has_placeholders() {
111-
t1 = tcx.fold_regions(t1, &mut false, |r, _| match *r {
111+
t1 = tcx.fold_regions(t1, |r, _| match *r {
112112
ty::RePlaceholder(placeholder) => {
113113
self.constraints.placeholder_region(self.infcx, placeholder)
114114
}
@@ -120,7 +120,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
120120
&mut *self,
121121
tcx,
122122
region_bound_pairs,
123-
implicit_region_bound,
123+
Some(implicit_region_bound),
124124
param_env,
125125
)
126126
.type_must_outlive(origin, t1, r2);

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) struct CreateResult<'tcx> {
6161
pub(crate) fn create<'tcx>(
6262
infcx: &InferCtxt<'_, 'tcx>,
6363
param_env: ty::ParamEnv<'tcx>,
64-
implicit_region_bound: Option<ty::Region<'tcx>>,
64+
implicit_region_bound: ty::Region<'tcx>,
6565
universal_regions: &Rc<UniversalRegions<'tcx>>,
6666
constraints: &mut MirTypeckRegionConstraints<'tcx>,
6767
) -> CreateResult<'tcx> {
@@ -223,7 +223,7 @@ struct UniversalRegionRelationsBuilder<'this, 'tcx> {
223223
infcx: &'this InferCtxt<'this, 'tcx>,
224224
param_env: ty::ParamEnv<'tcx>,
225225
universal_regions: Rc<UniversalRegions<'tcx>>,
226-
implicit_region_bound: Option<ty::Region<'tcx>>,
226+
implicit_region_bound: ty::Region<'tcx>,
227227
constraints: &'this mut MirTypeckRegionConstraints<'tcx>,
228228

229229
// outputs:

compiler/rustc_borrowck/src/type_check/input_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
230230
self.infcx,
231231
&self.borrowck_context.universal_regions,
232232
&self.region_bound_pairs,
233-
Some(self.implicit_region_bound),
233+
self.implicit_region_bound,
234234
self.param_env,
235235
Locations::All(DUMMY_SP),
236236
DUMMY_SP,

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
157157
} = free_region_relations::create(
158158
infcx,
159159
param_env,
160-
Some(implicit_region_bound),
160+
implicit_region_bound,
161161
universal_regions,
162162
&mut constraints,
163163
);
@@ -1142,7 +1142,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11421142
self.infcx,
11431143
self.borrowck_context.universal_regions,
11441144
self.region_bound_pairs,
1145-
Some(self.implicit_region_bound),
1145+
self.implicit_region_bound,
11461146
self.param_env,
11471147
locations,
11481148
locations.span(self.body),

compiler/rustc_borrowck/src/universal_regions.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
725725
where
726726
T: TypeFoldable<'tcx>,
727727
{
728-
self.tcx.fold_regions(value, &mut false, |_region, _depth| self.next_nll_region_var(origin))
728+
self.tcx.fold_regions(value, |_region, _depth| self.next_nll_region_var(origin))
729729
}
730730

731731
#[instrument(level = "debug", skip(self, indices))]
@@ -817,9 +817,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
817817
where
818818
T: TypeFoldable<'tcx>,
819819
{
820-
tcx.fold_regions(value, &mut false, |region, _| {
821-
tcx.mk_region(ty::ReVar(self.to_region_vid(region)))
822-
})
820+
tcx.fold_regions(value, |region, _| tcx.mk_region(ty::ReVar(self.to_region_vid(region))))
823821
}
824822
}
825823

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn find_param_with_region<'tcx>(
7979
// May return None; sometimes the tables are not yet populated.
8080
let ty = fn_sig.inputs()[index];
8181
let mut found_anon_region = false;
82-
let new_param_ty = tcx.fold_regions(ty, &mut false, |r, _| {
82+
let new_param_ty = tcx.fold_regions(ty, |r, _| {
8383
if r == anon_region {
8484
found_anon_region = true;
8585
replace_region

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
868868
where
869869
T: TypeFoldable<'tcx>,
870870
{
871-
tcx.fold_regions(value, &mut false, |r, _db| match *r {
871+
tcx.fold_regions(value, |r, _db| match *r {
872872
ty::ReVar(rid) => self.resolve_var(rid),
873873
_ => r,
874874
})

compiler/rustc_infer/src/infer/outlives/obligations.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,13 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
141141
/// `('a, K)` in this list tells us that the bounds in scope
142142
/// indicate that `K: 'a`, where `K` is either a generic
143143
/// parameter like `T` or a projection like `T::Item`.
144-
/// - `implicit_region_bound`: if some, this is a region bound
145-
/// that is considered to hold for all type parameters (the
146-
/// function body).
147144
/// - `param_env` is the parameter environment for the enclosing function.
148145
/// - `body_id` is the body-id whose region obligations are being
149146
/// processed.
150147
#[instrument(level = "debug", skip(self, region_bound_pairs_map))]
151148
pub fn process_registered_region_obligations(
152149
&self,
153150
region_bound_pairs_map: &FxHashMap<hir::HirId, RegionBoundPairs<'tcx>>,
154-
implicit_region_bound: Option<ty::Region<'tcx>>,
155151
param_env: ty::ParamEnv<'tcx>,
156152
) {
157153
assert!(
@@ -170,13 +166,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
170166
let sup_type = self.resolve_vars_if_possible(sup_type);
171167

172168
if let Some(region_bound_pairs) = region_bound_pairs_map.get(&body_id) {
173-
let outlives = &mut TypeOutlives::new(
174-
self,
175-
self.tcx,
176-
&region_bound_pairs,
177-
implicit_region_bound,
178-
param_env,
179-
);
169+
let outlives =
170+
&mut TypeOutlives::new(self, self.tcx, &region_bound_pairs, None, param_env);
180171
outlives.type_must_outlive(origin, sup_type, sub_region);
181172
} else {
182173
self.tcx.sess.delay_span_bug(

compiler/rustc_infer/src/infer/outlives/verify.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
1616
pub struct VerifyBoundCx<'cx, 'tcx> {
1717
tcx: TyCtxt<'tcx>,
1818
region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
19+
/// During borrowck, if there are no outlives bounds on a generic
20+
/// parameter `T`, we assume that `T: 'in_fn_body` holds.
21+
///
22+
/// Outside of borrowck the only way to prove `T: '?0` is by
23+
/// setting `'?0` to `'empty`.
1924
implicit_region_bound: Option<ty::Region<'tcx>>,
2025
param_env: ty::ParamEnv<'tcx>,
2126
}
@@ -263,8 +268,8 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
263268
// fn foo<'a, A>(x: &'a A) { x.bar() }
264269
//
265270
// The problem is that the type of `x` is `&'a A`. To be
266-
// well-formed, then, A must be lower-generic by `'a`, but we
267-
// don't know that this holds from first principles.
271+
// well-formed, then, A must outlive `'a`, but we don't know that
272+
// this holds from first principles.
268273
let from_region_bound_pairs = self.region_bound_pairs.iter().filter_map(|&(r, p)| {
269274
debug!(
270275
"declared_generic_bounds_from_env_for_erased_ty: region_bound_pair = {:?}",

compiler/rustc_middle/src/ty/fold.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,12 @@ impl<'tcx> TyCtxt<'tcx> {
465465
pub fn fold_regions<T>(
466466
self,
467467
value: T,
468-
skipped_regions: &mut bool,
469468
mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
470469
) -> T
471470
where
472471
T: TypeFoldable<'tcx>,
473472
{
474-
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
473+
value.fold_with(&mut RegionFolder::new(self, &mut f))
475474
}
476475

477476
/// Invoke `callback` on every region appearing free in `value`.
@@ -579,7 +578,6 @@ impl<'tcx> TyCtxt<'tcx> {
579578
580579
pub struct RegionFolder<'a, 'tcx> {
581580
tcx: TyCtxt<'tcx>,
582-
skipped_regions: &'a mut bool,
583581

584582
/// Stores the index of a binder *just outside* the stuff we have
585583
/// visited. So this begins as INNERMOST; when we pass through a
@@ -597,10 +595,9 @@ impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
597595
#[inline]
598596
pub fn new(
599597
tcx: TyCtxt<'tcx>,
600-
skipped_regions: &'a mut bool,
601598
fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
602599
) -> RegionFolder<'a, 'tcx> {
603-
RegionFolder { tcx, skipped_regions, current_index: ty::INNERMOST, fold_region_fn }
600+
RegionFolder { tcx, current_index: ty::INNERMOST, fold_region_fn }
604601
}
605602
}
606603

@@ -624,7 +621,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
624621
match *r {
625622
ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
626623
debug!(?self.current_index, "skipped bound region");
627-
*self.skipped_regions = true;
628624
r
629625
}
630626
_ => {

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
220220
.map(|&(id, _)| (id, vec![]))
221221
.collect();
222222

223-
infcx.process_registered_region_obligations(&body_id_map, None, full_env);
223+
infcx.process_registered_region_obligations(&body_id_map, full_env);
224224

225225
let region_data = infcx
226226
.inner

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,7 @@ fn resolve_negative_obligation<'cx, 'tcx>(
407407
// function bodies with closures).
408408
outlives_env.save_implied_bounds(CRATE_HIR_ID);
409409

410-
infcx.process_registered_region_obligations(
411-
outlives_env.region_bound_pairs_map(),
412-
Some(tcx.lifetimes.re_root_empty),
413-
param_env,
414-
);
410+
infcx.process_registered_region_obligations(outlives_env.region_bound_pairs_map(), param_env);
415411

416412
let errors = infcx.resolve_regions(region_context, &outlives_env);
417413

compiler/rustc_typeck/src/check/generator_interior.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ pub fn resolve_interior<'a, 'tcx>(
225225
// Note that each region slot in the types gets a new fresh late bound region,
226226
// which means that none of the regions inside relate to any other, even if
227227
// typeck had previously found constraints that would cause them to be related.
228-
let folded = fcx.tcx.fold_regions(erased, &mut false, |_, current_depth| {
228+
let folded = fcx.tcx.fold_regions(erased, |_, current_depth| {
229229
let br = ty::BoundRegion {
230230
var: ty::BoundVar::from_u32(counter),
231231
kind: ty::BrAnon(counter),

compiler/rustc_typeck/src/check/regionck.rs

-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
366366
fn resolve_regions_and_report_errors(&self) {
367367
self.infcx.process_registered_region_obligations(
368368
self.outlives_environment.region_bound_pairs_map(),
369-
Some(self.tcx.lifetimes.re_root_empty),
370369
self.param_env,
371370
);
372371

compiler/rustc_typeck/src/check/wfcheck.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,7 @@ fn ty_known_to_outlive<'tcx>(
596596
) -> bool {
597597
resolve_regions_with_wf_tys(tcx, id, param_env, &wf_tys, |infcx, region_bound_pairs| {
598598
let origin = infer::RelateParamBound(DUMMY_SP, ty, None);
599-
let outlives = &mut TypeOutlives::new(
600-
infcx,
601-
tcx,
602-
region_bound_pairs,
603-
Some(infcx.tcx.lifetimes.re_root_empty),
604-
param_env,
605-
);
599+
let outlives = &mut TypeOutlives::new(infcx, tcx, region_bound_pairs, None, param_env);
606600
outlives.type_must_outlive(origin, ty, region);
607601
})
608602
}

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
393393
}
394394

395395
fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
396-
let ty = self.tcx.fold_regions(ty, &mut false, |r, _| match *r {
396+
let ty = self.tcx.fold_regions(ty, |r, _| match *r {
397397
ty::ReErased => self.tcx.lifetimes.re_static,
398398
_ => r,
399399
});
@@ -1917,7 +1917,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
19171917
Some(ty) => {
19181918
let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
19191919
// Typeck doesn't expect erased regions to be returned from `type_of`.
1920-
let fn_sig = tcx.fold_regions(fn_sig, &mut false, |r, _| match *r {
1920+
let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r {
19211921
ty::ReErased => tcx.lifetimes.re_static,
19221922
_ => r,
19231923
});

compiler/rustc_typeck/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ fn infer_placeholder_type<'a>(
772772
}
773773

774774
// Typeck doesn't expect erased regions to be returned from `type_of`.
775-
tcx.fold_regions(ty, &mut false, |r, _| match *r {
775+
tcx.fold_regions(ty, |r, _| match *r {
776776
ty::ReErased => tcx.lifetimes.re_static,
777777
_ => r,
778778
})

0 commit comments

Comments
 (0)