From f840fcb2f525c13809d6a736e434155edf075a06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Mar 2020 16:00:11 +0100 Subject: Simplify Arena to use a generic index --- crates/ra_hir_def/src/adt.rs | 18 ++--- crates/ra_hir_def/src/body.rs | 4 +- crates/ra_hir_def/src/body/lower.rs | 6 +- crates/ra_hir_def/src/body/scope.rs | 8 +- crates/ra_hir_def/src/expr.rs | 17 ++--- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/lib.rs | 18 ++--- crates/ra_hir_def/src/nameres.rs | 4 +- crates/ra_hir_def/src/nameres/collector.rs | 2 +- crates/ra_hir_def/src/nameres/raw.rs | 88 +++++++++++----------- .../ra_hir_def/src/nameres/tests/mod_resolution.rs | 8 +- crates/ra_hir_def/src/trace.rs | 22 +++--- 12 files changed, 85 insertions(+), 112 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index d55c49938..de07fc952 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -27,7 +27,7 @@ pub struct StructData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub name: Name, - pub variants: Arena, + pub variants: Arena, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -38,8 +38,8 @@ pub struct EnumVariantData { #[derive(Debug, Clone, PartialEq, Eq)] pub enum VariantData { - Record(Arena), - Tuple(Arena), + Record(Arena), + Tuple(Arena), Unit, } @@ -104,7 +104,7 @@ impl HasChildSource for EnumId { fn lower_enum( db: &dyn DefDatabase, - trace: &mut Trace, + trace: &mut Trace, ast: &InFile, ) { for var in ast.value.variant_list().into_iter().flat_map(|it| it.variants()) { @@ -128,8 +128,8 @@ impl VariantData { } } - pub fn fields(&self) -> &Arena { - const EMPTY: &Arena = &Arena::new(); + pub fn fields(&self) -> &Arena { + const EMPTY: &Arena = &Arena::new(); match &self { VariantData::Record(fields) | VariantData::Tuple(fields) => fields, _ => EMPTY, @@ -183,11 +183,7 @@ pub enum StructKind { fn lower_struct( db: &dyn DefDatabase, - trace: &mut Trace< - LocalStructFieldId, - StructFieldData, - Either, - >, + trace: &mut Trace>, ast: &InFile, ) -> StructKind { match &ast.value { diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 34561ee73..27a297e8b 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -121,8 +121,8 @@ pub(crate) struct Mark { /// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq)] pub struct Body { - pub exprs: Arena, - pub pats: Arena, + pub exprs: Arena, + pub pats: Arena, /// The patterns for the function's parameters. While the parameter types are /// part of the function signature, the patterns are not (they don't change /// the external type of the function). diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 6238de606..e8c58ed32 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -24,8 +24,8 @@ use crate::{ builtin_type::{BuiltinFloat, BuiltinInt}, db::DefDatabase, expr::{ - ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, - MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, + dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, + LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, }, item_scope::BuiltinShadowMode, path::GenericArgs, @@ -51,7 +51,7 @@ pub(super) fn lower( exprs: Arena::default(), pats: Arena::default(), params: Vec::new(), - body_expr: ExprId::dummy(), + body_expr: dummy_expr_id(), item_scope: Default::default(), }, } diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 7c3db8869..4d489f692 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use hir_expand::name::Name; -use ra_arena::{impl_arena_id, Arena, RawId}; +use ra_arena::{Arena, Idx}; use rustc_hash::FxHashMap; use crate::{ @@ -12,13 +12,11 @@ use crate::{ DefWithBodyId, }; -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ScopeId(RawId); -impl_arena_id!(ScopeId); +pub type ScopeId = Idx; #[derive(Debug, PartialEq, Eq)] pub struct ExprScopes { - scopes: Arena, + scopes: Arena, scope_by_expr: FxHashMap, } diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index 66d004717..197bbe9bd 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -13,7 +13,7 @@ //! See also a neighboring `body` module. use hir_expand::name::Name; -use ra_arena::{impl_arena_id, RawId}; +use ra_arena::{Idx, RawId}; use ra_syntax::ast::RangeOp; use crate::{ @@ -22,19 +22,12 @@ use crate::{ type_ref::{Mutability, TypeRef}, }; -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ExprId(RawId); -impl_arena_id!(ExprId); - -impl ExprId { - pub fn dummy() -> ExprId { - ExprId((!0).into()) - } +pub type ExprId = Idx; +pub(crate) fn dummy_expr_id() -> ExprId { + ExprId::from_raw(RawId::from(!0)) } -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct PatId(RawId); -impl_arena_id!(PatId); +pub type PatId = Idx; #[derive(Debug, Clone, Eq, PartialEq)] pub enum Literal { diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 24adc8153..b687ce2b2 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -43,7 +43,7 @@ pub enum TypeParamProvenance { /// Data about the generic parameters of a function, struct, impl, etc. #[derive(Clone, PartialEq, Eq, Debug)] pub struct GenericParams { - pub types: Arena, + pub types: Arena, // lifetimes: Arena, pub where_predicates: Vec, } diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index d0f043ed0..516dd773e 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -50,7 +50,7 @@ use hir_expand::{ ast_id_map::FileAstId, eager::expand_eager_macro, hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; -use ra_arena::{impl_arena_id, RawId}; +use ra_arena::Idx; use ra_db::{impl_intern_key, salsa, CrateId}; use ra_syntax::{ast, AstNode}; @@ -64,9 +64,7 @@ pub struct ModuleId { } /// An ID of a module, **local** to a specific crate -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct LocalModuleId(RawId); -impl_arena_id!(LocalModuleId); +pub type LocalModuleId = Idx; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ItemLoc { @@ -127,9 +125,7 @@ pub struct EnumVariantId { pub local_id: LocalEnumVariantId, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalEnumVariantId(RawId); -impl_arena_id!(LocalEnumVariantId); +pub type LocalEnumVariantId = Idx; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StructFieldId { @@ -137,9 +133,7 @@ pub struct StructFieldId { pub local_id: LocalStructFieldId, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalStructFieldId(RawId); -impl_arena_id!(LocalStructFieldId); +pub type LocalStructFieldId = Idx; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ConstId(salsa::InternId); @@ -172,9 +166,7 @@ pub struct TypeParamId { pub local_id: LocalTypeParamId, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalTypeParamId(RawId); -impl_arena_id!(LocalTypeParamId); +pub type LocalTypeParamId = Idx; macro_rules! impl_froms { ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index be53313ee..40bdc34f5 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -77,7 +77,7 @@ use crate::{ #[derive(Debug, PartialEq, Eq)] pub struct CrateDefMap { pub root: LocalModuleId, - pub modules: Arena, + pub modules: Arena, pub(crate) krate: CrateId, /// The prelude module for this crate. This either comes from an import /// marked with the `prelude_import` attribute, or (in the normal case) from @@ -187,7 +187,7 @@ impl CrateDefMap { }); let def_map = { let edition = db.crate_graph()[krate].edition; - let mut modules: Arena = Arena::default(); + let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { krate, diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7a042e69f..5b292c250 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -966,7 +966,7 @@ mod tests { let def_map = { let edition = db.crate_graph()[krate].edition; - let mut modules: Arena = Arena::default(); + let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { krate, diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 0e4931f58..1631e87b8 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -12,7 +12,7 @@ use hir_expand::{ hygiene::Hygiene, name::{AsName, Name}, }; -use ra_arena::{impl_arena_id, Arena, RawId}; +use ra_arena::{Arena, Idx}; use ra_prof::profile; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, @@ -34,11 +34,11 @@ use crate::{ /// on most edits. #[derive(Debug, Default, PartialEq, Eq)] pub struct RawItems { - modules: Arena, - imports: Arena, - defs: Arena, - macros: Arena, - impls: Arena, + modules: Arena, + imports: Arena, + defs: Arena, + macros: Arena, + impls: Arena, /// items for top-level module items: Vec, } @@ -68,9 +68,9 @@ impl RawItems { } } -impl Index for RawItems { +impl Index> for RawItems { type Output = ModuleData; - fn index(&self, idx: Module) -> &ModuleData { + fn index(&self, idx: Idx) -> &ModuleData { &self.modules[idx] } } @@ -82,23 +82,23 @@ impl Index for RawItems { } } -impl Index for RawItems { +impl Index> for RawItems { type Output = DefData; - fn index(&self, idx: Def) -> &DefData { + fn index(&self, idx: Idx) -> &DefData { &self.defs[idx] } } -impl Index for RawItems { +impl Index> for RawItems { type Output = MacroData; - fn index(&self, idx: Macro) -> &MacroData { + fn index(&self, idx: Idx) -> &MacroData { &self.macros[idx] } } -impl Index for RawItems { +impl Index> for RawItems { type Output = ImplData; - fn index(&self, idx: Impl) -> &ImplData { + fn index(&self, idx: Idx) -> &ImplData { &self.impls[idx] } } @@ -111,17 +111,13 @@ pub(super) struct RawItem { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { - Module(Module), + Module(Idx), Import(Import), - Def(Def), - Macro(Macro), - Impl(Impl), + Def(Idx), + Macro(Idx), + Impl(Idx), } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(super) struct Module(RawId); -impl_arena_id!(Module); - #[derive(Debug, PartialEq, Eq)] pub(super) enum ModuleData { Declaration { @@ -137,9 +133,7 @@ pub(super) enum ModuleData { }, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct Import(RawId); -impl_arena_id!(Import); +pub(crate) type Import = Idx; #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { @@ -152,9 +146,7 @@ pub struct ImportData { pub(super) visibility: RawVisibility, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(super) struct Def(RawId); -impl_arena_id!(Def); +// type Def = Idx; #[derive(Debug, PartialEq, Eq)] pub(super) struct DefData { @@ -190,10 +182,6 @@ impl DefKind { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(super) struct Macro(RawId); -impl_arena_id!(Macro); - #[derive(Debug, PartialEq, Eq)] pub(super) struct MacroData { pub(super) ast_id: FileAstId, @@ -203,10 +191,6 @@ pub(super) struct MacroData { pub(super) builtin: bool, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(super) struct Impl(RawId); -impl_arena_id!(Impl); - #[derive(Debug, PartialEq, Eq)] pub(super) struct ImplData { pub(super) ast_id: FileAstId, @@ -220,7 +204,11 @@ struct RawItemsCollector { } impl RawItemsCollector { - fn process_module(&mut self, current_module: Option, body: impl ast::ModuleItemOwner) { + fn process_module( + &mut self, + current_module: Option>, + body: impl ast::ModuleItemOwner, + ) { for item_or_macro in body.items_with_macros() { match item_or_macro { ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m), @@ -229,7 +217,7 @@ impl RawItemsCollector { } } - fn add_item(&mut self, current_module: Option, item: ast::ModuleItem) { + fn add_item(&mut self, current_module: Option>, item: ast::ModuleItem) { let attrs = self.parse_attrs(&item); let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); let (kind, name) = match item { @@ -285,7 +273,7 @@ impl RawItemsCollector { } } - fn add_module(&mut self, current_module: Option, module: ast::Module) { + fn add_module(&mut self, current_module: Option>, module: ast::Module) { let name = match module.name() { Some(it) => it.as_name(), None => return, @@ -315,7 +303,7 @@ impl RawItemsCollector { tested_by!(name_res_works_for_broken_modules); } - fn add_use_item(&mut self, current_module: Option, use_item: ast::UseItem) { + fn add_use_item(&mut self, current_module: Option>, use_item: ast::UseItem) { // FIXME: cfg_attr let is_prelude = use_item.has_atom_attr("prelude_import"); let attrs = self.parse_attrs(&use_item); @@ -345,7 +333,7 @@ impl RawItemsCollector { fn add_extern_crate_item( &mut self, - current_module: Option, + current_module: Option>, extern_crate: ast::ExternCrateItem, ) { if let Some(name_ref) = extern_crate.name_ref() { @@ -371,7 +359,7 @@ impl RawItemsCollector { } } - fn add_macro(&mut self, current_module: Option, m: ast::MacroCall) { + fn add_macro(&mut self, current_module: Option>, m: ast::MacroCall) { let attrs = self.parse_attrs(&m); let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { Some(it) => it, @@ -391,19 +379,29 @@ impl RawItemsCollector { self.push_item(current_module, attrs, RawItemKind::Macro(m)); } - fn add_impl(&mut self, current_module: Option, imp: ast::ImplDef) { + fn add_impl(&mut self, current_module: Option>, imp: ast::ImplDef) { let attrs = self.parse_attrs(&imp); let ast_id = self.source_ast_id_map.ast_id(&imp); let imp = self.raw_items.impls.alloc(ImplData { ast_id }); self.push_item(current_module, attrs, RawItemKind::Impl(imp)) } - fn push_import(&mut self, current_module: Option, attrs: Attrs, data: ImportData) { + fn push_import( + &mut self, + current_module: Option>, + attrs: Attrs, + data: ImportData, + ) { let import = self.raw_items.imports.alloc(data); self.push_item(current_module, attrs, RawItemKind::Import(import)) } - fn push_item(&mut self, current_module: Option, attrs: Attrs, kind: RawItemKind) { + fn push_item( + &mut self, + current_module: Option>, + attrs: Attrs, + kind: RawItemKind, + ) { match current_module { Some(module) => match &mut self.raw_items.modules[module] { ModuleData::Definition { items, .. } => items, diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index b502a4079..37fcdfb8c 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -710,9 +710,7 @@ fn unresolved_module_diagnostics() { @r###" [ UnresolvedModule { - module: LocalModuleId( - 0, - ), + module: Idx::(0), declaration: InFile { file_id: HirFileId( FileId( @@ -722,9 +720,7 @@ fn unresolved_module_diagnostics() { ), ), value: FileAstId { - raw: ErasedFileAstId( - 1, - ), + raw: Idx::(1), _ty: PhantomData, }, }, diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs index 9769e88df..ced07577d 100644 --- a/crates/ra_hir_def/src/trace.rs +++ b/crates/ra_hir_def/src/trace.rs @@ -9,28 +9,28 @@ //! absolute offsets. The `Trace` structure (inspired, at least in name, by //! Kotlin's `BindingTrace`) allows use the same code to compute both //! projections. -use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId}; +use ra_arena::{map::ArenaMap, Arena, Idx, RawId}; -pub(crate) struct Trace { - arena: Option>, - map: Option>, +pub(crate) struct Trace { + arena: Option>, + map: Option, V>>, len: u32, } -impl Trace { - pub(crate) fn new_for_arena() -> Trace { +impl Trace { + pub(crate) fn new_for_arena() -> Trace { Trace { arena: Some(Arena::default()), map: None, len: 0 } } - pub(crate) fn new_for_map() -> Trace { + pub(crate) fn new_for_map() -> Trace { Trace { arena: None, map: Some(ArenaMap::default()), len: 0 } } - pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> ID { + pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> Idx { let id = if let Some(arena) = &mut self.arena { arena.alloc(data()) } else { - let id = ID::from_raw(RawId::from(self.len)); + let id = Idx::::from_raw(RawId::from(self.len)); self.len += 1; id }; @@ -41,11 +41,11 @@ impl Trace { id } - pub(crate) fn into_arena(mut self) -> Arena { + pub(crate) fn into_arena(mut self) -> Arena { self.arena.take().unwrap() } - pub(crate) fn into_map(mut self) -> ArenaMap { + pub(crate) fn into_map(mut self) -> ArenaMap, V> { self.map.take().unwrap() } } -- cgit v1.2.3