Skip to content

Commit fbedbcd

Browse files
committed
Remove DefId from EarlyParamRegion (type system)
1 parent 9fe7297 commit fbedbcd

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,9 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
538538
// the cases that were stabilized with the `impl_trait_projection`
539539
// feature -- see <https://github.com/rust-lang/rust/pull/115659>.
540540
if let DefKind::LifetimeParam = tcx.def_kind(def_id)
541-
&& let ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
542-
| ty::ReLateParam(ty::LateParamRegion {
543-
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
544-
..
545-
}) = *tcx.map_opaque_lifetime_to_parent_lifetime(def_id.expect_local())
541+
&& let Some(def_id) = tcx
542+
.map_opaque_lifetime_to_parent_lifetime(def_id.expect_local())
543+
.opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id()))
546544
{
547545
shadowed_captures.insert(def_id);
548546
}
@@ -585,12 +583,9 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
585583
// Check if the lifetime param was captured but isn't named in the precise captures list.
586584
if variances[param.index as usize] == ty::Invariant {
587585
if let DefKind::OpaqueTy = tcx.def_kind(tcx.parent(param.def_id))
588-
&& let ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
589-
| ty::ReLateParam(ty::LateParamRegion {
590-
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
591-
..
592-
}) = *tcx
586+
&& let Some(def_id) = tcx
593587
.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local())
588+
.opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id()))
594589
{
595590
tcx.dcx().emit_err(errors::LifetimeNotCaptured {
596591
opaque_span,

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
876876
ty::ReLateParam(_) => {}
877877
// Remap early-bound regions as long as they don't come from the `impl` itself,
878878
// in which case we don't really need to renumber them.
879-
ty::ReEarlyParam(ebr) if self.tcx.parent(ebr.def_id) != self.impl_def_id => {}
879+
ty::ReEarlyParam(ebr)
880+
if ebr.index >= self.tcx.generics_of(self.impl_def_id).count() as u32 => {}
880881
_ => return Ok(region),
881882
}
882883

@@ -889,12 +890,8 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
889890
);
890891
}
891892
} else {
892-
let guar = match region.kind() {
893-
ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
894-
| ty::ReLateParam(ty::LateParamRegion {
895-
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
896-
..
897-
}) => {
893+
let guar = match region.opt_param_def_id(self.tcx, self.tcx.parent(self.def_id)) {
894+
Some(def_id) => {
898895
let return_span = if let ty::Alias(ty::Opaque, opaque_ty) = self.ty.kind() {
899896
self.tcx.def_span(opaque_ty.def_id)
900897
} else {
@@ -914,7 +911,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
914911
.with_note(format!("hidden type inferred to be `{}`", self.ty))
915912
.emit()
916913
}
917-
_ => {
914+
None => {
918915
// This code path is not reached in any tests, but may be
919916
// reachable. If this is triggered, it should be converted
920917
// to `delayed_bug` and the triggering case turned into a

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -2101,16 +2101,14 @@ fn lint_redundant_lifetimes<'tcx>(
21012101
}
21022102

21032103
for &victim in &lifetimes[(idx + 1)..] {
2104-
// We should only have late-bound lifetimes of the `BrNamed` variety,
2105-
// since we get these signatures straight from `hir_lowering`. And any
2106-
// other regions (ReError/ReStatic/etc.) shouldn't matter, since we
2104+
// All region parameters should have a `DefId` available as:
2105+
// - Late-bound parameters should be of the`BrNamed` variety,
2106+
// since we get these signatures straight from `hir_lowering`.
2107+
// - Early-bound parameters unconditionally have a `DefId` available.
2108+
//
2109+
// Any other regions (ReError/ReStatic/etc.) shouldn't matter, since we
21072110
// can't really suggest to remove them.
2108-
let (ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
2109-
| ty::ReLateParam(ty::LateParamRegion {
2110-
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
2111-
..
2112-
})) = victim.kind()
2113-
else {
2111+
let Some(def_id) = victim.opt_param_def_id(tcx, owner_id.to_def_id()) else {
21142112
continue;
21152113
};
21162114

compiler/rustc_middle/src/ty/region.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ impl<'tcx> Region<'tcx> {
320320
_ => bug!("expected region {:?} to be of kind ReVar", self),
321321
}
322322
}
323+
324+
/// Given some item `binding_item`, check if this region is a generic parameter introduced by it
325+
/// or one of the parent generics. Returns the `DefId` of the parameter definition if so.
326+
pub fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option<DefId> {
327+
match self.kind() {
328+
ty::ReEarlyParam(ebr) => {
329+
Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
330+
}
331+
ty::ReLateParam(ty::LateParamRegion {
332+
bound_region: ty::BoundRegionKind::BrNamed(def_id, _),
333+
..
334+
}) => Some(def_id),
335+
_ => None,
336+
}
337+
}
323338
}
324339

325340
impl<'tcx> Deref for Region<'tcx> {
@@ -334,16 +349,13 @@ impl<'tcx> Deref for Region<'tcx> {
334349
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
335350
#[derive(HashStable)]
336351
pub struct EarlyParamRegion {
337-
pub def_id: DefId,
338352
pub index: u32,
339353
pub name: Symbol,
340354
}
341355

342356
impl std::fmt::Debug for EarlyParamRegion {
343357
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
344-
// FIXME(BoxyUwU): self.def_id goes first because of `erased-regions-in-hidden-ty.rs` being impossible to write
345-
// error annotations for otherwise. :). Ideally this would be `self.name, self.index, self.def_id`.
346-
write!(f, "{:?}_{}/#{}", self.def_id, self.name, self.index)
358+
write!(f, "{}/#{}", self.name, self.index)
347359
}
348360
}
349361

0 commit comments

Comments
 (0)