Remove vec_linked_list.

It provides a way to effectively embed a linked list within an
`IndexVec` and also iterate over that list. It's written in a very
generic way, involving two traits `Links` and `LinkElem`. But the
`Links` trait is only impl'd for `IndexVec` and `&IndexVec`, and the
whole thing is only used in one module within `rustc_borrowck`. So I
think it's over-engineered and hard to read. Plus it has no comments.

This commit removes it, and adds a (non-generic) local iterator for the
use within `rustc_borrowck`. Much simpler.
This commit is contained in:
Nicholas Nethercote 2024-05-07 09:35:50 +10:00
parent f5d7d346a4
commit d3d01e1cd3
3 changed files with 32 additions and 81 deletions

View File

@ -1,4 +1,3 @@
use rustc_data_structures::vec_linked_list as vll;
use rustc_index::IndexVec;
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::{Body, Local, Location};
@ -37,9 +36,12 @@ pub(crate) struct LocalUseMap {
/// we add for each local variable.
first_drop_at: IndexVec<Local, Option<AppearanceIndex>>,
appearances: IndexVec<AppearanceIndex, Appearance>,
appearances: Appearances,
}
// The `Appearance::next` field effectively embeds a linked list within `Appearances`.
type Appearances = IndexVec<AppearanceIndex, Appearance>;
struct Appearance {
point_index: PointIndex,
next: Option<AppearanceIndex>,
@ -49,14 +51,34 @@ rustc_index::newtype_index! {
pub struct AppearanceIndex {}
}
impl vll::LinkElem for Appearance {
type LinkIndex = AppearanceIndex;
fn appearances_iter(
first: Option<AppearanceIndex>,
appearances: &Appearances,
) -> impl Iterator<Item = AppearanceIndex> + '_ {
AppearancesIter { appearances, current: first }
}
fn next(elem: &Self) -> Option<AppearanceIndex> {
elem.next
// Iterates over `Appearances` by following `next` fields.
struct AppearancesIter<'a> {
appearances: &'a Appearances,
current: Option<AppearanceIndex>,
}
impl<'a> Iterator for AppearancesIter<'a> {
type Item = AppearanceIndex;
fn next(&mut self) -> Option<AppearanceIndex> {
if let Some(c) = self.current {
self.current = self.appearances[c].next;
Some(c)
} else {
None
}
}
}
//-----------------------------------------------------------------------------
impl LocalUseMap {
pub(crate) fn build(
live_locals: &[Local],
@ -86,17 +108,17 @@ impl LocalUseMap {
}
pub(crate) fn defs(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_def_at[local], &self.appearances)
appearances_iter(self.first_def_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
pub(crate) fn uses(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_use_at[local], &self.appearances)
appearances_iter(self.first_use_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
pub(crate) fn drops(&self, local: Local) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_drop_at[local], &self.appearances)
appearances_iter(self.first_drop_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
}
@ -146,7 +168,7 @@ impl LocalUseMapBuild<'_> {
fn insert(
elements: &DenseLocationMap,
first_appearance: &mut Option<AppearanceIndex>,
appearances: &mut IndexVec<AppearanceIndex, Appearance>,
appearances: &mut Appearances,
location: Location,
) {
let point_index = elements.point_from_location(location);

View File

@ -85,7 +85,6 @@ pub mod temp_dir;
pub mod transitive_relation;
pub mod unhash;
pub mod unord;
pub mod vec_linked_list;
pub mod work_queue;
mod atomic_ref;

View File

@ -1,70 +0,0 @@
use rustc_index::{Idx, IndexVec};
pub fn iter<Ls>(
first: Option<Ls::LinkIndex>,
links: &Ls,
) -> impl Iterator<Item = Ls::LinkIndex> + '_
where
Ls: Links,
{
VecLinkedListIterator { links, current: first }
}
pub struct VecLinkedListIterator<Ls>
where
Ls: Links,
{
links: Ls,
current: Option<Ls::LinkIndex>,
}
impl<Ls> Iterator for VecLinkedListIterator<Ls>
where
Ls: Links,
{
type Item = Ls::LinkIndex;
fn next(&mut self) -> Option<Ls::LinkIndex> {
if let Some(c) = self.current {
self.current = <Ls as Links>::next(&self.links, c);
Some(c)
} else {
None
}
}
}
pub trait Links {
type LinkIndex: Copy;
fn next(links: &Self, index: Self::LinkIndex) -> Option<Self::LinkIndex>;
}
impl<Ls> Links for &Ls
where
Ls: Links,
{
type LinkIndex = Ls::LinkIndex;
fn next(links: &Self, index: Ls::LinkIndex) -> Option<Ls::LinkIndex> {
<Ls as Links>::next(links, index)
}
}
pub trait LinkElem {
type LinkIndex: Copy;
fn next(elem: &Self) -> Option<Self::LinkIndex>;
}
impl<L, E> Links for IndexVec<L, E>
where
E: LinkElem<LinkIndex = L>,
L: Idx,
{
type LinkIndex = L;
fn next(links: &Self, index: L) -> Option<L> {
<E as LinkElem>::next(&links[index])
}
}