Skip to content

Commit 10ac87b

Browse files
committed
Auto merge of rust-lang#133961 - lcnr:borrowck-cleanup, r=<try>
cleanup region handling: add `LateParamRegionKind` The second commit is to enable a split between `BoundRegionKind` and `LateParamRegionKind`, by avoiding `BoundRegionKind` where it isn't necessary. The third comment then adds `LateParamRegionKind` to avoid having the same late-param region for separate bound regions. This fixes rust-lang#124021. r? `@compiler-errors`
2 parents bc145ce + 4086a6b commit 10ac87b

File tree

28 files changed

+288
-199
lines changed

28 files changed

+288
-199
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
189189
/// Returns `true` if a closure is inferred to be an `FnMut` closure.
190190
fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
191191
if let Some(ty::ReLateParam(late_param)) = self.to_error_region(fr).as_deref()
192-
&& let ty::BoundRegionKind::ClosureEnv = late_param.bound_region
192+
&& let ty::LateParamRegionKind::ClosureEnv = late_param.kind
193193
&& let DefiningTy::Closure(_, args) = self.regioncx.universal_regions().defining_ty
194194
{
195195
return args.as_closure().kind() == ty::ClosureKind::FnMut;
@@ -848,7 +848,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
848848
return;
849849
};
850850

851-
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.def_id);
851+
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
852852

853853
let param = if let Some(param) =
854854
find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
@@ -875,15 +875,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
875875
Some(arg),
876876
captures,
877877
Some((param.param_ty_span, param.param_ty.to_string())),
878-
Some(suitable_region.def_id),
878+
Some(suitable_region.scope),
879879
);
880880
return;
881881
}
882882

883883
let Some((alias_tys, alias_span, lt_addition_span)) = self
884884
.infcx
885885
.tcx
886-
.return_type_impl_or_dyn_traits_with_type_alias(suitable_region.def_id)
886+
.return_type_impl_or_dyn_traits_with_type_alias(suitable_region.scope)
887887
else {
888888
return;
889889
};
@@ -1018,18 +1018,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10181018
return;
10191019
};
10201020

1021-
let Some((ty_sub, _)) =
1022-
self.infcx.tcx.is_suitable_region(self.mir_def_id(), sub).and_then(|anon_reg| {
1023-
find_anon_type(self.infcx.tcx, self.mir_def_id(), sub, &anon_reg.bound_region)
1024-
})
1021+
let Some((ty_sub, _)) = self
1022+
.infcx
1023+
.tcx
1024+
.is_suitable_region(self.mir_def_id(), sub)
1025+
.and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sub))
10251026
else {
10261027
return;
10271028
};
10281029

1029-
let Some((ty_sup, _)) =
1030-
self.infcx.tcx.is_suitable_region(self.mir_def_id(), sup).and_then(|anon_reg| {
1031-
find_anon_type(self.infcx.tcx, self.mir_def_id(), sup, &anon_reg.bound_region)
1032-
})
1030+
let Some((ty_sup, _)) = self
1031+
.infcx
1032+
.tcx
1033+
.is_suitable_region(self.mir_def_id(), sup)
1034+
.and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sup))
10331035
else {
10341036
return;
10351037
};

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,17 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
300300
Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
301301
}
302302

