refactor(index): use index_vec crate (#383)

This commit is contained in:
Boshen authored and GitHub committed 2023-05-24 21:45:06 +08:00
1 parent 5c5459dc59
commit 30fc21ce54
19 files changed
+42 -269

No files matched your search

+5
View File
@@ -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.
Generated
+9 -2
View File
@@ -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"
+1
View File
@@ -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"
-3
View File
@@ -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 }
+1 -2
View File
@@ -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
}
+3 -12
View File
@@ -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;
}
+3
View File
@@ -9,3 +9,6 @@ homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
[dependencies]
index_vec = { workspace = true }
-71
View File
@@ -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
}
}
+1 -5
View File
@@ -2,11 +2,7 @@
//!
//! <https://github.com/rust-lang/rust/tree/master/compiler/rustc_index>
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.
///
-95
View File
@@ -1,95 +0,0 @@
//! Adapted from <https://github.com/rust-lang/rust/blob/master/compiler/rustc_index/src/vec.rs>
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<I: Idx, T> {
raw: Vec<T>,
_marker: PhantomData<fn(&I)>,
}
impl<I: Idx, T> IndexVec<I, T> {
#[inline]
pub fn new() -> Self {
Self { raw: Vec::new(), _marker: PhantomData }
}
#[inline]
pub fn from_raw(raw: Vec<T>) -> 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<Item = (I, &T)> + ExactSizeIterator + '_ {
self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t))
}
}
impl<I: Idx, T> Index<I> for IndexVec<I, T> {
type Output = T;
#[inline]
fn index(&self, index: I) -> &T {
&self.raw[index.index()]
}
}
impl<I: Idx, T> IndexMut<I> for IndexVec<I, T> {
#[inline]
fn index_mut(&mut self, index: I) -> &mut T {
&mut self.raw[index.index()]
}
}
impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.raw, fmt)
}
}
@@ -3,7 +3,6 @@ use std::{
ops::{Index, IndexMut},
};
use oxc_index::Idx;
use oxc_span::{Atom, Span};
use super::{
+3 -12
View File
@@ -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;
}
+1 -4
View File
@@ -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! {
+3 -12
View File
@@ -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;
}
-1
View File
@@ -4,7 +4,6 @@ use std::{
};
use oxc_ast::ast::IdentifierReference;
use oxc_index::Idx;
use oxc_span::Span;
use super::{
+3 -3
View File
@@ -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<SymbolId, Slot> = IndexVec::from_raw(vec![0; symbol_table.len()]);
let mut slots: IndexVec<SymbolId, Slot> = 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);
+3 -17
View File
@@ -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<super::ReferenceId>, 8);
define_index_type! {
pub struct ReferenceId = u32;
}
#[derive(Debug, Clone)]
+3 -17
View File
@@ -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<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
#[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<super::ScopeId>, 8);
define_index_type! {
pub struct ScopeId = u32;
}
bitflags! {
+3 -12
View File
@@ -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")]