Skip to content

Commit 9d044a0

Browse files
committed
introduce LateParamRegionKind
1 parent 32ca4a6 commit 9d044a0

File tree

19 files changed

+157
-59
lines changed

19 files changed

+157
-59
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
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;

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/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
@@ -2340,8 +2340,11 @@ fn lint_redundant_lifetimes<'tcx>(
23402340
);
23412341
// If we are in a function, add its late-bound lifetimes too.
23422342
if matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
2343-
for var in tcx.fn_sig(owner_id).instantiate_identity().bound_vars() {
2343+
for (idx, var) in
2344+
tcx.fn_sig(owner_id).instantiate_identity().bound_vars().iter().enumerate()
2345+
{
23442346
let ty::BoundVariableKind::Region(kind) = var else { continue };
2347+
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
23452348
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
23462349
}
23472350
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
356356
ty::Region::new_late_param(
357357
tcx,
358358
scope.to_def_id(),
359-
ty::BoundRegionKind::Named(id.to_def_id(), name),
359+
ty::LateParamRegionKind::Named(id.to_def_id(), name),
360360
)
361361

362362
// (*) -- 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,7 @@ impl<'tcx> TyCtxt<'tcx> {
30923092
return ty::Region::new_late_param(
30933093
self,
30943094
new_parent.to_def_id(),
3095-
ty::BoundRegionKind::Named(
3095+
ty::LateParamRegionKind::Named(
30963096
lbv.to_def_id(),
30973097
self.item_name(lbv.to_def_id()),
30983098
),

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
}) => {

compiler/rustc_middle/src/ty/region.rs

+73-8
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ impl<'tcx> Region<'tcx> {
7070
pub fn new_late_param(
7171
tcx: TyCtxt<'tcx>,
7272
scope: DefId,
73-
bound_region: ty::BoundRegionKind,
73+
kind: LateParamRegionKind,
7474
) -> Region<'tcx> {
75-
tcx.intern_region(ty::ReLateParam(ty::LateParamRegion { scope, bound_region }))
75+
let data = LateParamRegion { scope, kind };
76+
tcx.intern_region(ty::ReLateParam(data))
7677
}
7778

7879
#[inline]
@@ -125,8 +126,8 @@ impl<'tcx> Region<'tcx> {
125126
match kind {
126127
ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
127128
ty::ReBound(debruijn, region) => Region::new_bound(tcx, debruijn, region),
128-
ty::ReLateParam(ty::LateParamRegion { scope, bound_region }) => {
129-
Region::new_late_param(tcx, scope, bound_region)
129+
ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
130+
Region::new_late_param(tcx, scope, kind)
130131
}
131132
ty::ReStatic => tcx.lifetimes.re_static,
132133
ty::ReVar(vid) => Region::new_var(tcx, vid),
@@ -166,7 +167,7 @@ impl<'tcx> Region<'tcx> {
166167
match *self {
167168
ty::ReEarlyParam(ebr) => Some(ebr.name),
168169
ty::ReBound(_, br) => br.kind.get_name(),
169-
ty::ReLateParam(fr) => fr.bound_region.get_name(),
170+
ty::ReLateParam(fr) => fr.kind.get_name(),
170171
ty::ReStatic => Some(kw::StaticLifetime),
171172
ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(),
172173
_ => None,
@@ -188,7 +189,7 @@ impl<'tcx> Region<'tcx> {
188189
match *self {
189190
ty::ReEarlyParam(ebr) => ebr.has_name(),
190191
ty::ReBound(_, br) => br.kind.is_named(),
191-
ty::ReLateParam(fr) => fr.bound_region.is_named(),
192+
ty::ReLateParam(fr) => fr.kind.is_named(),
192193
ty::ReStatic => true,
193194
ty::ReVar(..) => false,
194195
ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(),
@@ -311,7 +312,7 @@ impl<'tcx> Region<'tcx> {
311312
Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
312313
}
313314
ty::ReLateParam(ty::LateParamRegion {
314-
bound_region: ty::BoundRegionKind::Named(def_id, _),
315+
kind: ty::LateParamRegionKind::Named(def_id, _),
315316
..
316317
}) => Some(def_id),
317318
_ => None,
@@ -359,7 +360,71 @@ impl std::fmt::Debug for EarlyParamRegion {
359360
/// different parameters apart.
360361
pub struct LateParamRegion {
361362
pub scope: DefId,
362-
pub bound_region: BoundRegionKind,
363+
pub kind: LateParamRegionKind,
364+
}
365+
366+
/// When liberating bound regions, we map their [`BoundRegionKind`]
367+
/// to this as we need to track the index of anonymous regions. We
368+
/// otherwise end up liberating multiple bound regions to the same
369+
/// late-bound region.
370+
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
371+
#[derive(HashStable)]
372+
pub enum LateParamRegionKind {
373+
/// An anonymous region parameter for a given fn (&T)
374+
///
375+
/// Unlike [`BoundRegionKind::Anon`], this tracks the index of the
376+
/// liberated bound region.
377+
///
378+
/// We should ideally never liberate anonymous regions, but do so for the
379+
/// sake of diagnostics in `FnCtxt::sig_of_closure_with_expectation`.
380+
Anon(u32),
381+
382+
/// Named region parameters for functions (a in &'a T)
383+
///
384+
/// The `DefId` is needed to distinguish free regions in
385+
/// the event of shadowing.
386+
Named(DefId, Symbol),
387+
388+
/// Anonymous region for the implicit env pointer parameter
389+
/// to a closure
390+
ClosureEnv,
391+
}
392+
393+
impl LateParamRegionKind {
394+
pub fn from_bound(var: BoundVar, br: BoundRegionKind) -> LateParamRegionKind {
395+
match br {
396+
BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
397+
BoundRegionKind::Named(def_id, name) => LateParamRegionKind::Named(def_id, name),
398+
BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
399+
}
400+
}
401+
402+
pub fn is_named(&self) -> bool {
403+
match *self {
404+
LateParamRegionKind::Named(_, name) => {
405+
name != kw::UnderscoreLifetime && name != kw::Empty
406+
}
407+
_ => false,
408+
}
409+
}
410+
411+
pub fn get_name(&self) -> Option<Symbol> {
412+
if self.is_named() {
413+
match *self {
414+
LateParamRegionKind::Named(_, name) => return Some(name),
415+
_ => unreachable!(),
416+
}
417+
}
418+
419+
None
420+
}
421+
422+
pub fn get_id(&self) -> Option<DefId> {
423+
match *self {
424+
LateParamRegionKind::Named(id, _) => Some(id),
425+
_ => None,
426+
}
427+
}
363428
}
364429

365430
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]

compiler/rustc_middle/src/ty/structural_impls.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,23 @@ impl fmt::Debug for ty::BoundRegionKind {
7777

7878
impl fmt::Debug for ty::LateParamRegion {
7979
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80-
write!(f, "ReLateParam({:?}, {:?})", self.scope, self.bound_region)
80+
write!(f, "ReLateParam({:?}, {:?})", self.scope, self.kind)
81+
}
82+
}
83+
84+
impl fmt::Debug for ty::LateParamRegionKind {
85+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86+
match *self {
87+
ty::LateParamRegionKind::Anon(idx) => write!(f, "BrAnon({idx})"),
88+
ty::LateParamRegionKind::Named(did, name) => {
89+
if did.is_crate_root() {
90+
write!(f, "BrNamed({name})")
91+
} else {
92+
write!(f, "BrNamed({did:?}, {name})")
93+
}
94+
}
95+
ty::LateParamRegionKind::ClosureEnv => write!(f, "BrEnv"),
96+
}
8197
}
8298
}
8399

0 commit comments

Comments
 (0)