From 071a19537d4399fd04d1e9594ab7878502a12d21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:03:17 +0300 Subject: strongy-typed ids for macros --- crates/ra_hir/src/ids.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src/ids.rs') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index e73dd5d21..b503e0ee5 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -10,7 +10,7 @@ use ra_arena::{RawId, ArenaId, impl_arena_id}; use mbe::MacroRules; use crate::{ - Module, DefDatabase, SourceItemId, SourceFileItemId, + Module, DefDatabase, SourceItemId, SourceFileItemId, AstId, }; #[derive(Debug, Default)] @@ -68,7 +68,7 @@ impl HirFileId { HirFileIdRepr::File(file_id) => file_id, HirFileIdRepr::Macro(macro_call_id) => { let loc = macro_call_id.loc(db); - loc.source_item_id.file_id.original_file(db) + loc.ast_id.file_id().original_file(db) } } } @@ -96,8 +96,7 @@ impl HirFileId { fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option> { let loc = macro_call_id.loc(db); - let syntax = db.file_item(loc.source_item_id); - let macro_call = ast::MacroCall::cast(&syntax).unwrap(); + let macro_call = loc.ast_id.to_node(db); let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?; let macro_rules = db.macro_def(loc.def)?; @@ -124,15 +123,10 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroDefId { - MacroByExample { source_item_id: SourceItemId }, -} +pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { - let syntax_node = match id { - MacroDefId::MacroByExample { source_item_id } => db.file_item(source_item_id), - }; - let macro_call = ast::MacroCall::cast(&syntax_node).unwrap(); + let macro_call = id.0.to_node(db); let arg = macro_call.token_tree()?; let (tt, _) = mbe::ast_to_token_tree(arg)?; let rules = MacroRules::parse(&tt).ok()?; @@ -148,7 +142,7 @@ impl_arena_id!(MacroCallId); #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MacroCallLoc { pub(crate) def: MacroDefId, - pub(crate) source_item_id: SourceItemId, + pub(crate) ast_id: AstId, } impl MacroCallId { -- cgit v1.2.3 From 8f324773127c733b12d1c5ee98a3d9c6a5360db0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:27:22 +0300 Subject: more type safety --- crates/ra_hir/src/ids.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'crates/ra_hir/src/ids.rs') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b503e0ee5..c2df5ce00 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -1,5 +1,4 @@ use std::{ - marker::PhantomData, hash::{Hash, Hasher}, sync::Arc, }; @@ -10,7 +9,7 @@ use ra_arena::{RawId, ArenaId, impl_arena_id}; use mbe::MacroRules; use crate::{ - Module, DefDatabase, SourceItemId, SourceFileItemId, AstId, + Module, DefDatabase, AstId, FileAstId, }; #[derive(Debug, Default)] @@ -123,6 +122,7 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] + pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { @@ -161,26 +161,25 @@ impl MacroCallLoc { #[derive(Debug)] pub struct ItemLoc { pub(crate) module: Module, - raw: SourceItemId, - _ty: PhantomData, + ast_id: AstId, } impl PartialEq for ItemLoc { fn eq(&self, other: &Self) -> bool { - self.module == other.module && self.raw == other.raw + self.module == other.module && self.ast_id == other.ast_id } } impl Eq for ItemLoc {} impl Hash for ItemLoc { fn hash(&self, hasher: &mut H) { self.module.hash(hasher); - self.raw.hash(hasher); + self.ast_id.hash(hasher); } } impl Clone for ItemLoc { fn clone(&self) -> ItemLoc { - ItemLoc { module: self.module, raw: self.raw, _ty: PhantomData } + ItemLoc { module: self.module, ast_id: self.ast_id } } } @@ -208,25 +207,18 @@ pub(crate) trait AstItemDef: ArenaId + Clone { fn interner(interner: &HirInterner) -> &LocationInterner, Self>; fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { let items = ctx.db.file_items(ctx.file_id); - let item_id = items.id_of(ctx.file_id, ast.syntax()); - Self::from_source_item_id_unchecked(ctx, item_id) + let item_id = items.ast_id(ast); + Self::from_ast_id(ctx, item_id) } - fn from_source_item_id_unchecked( - ctx: LocationCtx<&impl DefDatabase>, - item_id: SourceFileItemId, - ) -> Self { - let raw = SourceItemId { file_id: ctx.file_id, item_id }; - let loc = ItemLoc { module: ctx.module, raw, _ty: PhantomData }; - + fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId) -> Self { + let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; Self::interner(ctx.db.as_ref()).loc2id(&loc) } fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { let int = Self::interner(db.as_ref()); let loc = int.id2loc(self); - let syntax = db.file_item(loc.raw); - let ast = - N::cast(&syntax).unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", loc.raw)).to_owned(); - (loc.raw.file_id, ast) + let ast = loc.ast_id.to_node(db); + (loc.ast_id.file_id(), ast) } fn module(self, db: &impl DefDatabase) -> Module { let int = Self::interner(db.as_ref()); -- cgit v1.2.3 From b17217b34afbdbdc6b1d8ec480fcf06ec4bd587e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:57:57 +0300 Subject: simplify --- crates/ra_hir/src/ids.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/ids.rs') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index c2df5ce00..81b3cfd22 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -82,7 +82,10 @@ impl HirFileId { } } - pub(crate) fn hir_parse(db: &impl DefDatabase, file_id: HirFileId) -> TreeArc { + pub(crate) fn hir_parse_query( + db: &impl DefDatabase, + file_id: HirFileId, + ) -> TreeArc { match file_id.0 { HirFileIdRepr::File(file_id) => db.parse(file_id), HirFileIdRepr::Macro(macro_call_id) => { @@ -122,7 +125,6 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] - pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { @@ -152,7 +154,6 @@ impl MacroCallId { } impl MacroCallLoc { - #[allow(unused)] pub(crate) fn id(&self, db: &impl AsRef) -> MacroCallId { db.as_ref().macros.loc2id(&self) } -- cgit v1.2.3 From e28db444dfdc797002bd3561940cde2659b831de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 19:00:11 +0300 Subject: rename --- crates/ra_hir/src/ids.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ids.rs') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 81b3cfd22..d8a25e246 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -207,7 +207,7 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> { pub(crate) trait AstItemDef: ArenaId + Clone { fn interner(interner: &HirInterner) -> &LocationInterner, Self>; fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { - let items = ctx.db.file_items(ctx.file_id); + let items = ctx.db.ast_id_map(ctx.file_id); let item_id = items.ast_id(ast); Self::from_ast_id(ctx, item_id) } -- cgit v1.2.3 From 1325a31e34a3b4bf5104a743bcb8217ef5c4f3cd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 19:15:39 +0300 Subject: reduce visibility --- crates/ra_hir/src/ids.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir/src/ids.rs') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index d8a25e246..eb9939df7 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -21,7 +21,7 @@ pub struct HirInterner { consts: LocationInterner, ConstId>, statics: LocationInterner, StaticId>, traits: LocationInterner, TraitId>, - types: LocationInterner, TypeId>, + types: LocationInterner, TypeAliasId>, } impl HirInterner { @@ -229,7 +229,7 @@ pub(crate) trait AstItemDef: ArenaId + Clone { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct FunctionId(RawId); +pub(crate) struct FunctionId(RawId); impl_arena_id!(FunctionId); impl AstItemDef for FunctionId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -238,7 +238,7 @@ impl AstItemDef for FunctionId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StructId(RawId); +pub(crate) struct StructId(RawId); impl_arena_id!(StructId); impl AstItemDef for StructId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -247,7 +247,7 @@ impl AstItemDef for StructId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct EnumId(RawId); +pub(crate) struct EnumId(RawId); impl_arena_id!(EnumId); impl AstItemDef for EnumId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -256,7 +256,7 @@ impl AstItemDef for EnumId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ConstId(RawId); +pub(crate) struct ConstId(RawId); impl_arena_id!(ConstId); impl AstItemDef for ConstId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -265,7 +265,7 @@ impl AstItemDef for ConstId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StaticId(RawId); +pub(crate) struct StaticId(RawId); impl_arena_id!(StaticId); impl AstItemDef for StaticId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -274,7 +274,7 @@ impl AstItemDef for StaticId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TraitId(RawId); +pub(crate) struct TraitId(RawId); impl_arena_id!(TraitId); impl AstItemDef for TraitId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -283,9 +283,9 @@ impl AstItemDef for TraitId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TypeId(RawId); -impl_arena_id!(TypeId); -impl AstItemDef for TypeId { +pub(crate) struct TypeAliasId(RawId); +impl_arena_id!(TypeAliasId); +impl AstItemDef for TypeAliasId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { &interner.types } -- cgit v1.2.3