From d3d01e1cd3a4157692e136f3a1f8e0a5e37f3e36 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 May 2024 09:35:50 +1000 Subject: [PATCH] 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. --- .../src/type_check/liveness/local_use_map.rs | 42 ++++++++--- compiler/rustc_data_structures/src/lib.rs | 1 - .../src/vec_linked_list.rs | 70 ------------------- 3 files changed, 32 insertions(+), 81 deletions(-) delete mode 100644 compiler/rustc_data_structures/src/vec_linked_list.rs diff --git a/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs b/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs index da5456692ab..ccd9fb25739 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs @@ -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>, - appearances: IndexVec, + appearances: Appearances, } +// The `Appearance::next` field effectively embeds a linked list within `Appearances`. +type Appearances = IndexVec; + struct Appearance { point_index: PointIndex, next: Option, @@ -49,14 +51,34 @@ rustc_index::newtype_index! { pub struct AppearanceIndex {} } -impl vll::LinkElem for Appearance { - type LinkIndex = AppearanceIndex; +fn appearances_iter( + first: Option, + appearances: &Appearances, +) -> impl Iterator + '_ { + AppearancesIter { appearances, current: first } +} - fn next(elem: &Self) -> Option { - elem.next +// Iterates over `Appearances` by following `next` fields. +struct AppearancesIter<'a> { + appearances: &'a Appearances, + current: Option, +} + +impl<'a> Iterator for AppearancesIter<'a> { + type Item = AppearanceIndex; + + fn next(&mut self) -> Option { + 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 + '_ { - 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 + '_ { - 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 + '_ { - 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, - appearances: &mut IndexVec, + appearances: &mut Appearances, location: Location, ) { let point_index = elements.point_from_location(location); diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 2e5f3806b12..407ee0453e5 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -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; diff --git a/compiler/rustc_data_structures/src/vec_linked_list.rs b/compiler/rustc_data_structures/src/vec_linked_list.rs deleted file mode 100644 index fda72c9a3b2..00000000000 --- a/compiler/rustc_data_structures/src/vec_linked_list.rs +++ /dev/null @@ -1,70 +0,0 @@ -use rustc_index::{Idx, IndexVec}; - -pub fn iter( - first: Option, - links: &Ls, -) -> impl Iterator + '_ -where - Ls: Links, -{ - VecLinkedListIterator { links, current: first } -} - -pub struct VecLinkedListIterator -where - Ls: Links, -{ - links: Ls, - current: Option, -} - -impl Iterator for VecLinkedListIterator -where - Ls: Links, -{ - type Item = Ls::LinkIndex; - - fn next(&mut self) -> Option { - if let Some(c) = self.current { - self.current = ::next(&self.links, c); - Some(c) - } else { - None - } - } -} - -pub trait Links { - type LinkIndex: Copy; - - fn next(links: &Self, index: Self::LinkIndex) -> Option; -} - -impl Links for &Ls -where - Ls: Links, -{ - type LinkIndex = Ls::LinkIndex; - - fn next(links: &Self, index: Ls::LinkIndex) -> Option { - ::next(links, index) - } -} - -pub trait LinkElem { - type LinkIndex: Copy; - - fn next(elem: &Self) -> Option; -} - -impl Links for IndexVec -where - E: LinkElem, - L: Idx, -{ - type LinkIndex = L; - - fn next(links: &Self, index: L) -> Option { - ::next(&links[index]) - } -}