introduce LateParamRegionKind

This commit is contained in:
lcnr 2024-12-06 14:46:28 +01:00
parent 3378a5e084
commit 085d931810
19 changed files with 157 additions and 59 deletions

View File

@ -188,7 +188,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
/// Returns `true` if a closure is inferred to be an `FnMut` closure.
fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
if let Some(ty::ReLateParam(late_param)) = self.to_error_region(fr).as_deref()
&& let ty::BoundRegionKind::ClosureEnv = late_param.bound_region
&& let ty::LateParamRegionKind::ClosureEnv = late_param.kind
&& let DefiningTy::Closure(_, args) = self.regioncx.universal_regions().defining_ty
{
return args.as_closure().kind() == ty::ClosureKind::FnMut;

View File

@ -299,17 +299,17 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
}
ty::ReLateParam(late_param) => match late_param.bound_region {
ty::BoundRegionKind::Named(region_def_id, name) => {
ty::ReLateParam(late_param) => match late_param.kind {
ty::LateParamRegionKind::Named(region_def_id, name) => {
// Get the span to point to, even if we don't use the name.
let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
debug!(
"bound region named: {:?}, is_named: {:?}",
name,
late_param.bound_region.is_named()
late_param.kind.is_named()
);
if late_param.bound_region.is_named() {
if late_param.kind.is_named() {
// A named region that is actually named.
Some(RegionName {
name,
@ -331,7 +331,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
}
}
ty::BoundRegionKind::ClosureEnv => {
ty::LateParamRegionKind::ClosureEnv => {
let def_ty = self.regioncx.universal_regions().defining_ty;
let closure_kind = match def_ty {
@ -368,7 +368,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
})
}
ty::BoundRegionKind::Anon => None,
ty::LateParamRegionKind::Anon(_) => None,
},
ty::ReBound(..)

View File

@ -842,8 +842,9 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
{
let (value, _map) = self.tcx.instantiate_bound_regions(value, |br| {
debug!(?br);
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
let liberated_region =
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), br.kind);
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), kind);
let region_vid = {
let name = match br.kind.get_name() {
Some(name) => name,
@ -941,12 +942,13 @@ fn for_each_late_bound_region_in_item<'tcx>(
return;
}
for bound_var in tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)) {
let ty::BoundVariableKind::Region(bound_region) = bound_var else {
continue;
};
let liberated_region =
ty::Region::new_late_param(tcx, mir_def_id.to_def_id(), bound_region);
f(liberated_region);
for (idx, bound_var) in
tcx.late_bound_vars(tcx.local_def_id_to_hir_id(mir_def_id)).iter().enumerate()
{
if let ty::BoundVariableKind::Region(kind) = bound_var {
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
let liberated_region = ty::Region::new_late_param(tcx, mir_def_id.to_def_id(), kind);
f(liberated_region);
}
}
}

View File

@ -430,12 +430,12 @@ fn compare_method_predicate_entailment<'tcx>(
Ok(())
}
struct RemapLateBound<'a, 'tcx> {
struct RemapLateParam<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
mapping: &'a FxIndexMap<ty::BoundRegionKind, ty::BoundRegionKind>,
mapping: &'a FxIndexMap<ty::LateParamRegionKind, ty::LateParamRegionKind>,
}
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateBound<'_, 'tcx> {
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateParam<'_, 'tcx> {
fn cx(&self) -> TyCtxt<'tcx> {
self.tcx
}
@ -445,7 +445,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateBound<'_, 'tcx> {
ty::Region::new_late_param(
self.tcx,
fr.scope,
self.mapping.get(&fr.bound_region).copied().unwrap_or(fr.bound_region),
self.mapping.get(&fr.kind).copied().unwrap_or(fr.kind),
)
} else {
r

View File

@ -289,11 +289,16 @@ fn report_mismatched_rpitit_signature<'tcx>(
tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(),
tcx.fn_sig(impl_m_def_id).skip_binder().bound_vars(),
)
.filter_map(|(impl_bv, trait_bv)| {
.enumerate()
.filter_map(|(idx, (impl_bv, trait_bv))| {
if let ty::BoundVariableKind::Region(impl_bv) = impl_bv
&& let ty::BoundVariableKind::Region(trait_bv) = trait_bv
{
Some((impl_bv, trait_bv))
let var = ty::BoundVar::from_usize(idx);
Some((
ty::LateParamRegionKind::from_bound(var, impl_bv),
ty::LateParamRegionKind::from_bound(var, trait_bv),
))
} else {
None
}
@ -301,7 +306,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
.collect();
let mut return_ty =
trait_m_sig.output().fold_with(&mut super::RemapLateBound { tcx, mapping: &mapping });
trait_m_sig.output().fold_with(&mut super::RemapLateParam { tcx, mapping: &mapping });
if tcx.asyncness(impl_m_def_id).is_async() && tcx.asyncness(trait_m_def_id).is_async() {
let ty::Alias(ty::Projection, future_ty) = return_ty.kind() else {

View File

@ -2339,8 +2339,11 @@ fn lint_redundant_lifetimes<'tcx>(
);
// If we are in a function, add its late-bound lifetimes too.
if matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
for var in tcx.fn_sig(owner_id).instantiate_identity().bound_vars() {
for (idx, var) in
tcx.fn_sig(owner_id).instantiate_identity().bound_vars().iter().enumerate()
{
let ty::BoundVariableKind::Region(kind) = var else { continue };
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
}
}

View File

@ -355,7 +355,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
ty::Region::new_late_param(
tcx,
scope.to_def_id(),
ty::BoundRegionKind::Named(id.to_def_id(), name),
ty::LateParamRegionKind::Named(id.to_def_id(), name),
)
// (*) -- not late-bound, won't change

View File

@ -325,7 +325,7 @@ where
ParamKind::Free(def_id, name) => ty::Region::new_late_param(
self.tcx,
self.parent_def_id.to_def_id(),
ty::BoundRegionKind::Named(def_id, name),
ty::LateParamRegionKind::Named(def_id, name),
),
// Totally ignore late bound args from binders.
ParamKind::Late => return true,
@ -475,7 +475,7 @@ fn extract_def_id_from_arg<'tcx>(
)
| ty::ReLateParam(ty::LateParamRegion {
scope: _,
bound_region: ty::BoundRegionKind::Named(def_id, ..),
kind: ty::LateParamRegionKind::Named(def_id, ..),
}) => def_id,
_ => unreachable!(),
},
@ -544,7 +544,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
)
| ty::ReLateParam(ty::LateParamRegion {
scope: _,
bound_region: ty::BoundRegionKind::Named(def_id, ..),
kind: ty::LateParamRegionKind::Named(def_id, ..),
}) => def_id,
_ => {
return Ok(a);

View File

@ -3091,7 +3091,7 @@ impl<'tcx> TyCtxt<'tcx> {
return ty::Region::new_late_param(
self,
new_parent.to_def_id(),
ty::BoundRegionKind::Named(
ty::LateParamRegionKind::Named(
lbv.to_def_id(),
self.item_name(lbv.to_def_id()),
),

View File

@ -270,7 +270,8 @@ impl<'tcx> TyCtxt<'tcx> {
T: TypeFoldable<TyCtxt<'tcx>>,
{
self.instantiate_bound_regions_uncached(value, |br| {
ty::Region::new_late_param(self, all_outlive_scope, br.kind)
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
ty::Region::new_late_param(self, all_outlive_scope, kind)
})
}

View File

@ -83,7 +83,8 @@ pub use self::predicate::{
TypeOutlivesPredicate,
};
pub use self::region::{
BoundRegion, BoundRegionKind, EarlyParamRegion, LateParamRegion, Region, RegionKind, RegionVid,
BoundRegion, BoundRegionKind, EarlyParamRegion, LateParamRegion, LateParamRegionKind, Region,
RegionKind, RegionVid,
};
pub use self::rvalue_scopes::RvalueScopes;
pub use self::sty::{

View File

@ -2374,8 +2374,8 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
match *region {
ty::ReEarlyParam(ref data) => data.has_name(),
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => kind.is_named(),
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
| ty::ReLateParam(ty::LateParamRegion { bound_region: br, .. })
| ty::RePlaceholder(ty::Placeholder {
bound: ty::BoundRegion { kind: br, .. }, ..
}) => {
@ -2448,8 +2448,13 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
return Ok(());
}
}
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => {
if let Some(name) = kind.get_name() {
p!(write("{}", name));
return Ok(());
}
}
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
| ty::ReLateParam(ty::LateParamRegion { bound_region: br, .. })
| ty::RePlaceholder(ty::Placeholder {
bound: ty::BoundRegion { kind: br, .. }, ..
}) => {

View File

@ -69,9 +69,10 @@ impl<'tcx> Region<'tcx> {
pub fn new_late_param(
tcx: TyCtxt<'tcx>,
scope: DefId,
bound_region: ty::BoundRegionKind,
kind: LateParamRegionKind,
) -> Region<'tcx> {
tcx.intern_region(ty::ReLateParam(ty::LateParamRegion { scope, bound_region }))
let data = LateParamRegion { scope, kind };
tcx.intern_region(ty::ReLateParam(data))
}
#[inline]
@ -124,8 +125,8 @@ impl<'tcx> Region<'tcx> {
match kind {
ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
ty::ReBound(debruijn, region) => Region::new_bound(tcx, debruijn, region),
ty::ReLateParam(ty::LateParamRegion { scope, bound_region }) => {
Region::new_late_param(tcx, scope, bound_region)
ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
Region::new_late_param(tcx, scope, kind)
}
ty::ReStatic => tcx.lifetimes.re_static,
ty::ReVar(vid) => Region::new_var(tcx, vid),
@ -165,7 +166,7 @@ impl<'tcx> Region<'tcx> {
match *self {
ty::ReEarlyParam(ebr) => Some(ebr.name),
ty::ReBound(_, br) => br.kind.get_name(),
ty::ReLateParam(fr) => fr.bound_region.get_name(),
ty::ReLateParam(fr) => fr.kind.get_name(),
ty::ReStatic => Some(kw::StaticLifetime),
ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(),
_ => None,
@ -187,7 +188,7 @@ impl<'tcx> Region<'tcx> {
match *self {
ty::ReEarlyParam(ebr) => ebr.has_name(),
ty::ReBound(_, br) => br.kind.is_named(),
ty::ReLateParam(fr) => fr.bound_region.is_named(),
ty::ReLateParam(fr) => fr.kind.is_named(),
ty::ReStatic => true,
ty::ReVar(..) => false,
ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(),
@ -310,7 +311,7 @@ impl<'tcx> Region<'tcx> {
Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
}
ty::ReLateParam(ty::LateParamRegion {
bound_region: ty::BoundRegionKind::Named(def_id, _),
kind: ty::LateParamRegionKind::Named(def_id, _),
..
}) => Some(def_id),
_ => None,
@ -358,7 +359,71 @@ impl std::fmt::Debug for EarlyParamRegion {
/// different parameters apart.
pub struct LateParamRegion {
pub scope: DefId,
pub bound_region: BoundRegionKind,
pub kind: LateParamRegionKind,
}
/// When liberating bound regions, we map their [`BoundRegionKind`]
/// to this as we need to track the index of anonymous regions. We
/// otherwise end up liberating multiple bound regions to the same
/// late-bound region.
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
#[derive(HashStable)]
pub enum LateParamRegionKind {
/// An anonymous region parameter for a given fn (&T)
///
/// Unlike [`BoundRegionKind::Anon`], this tracks the index of the
/// liberated bound region.
///
/// We should ideally never liberate anonymous regions, but do so for the
/// sake of diagnostics in `FnCtxt::sig_of_closure_with_expectation`.
Anon(u32),
/// Named region parameters for functions (a in &'a T)
///
/// The `DefId` is needed to distinguish free regions in
/// the event of shadowing.
Named(DefId, Symbol),
/// Anonymous region for the implicit env pointer parameter
/// to a closure
ClosureEnv,
}
impl LateParamRegionKind {
pub fn from_bound(var: BoundVar, br: BoundRegionKind) -> LateParamRegionKind {
match br {
BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
BoundRegionKind::Named(def_id, name) => LateParamRegionKind::Named(def_id, name),
BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
}
}
pub fn is_named(&self) -> bool {
match *self {
LateParamRegionKind::Named(_, name) => {
name != kw::UnderscoreLifetime && name != kw::Empty
}
_ => false,
}
}
pub fn get_name(&self) -> Option<Symbol> {
if self.is_named() {
match *self {
LateParamRegionKind::Named(_, name) => return Some(name),
_ => unreachable!(),
}
}
None
}
pub fn get_id(&self) -> Option<DefId> {
match *self {
LateParamRegionKind::Named(id, _) => Some(id),
_ => None,
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]

View File

@ -77,7 +77,23 @@ impl fmt::Debug for ty::BoundRegionKind {
impl fmt::Debug for ty::LateParamRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ReLateParam({:?}, {:?})", self.scope, self.bound_region)
write!(f, "ReLateParam({:?}, {:?})", self.scope, self.kind)
}
}
impl fmt::Debug for ty::LateParamRegionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ty::LateParamRegionKind::Anon(idx) => write!(f, "BrAnon({idx})"),
ty::LateParamRegionKind::Named(did, name) => {
if did.is_crate_root() {
write!(f, "BrNamed({name})")
} else {
write!(f, "BrNamed({did:?}, {name})")
}
}
ty::LateParamRegionKind::ClosureEnv => write!(f, "BrEnv"),
}
}
}

View File

@ -54,13 +54,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let param = anon_param_info.param;
let new_ty = anon_param_info.param_ty;
let new_ty_span = anon_param_info.param_ty_span;
let br = anon_param_info.br;
let is_first = anon_param_info.is_first;
let scope_def_id = region_info.scope;
let is_impl_item = region_info.is_impl_item;
match br {
ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime) | ty::BoundRegionKind::Anon => {}
match anon_param_info.kind {
ty::LateParamRegionKind::Named(_, kw::UnderscoreLifetime)
| ty::LateParamRegionKind::Anon(_) => {}
_ => {
/* not an anonymous region */
debug!("try_report_named_anon_conflict: not an anonymous region");

View File

@ -17,8 +17,8 @@ pub struct AnonymousParamInfo<'tcx> {
pub param: &'tcx hir::Param<'tcx>,
/// The type corresponding to the anonymous region parameter.
pub param_ty: Ty<'tcx>,
/// The `ty::BoundRegionKind` corresponding to the anonymous region.
pub br: ty::BoundRegionKind,
/// The `ty::LateParamRegionKind` corresponding to the anonymous region.
pub kind: ty::LateParamRegionKind,
/// The `Span` of the parameter type.
pub param_ty_span: Span,
/// Signals that the argument is the first parameter in the declaration.
@ -43,11 +43,11 @@ pub fn find_param_with_region<'tcx>(
anon_region: Region<'tcx>,
replace_region: Region<'tcx>,
) -> Option<AnonymousParamInfo<'tcx>> {
let (id, br) = match *anon_region {
ty::ReLateParam(late_param) => (late_param.scope, late_param.bound_region),
let (id, kind) = match *anon_region {
ty::ReLateParam(late_param) => (late_param.scope, late_param.kind),
ty::ReEarlyParam(ebr) => {
let region_def = tcx.generics_of(generic_param_scope).region_param(ebr, tcx).def_id;
(tcx.parent(region_def), ty::BoundRegionKind::Named(region_def, ebr.name))
(tcx.parent(region_def), ty::LateParamRegionKind::Named(region_def, ebr.name))
}
_ => return None, // not a free region
};
@ -96,7 +96,7 @@ pub fn find_param_with_region<'tcx>(
let ty_hir_id = fn_decl.inputs[index].hir_id;
let param_ty_span = hir.span(ty_hir_id);
let is_first = index == 0;
AnonymousParamInfo { param, param_ty: new_param_ty, param_ty_span, br, is_first }
AnonymousParamInfo { param, param_ty: new_param_ty, param_ty_span, kind, is_first }
})
})
}

View File

@ -1095,13 +1095,13 @@ fn msg_span_from_named_region<'tcx>(
(text, Some(span))
}
ty::ReLateParam(ref fr) => {
if !fr.bound_region.is_named()
if !fr.kind.is_named()
&& let Some((ty, _)) = find_anon_type(tcx, generic_param_scope, region)
{
("the anonymous lifetime defined here".to_string(), Some(ty.span))
} else {
match fr.bound_region {
ty::BoundRegionKind::Named(param_def_id, name) => {
match fr.kind {
ty::LateParamRegionKind::Named(param_def_id, name) => {
let span = tcx.def_span(param_def_id);
let text = if name == kw::UnderscoreLifetime {
"the anonymous lifetime as defined here".to_string()
@ -1110,7 +1110,7 @@ fn msg_span_from_named_region<'tcx>(
};
(text, Some(span))
}
ty::BoundRegionKind::Anon => (
ty::LateParamRegionKind::Anon(_) => (
"the anonymous lifetime as defined here".to_string(),
Some(tcx.def_span(generic_param_scope)),
),

View File

@ -39,14 +39,14 @@ impl<'a> DescriptionCtx<'a> {
}
}
ty::ReLateParam(ref fr) => {
if !fr.bound_region.is_named()
if !fr.kind.is_named()
&& let Some((ty, _)) = find_anon_type(tcx, generic_param_scope, region)
{
(Some(ty.span), "defined_here", String::new())
} else {
let scope = fr.scope.expect_local();
match fr.bound_region {
ty::BoundRegionKind::Named(_, name) => {
match fr.kind {
ty::LateParamRegionKind::Named(_, name) => {
let span = if let Some(param) = tcx
.hir()
.get_generics(scope)
@ -62,7 +62,7 @@ impl<'a> DescriptionCtx<'a> {
(Some(span), "as_defined", name.to_string())
}
}
ty::BoundRegionKind::Anon => {
ty::LateParamRegionKind::Anon(_) => {
let span = Some(tcx.def_span(scope));
(span, "defined_here", String::new())
}

View File

@ -475,7 +475,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
.def_id,
),
ty::ReBound(_, r) => r.kind.get_id(),
ty::ReLateParam(r) => r.bound_region.get_id(),
ty::ReLateParam(r) => r.kind.get_id(),
ty::ReStatic
| ty::ReVar(_)
| ty::RePlaceholder(_)