Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl<'tcx> UniverseInfo<'tcx> {
pub(crate) fn report_erroneous_element(
&self,
mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
placeholder: ty::PlaceholderRegion,
error_element: RegionElement,
placeholder: ty::PlaceholderRegion<'tcx>,
error_element: RegionElement<'tcx>,
cause: ObligationCause<'tcx>,
) {
match *self {
Expand Down Expand Up @@ -152,8 +152,8 @@ pub(crate) trait TypeOpInfo<'tcx> {
fn report_erroneous_element(
&self,
mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
placeholder: ty::PlaceholderRegion,
error_element: RegionElement,
placeholder: ty::PlaceholderRegion<'tcx>,
error_element: RegionElement<'tcx>,
cause: ObligationCause<'tcx>,
) {
let tcx = mbcx.infcx.tcx;
Expand All @@ -169,23 +169,22 @@ pub(crate) trait TypeOpInfo<'tcx> {

let placeholder_region = ty::Region::new_placeholder(
tcx,
ty::Placeholder { universe: adjusted_universe.into(), bound: placeholder.bound },
ty::Placeholder::new(adjusted_universe.into(), placeholder.bound),
);

let error_region = if let RegionElement::PlaceholderRegion(error_placeholder) =
error_element
{
let adjusted_universe =
error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32());
adjusted_universe.map(|adjusted| {
ty::Region::new_placeholder(
tcx,
ty::Placeholder { universe: adjusted.into(), bound: error_placeholder.bound },
)
})
} else {
None
};
let error_region =
if let RegionElement::PlaceholderRegion(error_placeholder) = error_element {
let adjusted_universe =
error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32());
adjusted_universe.map(|adjusted| {
ty::Region::new_placeholder(
tcx,
ty::Placeholder::new(adjusted.into(), error_placeholder.bound),
)
})
} else {
None
};

debug!(?placeholder_region);

Expand Down Expand Up @@ -440,7 +439,7 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>(
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
region_constraints: &RegionConstraintData<'tcx>,
mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin,
mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin<'tcx>,
mut universe_of_region: impl FnMut(RegionVid) -> UniverseIndex,
) -> Option<Diag<'a>> {
let placeholder_universe = match placeholder_region.kind() {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ pub(crate) enum RegionErrorKind<'tcx> {
/// The placeholder free region.
longer_fr: RegionVid,
/// The region element that erroneously must be outlived by `longer_fr`.
error_element: RegionElement,
error_element: RegionElement<'tcx>,
/// The placeholder region.
placeholder: ty::PlaceholderRegion,
placeholder: ty::PlaceholderRegion<'tcx>,
},

/// Any other lifetime error.
RegionError {
/// The origin of the region.
fr_origin: NllRegionVariableOrigin,
fr_origin: NllRegionVariableOrigin<'tcx>,
/// The region that should outlive `shorter_fr`.
longer_fr: RegionVid,
/// The region that should be shorter, but we can't prove it.
Expand Down Expand Up @@ -427,7 +427,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
pub(crate) fn report_region_error(
&mut self,
fr: RegionVid,
fr_origin: NllRegionVariableOrigin,
fr_origin: NllRegionVariableOrigin<'tcx>,
outlived_fr: RegionVid,
outlives_suggestion: &mut OutlivesSuggestionBuilder,
) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/handle_placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) struct LoweredConstraints<'tcx> {
pub(crate) type_tests: Vec<TypeTest<'tcx>>,
pub(crate) liveness_constraints: LivenessValues,
pub(crate) universe_causes: FxIndexMap<UniverseIndex, UniverseInfo<'tcx>>,
pub(crate) placeholder_indices: PlaceholderIndices,
pub(crate) placeholder_indices: PlaceholderIndices<'tcx>,
}

impl<'d, 'tcx, A: scc::Annotation> SccAnnotations<'d, 'tcx, A> {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {

pub(crate) fn next_region_var<F>(
&self,
origin: RegionVariableOrigin,
origin: RegionVariableOrigin<'tcx>,
get_ctxt_fn: F,
) -> ty::Region<'tcx>
where
Expand All @@ -683,7 +683,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
#[instrument(skip(self, get_ctxt_fn), level = "debug")]
pub(crate) fn next_nll_region_var<F>(
&self,
origin: NllRegionVariableOrigin,
origin: NllRegionVariableOrigin<'tcx>,
get_ctxt_fn: F,
) -> ty::Region<'tcx>
where
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub struct RegionInferenceContext<'tcx> {
/// The final inferred values of the region variables; we compute
/// one value per SCC. To get the value for any given *region*,
/// you first find which scc it is a part of.
scc_values: RegionValues<ConstraintSccIndex>,
scc_values: RegionValues<'tcx, ConstraintSccIndex>,

/// Type constraints that we check after solving.
type_tests: Vec<TypeTest<'tcx>>,
Expand All @@ -125,7 +125,7 @@ pub(crate) struct RegionDefinition<'tcx> {
/// What kind of variable is this -- a free region? existential
/// variable? etc. (See the `NllRegionVariableOrigin` for more
/// info.)
pub(crate) origin: NllRegionVariableOrigin,
pub(crate) origin: NllRegionVariableOrigin<'tcx>,

/// Which universe is this region variable defined in? This is
/// most often `ty::UniverseIndex::ROOT`, but when we encounter
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// Returns `true` if the region `r` contains the point `p`.
///
/// Panics if called before `solve()` executes,
pub(crate) fn region_contains(&self, r: RegionVid, p: impl ToElementIndex) -> bool {
pub(crate) fn region_contains(&self, r: RegionVid, p: impl ToElementIndex<'tcx>) -> bool {
let scc = self.constraint_sccs.scc(r);
self.scc_values.contains(scc, p)
}
Expand Down Expand Up @@ -481,7 +481,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
pub(crate) fn placeholders_contained_in(
&self,
r: RegionVid,
) -> impl Iterator<Item = ty::PlaceholderRegion> {
) -> impl Iterator<Item = ty::PlaceholderRegion<'tcx>> {
let scc = self.constraint_sccs.scc(r);
self.scc_values.placeholders_contained_in(scc)
}
Expand Down Expand Up @@ -1311,7 +1311,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
fn check_bound_universal_region(
&self,
longer_fr: RegionVid,
placeholder: ty::PlaceholderRegion,
placeholder: ty::PlaceholderRegion<'tcx>,
errors_buffer: &mut RegionErrors<'tcx>,
) {
debug!("check_bound_universal_region(fr={:?}, placeholder={:?})", longer_fr, placeholder,);
Expand Down Expand Up @@ -1523,7 +1523,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
pub(crate) fn region_from_element(
&self,
longer_fr: RegionVid,
element: &RegionElement,
element: &RegionElement<'tcx>,
) -> RegionVid {
match *element {
RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l),
Expand Down Expand Up @@ -1564,7 +1564,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
pub(crate) fn best_blame_constraint(
&self,
from_region: RegionVid,
from_region_origin: NllRegionVariableOrigin,
from_region_origin: NllRegionVariableOrigin<'tcx>,
to_region: RegionVid,
) -> (BlameConstraint<'tcx>, Vec<OutlivesConstraint<'tcx>>) {
assert!(from_region != to_region, "Trying to blame a region for itself!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(super) struct RegionCtxt<'a, 'tcx> {
pub(super) constraint_sccs: ConstraintSccs,
pub(super) scc_annotations: IndexVec<ConstraintSccIndex, RegionTracker>,
pub(super) rev_scc_graph: ReverseSccGraph,
pub(super) scc_values: RegionValues<ConstraintSccIndex>,
pub(super) scc_values: RegionValues<'tcx, ConstraintSccIndex>,
}

impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
Expand Down
77 changes: 45 additions & 32 deletions compiler/rustc_borrowck/src/region_infer/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rustc_middle::ty::{self, RegionVid};
use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex};
use tracing::debug;

use crate::BorrowIndex;
use crate::polonius::LiveLoans;
use crate::{BorrowIndex, TyCtxt};

rustc_index::newtype_index! {
/// A single integer representing a `ty::Placeholder`.
Expand All @@ -22,7 +22,7 @@ rustc_index::newtype_index! {
/// An individual element in a region value -- the value of a
/// particular region variable consists of a set of these elements.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum RegionElement {
pub(crate) enum RegionElement<'tcx> {
/// A point in the control-flow graph.
Location(Location),

Expand All @@ -32,7 +32,7 @@ pub(crate) enum RegionElement {

/// A placeholder (e.g., instantiated from a `for<'a> fn(&'a u32)`
/// type).
PlaceholderRegion(ty::PlaceholderRegion),
PlaceholderRegion(ty::PlaceholderRegion<'tcx>),
}

/// Records the CFG locations where each region is live. When we initially compute liveness, we use
Expand Down Expand Up @@ -196,25 +196,28 @@ impl LivenessValues {
/// NLL.
#[derive(Debug, Default)]
#[derive(Clone)] // FIXME(#146079)
pub(crate) struct PlaceholderIndices {
indices: FxIndexSet<ty::PlaceholderRegion>,
pub(crate) struct PlaceholderIndices<'tcx> {
indices: FxIndexSet<ty::PlaceholderRegion<'tcx>>,
}

impl PlaceholderIndices {
impl<'tcx> PlaceholderIndices<'tcx> {
/// Returns the `PlaceholderIndex` for the inserted `PlaceholderRegion`
pub(crate) fn insert(&mut self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex {
pub(crate) fn insert(&mut self, placeholder: ty::PlaceholderRegion<'tcx>) -> PlaceholderIndex {
let (index, _) = self.indices.insert_full(placeholder);
index.into()
}

pub(crate) fn lookup_index(&self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex {
pub(crate) fn lookup_index(
&self,
placeholder: ty::PlaceholderRegion<'tcx>,
) -> PlaceholderIndex {
self.indices.get_index_of(&placeholder).unwrap().into()
}

pub(crate) fn lookup_placeholder(
&self,
placeholder: PlaceholderIndex,
) -> ty::PlaceholderRegion {
) -> ty::PlaceholderRegion<'tcx> {
self.indices[placeholder.index()]
}

Expand All @@ -241,9 +244,9 @@ impl PlaceholderIndices {
/// Here, the variable `'0` would contain the free region `'a`,
/// because (since it is returned) it must live for at least `'a`. But
/// it would also contain various points from within the function.
pub(crate) struct RegionValues<N: Idx> {
pub(crate) struct RegionValues<'tcx, N: Idx> {
location_map: Rc<DenseLocationMap>,
placeholder_indices: PlaceholderIndices,
placeholder_indices: PlaceholderIndices<'tcx>,
points: SparseIntervalMatrix<N, PointIndex>,
free_regions: SparseBitMatrix<N, RegionVid>,

Expand All @@ -252,14 +255,14 @@ pub(crate) struct RegionValues<N: Idx> {
placeholders: SparseBitMatrix<N, PlaceholderIndex>,
}

impl<N: Idx> RegionValues<N> {
impl<'tcx, N: Idx> RegionValues<'tcx, N> {
/// Creates a new set of "region values" that tracks causal information.
/// Each of the regions in num_region_variables will be initialized with an
/// empty set of points and no causal information.
pub(crate) fn new(
location_map: Rc<DenseLocationMap>,
num_universal_regions: usize,
placeholder_indices: PlaceholderIndices,
placeholder_indices: PlaceholderIndices<'tcx>,
) -> Self {
let num_points = location_map.num_points();
let num_placeholders = placeholder_indices.len();
Expand All @@ -274,7 +277,7 @@ impl<N: Idx> RegionValues<N> {

/// Adds the given element to the value for the given region. Returns whether
/// the element is newly added (i.e., was not already present).
pub(crate) fn add_element(&mut self, r: N, elem: impl ToElementIndex) -> bool {
pub(crate) fn add_element(&mut self, r: N, elem: impl ToElementIndex<'tcx>) -> bool {
debug!("add(r={:?}, elem={:?})", r, elem);
elem.add_to_row(self, r)
}
Expand All @@ -293,7 +296,7 @@ impl<N: Idx> RegionValues<N> {
}

/// Returns `true` if the region `r` contains the given element.
pub(crate) fn contains(&self, r: N, elem: impl ToElementIndex) -> bool {
pub(crate) fn contains(&self, r: N, elem: impl ToElementIndex<'tcx>) -> bool {
elem.contained_in_row(self, r)
}

Expand Down Expand Up @@ -359,7 +362,7 @@ impl<N: Idx> RegionValues<N> {
pub(crate) fn placeholders_contained_in(
&self,
r: N,
) -> impl Iterator<Item = ty::PlaceholderRegion> {
) -> impl Iterator<Item = ty::PlaceholderRegion<'tcx>> {
self.placeholders
.row(r)
.into_iter()
Expand All @@ -368,7 +371,7 @@ impl<N: Idx> RegionValues<N> {
}

/// Returns all the elements contained in a given region's value.
pub(crate) fn elements_contained_in(&self, r: N) -> impl Iterator<Item = RegionElement> {
pub(crate) fn elements_contained_in(&self, r: N) -> impl Iterator<Item = RegionElement<'tcx>> {
let points_iter = self.locations_outlived_by(r).map(RegionElement::Location);

let free_regions_iter =
Expand All @@ -386,42 +389,50 @@ impl<N: Idx> RegionValues<N> {
}
}

pub(crate) trait ToElementIndex: Debug + Copy {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<N>, row: N) -> bool;
pub(crate) trait ToElementIndex<'tcx>: Debug + Copy {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<'tcx, N>, row: N) -> bool;

fn contained_in_row<N: Idx>(self, values: &RegionValues<N>, row: N) -> bool;
fn contained_in_row<N: Idx>(self, values: &RegionValues<'tcx, N>, row: N) -> bool;
}

impl ToElementIndex for Location {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<N>, row: N) -> bool {
impl ToElementIndex<'_> for Location {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<'_, N>, row: N) -> bool {
let index = values.location_map.point_from_location(self);
values.points.insert(row, index)
}

fn contained_in_row<N: Idx>(self, values: &RegionValues<N>, row: N) -> bool {
fn contained_in_row<N: Idx>(self, values: &RegionValues<'_, N>, row: N) -> bool {
let index = values.location_map.point_from_location(self);
values.points.contains(row, index)
}
}

impl ToElementIndex for RegionVid {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<N>, row: N) -> bool {
impl ToElementIndex<'_> for RegionVid {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<'_, N>, row: N) -> bool {
values.free_regions.insert(row, self)
}

fn contained_in_row<N: Idx>(self, values: &RegionValues<N>, row: N) -> bool {
fn contained_in_row<N: Idx>(self, values: &RegionValues<'_, N>, row: N) -> bool {
values.free_regions.contains(row, self)
}
}

impl ToElementIndex for ty::PlaceholderRegion {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<N>, row: N) -> bool {
let index = values.placeholder_indices.lookup_index(self);
impl<'tcx> ToElementIndex<'tcx> for ty::PlaceholderRegion<'tcx> {
fn add_to_row<N: Idx>(self, values: &mut RegionValues<'tcx, N>, row: N) -> bool
where
Self: Into<ty::Placeholder<TyCtxt<'tcx>, ty::BoundRegion>>,
{
let placeholder: ty::Placeholder<TyCtxt<'tcx>, ty::BoundRegion> = self.into();
let index = values.placeholder_indices.lookup_index(placeholder);
values.placeholders.insert(row, index)
}

fn contained_in_row<N: Idx>(self, values: &RegionValues<N>, row: N) -> bool {
let index = values.placeholder_indices.lookup_index(self);
fn contained_in_row<N: Idx>(self, values: &RegionValues<'tcx, N>, row: N) -> bool
where
Self: Into<ty::Placeholder<TyCtxt<'tcx>, ty::BoundRegion>>,
{
let placeholder: ty::Placeholder<TyCtxt<'tcx>, ty::BoundRegion> = self.into();
let index = values.placeholder_indices.lookup_index(placeholder);
values.placeholders.contains(row, index)
}
}
Expand All @@ -441,7 +452,9 @@ pub(crate) fn pretty_print_points(
}

/// For debugging purposes, returns a pretty-printed string of the given region elements.
fn pretty_print_region_elements(elements: impl IntoIterator<Item = RegionElement>) -> String {
fn pretty_print_region_elements<'tcx>(
elements: impl IntoIterator<Item = RegionElement<'tcx>>,
) -> String {
let mut result = String::new();
result.push('{');

Expand Down
Loading
Loading