Skip to content

Commit

Permalink
Auto merge of #133961 - lcnr:borrowck-cleanup, r=<try>
Browse files Browse the repository at this point in the history
some cleanup to  our region handling

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.

r? `@compiler-errors`
  • Loading branch information
bors committed Dec 6, 2024
2 parents bc145ce + ad6c287 commit 82684f3
Show file tree
Hide file tree
Showing 24 changed files with 263 additions and 193 deletions.
26 changes: 14 additions & 12 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,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;
Expand Down Expand Up @@ -848,7 +848,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return;
};

let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.def_id);
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);

let param = if let Some(param) =
find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
Expand All @@ -875,15 +875,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
Some(arg),
captures,
Some((param.param_ty_span, param.param_ty.to_string())),
Some(suitable_region.def_id),
Some(suitable_region.scope),
);
return;
}

let Some((alias_tys, alias_span, lt_addition_span)) = self
.infcx
.tcx
.return_type_impl_or_dyn_traits_with_type_alias(suitable_region.def_id)
.return_type_impl_or_dyn_traits_with_type_alias(suitable_region.scope)
else {
return;
};
Expand Down Expand Up @@ -1018,18 +1018,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return;
};

let Some((ty_sub, _)) =
self.infcx.tcx.is_suitable_region(self.mir_def_id(), sub).and_then(|anon_reg| {
find_anon_type(self.infcx.tcx, self.mir_def_id(), sub, &anon_reg.bound_region)
})
let Some((ty_sub, _)) = self
.infcx
.tcx
.is_suitable_region(self.mir_def_id(), sub)
.and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sub))
else {
return;
};

let Some((ty_sup, _)) =
self.infcx.tcx.is_suitable_region(self.mir_def_id(), sup).and_then(|anon_reg| {
find_anon_type(self.infcx.tcx, self.mir_def_id(), sup, &anon_reg.bound_region)
})
let Some((ty_sup, _)) = self
.infcx
.tcx
.is_suitable_region(self.mir_def_id(), sup)
.and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sup))
else {
return;
};
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,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,
Expand All @@ -332,7 +332,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 {
Expand Down Expand Up @@ -369,7 +369,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
})
}

ty::BoundRegionKind::Anon => None,
ty::LateParamRegionKind::Anon(_) => None,
},

ty::ReBound(..)
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ fn do_mir_borrowck<'tcx>(
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
let def = input_body.source.def_id().expect_local();
let infcx = BorrowckInferCtxt::new(tcx, def);
if let Some(e) = input_body.tainted_by_errors {
infcx.set_tainted_by_errors(e);
}

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

let diags = &mut diags::BorrowckDiags::new();

// Gather the upvars of a closure, if any.
if let Some(e) = input_body.tainted_by_errors {
infcx.set_tainted_by_errors(e);
}

// Replace all regions with fresh inference variables. This
// requires first making our own copy of the MIR. This copy will
// be modified (in place) to contain non-lexical lifetimes. It
Expand Down Expand Up @@ -224,6 +220,7 @@ fn do_mir_borrowck<'tcx>(

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

let movable_coroutine =
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
let (value, _map) = self.tcx.instantiate_bound_regions(value, |br| {
debug!(?br);
let liberated_region =
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), br.kind);
ty::Region::new_late_param_from_bound(self.tcx, all_outlive_scope.to_def_id(), br);
let region_vid = {
let name = match br.kind.get_name() {
Some(name) => name,
Expand Down Expand Up @@ -942,12 +942,14 @@ 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 bound = ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind };
let liberated_region =
ty::Region::new_late_param_from_bound(tcx, mir_def_id.to_def_id(), bound);
f(liberated_region);
}
}
}
8 changes: 4 additions & 4 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,24 @@ 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
}
})
.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 {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2296,9 +2296,13 @@ 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 };
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
let bound: ty::BoundRegion =
ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind };
lifetimes.push(ty::Region::new_late_param_from_bound(tcx, owner_id.to_def_id(), bound));
}
}
lifetimes.retain(|candidate| candidate.has_name());
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,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
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/impl_trait_overcaptures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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!(),
},
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 8 additions & 11 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,10 +1115,10 @@ impl<'tcx> CommonConsts<'tcx> {
/// either a `ReEarlyParam` or `ReLateParam`.
#[derive(Debug)]
pub struct FreeRegionInfo {
/// `LocalDefId` of the free region.
pub def_id: LocalDefId,
/// the bound region corresponding to free region.
pub bound_region: ty::BoundRegionKind,
/// `LocalDefId` of the scope.
pub scope: LocalDefId,
/// the `DefId` of the free region.
pub region_def_id: DefId,
/// checks if bound region is in Impl Item
pub is_impl_item: bool,
}
Expand Down Expand Up @@ -1979,7 +1979,7 @@ impl<'tcx> TyCtxt<'tcx> {
generic_param_scope: LocalDefId,
mut region: Region<'tcx>,
) -> Option<FreeRegionInfo> {
let (suitable_region_binding_scope, bound_region) = loop {
let (suitable_region_binding_scope, region_def_id) = loop {
let def_id =
region.opt_param_def_id(self, generic_param_scope.to_def_id())?.as_local()?;
let scope = self.local_parent(def_id);
Expand All @@ -1989,10 +1989,7 @@ impl<'tcx> TyCtxt<'tcx> {
region = self.map_opaque_lifetime_to_parent_lifetime(def_id);
continue;
}
break (
scope,
ty::BoundRegionKind::Named(def_id.into(), self.item_name(def_id.into())),
);
break (scope, def_id.into());
};

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

Some(FreeRegionInfo { def_id: suitable_region_binding_scope, bound_region, is_impl_item })
Some(FreeRegionInfo { scope: suitable_region_binding_scope, region_def_id, is_impl_item })
}

/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in its return type.
Expand Down Expand Up @@ -3101,7 +3098,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()),
),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ 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)
ty::Region::new_late_param_from_bound(self, all_outlive_scope, br)
})
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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::{
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2375,8 +2375,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, .. }, ..
}) => {
Expand Down Expand Up @@ -2449,8 +2449,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, .. }, ..
}) => {
Expand Down
Loading

0 comments on commit 82684f3

Please sign in to comment.