rust/compiler/rustc_borrowck/src/member_constraints.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

227 lines
7.2 KiB
Rust
Raw Normal View History

2019-06-05 23:07:15 +00:00
use std::hash::Hash;
use std::ops::Index;
use rustc_data_structures::fx::FxIndexMap;
use rustc_index::{IndexSlice, IndexVec};
2020-03-29 14:41:09 +00:00
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
use tracing::instrument;
2019-06-05 23:07:15 +00:00
2019-06-18 12:52:23 +00:00
/// Compactly stores a set of `R0 member of [R1...Rn]` constraints,
2019-06-24 19:34:37 +00:00
/// indexed by the region `R0`.
#[derive(Debug)]
pub(crate) struct MemberConstraintSet<'tcx, R>
2019-06-05 23:07:15 +00:00
where
R: Copy + Eq,
2019-06-05 23:07:15 +00:00
{
2019-06-24 19:34:37 +00:00
/// Stores the first "member" constraint for a given `R0`. This is an
2019-06-05 23:07:15 +00:00
/// index into the `constraints` vector below.
first_constraints: FxIndexMap<R, NllMemberConstraintIndex>,
2019-06-05 23:07:15 +00:00
2019-06-18 12:52:23 +00:00
/// Stores the data about each `R0 member of [R1..Rn]` constraint.
2019-06-05 23:07:15 +00:00
/// These are organized into a linked list, so each constraint
2019-06-24 19:34:37 +00:00
/// contains the index of the next constraint with the same `R0`.
2024-12-19 09:08:05 +00:00
constraints: IndexVec<NllMemberConstraintIndex, MemberConstraint<'tcx>>,
2019-06-05 23:07:15 +00:00
/// Stores the `R1..Rn` regions for *all* sets. For any given
/// constraint, we keep two indices so that we can pull out a
/// slice.
2019-06-18 12:52:23 +00:00
choice_regions: Vec<ty::RegionVid>,
2019-06-05 23:07:15 +00:00
}
2019-06-18 12:52:23 +00:00
/// Represents a `R0 member of [R1..Rn]` constraint
#[derive(Debug)]
2024-12-19 09:08:05 +00:00
pub(crate) struct MemberConstraint<'tcx> {
2019-06-18 12:52:23 +00:00
next_constraint: Option<NllMemberConstraintIndex>,
2019-06-05 23:07:15 +00:00
/// The span where the hidden type was instantiated.
pub(crate) definition_span: Span,
2019-06-24 19:34:37 +00:00
/// The hidden type in which `R0` appears. (Used in error reporting.)
pub(crate) hidden_ty: Ty<'tcx>,
2019-06-05 23:07:15 +00:00
pub(crate) key: ty::OpaqueTypeKey<'tcx>,
2019-06-24 19:34:37 +00:00
/// The region `R0`.
pub(crate) member_region_vid: ty::RegionVid,
2019-06-05 23:07:15 +00:00
2019-06-18 12:52:23 +00:00
/// Index of `R1` in `choice_regions` vector from `MemberConstraintSet`.
2019-06-05 23:07:15 +00:00
start_index: usize,
2019-06-18 12:52:23 +00:00
/// Index of `Rn` in `choice_regions` vector from `MemberConstraintSet`.
2019-06-05 23:07:15 +00:00
end_index: usize,
}
rustc_index::newtype_index! {
#[debug_format = "MemberConstraintIndex({})"]
pub(crate) struct NllMemberConstraintIndex {}
2019-06-05 23:07:15 +00:00
}
2021-12-15 07:38:12 +00:00
impl Default for MemberConstraintSet<'_, ty::RegionVid> {
fn default() -> Self {
2019-06-05 23:07:15 +00:00
Self {
first_constraints: Default::default(),
constraints: Default::default(),
2019-06-18 12:52:23 +00:00
choice_regions: Default::default(),
2019-06-05 23:07:15 +00:00
}
}
}
2019-06-05 23:07:15 +00:00
2019-06-18 12:52:23 +00:00
impl<'tcx> MemberConstraintSet<'tcx, ty::RegionVid> {
pub(crate) fn is_empty(&self) -> bool {
self.constraints.is_empty()
}
2019-06-24 19:34:37 +00:00
/// Pushes a member constraint into the set.
#[instrument(level = "debug", skip(self))]
pub(crate) fn add_member_constraint(
2019-06-05 23:07:15 +00:00
&mut self,
key: ty::OpaqueTypeKey<'tcx>,
hidden_ty: Ty<'tcx>,
definition_span: Span,
member_region_vid: ty::RegionVid,
choice_regions: &[ty::RegionVid],
2019-06-05 23:07:15 +00:00
) {
2019-06-18 12:52:23 +00:00
let next_constraint = self.first_constraints.get(&member_region_vid).cloned();
let start_index = self.choice_regions.len();
self.choice_regions.extend(choice_regions);
let end_index = self.choice_regions.len();
2024-12-19 09:08:05 +00:00
let constraint_index = self.constraints.push(MemberConstraint {
2019-06-05 23:07:15 +00:00
next_constraint,
2019-06-18 12:52:23 +00:00
member_region_vid,
definition_span,
hidden_ty,
key,
2019-06-05 23:07:15 +00:00
start_index,
end_index,
});
2019-06-18 12:52:23 +00:00
self.first_constraints.insert(member_region_vid, constraint_index);
}
2019-06-05 23:07:15 +00:00
}
2021-12-15 07:38:12 +00:00
impl<'tcx, R1> MemberConstraintSet<'tcx, R1>
2019-06-05 23:07:15 +00:00
where
R1: Copy + Hash + Eq,
{
2019-06-18 12:52:23 +00:00
/// Remap the "member region" key using `map_fn`, producing a new
/// member constraint set. This is used in the NLL code to map from
2019-06-05 23:07:15 +00:00
/// the original `RegionVid` to an scc index. In some cases, we
2019-06-24 19:34:37 +00:00
/// may have multiple `R1` values mapping to the same `R2` key -- that
2019-06-05 23:07:15 +00:00
/// is ok, the two sets will be merged.
pub(crate) fn into_mapped<R2>(
2019-06-18 12:52:23 +00:00
self,
mut map_fn: impl FnMut(R1) -> R2,
) -> MemberConstraintSet<'tcx, R2>
2019-06-05 23:07:15 +00:00
where
R2: Copy + Hash + Eq,
{
// We can re-use most of the original data, just tweaking the
// linked list links a bit.
//
2019-06-24 19:34:37 +00:00
// For example if we had two keys `Ra` and `Rb` that both now
// wind up mapped to the same key `S`, we would append the
// linked list for `Ra` onto the end of the linked list for
// `Rb` (or vice versa) -- this basically just requires
2020-03-06 11:13:55 +00:00
// rewriting the final link from one list to point at the other
2019-06-24 19:34:37 +00:00
// other (see `append_list`).
2019-06-05 23:07:15 +00:00
2019-06-18 12:52:23 +00:00
let MemberConstraintSet { first_constraints, mut constraints, choice_regions } = self;
2019-06-05 23:07:15 +00:00
let mut first_constraints2 = FxIndexMap::default();
2019-06-05 23:07:15 +00:00
first_constraints2.reserve(first_constraints.len());
for (r1, start1) in first_constraints {
let r2 = map_fn(r1);
if let Some(&start2) = first_constraints2.get(&r2) {
append_list(&mut constraints, start1, start2);
}
first_constraints2.insert(r2, start1);
}
MemberConstraintSet { first_constraints: first_constraints2, constraints, choice_regions }
}
}
2022-05-22 19:48:19 +00:00
impl<'tcx, R> MemberConstraintSet<'tcx, R>
2019-06-05 23:07:15 +00:00
where
R: Copy + Hash + Eq,
{
pub(crate) fn all_indices(&self) -> impl Iterator<Item = NllMemberConstraintIndex> {
self.constraints.indices()
}
2019-06-05 23:07:15 +00:00
/// Iterate down the constraint indices associated with a given
/// peek-region. You can then use `choice_regions` and other
2019-06-05 23:07:15 +00:00
/// methods to access data.
pub(crate) fn indices(
2019-06-05 23:07:15 +00:00
&self,
2019-06-18 12:52:23 +00:00
member_region_vid: R,
) -> impl Iterator<Item = NllMemberConstraintIndex> {
2019-06-18 12:52:23 +00:00
let mut next = self.first_constraints.get(&member_region_vid).cloned();
std::iter::from_fn(move || -> Option<NllMemberConstraintIndex> {
2019-06-05 23:07:15 +00:00
if let Some(current) = next {
next = self.constraints[current].next_constraint;
Some(current)
} else {
None
}
})
}
2019-06-18 12:52:23 +00:00
/// Returns the "choice regions" for a given member
2019-06-24 19:34:37 +00:00
/// constraint. This is the `R1..Rn` from a constraint like:
2019-06-05 23:07:15 +00:00
///
2022-04-15 22:04:34 +00:00
/// ```text
2019-06-18 12:52:23 +00:00
/// R0 member of [R1..Rn]
2019-06-05 23:07:15 +00:00
/// ```
pub(crate) fn choice_regions(&self, pci: NllMemberConstraintIndex) -> &[ty::RegionVid] {
2024-12-19 09:08:05 +00:00
let MemberConstraint { start_index, end_index, .. } = &self.constraints[pci];
2019-06-18 12:52:23 +00:00
&self.choice_regions[*start_index..*end_index]
2019-06-05 23:07:15 +00:00
}
}
2019-06-18 12:52:23 +00:00
impl<'tcx, R> Index<NllMemberConstraintIndex> for MemberConstraintSet<'tcx, R>
2019-06-05 23:07:15 +00:00
where
R: Copy + Eq,
2019-06-05 23:07:15 +00:00
{
2024-12-19 09:08:05 +00:00
type Output = MemberConstraint<'tcx>;
2019-06-05 23:07:15 +00:00
2024-12-19 09:08:05 +00:00
fn index(&self, i: NllMemberConstraintIndex) -> &MemberConstraint<'tcx> {
2019-06-05 23:07:15 +00:00
&self.constraints[i]
}
}
/// Given a linked list starting at `source_list` and another linked
/// list starting at `target_list`, modify `target_list` so that it is
/// followed by `source_list`.
///
/// Before:
///
2022-04-15 22:04:34 +00:00
/// ```text
2019-06-05 23:07:15 +00:00
/// target_list: A -> B -> C -> (None)
/// source_list: D -> E -> F -> (None)
/// ```
///
/// After:
///
2022-04-15 22:04:34 +00:00
/// ```text
2019-06-05 23:07:15 +00:00
/// target_list: A -> B -> C -> D -> E -> F -> (None)
/// ```
fn append_list(
2024-12-19 09:08:05 +00:00
constraints: &mut IndexSlice<NllMemberConstraintIndex, MemberConstraint<'_>>,
2019-06-18 12:52:23 +00:00
target_list: NllMemberConstraintIndex,
source_list: NllMemberConstraintIndex,
2019-06-05 23:07:15 +00:00
) {
let mut p = target_list;
loop {
2023-04-28 18:19:48 +00:00
let r = &mut constraints[p];
2019-06-05 23:07:15 +00:00
match r.next_constraint {
Some(q) => p = q,
None => {
r.next_constraint = Some(source_list);
return;
}
}
}
}