303-
ty::ReLateParam(late_param) => match late_param.bound_region {
304-
ty::BoundRegionKind::Named(region_def_id, name) => {
303+
ty::ReLateParam(late_param) => match late_param.kind {
304+
ty::LateParamRegionKind::Named(region_def_id, name) => {
305305
// Get the span to point to, even if we don't use the name.
306306
let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
307307
debug!(
308308
"bound region named: {:?}, is_named: {:?}",
309309
name,
310-
late_param.bound_region.is_named()
310+
late_param.kind.is_named()
311311
);
312312

313-
if late_param.bound_region.is_named() {
313+
if late_param.kind.is_named() {
314314
// A named region that is actually named.
315315
Some(RegionName {
316316
name,
@@ -332,7 +332,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
332332
}
333333
}
334334

335-
ty::BoundRegionKind::ClosureEnv => {
335+
ty::LateParamRegionKind::ClosureEnv => {
336336
let def_ty = self.regioncx.universal_regions().defining_ty;
337337

338338
let closure_kind = match def_ty {
@@ -369,7 +369,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
369369
})
370370
}
371371

372-
ty::BoundRegionKind::Anon => None,
372+
ty::LateParamRegionKind::Anon(_) => None,
373373
},
374374

375375
ty::ReBound(..)

compiler/rustc_borrowck/src/lib.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ fn do_mir_borrowck<'tcx>(
141141
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
142142
let def = input_body.source.def_id().expect_local();
143143
let infcx = BorrowckInferCtxt::new(tcx, def);
144+
if let Some(e) = input_body.tainted_by_errors {
145+
infcx.set_tainted_by_errors(e);
146+
}
144147

145148
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
146149
for var_debug_info in &input_body.var_debug_info {
@@ -162,13 +165,6 @@ fn do_mir_borrowck<'tcx>(
162165
}
163166
}
164167

165-
let diags = &mut diags::BorrowckDiags::new();
166-
167-
// Gather the upvars of a closure, if any.
168-
if let Some(e) = input_body.tainted_by_errors {
169-
infcx.set_tainted_by_errors(e);
170-
}
171-
172168
// Replace all regions with fresh inference variables. This
173169
// requires first making our own copy of the MIR. This copy will
174170
// be modified (in place) to contain non-lexical lifetimes. It
@@ -224,6 +220,7 @@ fn do_mir_borrowck<'tcx>(
224220

225221
// We also have a `#[rustc_regions]` annotation that causes us to dump
226222
// information.
223+
let diags = &mut diags::BorrowckDiags::new();
227224
nll::dump_annotation(&infcx, body, &regioncx, &opt_closure_req, &opaque_type_values, diags);
228225

229226
let movable_coroutine =

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ impl<'tcx> RegionDefinition<'tcx> {
22302230
fn new(universe: ty::UniverseIndex, rv_origin: RegionVariableOrigin) -> Self {
22312231
// Create a new region definition. Note that, for free
22322232
// regions, the `external_name` field gets updated later in
2233-
// `init_universal_regions`.
2233+
// `init_free_and_bound_regions`.
22342234

22352235
let origin = match rv_origin {
22362236
RegionVariableOrigin::Nll(origin) => origin,

compiler/rustc_borrowck/src/universal_regions.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,9 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
843843
{
844844
let (value, _map) = self.tcx.instantiate_bound_regions(value, |br| {
845845
debug!(?br);
846+
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
846847
let liberated_region =
847-
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), br.kind);
848+
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), kind);
848849
let region_vid = {
849850
let name = match br.kind.get_name() {
850851
Some(name) => name,
@@ -942,12 +943,13 @@ fn for_each_late_bound_region_in_item<'tcx>(
942943
return;
943944
}
944945

945-
for bound_var in tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)) {
946-
let ty::BoundVariableKind::Region(bound_region) = bound_var else {
947-
continue;
948-
};
949-
let liberated_region =
950-
ty::Region::new_late_param(tcx, mir_def_id.to_def_id(), bound_region);
951-
f(liberated_region);
946+
for (idx, bound_var) in
947+
tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)).iter().enumerate()
948+
{
949+
if let ty::BoundVariableKind::Region(kind) = bound_var {
950+
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
951+
let liberated_region = ty::Region::new_late_param(tcx, mir_def_id.to_def_id(), kind);
952+
f(liberated_region);
953+
}
952954
}
953955
}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ fn compare_method_predicate_entailment<'tcx>(
430430
Ok(())
431431
}
432432

433-
struct RemapLateBound<'a, 'tcx> {
433+
struct RemapLateParam<'a, 'tcx> {
434434
tcx: TyCtxt<'tcx>,
435-
mapping: &'a FxIndexMap<ty::BoundRegionKind, ty::BoundRegionKind>,
435+
mapping: &'a FxIndexMap<ty::LateParamRegionKind, ty::LateParamRegionKind>,
436436
}
437437

438-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateBound<'_, 'tcx> {
438+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateParam<'_, 'tcx> {
439439
fn cx(&self) -> TyCtxt<'tcx> {
440440
self.tcx
441441
}
@@ -445,7 +445,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateBound<'_, 'tcx> {
445445
ty::Region::new_late_param(
446446
self.tcx,
447447
fr.scope,
448-
self.mapping.get(&fr.bound_region).copied().unwrap_or(fr.bound_region),
448+
self.mapping.get(&fr.kind).copied().unwrap_or(fr.kind),
449449
)
450450
} else {
451451
r

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,24 @@ fn report_mismatched_rpitit_signature<'tcx>(
289289
tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(),
290290
tcx.fn_sig(impl_m_def_id).skip_binder().bound_vars(),
291291
)
292-
.filter_map(|(impl_bv, trait_bv)| {
292+
.enumerate()
293+
.filter_map(|(idx, (impl_bv, trait_bv))| {
293294
if let ty::BoundVariableKind::Region(impl_bv) = impl_bv
294295
&& let ty::BoundVariableKind::Region(trait_bv) = trait_bv
295296
{
296-
Some((impl_bv, trait_bv))
297+
let var = ty::BoundVar::from_usize(idx);
298+
Some((
299+
ty::LateParamRegionKind::from_bound(var, impl_bv),
300+
ty::LateParamRegionKind::from_bound(var, trait_bv),
301+
))
297302
} else {
298303
None
299304
}
300305
})
301306
.collect();
302307

303308
let mut return_ty =
304-
trait_m_sig.output().fold_with(&mut super::RemapLateBound { tcx, mapping: &mapping });
309+
trait_m_sig.output().fold_with(&mut super::RemapLateParam { tcx, mapping: &mapping });
305310

306311
if tcx.asyncness(impl_m_def_id).is_async() && tcx.asyncness(trait_m_def_id).is_async() {
307312
let ty::Alias(ty::Projection, future_ty) = return_ty.kind() else {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2296,8 +2296,11 @@ fn lint_redundant_lifetimes<'tcx>(
22962296
);
22972297
// If we are in a function, add its late-bound lifetimes too.
22982298
if matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
2299-
for var in tcx.fn_sig(owner_id).instantiate_identity().bound_vars() {
2299+
for (idx, var) in
2300+
tcx.fn_sig(owner_id).instantiate_identity().bound_vars().iter().enumerate()
2301+
{
23002302
let ty::BoundVariableKind::Region(kind) = var else { continue };
2303+
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
23012304
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
23022305
}
23032306
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
349349
ty::Region::new_late_param(
350350
tcx,
351351
scope.to_def_id(),
352-
ty::BoundRegionKind::Named(id.to_def_id(), name),
352+
ty::LateParamRegionKind::Named(id.to_def_id(), name),
353353
)
354354

355355
// (*) -- not late-bound, won't change

compiler/rustc_lint/src/impl_trait_overcaptures.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ where
325325
ParamKind::Free(def_id, name) => ty::Region::new_late_param(
326326
self.tcx,
327327
self.parent_def_id.to_def_id(),
328-
ty::BoundRegionKind::Named(def_id, name),
328+
ty::LateParamRegionKind::Named(def_id, name),
329329
),
330330
// Totally ignore late bound args from binders.
331331
ParamKind::Late => return true,
@@ -475,7 +475,7 @@ fn extract_def_id_from_arg<'tcx>(
475475
)
476476
| ty::ReLateParam(ty::LateParamRegion {
477477
scope: _,
478-
bound_region: ty::BoundRegionKind::Named(def_id, ..),
478+
kind: ty::LateParamRegionKind::Named(def_id, ..),
479479
}) => def_id,
480480
_ => unreachable!(),
481481
},
@@ -544,7 +544,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
544544
)
545545
| ty::ReLateParam(ty::LateParamRegion {
546546
scope: _,
547-
bound_region: ty::BoundRegionKind::Named(def_id, ..),
547+
kind: ty::LateParamRegionKind::Named(def_id, ..),
548548
}) => def_id,
549549
_ => {
550550
return Ok(a);

compiler/rustc_middle/src/ty/context.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,10 @@ impl<'tcx> CommonConsts<'tcx> {
11151115
/// either a `ReEarlyParam` or `ReLateParam`.
11161116
#[derive(Debug)]
11171117
pub struct FreeRegionInfo {
1118-
/// `LocalDefId` of the free region.
1119-
pub def_id: LocalDefId,
1120-
/// the bound region corresponding to free region.
1121-
pub bound_region: ty::BoundRegionKind,
1118+
/// `LocalDefId` of the scope.
1119+
pub scope: LocalDefId,
1120+
/// the `DefId` of the free region.
1121+
pub region_def_id: DefId,
11221122
/// checks if bound region is in Impl Item
11231123
pub is_impl_item: bool,
11241124
}
@@ -1979,7 +1979,7 @@ impl<'tcx> TyCtxt<'tcx> {
19791979
generic_param_scope: LocalDefId,
19801980
mut region: Region<'tcx>,
19811981
) -> Option<FreeRegionInfo> {
1982-
let (suitable_region_binding_scope, bound_region) = loop {
1982+
let (suitable_region_binding_scope, region_def_id) = loop {
19831983
let def_id =
19841984
region.opt_param_def_id(self, generic_param_scope.to_def_id())?.as_local()?;
19851985
let scope = self.local_parent(def_id);
@@ -1989,10 +1989,7 @@ impl<'tcx> TyCtxt<'tcx> {
19891989
region = self.map_opaque_lifetime_to_parent_lifetime(def_id);
19901990
continue;
19911991
}
1992-
break (
1993-
scope,
1994-
ty::BoundRegionKind::Named(def_id.into(), self.item_name(def_id.into())),
1995-
);
1992+
break (scope, def_id.into());
19961993
};
19971994

19981995
let is_impl_item = match self.hir_node_by_def_id(suitable_region_binding_scope) {
@@ -2001,7 +1998,7 @@ impl<'tcx> TyCtxt<'tcx> {
20011998
_ => false,
20021999
};
20032000

2004-
Some(FreeRegionInfo { def_id: suitable_region_binding_scope, bound_region, is_impl_item })
2001+
Some(FreeRegionInfo { scope: suitable_region_binding_scope, region_def_id, is_impl_item })
20052002
}
20062003

20072004
/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in its return type.
@@ -3101,7 +3098,7 @@ impl<'tcx> TyCtxt<'tcx> {
31013098
return ty::Region::new_late_param(
31023099
self,
31033100
new_parent.to_def_id(),
3104-
ty::BoundRegionKind::Named(
3101+
ty::LateParamRegionKind::Named(
31053102
lbv.to_def_id(),
31063103
self.item_name(lbv.to_def_id()),
31073104
),

compiler/rustc_middle/src/ty/fold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ impl<'tcx> TyCtxt<'tcx> {
270270
T: TypeFoldable<TyCtxt<'tcx>>,
271271
{
272272
self.instantiate_bound_regions_uncached(value, |br| {
273-
ty::Region::new_late_param(self, all_outlive_scope, br.kind)
273+
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
274+
ty::Region::new_late_param(self, all_outlive_scope, kind)
274275
})
275276
}
276277

compiler/rustc_middle/src/ty/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub use self::predicate::{
8484
TypeOutlivesPredicate,
8585
};
8686
pub use self::region::{
87-
BoundRegion, BoundRegionKind, EarlyParamRegion, LateParamRegion, Region, RegionKind, RegionVid,
87+
BoundRegion, BoundRegionKind, EarlyParamRegion, LateParamRegion, LateParamRegionKind, Region,
88+
RegionKind, RegionVid,
8889
};
8990
pub use self::rvalue_scopes::RvalueScopes;
9091
pub use self::sty::{

compiler/rustc_middle/src/ty/print/pretty.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2375,8 +2375,8 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
23752375
match *region {
23762376
ty::ReEarlyParam(ref data) => data.has_name(),
23772377

2378+
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => kind.is_named(),
23782379
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
2379-
| ty::ReLateParam(ty::LateParamRegion { bound_region: br, .. })
23802380
| ty::RePlaceholder(ty::Placeholder {
23812381
bound: ty::BoundRegion { kind: br, .. }, ..
23822382
}) => {
@@ -2449,8 +2449,13 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
24492449
return Ok(());
24502450
}
24512451
}
2452+
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => {
2453+
if let Some(name) = kind.get_name() {
2454+
p!(write("{}", name));
2455+
return Ok(());
2456+
}
2457+
}
24522458
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
2453-
| ty::ReLateParam(ty::LateParamRegion { bound_region: br, .. })
24542459
| ty::RePlaceholder(ty::Placeholder {
24552460
bound: ty::BoundRegion { kind: br, .. }, ..
24562461
}) => {

0 commit comments

Comments
 (0)