From 30fc21ce546166f78b8a62f7e610ea1411937beb Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 24 May 2023 21:45:06 +0800 Subject: [PATCH] refactor(index): use index_vec crate (#383) --- .cargo/config.toml | 5 ++ Cargo.lock | 11 ++- Cargo.toml | 1 + crates/oxc_ast_lower/Cargo.toml | 3 - crates/oxc_hir/src/hir_builder.rs | 3 +- crates/oxc_hir/src/lib.rs | 15 +--- crates/oxc_index/Cargo.toml | 3 + crates/oxc_index/src/idx.rs | 71 --------------- crates/oxc_index/src/lib.rs | 6 +- crates/oxc_index/src/vec.rs | 95 --------------------- crates/oxc_semantic/src/symbol/builder.rs | 1 - crates/oxc_semantic/src/symbol/id.rs | 15 +--- crates/oxc_semantic/src/symbol/mod.rs | 5 +- crates/oxc_semantic/src/symbol/reference.rs | 15 +--- crates/oxc_semantic/src/symbol/table.rs | 1 - crates/oxc_semantic2/src/mangler.rs | 6 +- crates/oxc_semantic2/src/reference.rs | 20 +---- crates/oxc_semantic2/src/scope.rs | 20 +---- crates/oxc_semantic2/src/symbol.rs | 15 +--- 19 files changed, 42 insertions(+), 269 deletions(-) delete mode 100644 crates/oxc_index/src/idx.rs delete mode 100644 crates/oxc_index/src/vec.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 66eda1d0f6..3691ef67b7 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -62,6 +62,11 @@ rustflags = [ # Having #[must_use] every where also hinders readability. "-Aclippy::must_use_candidate", + # Generated from the `index_vec` crate. + # this error originates in the macro `$crate::__define_index_type_inner` which comes from the expansion of the macro `define_index_type` + "-Aclippy::used_underscore_binding", + "-Aclippy::doc_markdown", + # nursery # `const` functions do not make sense for our project because this is not a `const` library. diff --git a/Cargo.lock b/Cargo.lock index bd2c64ce03..c3767cca93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,6 +598,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "index_vec" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74086667896a940438f2118212f313abba4aff3831fef6f4b17d02add5c8bb60" + [[package]] name = "indexmap" version = "1.9.3" @@ -977,11 +983,9 @@ dependencies = [ name = "oxc_ast_lower" version = "0.0.0" dependencies = [ - "bitflags 2.2.1", "oxc_allocator", "oxc_ast", "oxc_hir", - "oxc_index", "oxc_parser", "oxc_semantic2", "oxc_span", @@ -1096,6 +1100,9 @@ dependencies = [ [[package]] name = "oxc_index" version = "0.0.0" +dependencies = [ + "index_vec", +] [[package]] name = "oxc_linter" diff --git a/Cargo.toml b/Cargo.toml index 0965eaa653..d218b5e887 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ ureq = { version = "2.6.2", default-features = false } url = { version = "2.3.1" } walkdir = { version = "2.3.3" } indexmap = { version = "1.0.3" } +index_vec = { version = "0.1.3" } [workspace.metadata.workspaces] allow_branch = "main" diff --git a/crates/oxc_ast_lower/Cargo.toml b/crates/oxc_ast_lower/Cargo.toml index 6b1fb51228..9e879f0c41 100644 --- a/crates/oxc_ast_lower/Cargo.toml +++ b/crates/oxc_ast_lower/Cargo.toml @@ -15,11 +15,8 @@ oxc_allocator = { workspace = true } oxc_ast = { workspace = true } oxc_hir = { workspace = true } oxc_span = { workspace = true } -oxc_index = { workspace = true } oxc_semantic2 = { workspace = true } -bitflags = { workspace = true } - [dev_dependencies] oxc_allocator = { workspace = true } oxc_parser = { workspace = true } diff --git a/crates/oxc_hir/src/hir_builder.rs b/crates/oxc_hir/src/hir_builder.rs index c92aa9131b..9318205d70 100644 --- a/crates/oxc_hir/src/hir_builder.rs +++ b/crates/oxc_hir/src/hir_builder.rs @@ -7,7 +7,6 @@ use num_bigint::BigUint; use oxc_allocator::{Allocator, Box, String, Vec}; -use oxc_index::Idx; use oxc_semantic2::{reference::ReferenceId, symbol::SymbolId}; use oxc_span::{Atom, Span}; use oxc_syntax::{ @@ -32,7 +31,7 @@ impl<'a> HirBuilder<'a> { } fn next_id(&mut self) -> HirId { - self.hir_id.increment(); + self.hir_id += 1; self.hir_id } diff --git a/crates/oxc_hir/src/lib.rs b/crates/oxc_hir/src/lib.rs index dd51715555..b10fce15da 100644 --- a/crates/oxc_hir/src/lib.rs +++ b/crates/oxc_hir/src/lib.rs @@ -7,19 +7,10 @@ pub mod hir; mod hir_builder; mod visit_mut; -use oxc_index::Idx; +use oxc_index::define_index_type; pub use crate::{hir_builder::HirBuilder, visit_mut::VisitMut}; -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct HirId(usize); - -impl Idx for HirId { - fn new(idx: usize) -> Self { - Self(idx) - } - - fn index(self) -> usize { - self.0 - } +define_index_type! { + pub struct HirId = usize; } diff --git a/crates/oxc_index/Cargo.toml b/crates/oxc_index/Cargo.toml index 153ecb1396..1a25dddc52 100644 --- a/crates/oxc_index/Cargo.toml +++ b/crates/oxc_index/Cargo.toml @@ -9,3 +9,6 @@ homepage.workspace = true keywords.workspace = true license.workspace = true repository.workspace = true + +[dependencies] +index_vec = { workspace = true } diff --git a/crates/oxc_index/src/idx.rs b/crates/oxc_index/src/idx.rs deleted file mode 100644 index cbd7377477..0000000000 --- a/crates/oxc_index/src/idx.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::{fmt::Debug, hash::Hash, num::NonZeroUsize}; - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct NonZeroIdx(NonZeroUsize); - -impl Default for NonZeroIdx { - fn default() -> Self { - Self::new(0) - } -} - -impl Idx for NonZeroIdx { - fn new(idx: usize) -> Self { - Self(unsafe { NonZeroUsize::new_unchecked(idx + 1) }) - } - - fn index(self) -> usize { - self.0.get() - 1 - } -} - -/// Represents some newtyped `usize` wrapper. -/// -/// Purpose: avoid mixing indexes for different bitvector domains. -pub trait Idx: 'static + Copy + Eq + PartialEq + Debug + Hash { - fn new(idx: usize) -> Self; - - fn index(self) -> usize; - - #[inline] - fn increment(&mut self) { - *self = self.plus(1); - } - - #[inline] - fn increment_by(&mut self, amount: usize) { - *self = self.plus(amount); - } - - #[inline] - #[must_use = "Use `increment_by` if you wanted to update the index in-place"] - fn plus(self, amount: usize) -> Self { - Self::new(self.index() + amount) - } -} - -impl Idx for usize { - #[inline] - fn new(idx: usize) -> Self { - idx - } - - #[inline] - fn index(self) -> usize { - self - } -} - -impl Idx for u32 { - #[allow(clippy::cast_possible_truncation)] - #[inline] - fn new(idx: usize) -> Self { - debug_assert!(Self::try_from(idx).is_ok()); - idx as Self - } - - #[inline] - fn index(self) -> usize { - self as usize - } -} diff --git a/crates/oxc_index/src/lib.rs b/crates/oxc_index/src/lib.rs index 5b528b4f1c..e018e724e4 100644 --- a/crates/oxc_index/src/lib.rs +++ b/crates/oxc_index/src/lib.rs @@ -2,11 +2,7 @@ //! //! -mod idx; -mod vec; - -pub use idx::{Idx, NonZeroIdx}; -pub use vec::IndexVec; +pub use index_vec::{define_index_type, index_vec, IndexSlice, IndexVec}; /// Type size assertion. The first argument is a type and the second argument is its expected size. /// diff --git a/crates/oxc_index/src/vec.rs b/crates/oxc_index/src/vec.rs deleted file mode 100644 index b70d23791c..0000000000 --- a/crates/oxc_index/src/vec.rs +++ /dev/null @@ -1,95 +0,0 @@ -//! Adapted from - -use std::{ - fmt, - marker::PhantomData, - ops::{Index, IndexMut}, -}; - -use crate::Idx; - -/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`. -#[derive(Clone, Default)] -#[repr(transparent)] -pub struct IndexVec { - raw: Vec, - _marker: PhantomData, -} - -impl IndexVec { - #[inline] - pub fn new() -> Self { - Self { raw: Vec::new(), _marker: PhantomData } - } - - #[inline] - pub fn from_raw(raw: Vec) -> Self { - Self { raw, _marker: PhantomData } - } - - #[inline] - pub fn with_capacity(capacity: usize) -> Self { - Self::from_raw(Vec::with_capacity(capacity)) - } - - #[inline] - pub fn len(&self) -> usize { - self.raw.len() - } - - #[inline] - pub fn is_empty(&self) -> bool { - self.raw.is_empty() - } - - #[inline] - #[must_use = "Use the returned Idx"] - pub fn push(&mut self, d: T) -> I { - let idx = self.next_index(); - self.raw.push(d); - idx - } - - /// Gives the next index that will be assigned when `push` is called. - /// - /// Manual bounds checks can be done using `idx < slice.next_index()` - /// (as opposed to `idx.index() < slice.len()`). - #[inline] - pub fn next_index(&self) -> I { - I::new(self.len()) - } - - #[inline] - pub fn iter(&self) -> std::slice::Iter<'_, T> { - self.raw.iter() - } - - #[inline] - pub fn iter_enumerated( - &self, - ) -> impl DoubleEndedIterator + ExactSizeIterator + '_ { - self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t)) - } -} - -impl Index for IndexVec { - type Output = T; - - #[inline] - fn index(&self, index: I) -> &T { - &self.raw[index.index()] - } -} - -impl IndexMut for IndexVec { - #[inline] - fn index_mut(&mut self, index: I) -> &mut T { - &mut self.raw[index.index()] - } -} - -impl fmt::Debug for IndexVec { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.raw, fmt) - } -} diff --git a/crates/oxc_semantic/src/symbol/builder.rs b/crates/oxc_semantic/src/symbol/builder.rs index dc578c49e1..735faf1ab5 100644 --- a/crates/oxc_semantic/src/symbol/builder.rs +++ b/crates/oxc_semantic/src/symbol/builder.rs @@ -3,7 +3,6 @@ use std::{ ops::{Index, IndexMut}, }; -use oxc_index::Idx; use oxc_span::{Atom, Span}; use super::{ diff --git a/crates/oxc_semantic/src/symbol/id.rs b/crates/oxc_semantic/src/symbol/id.rs index 2aabdd5140..79896ecdda 100644 --- a/crates/oxc_semantic/src/symbol/id.rs +++ b/crates/oxc_semantic/src/symbol/id.rs @@ -1,16 +1,7 @@ use std::hash::Hash; -use oxc_index::Idx; +use oxc_index::define_index_type; -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct SymbolId(usize); - -impl Idx for SymbolId { - fn new(idx: usize) -> Self { - Self(idx) - } - - fn index(self) -> usize { - self.0 - } +define_index_type! { + pub struct SymbolId = u32; } diff --git a/crates/oxc_semantic/src/symbol/mod.rs b/crates/oxc_semantic/src/symbol/mod.rs index e3b5904a93..90b0536f90 100644 --- a/crates/oxc_semantic/src/symbol/mod.rs +++ b/crates/oxc_semantic/src/symbol/mod.rs @@ -36,10 +36,7 @@ pub struct Symbol { #[cfg(target_pointer_width = "64")] mod size_asserts { use oxc_index::static_assert_size; - - use super::Symbol; - - static_assert_size!(Symbol, 96); + static_assert_size!(super::Symbol, 88); } bitflags! { diff --git a/crates/oxc_semantic/src/symbol/reference.rs b/crates/oxc_semantic/src/symbol/reference.rs index 4c510138b4..4f9731d358 100644 --- a/crates/oxc_semantic/src/symbol/reference.rs +++ b/crates/oxc_semantic/src/symbol/reference.rs @@ -1,7 +1,7 @@ #![allow(non_upper_case_globals)] use bitflags::bitflags; -use oxc_index::Idx; +use oxc_index::define_index_type; use oxc_span::Span; use super::SymbolId; @@ -75,15 +75,6 @@ impl ResolvedReference { } } -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ResolvedReferenceId(usize); - -impl Idx for ResolvedReferenceId { - fn new(idx: usize) -> Self { - Self(idx) - } - - fn index(self) -> usize { - self.0 - } +define_index_type! { + pub struct ResolvedReferenceId = u32; } diff --git a/crates/oxc_semantic/src/symbol/table.rs b/crates/oxc_semantic/src/symbol/table.rs index c0f22c9fdb..ba7e7a0fa4 100644 --- a/crates/oxc_semantic/src/symbol/table.rs +++ b/crates/oxc_semantic/src/symbol/table.rs @@ -4,7 +4,6 @@ use std::{ }; use oxc_ast::ast::IdentifierReference; -use oxc_index::Idx; use oxc_span::Span; use super::{ diff --git a/crates/oxc_semantic2/src/mangler.rs b/crates/oxc_semantic2/src/mangler.rs index db6744181b..2bf3d19af2 100644 --- a/crates/oxc_semantic2/src/mangler.rs +++ b/crates/oxc_semantic2/src/mangler.rs @@ -1,4 +1,4 @@ -use oxc_index::{Idx, IndexVec}; +use oxc_index::{index_vec, IndexVec}; use oxc_span::Atom; use crate::{ @@ -63,7 +63,7 @@ impl Mangler { let mut total_number_of_slots: Slot = 0; // All symbols with their assigned slots - let mut slots: IndexVec = IndexVec::from_raw(vec![0; symbol_table.len()]); + let mut slots: IndexVec = index_vec![0; symbol_table.len()]; // Keep track of the maximum slot number for each scope let mut max_slot_for_scope = vec![0; scope_tree.len()]; @@ -134,7 +134,7 @@ impl Mangler { if !symbol_table.get_flag(symbol_id).is_variable() { continue; } - let index = slot.index(); + let index = *slot; frequencies[index].slot = *slot; frequencies[index].frequency += symbol_table.resolved_references[symbol_id].len(); frequencies[index].symbol_ids.push(symbol_id); diff --git a/crates/oxc_semantic2/src/reference.rs b/crates/oxc_semantic2/src/reference.rs index a3dcb4e666..2c7a701fcc 100644 --- a/crates/oxc_semantic2/src/reference.rs +++ b/crates/oxc_semantic2/src/reference.rs @@ -1,24 +1,10 @@ -use oxc_index::{Idx, NonZeroIdx}; +use oxc_index::define_index_type; use oxc_span::Atom; use crate::symbol::SymbolId; -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ReferenceId(NonZeroIdx); - -impl Idx for ReferenceId { - fn new(idx: usize) -> Self { - Self(NonZeroIdx::new(idx)) - } - - fn index(self) -> usize { - self.0.index() - } -} - -#[cfg(target_pointer_width = "64")] -mod size_asserts { - oxc_index::static_assert_size!(Option, 8); +define_index_type! { + pub struct ReferenceId = u32; } #[derive(Debug, Clone)] diff --git a/crates/oxc_semantic2/src/scope.rs b/crates/oxc_semantic2/src/scope.rs index 8372b0f2e6..96103b31a3 100644 --- a/crates/oxc_semantic2/src/scope.rs +++ b/crates/oxc_semantic2/src/scope.rs @@ -4,7 +4,7 @@ use std::hash::BuildHasherDefault; use bitflags::bitflags; use indexmap::IndexMap; -use oxc_index::{Idx, IndexVec, NonZeroIdx}; +use oxc_index::{define_index_type, IndexVec}; use oxc_span::Atom; use rustc_hash::{FxHashMap, FxHasher}; @@ -12,22 +12,8 @@ use crate::{reference::ReferenceId, symbol::SymbolId}; type FxIndexMap = IndexMap>; -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ScopeId(NonZeroIdx); - -impl Idx for ScopeId { - fn new(idx: usize) -> Self { - Self(NonZeroIdx::new(idx)) - } - - fn index(self) -> usize { - self.0.index() - } -} - -#[cfg(target_pointer_width = "64")] -mod size_asserts { - oxc_index::static_assert_size!(Option, 8); +define_index_type! { + pub struct ScopeId = u32; } bitflags! { diff --git a/crates/oxc_semantic2/src/symbol.rs b/crates/oxc_semantic2/src/symbol.rs index 1231757445..63ee9a178d 100644 --- a/crates/oxc_semantic2/src/symbol.rs +++ b/crates/oxc_semantic2/src/symbol.rs @@ -1,22 +1,13 @@ #![allow(non_upper_case_globals)] use bitflags::bitflags; -use oxc_index::{Idx, IndexVec, NonZeroIdx}; +use oxc_index::{define_index_type, IndexVec}; use oxc_span::{Atom, Span}; use crate::reference::{Reference, ReferenceId}; -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct SymbolId(NonZeroIdx); - -impl Idx for SymbolId { - fn new(idx: usize) -> Self { - Self(NonZeroIdx::new(idx)) - } - - fn index(self) -> usize { - self.0.index() - } +define_index_type! { + pub struct SymbolId = u32; } #[cfg(target_pointer_width = "64")]