From dce5c640f8f4b8fa3f9eded0c115689f85250877 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Tue, 5 Oct 2021 14:10:25 +1100 Subject: [PATCH] Move `IdxRange` into `la_arena` --- Cargo.lock | 2 +- crates/hir_def/Cargo.toml | 2 +- crates/hir_def/src/body/scope.rs | 2 +- crates/hir_def/src/item_tree.rs | 62 ++------------ crates/hir_def/src/item_tree/lower.rs | 18 ++-- crates/hir_expand/Cargo.toml | 2 +- crates/hir_ty/Cargo.toml | 2 +- crates/profile/Cargo.toml | 2 +- crates/project_model/Cargo.toml | 2 +- lib/arena/Cargo.toml | 2 +- lib/arena/src/lib.rs | 117 +++++++++++++++++++++++++- 11 files changed, 137 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17dae55e44..6a22fcec73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -796,7 +796,7 @@ dependencies = [ [[package]] name = "la-arena" -version = "0.2.1" +version = "0.3.0" [[package]] name = "lazy_static" diff --git a/crates/hir_def/Cargo.toml b/crates/hir_def/Cargo.toml index 9573d5ac35..8f989bc0ae 100644 --- a/crates/hir_def/Cargo.toml +++ b/crates/hir_def/Cargo.toml @@ -21,7 +21,7 @@ fst = { version = "0.4", default-features = false } itertools = "0.10.0" indexmap = "1.4.0" smallvec = "1.4.0" -la-arena = { version = "0.2.0", path = "../../lib/arena" } +la-arena = { version = "0.3.0", path = "../../lib/arena" } stdx = { path = "../stdx", version = "0.0.0" } base_db = { path = "../base_db", version = "0.0.0" } diff --git a/crates/hir_def/src/body/scope.rs b/crates/hir_def/src/body/scope.rs index 8a48ad9199..82b23f2f46 100644 --- a/crates/hir_def/src/body/scope.rs +++ b/crates/hir_def/src/body/scope.rs @@ -174,7 +174,7 @@ fn compute_block_scopes( fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { let make_label = - |label: &Option<_>| label.map(|label| (label, body.labels[label].name.clone())); + |label: &Option| label.map(|label| (label, body.labels[label].name.clone())); scopes.set_scope(expr, scope); match &body[expr] { diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index b7a5758a0a..73b00887e7 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -36,11 +36,10 @@ mod pretty; mod tests; use std::{ - any::type_name, fmt::{self, Debug}, hash::{Hash, Hasher}, marker::PhantomData, - ops::{Index, Range}, + ops::Index, sync::Arc, }; @@ -53,7 +52,7 @@ use hir_expand::{ name::{name, AsName, Name}, ExpandTo, HirFileId, InFile, }; -use la_arena::{Arena, Idx, RawIdx}; +use la_arena::{Arena, Idx, IdxRange, RawIdx}; use profile::Count; use rustc_hash::FxHashMap; use smallvec::SmallVec; @@ -606,7 +605,7 @@ pub struct Function { pub visibility: RawVisibilityId, pub explicit_generic_params: Interned, pub abi: Option>, - pub params: IdRange, + pub params: IdxRange, pub ret_type: Interned, pub async_ret_type: Option>, pub ast_id: FileAstId, @@ -659,7 +658,7 @@ pub struct Enum { pub name: Name, pub visibility: RawVisibilityId, pub generic_params: Interned, - pub variants: IdRange, + pub variants: IdxRange, pub ast_id: FileAstId, } @@ -947,59 +946,10 @@ pub struct Variant { pub fields: Fields, } -/// A range of densely allocated ItemTree IDs. -pub struct IdRange { - range: Range, - _p: PhantomData, -} - -impl IdRange { - fn new(range: Range>) -> Self { - Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData } - } - - fn is_empty(&self) -> bool { - self.range.is_empty() - } -} - -impl Iterator for IdRange { - type Item = Idx; - fn next(&mut self) -> Option { - self.range.next().map(|raw| Idx::from_raw(raw.into())) - } -} - -impl DoubleEndedIterator for IdRange { - fn next_back(&mut self) -> Option { - self.range.next_back().map(|raw| Idx::from_raw(raw.into())) - } -} - -impl fmt::Debug for IdRange { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple(&format!("IdRange::<{}>", type_name::())).field(&self.range).finish() - } -} - -impl Clone for IdRange { - fn clone(&self) -> Self { - Self { range: self.range.clone(), _p: PhantomData } - } -} - -impl PartialEq for IdRange { - fn eq(&self, other: &Self) -> bool { - self.range == other.range - } -} - -impl Eq for IdRange {} - #[derive(Debug, Clone, PartialEq, Eq)] pub enum Fields { - Record(IdRange), - Tuple(IdRange), + Record(IdxRange), + Tuple(IdxRange), Unit, } diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 9c278f5ac6..7074210a8a 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -229,7 +229,7 @@ impl<'a> Ctx<'a> { } } - fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange { + fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdxRange { let start = self.next_field_idx(); for field in fields.fields() { if let Some(data) = self.lower_record_field(&field) { @@ -238,7 +238,7 @@ impl<'a> Ctx<'a> { } } let end = self.next_field_idx(); - IdRange::new(start..end) + IdxRange::new(start..end) } fn lower_record_field(&mut self, field: &ast::RecordField) -> Option { @@ -249,7 +249,7 @@ impl<'a> Ctx<'a> { Some(res) } - fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange { + fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdxRange { let start = self.next_field_idx(); for (i, field) in fields.fields().enumerate() { let data = self.lower_tuple_field(i, &field); @@ -257,7 +257,7 @@ impl<'a> Ctx<'a> { self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene)); } let end = self.next_field_idx(); - IdRange::new(start..end) + IdxRange::new(start..end) } fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field { @@ -273,7 +273,7 @@ impl<'a> Ctx<'a> { let generic_params = self.lower_generic_params(GenericsOwner::Union, union); let fields = match union.record_field_list() { Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)), - None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())), + None => Fields::Record(IdxRange::new(self.next_field_idx()..self.next_field_idx())), }; let ast_id = self.source_ast_id_map.ast_id(union); let res = Union { name, visibility, generic_params, fields, ast_id }; @@ -287,14 +287,14 @@ impl<'a> Ctx<'a> { let variants = self.with_inherited_visibility(visibility, |this| match &enum_.variant_list() { Some(variant_list) => this.lower_variants(variant_list), - None => IdRange::new(this.next_variant_idx()..this.next_variant_idx()), + None => IdxRange::new(this.next_variant_idx()..this.next_variant_idx()), }); let ast_id = self.source_ast_id_map.ast_id(enum_); let res = Enum { name, visibility, generic_params, variants, ast_id }; Some(id(self.data().enums.alloc(res))) } - fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange { + fn lower_variants(&mut self, variants: &ast::VariantList) -> IdxRange { let start = self.next_variant_idx(); for variant in variants.variants() { if let Some(data) = self.lower_variant(&variant) { @@ -303,7 +303,7 @@ impl<'a> Ctx<'a> { } } let end = self.next_variant_idx(); - IdRange::new(start..end) + IdxRange::new(start..end) } fn lower_variant(&mut self, variant: &ast::Variant) -> Option { @@ -358,7 +358,7 @@ impl<'a> Ctx<'a> { } } let end_param = self.next_param_idx(); - let params = IdRange::new(start_param..end_param); + let params = IdxRange::new(start_param..end_param); let ret_type = match func.ret_type().and_then(|rt| rt.ty()) { Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref), diff --git a/crates/hir_expand/Cargo.toml b/crates/hir_expand/Cargo.toml index 240193f9cf..b73a0c420f 100644 --- a/crates/hir_expand/Cargo.toml +++ b/crates/hir_expand/Cargo.toml @@ -13,7 +13,7 @@ cov-mark = "2.0.0-pre.1" tracing = "0.1" either = "1.5.3" rustc-hash = "1.0.0" -la-arena = { version = "0.2.0", path = "../../lib/arena" } +la-arena = { version = "0.3.0", path = "../../lib/arena" } itertools = "0.10.0" base_db = { path = "../base_db", version = "0.0.0" } diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 0f1e75958a..d05c3ee5c3 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml @@ -20,7 +20,7 @@ scoped-tls = "1" chalk-solve = { version = "0.71", default-features = false } chalk-ir = "0.71" chalk-recursive = { version = "0.71", default-features = false } -la-arena = { version = "0.2.0", path = "../../lib/arena" } +la-arena = { version = "0.3.0", path = "../../lib/arena" } once_cell = { version = "1.5.0" } stdx = { path = "../stdx", version = "0.0.0" } diff --git a/crates/profile/Cargo.toml b/crates/profile/Cargo.toml index 7f7a50eb44..6014ef3e1c 100644 --- a/crates/profile/Cargo.toml +++ b/crates/profile/Cargo.toml @@ -12,7 +12,7 @@ doctest = false once_cell = "1.3.1" cfg-if = "1" libc = "0.2" -la-arena = { version = "0.2.0", path = "../../lib/arena" } +la-arena = { version = "0.3.0", path = "../../lib/arena" } countme = { version = "2.0.1", features = ["enable"] } jemalloc-ctl = { version = "0.4.1", package = "tikv-jemalloc-ctl", optional = true } diff --git a/crates/project_model/Cargo.toml b/crates/project_model/Cargo.toml index 949325fc82..2cba46a59b 100644 --- a/crates/project_model/Cargo.toml +++ b/crates/project_model/Cargo.toml @@ -17,7 +17,7 @@ serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.48" anyhow = "1.0.26" expect-test = "1.2.0-pre.1" -la-arena = { version = "0.2.0", path = "../../lib/arena" } +la-arena = { version = "0.3.0", path = "../../lib/arena" } cfg = { path = "../cfg", version = "0.0.0" } base_db = { path = "../base_db", version = "0.0.0" } diff --git a/lib/arena/Cargo.toml b/lib/arena/Cargo.toml index b5b0cf5e82..23f1d9e881 100644 --- a/lib/arena/Cargo.toml +++ b/lib/arena/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "la-arena" -version = "0.2.1" +version = "0.3.0" description = "Simple index-based arena without deletion." license = "MIT OR Apache-2.0" repository = "https://github.com/rust-analyzer/rust-analyzer" diff --git a/lib/arena/src/lib.rs b/lib/arena/src/lib.rs index 162d556fb7..9fe6d60623 100644 --- a/lib/arena/src/lib.rs +++ b/lib/arena/src/lib.rs @@ -7,7 +7,7 @@ use std::{ hash::{Hash, Hasher}, iter::FromIterator, marker::PhantomData, - ops::{Index, IndexMut}, + ops::{Index, IndexMut, Range, RangeInclusive}, }; mod map; @@ -89,6 +89,101 @@ impl Idx { } } +/// A range of densely allocated arena values. +pub struct IdxRange { + range: Range, + _p: PhantomData, +} + +impl IdxRange { + /// Creates a new index range + /// inclusive of the start value and exclusive of the end value. + /// + /// ``` + /// let mut arena = la_arena::Arena::new(); + /// let a = arena.alloc("a"); + /// let b = arena.alloc("b"); + /// let c = arena.alloc("c"); + /// let d = arena.alloc("d"); + /// + /// let range = la_arena::IdxRange::new(b..d); + /// assert_eq!(&arena[range], &["b", "c"]); + /// ``` + pub fn new(range: Range>) -> Self { + Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData } + } + + /// Creates a new index range + /// inclusive of the start value and end value. + /// + /// ``` + /// let mut arena = la_arena::Arena::new(); + /// let foo = arena.alloc("foo"); + /// let bar = arena.alloc("bar"); + /// let baz = arena.alloc("baz"); + /// + /// let range = la_arena::IdxRange::new_inclusive(foo..=baz); + /// assert_eq!(&arena[range], &["foo", "bar", "baz"]); + /// + /// let range = la_arena::IdxRange::new_inclusive(foo..=foo); + /// assert_eq!(&arena[range], &["foo"]); + /// ``` + pub fn new_inclusive(range: RangeInclusive>) -> Self { + Self { + range: u32::from(range.start().into_raw())..u32::from(range.end().into_raw()) + 1, + _p: PhantomData, + } + } + + /// Returns whether the index range is empty. + /// + /// ``` + /// let mut arena = la_arena::Arena::new(); + /// let one = arena.alloc(1); + /// let two = arena.alloc(2); + /// + /// assert!(la_arena::IdxRange::new(one..one).is_empty()); + /// ``` + pub fn is_empty(&self) -> bool { + self.range.is_empty() + } +} + +impl Iterator for IdxRange { + type Item = Idx; + fn next(&mut self) -> Option { + self.range.next().map(|raw| Idx::from_raw(raw.into())) + } +} + +impl DoubleEndedIterator for IdxRange { + fn next_back(&mut self) -> Option { + self.range.next_back().map(|raw| Idx::from_raw(raw.into())) + } +} + +impl fmt::Debug for IdxRange { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple(&format!("IdxRange::<{}>", std::any::type_name::())) + .field(&self.range) + .finish() + } +} + +impl Clone for IdxRange { + fn clone(&self) -> Self { + Self { range: self.range.clone(), _p: PhantomData } + } +} + +impl PartialEq for IdxRange { + fn eq(&self, other: &Self) -> bool { + self.range == other.range + } +} + +impl Eq for IdxRange {} + /// Yet another index-based arena. #[derive(Clone, PartialEq, Eq, Hash)] pub struct Arena { @@ -170,9 +265,9 @@ impl Arena { /// assert_eq!(arena[idx], 50); /// ``` pub fn alloc(&mut self, value: T) -> Idx { - let idx = RawIdx(self.data.len() as u32); + let idx = self.next_idx(); self.data.push(value); - Idx::from_raw(idx) + idx } /// Returns an iterator over the arena’s elements. @@ -221,6 +316,13 @@ impl Arena { pub fn shrink_to_fit(&mut self) { self.data.shrink_to_fit(); } + + /// Returns the index of the next value allocated on the arena. + /// + /// This method should remain private to make creating invalid `Idx`s harder. + fn next_idx(&self) -> Idx { + Idx::from_raw(RawIdx(self.data.len() as u32)) + } } impl Default for Arena { @@ -244,6 +346,15 @@ impl IndexMut> for Arena { } } +impl Index> for Arena { + type Output = [T]; + fn index(&self, range: IdxRange) -> &[T] { + let start = range.range.start as usize; + let end = range.range.end as usize; + &self.data[start..end] + } +} + impl FromIterator for Arena { fn from_iter(iter: I) -> Self where