diff --git a/Cargo.lock b/Cargo.lock index a764336209..66ff1285a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1021,6 +1021,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "ra_arena 0.1.0", "ra_db 0.1.0", + "ra_hir_expand 0.1.0", "ra_prof 0.1.0", "ra_syntax 0.1.0", ] diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3f1c36941d..1a790b2f30 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -343,7 +343,7 @@ pub struct Struct { impl Struct { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -405,7 +405,7 @@ impl Union { } pub fn module(self, db: &impl HirDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn ty(self, db: &impl HirDatabase) -> Ty { @@ -431,7 +431,7 @@ pub struct Enum { impl Enum { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -697,7 +697,7 @@ impl FnData { impl Function { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -775,7 +775,7 @@ pub struct Const { impl Const { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -872,7 +872,7 @@ pub struct Static { impl Static { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -901,7 +901,7 @@ pub struct Trait { impl Trait { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn name(self, db: &impl DefDatabase) -> Option { @@ -1003,7 +1003,7 @@ pub struct TypeAlias { impl TypeAlias { pub fn module(self, db: &impl DefDatabase) -> Module { - self.id.module(db) + Module { id: self.id.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 8b33f25f7a..5c7f61eefb 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,9 +1,6 @@ //! FIXME: write short doc here -use ra_syntax::{ - ast::{self, AstNode}, - SyntaxNode, -}; +use ra_syntax::ast::{self, AstNode}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, @@ -12,26 +9,13 @@ use crate::{ ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub struct Source { - pub file_id: HirFileId, - pub ast: T, -} +pub use hir_def::Source; pub trait HasSource { type Ast; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; } -impl Source { - pub(crate) fn map U, U>(self, f: F) -> Source { - Source { file_id: self.file_id, ast: f(self.ast) } - } - pub(crate) fn file_syntax(&self, db: &impl AstDatabase) -> SyntaxNode { - db.parse_or_expand(self.file_id).expect("source created from invalid file") - } -} - /// NB: Module is !HasSource, because it has two source nodes at the same time: /// definition and declaration. impl Module { diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index da8ae6ef47..8f6cb2da7d 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -2,8 +2,8 @@ use std::sync::Arc; -use ra_db::{salsa, SourceDatabase}; -use ra_syntax::{ast, SmolStr}; +use ra_db::salsa; +use ra_syntax::SmolStr; use crate::{ adt::{EnumData, StructData}, @@ -23,34 +23,12 @@ use crate::{ Static, Struct, StructField, Trait, TypeAlias, }; +pub use hir_def::db::{InternDatabase, InternDatabaseStorage}; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, }; -/// We store all interned things in the single QueryGroup. -/// -/// This is done mainly to allow both "volatile" `AstDatabase` and "stable" -/// `DefDatabase` to access macros, without adding hard dependencies between the -/// two. -#[salsa::query_group(InternDatabaseStorage)] -pub trait InternDatabase: SourceDatabase { - #[salsa::interned] - fn intern_function(&self, loc: ids::ItemLoc) -> ids::FunctionId; - #[salsa::interned] - fn intern_struct(&self, loc: ids::ItemLoc) -> ids::StructId; - #[salsa::interned] - fn intern_enum(&self, loc: ids::ItemLoc) -> ids::EnumId; - #[salsa::interned] - fn intern_const(&self, loc: ids::ItemLoc) -> ids::ConstId; - #[salsa::interned] - fn intern_static(&self, loc: ids::ItemLoc) -> ids::StaticId; - #[salsa::interned] - fn intern_trait(&self, loc: ids::ItemLoc) -> ids::TraitId; - #[salsa::interned] - fn intern_type_alias(&self, loc: ids::ItemLoc) -> ids::TypeAliasId; -} - // This database uses `AstDatabase` internally, #[salsa::query_group(DefDatabaseStorage)] #[salsa::requires(AstDatabase)] diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 291f2a14ac..93713bb142 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -208,6 +208,6 @@ where let module_src = crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax()); let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?; - let ctx = LocationCtx::new(db, module, src.file_id); + let ctx = LocationCtx::new(db, module.id, src.file_id); Some(DEF::from_ast(ctx, &src.ast)) } diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index dea288eb7b..fe083c0c65 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -5,16 +5,12 @@ //! This module defines a bunch of ids we are using. The most important ones are //! probably `HirFileId` and `DefId`. -use std::hash::{Hash, Hasher}; - use ra_db::salsa; -use ra_syntax::{ast, AstNode}; -use crate::{ - db::{AstDatabase, InternDatabase}, - AstId, FileAstId, Module, Source, +pub use hir_def::{ + AstItemDef, ConstId, EnumId, FunctionId, ItemLoc, LocationCtx, StaticId, StructId, TraitId, + TypeAliasId, }; - pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind}; macro_rules! impl_intern_key { @@ -30,163 +26,6 @@ macro_rules! impl_intern_key { }; } -#[derive(Debug)] -pub struct ItemLoc { - pub(crate) module: Module, - ast_id: AstId, -} - -impl PartialEq for ItemLoc { - fn eq(&self, other: &Self) -> bool { - 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.ast_id.hash(hasher); - } -} - -impl Clone for ItemLoc { - fn clone(&self) -> ItemLoc { - ItemLoc { module: self.module, ast_id: self.ast_id } - } -} - -#[derive(Clone, Copy)] -pub(crate) struct LocationCtx { - db: DB, - module: Module, - file_id: HirFileId, -} - -impl<'a, DB> LocationCtx<&'a DB> { - pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> { - LocationCtx { db, module, file_id } - } -} - -impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> { - pub(crate) fn to_def(self, ast: &N) -> DEF - where - N: AstNode, - DEF: AstItemDef, - { - DEF::from_ast(self, ast) - } -} - -pub(crate) trait AstItemDef: salsa::InternKey + Clone { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self; - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc; - - fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self { - let items = ctx.db.ast_id_map(ctx.file_id); - let item_id = items.ast_id(ast); - Self::from_ast_id(ctx, item_id) - } - fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId) -> Self { - let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; - Self::intern(ctx.db, loc) - } - fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source { - let loc = self.lookup_intern(db); - let ast = loc.ast_id.to_node(db); - Source { file_id: loc.ast_id.file_id(), ast } - } - fn module(self, db: &impl InternDatabase) -> Module { - let loc = self.lookup_intern(db); - loc.module - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct FunctionId(salsa::InternId); -impl_intern_key!(FunctionId); - -impl AstItemDef for FunctionId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_function(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_function(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StructId(salsa::InternId); -impl_intern_key!(StructId); -impl AstItemDef for StructId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_struct(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_struct(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct EnumId(salsa::InternId); -impl_intern_key!(EnumId); -impl AstItemDef for EnumId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_enum(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_enum(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ConstId(salsa::InternId); -impl_intern_key!(ConstId); -impl AstItemDef for ConstId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_const(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_const(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StaticId(salsa::InternId); -impl_intern_key!(StaticId); -impl AstItemDef for StaticId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_static(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_static(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TraitId(salsa::InternId); -impl_intern_key!(TraitId); -impl AstItemDef for TraitId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_trait(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_trait(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TypeAliasId(salsa::InternId); -impl_intern_key!(TypeAliasId); -impl AstItemDef for TypeAliasId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_type_alias(loc) - } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_type_alias(self) - } -} - /// This exists just for Chalk, because Chalk just has a single `StructId` where /// we have different kinds of ADTs, primitive types and special type /// constructors like tuples and function pointers. diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index fde47c264a..06f21fc33e 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -129,7 +129,7 @@ impl ImplData { ) -> Self { let target_trait = node.target_trait().map(TypeRef::from_ast); let target_type = TypeRef::from_ast_opt(node.target_type()); - let ctx = LocationCtx::new(db, module, file_id); + let ctx = LocationCtx::new(db, module.id, file_id); let negative = node.is_negative(); let items = if let Some(item_list) = node.item_list() { item_list diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index dd5f9d4ba1..a94a0554c8 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -641,7 +641,7 @@ where fn define_def(&mut self, def: &raw::DefData) { let module = Module::new(self.def_collector.def_map.krate, self.module_id); - let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); + let ctx = LocationCtx::new(self.def_collector.db, module.id, self.file_id); macro_rules! def { ($kind:ident, $ast_id:ident) => { diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index a907d6a9f9..730c332264 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -64,7 +64,7 @@ fn def_with_body_from_child_node( ) -> Option { let src = crate::ModuleSource::from_child_node(db, file_id, node); let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?; - let ctx = LocationCtx::new(db, module, file_id.into()); + let ctx = LocationCtx::new(db, module.id, file_id.into()); node.ancestors().find_map(|node| { if let Some(def) = ast::FnDef::cast(node.clone()) { diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs index e39511518d..22f1880493 100644 --- a/crates/ra_hir/src/traits.rs +++ b/crates/ra_hir/src/traits.rs @@ -27,7 +27,7 @@ impl TraitData { let src = tr.source(db); let name = src.ast.name().map(|n| n.as_name()); let module = tr.module(db); - let ctx = LocationCtx::new(db, module, src.file_id); + let ctx = LocationCtx::new(db, module.id, src.file_id); let auto = src.ast.is_auto(); let items = if let Some(item_list) = src.ast.item_list() { item_list diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index aec484feb2..ab66515bee 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -162,11 +162,11 @@ impl ToChalk for Trait { type Chalk = chalk_ir::TraitId; fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId { - self.id.into() + chalk_ir::TraitId(id_to_chalk(self.id)) } fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait { - Trait { id: trait_id.into() } + Trait { id: id_from_chalk(trait_id.0) } } } @@ -198,11 +198,11 @@ impl ToChalk for TypeAlias { type Chalk = chalk_ir::TypeId; fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TypeId { - self.id.into() + chalk_ir::TypeId(id_to_chalk(self.id)) } - fn from_chalk(_db: &impl HirDatabase, impl_id: chalk_ir::TypeId) -> TypeAlias { - TypeAlias { id: impl_id.into() } + fn from_chalk(_db: &impl HirDatabase, type_alias_id: chalk_ir::TypeId) -> TypeAlias { + TypeAlias { id: id_from_chalk(type_alias_id.0) } } } @@ -775,30 +775,6 @@ fn id_to_chalk(salsa_id: T) -> chalk_ir::RawId { chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() } } -impl From for crate::ids::TraitId { - fn from(trait_id: chalk_ir::TraitId) -> Self { - id_from_chalk(trait_id.0) - } -} - -impl From for chalk_ir::TraitId { - fn from(trait_id: crate::ids::TraitId) -> Self { - chalk_ir::TraitId(id_to_chalk(trait_id)) - } -} - -impl From for crate::ids::TypeAliasId { - fn from(type_id: chalk_ir::TypeId) -> Self { - id_from_chalk(type_id.0) - } -} - -impl From for chalk_ir::TypeId { - fn from(type_id: crate::ids::TypeAliasId) -> Self { - chalk_ir::TypeId(id_to_chalk(type_id)) - } -} - impl From for crate::ids::TypeCtorId { fn from(struct_id: chalk_ir::StructId) -> Self { id_from_chalk(struct_id.0) diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml index f22a678eb7..75e93f2547 100644 --- a/crates/ra_hir_def/Cargo.toml +++ b/crates/ra_hir_def/Cargo.toml @@ -11,3 +11,4 @@ ra_arena = { path = "../ra_arena" } ra_db = { path = "../ra_db" } ra_syntax = { path = "../ra_syntax" } ra_prof = { path = "../ra_prof" } +hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs new file mode 100644 index 0000000000..f6f976c860 --- /dev/null +++ b/crates/ra_hir_def/src/db.rs @@ -0,0 +1,22 @@ +//! Defines database & queries for name resolution. + +use ra_db::{salsa, SourceDatabase}; +use ra_syntax::ast; + +#[salsa::query_group(InternDatabaseStorage)] +pub trait InternDatabase: SourceDatabase { + #[salsa::interned] + fn intern_function(&self, loc: crate::ItemLoc) -> crate::FunctionId; + #[salsa::interned] + fn intern_struct(&self, loc: crate::ItemLoc) -> crate::StructId; + #[salsa::interned] + fn intern_enum(&self, loc: crate::ItemLoc) -> crate::EnumId; + #[salsa::interned] + fn intern_const(&self, loc: crate::ItemLoc) -> crate::ConstId; + #[salsa::interned] + fn intern_static(&self, loc: crate::ItemLoc) -> crate::StaticId; + #[salsa::interned] + fn intern_trait(&self, loc: crate::ItemLoc) -> crate::TraitId; + #[salsa::interned] + fn intern_type_alias(&self, loc: crate::ItemLoc) -> crate::TypeAliasId; +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index f5dd2ae6fb..4d6b9db033 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -1,5 +1,37 @@ +//! `hir_def` crate contains everything between macro expansion and type +//! inference. +//! +//! It defines various items (structs, enums, traits) which comprises Rust code, +//! as well as an algorithm for resolving paths to such entities. +//! +//! Note that `hir_def` is a work in progress, so not all of the above is +//! actually true. + +pub mod db; + +use std::hash::{Hash, Hasher}; + +use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId}; use ra_arena::{impl_arena_id, RawId}; -use ra_db::CrateId; +use ra_db::{salsa, CrateId}; +use ra_syntax::{ast, AstNode, SyntaxNode}; + +use crate::db::InternDatabase; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Source { + pub file_id: HirFileId, + pub ast: T, +} + +impl Source { + pub fn map U, U>(self, f: F) -> Source { + Source { file_id: self.file_id, ast: f(self.ast) } + } + pub fn file_syntax(&self, db: &impl AstDatabase) -> SyntaxNode { + db.parse_or_expand(self.file_id).expect("source created from invalid file") + } +} #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ModuleId { @@ -12,3 +44,173 @@ pub struct ModuleId { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CrateModuleId(RawId); impl_arena_id!(CrateModuleId); + +macro_rules! impl_intern_key { + ($name:ident) => { + impl salsa::InternKey for $name { + fn from_intern_id(v: salsa::InternId) -> Self { + $name(v) + } + fn as_intern_id(&self) -> salsa::InternId { + self.0 + } + } + }; +} + +#[derive(Debug)] +pub struct ItemLoc { + pub(crate) module: ModuleId, + ast_id: AstId, +} + +impl PartialEq for ItemLoc { + fn eq(&self, other: &Self) -> bool { + 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.ast_id.hash(hasher); + } +} + +impl Clone for ItemLoc { + fn clone(&self) -> ItemLoc { + ItemLoc { module: self.module, ast_id: self.ast_id } + } +} + +#[derive(Clone, Copy)] +pub struct LocationCtx { + db: DB, + module: ModuleId, + file_id: HirFileId, +} + +impl<'a, DB> LocationCtx<&'a DB> { + pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> { + LocationCtx { db, module, file_id } + } +} + +impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> { + pub fn to_def(self, ast: &N) -> DEF + where + N: AstNode, + DEF: AstItemDef, + { + DEF::from_ast(self, ast) + } +} + +pub trait AstItemDef: salsa::InternKey + Clone { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self; + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc; + + fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self { + let items = ctx.db.ast_id_map(ctx.file_id); + let item_id = items.ast_id(ast); + Self::from_ast_id(ctx, item_id) + } + fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId) -> Self { + let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; + Self::intern(ctx.db, loc) + } + fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source { + let loc = self.lookup_intern(db); + let ast = loc.ast_id.to_node(db); + Source { file_id: loc.ast_id.file_id(), ast } + } + fn module(self, db: &impl InternDatabase) -> ModuleId { + let loc = self.lookup_intern(db); + loc.module + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct FunctionId(salsa::InternId); +impl_intern_key!(FunctionId); + +impl AstItemDef for FunctionId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_function(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_function(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct StructId(salsa::InternId); +impl_intern_key!(StructId); +impl AstItemDef for StructId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_struct(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_struct(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct EnumId(salsa::InternId); +impl_intern_key!(EnumId); +impl AstItemDef for EnumId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_enum(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_enum(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ConstId(salsa::InternId); +impl_intern_key!(ConstId); +impl AstItemDef for ConstId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_const(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_const(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct StaticId(salsa::InternId); +impl_intern_key!(StaticId); +impl AstItemDef for StaticId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_static(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_static(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TraitId(salsa::InternId); +impl_intern_key!(TraitId); +impl AstItemDef for TraitId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_trait(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_trait(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TypeAliasId(salsa::InternId); +impl_intern_key!(TypeAliasId); +impl AstItemDef for TypeAliasId { + fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { + db.intern_type_alias(loc) + } + fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { + db.lookup_intern_type_alias(self) + } +} diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 23dece73c2..956d8ce49c 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -50,7 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; - let krate = ctx.module.and_then(|m| m.krate(ctx.db)); + let krate = ctx.module.map(|m| m.krate()); if let Some(krate) = krate { ty.iterate_impl_items(ctx.db, krate, |item| { match item { diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index 7fc1b1efa8..b899ed3a5f 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -51,7 +51,7 @@ fn impls_for_def( } }; - let krate = module.krate(db)?; + let krate = module.krate(); let impls = db.impls_in_crate(krate); Some( @@ -72,7 +72,7 @@ fn impls_for_trait( let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() }; let tr = hir::Trait::from_source(db, src)?; - let krate = module.krate(db)?; + let krate = module.krate(); let impls = db.impls_in_crate(krate); Some( diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index 5665098499..4c57566e2a 100644 --- a/crates/ra_ide_api/src/parent_module.rs +++ b/crates/ra_ide_api/src/parent_module.rs @@ -27,10 +27,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec { Some(it) => it, None => return Vec::new(), }; - let krate = match module.krate(db) { - Some(it) => it, - None => return Vec::new(), - }; + let krate = module.krate(); vec![krate.crate_id()] } diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index b6eb248b74..dbd1af5976 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -120,7 +120,7 @@ impl NameDefinition { return SearchScope::new(res); } if vis.as_str() == "pub" { - let krate = self.container.krate(db).unwrap(); + let krate = self.container.krate(); let crate_graph = db.crate_graph(); for crate_id in crate_graph.iter() { let mut crate_deps = crate_graph.dependencies(crate_id);