Merge #2126
2126: Move ids to hir_def crate r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
f996b6019b
23 changed files with 402 additions and 363 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
|
@ -1003,6 +1003,7 @@ dependencies = [
|
|||
"ra_arena 0.1.0",
|
||||
"ra_cfg 0.1.0",
|
||||
"ra_db 0.1.0",
|
||||
"ra_hir_def 0.1.0",
|
||||
"ra_hir_expand 0.1.0",
|
||||
"ra_mbe 0.1.0",
|
||||
"ra_prof 0.1.0",
|
||||
|
|
@ -1013,6 +1014,18 @@ dependencies = [
|
|||
"test_utils 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ra_hir_def"
|
||||
version = "0.1.0"
|
||||
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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ra_hir_expand"
|
||||
version = "0.1.0"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ ra_db = { path = "../ra_db" }
|
|||
mbe = { path = "../ra_mbe", package = "ra_mbe" }
|
||||
tt = { path = "../ra_tt", package = "ra_tt" }
|
||||
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
|
||||
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
|
||||
test_utils = { path = "../test_utils" }
|
||||
ra_prof = { path = "../ra_prof" }
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ pub(crate) mod docs;
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::{CrateModuleId, ModuleId};
|
||||
use ra_db::{CrateId, Edition, FileId};
|
||||
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
|
||||
|
||||
|
|
@ -23,7 +24,7 @@ use crate::{
|
|||
BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64,
|
||||
U8, USIZE,
|
||||
},
|
||||
nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
|
||||
nameres::{ImportId, ModuleScope, Namespace},
|
||||
resolve::{Resolver, Scope, TypeNs},
|
||||
traits::TraitData,
|
||||
ty::{
|
||||
|
|
@ -67,8 +68,7 @@ impl Crate {
|
|||
|
||||
pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
|
||||
let module_id = db.crate_def_map(self).root();
|
||||
let module = Module { krate: self, module_id };
|
||||
Some(module)
|
||||
Some(Module::new(self, module_id))
|
||||
}
|
||||
|
||||
pub fn edition(self, db: &impl DefDatabase) -> Edition {
|
||||
|
|
@ -83,8 +83,7 @@ impl Crate {
|
|||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Module {
|
||||
pub(crate) krate: Crate,
|
||||
pub(crate) module_id: CrateModuleId,
|
||||
pub(crate) id: ModuleId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
|
|
@ -175,12 +174,16 @@ impl ModuleSource {
|
|||
}
|
||||
|
||||
impl Module {
|
||||
pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
|
||||
Module { id: ModuleId { krate: krate.crate_id, module_id: crate_module_id } }
|
||||
}
|
||||
|
||||
/// Name of this module.
|
||||
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let parent = def_map[self.module_id].parent?;
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let parent = def_map[self.id.module_id].parent?;
|
||||
def_map[parent].children.iter().find_map(|(name, module_id)| {
|
||||
if *module_id == self.module_id {
|
||||
if *module_id == self.id.module_id {
|
||||
Some(name.clone())
|
||||
} else {
|
||||
None
|
||||
|
|
@ -200,29 +203,29 @@ impl Module {
|
|||
}
|
||||
|
||||
/// Returns the crate this module is part of.
|
||||
pub fn krate(self, _db: &impl DefDatabase) -> Option<Crate> {
|
||||
Some(self.krate)
|
||||
pub fn krate(self) -> Crate {
|
||||
Crate { crate_id: self.id.krate }
|
||||
}
|
||||
|
||||
/// Topmost parent of this module. Every module has a `crate_root`, but some
|
||||
/// might be missing `krate`. This can happen if a module's file is not included
|
||||
/// in the module tree of any target in `Cargo.toml`.
|
||||
pub fn crate_root(self, db: &impl DefDatabase) -> Module {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
self.with_module_id(def_map.root())
|
||||
}
|
||||
|
||||
/// Finds a child module with the specified name.
|
||||
pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let child_id = def_map[self.module_id].children.get(name)?;
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let child_id = def_map[self.id.module_id].children.get(name)?;
|
||||
Some(self.with_module_id(*child_id))
|
||||
}
|
||||
|
||||
/// Iterates over all child modules.
|
||||
pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let children = def_map[self.module_id]
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let children = def_map[self.id.module_id]
|
||||
.children
|
||||
.iter()
|
||||
.map(|(_, module_id)| self.with_module_id(*module_id))
|
||||
|
|
@ -232,8 +235,8 @@ impl Module {
|
|||
|
||||
/// Finds a parent module.
|
||||
pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let parent_id = def_map[self.module_id].parent?;
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let parent_id = def_map[self.id.module_id].parent?;
|
||||
Some(self.with_module_id(parent_id))
|
||||
}
|
||||
|
||||
|
|
@ -249,11 +252,11 @@ impl Module {
|
|||
|
||||
/// Returns a `ModuleScope`: a set of items, visible in this module.
|
||||
pub fn scope(self, db: &impl HirDatabase) -> ModuleScope {
|
||||
db.crate_def_map(self.krate)[self.module_id].scope.clone()
|
||||
db.crate_def_map(self.krate())[self.id.module_id].scope.clone()
|
||||
}
|
||||
|
||||
pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
|
||||
db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink);
|
||||
db.crate_def_map(self.krate()).add_diagnostics(db, self.id.module_id, sink);
|
||||
for decl in self.declarations(db) {
|
||||
match decl {
|
||||
crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
|
||||
|
|
@ -277,13 +280,13 @@ impl Module {
|
|||
}
|
||||
|
||||
pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
Resolver::default().push_module_scope(def_map, self.module_id)
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
Resolver::default().push_module_scope(def_map, self.id.module_id)
|
||||
}
|
||||
|
||||
pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
def_map[self.module_id]
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
def_map[self.id.module_id]
|
||||
.scope
|
||||
.entries()
|
||||
.filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
|
||||
|
|
@ -303,7 +306,7 @@ impl Module {
|
|||
}
|
||||
|
||||
fn with_module_id(self, module_id: CrateModuleId) -> Module {
|
||||
Module { module_id, krate: self.krate }
|
||||
Module::new(self.krate(), module_id)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -340,11 +343,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
|
||||
|
|
@ -402,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 {
|
||||
|
|
@ -428,11 +431,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
|
||||
|
|
@ -523,12 +526,14 @@ impl Adt {
|
|||
}
|
||||
|
||||
pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
|
||||
match self {
|
||||
Adt::Struct(s) => s.module(db),
|
||||
Adt::Union(s) => s.module(db),
|
||||
Adt::Enum(e) => e.module(db),
|
||||
}
|
||||
.krate(db)
|
||||
Some(
|
||||
match self {
|
||||
Adt::Struct(s) => s.module(db),
|
||||
Adt::Union(s) => s.module(db),
|
||||
Adt::Enum(e) => e.module(db),
|
||||
}
|
||||
.krate(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
|
||||
|
|
@ -692,11 +697,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
pub fn name(self, db: &impl HirDatabase) -> Name {
|
||||
|
|
@ -770,11 +775,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
|
||||
|
|
@ -867,11 +872,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
|
||||
|
|
@ -896,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<Name> {
|
||||
|
|
@ -998,11 +1003,11 @@ 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<Crate> {
|
||||
self.module(db).krate(db)
|
||||
Some(self.module(db).krate())
|
||||
}
|
||||
|
||||
/// The containing impl block, if this is a method.
|
||||
|
|
|
|||
|
|
@ -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,34 +9,21 @@ use crate::{
|
|||
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Source<T> {
|
||||
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<Self::Ast>;
|
||||
}
|
||||
|
||||
impl<T> Source<T> {
|
||||
pub(crate) fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
|
||||
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 {
|
||||
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
||||
pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let decl_id = def_map[self.module_id].declaration;
|
||||
let file_id = def_map[self.module_id].definition;
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let decl_id = def_map[self.id.module_id].declaration;
|
||||
let file_id = def_map[self.id.module_id].definition;
|
||||
let ast = ModuleSource::new(db, file_id, decl_id);
|
||||
let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
|
||||
Source { file_id, ast }
|
||||
|
|
@ -51,8 +35,8 @@ impl Module {
|
|||
self,
|
||||
db: &(impl DefDatabase + AstDatabase),
|
||||
) -> Option<Source<ast::Module>> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let decl = def_map[self.module_id].declaration?;
|
||||
let def_map = db.crate_def_map(self.krate());
|
||||
let decl = def_map[self.id.module_id].declaration?;
|
||||
let ast = decl.to_node(db);
|
||||
Some(Source { file_id: decl.file_id(), ast })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,40 +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<ast::FnDef>) -> ids::FunctionId;
|
||||
#[salsa::interned]
|
||||
fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
|
||||
#[salsa::interned]
|
||||
fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
|
||||
#[salsa::interned]
|
||||
fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
|
||||
#[salsa::interned]
|
||||
fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
|
||||
#[salsa::interned]
|
||||
fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
|
||||
#[salsa::interned]
|
||||
fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
|
||||
|
||||
// Interned IDs for Chalk integration
|
||||
#[salsa::interned]
|
||||
fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
|
||||
#[salsa::interned]
|
||||
fn intern_impl(&self, impl_: Impl) -> ids::GlobalImplId;
|
||||
}
|
||||
|
||||
// This database uses `AstDatabase` internally,
|
||||
#[salsa::query_group(DefDatabaseStorage)]
|
||||
#[salsa::requires(AstDatabase)]
|
||||
|
|
@ -176,6 +148,12 @@ pub trait HirDatabase: DefDatabase + AstDatabase {
|
|||
#[salsa::invoke(crate::ty::traits::trait_solver_query)]
|
||||
fn trait_solver(&self, krate: Crate) -> crate::ty::traits::TraitSolver;
|
||||
|
||||
// Interned IDs for Chalk integration
|
||||
#[salsa::interned]
|
||||
fn intern_type_ctor(&self, type_ctor: TypeCtor) -> ids::TypeCtorId;
|
||||
#[salsa::interned]
|
||||
fn intern_impl(&self, impl_: Impl) -> ids::GlobalImplId;
|
||||
|
||||
#[salsa::invoke(crate::ty::traits::chalk::associated_ty_data_query)]
|
||||
fn associated_ty_data(&self, id: chalk_ir::TypeId) -> Arc<chalk_rust_ir::AssociatedTyDatum>;
|
||||
|
||||
|
|
|
|||
|
|
@ -195,7 +195,7 @@ impl Module {
|
|||
.find_map(|krate| {
|
||||
let def_map = db.crate_def_map(krate);
|
||||
let module_id = def_map.find_module_by_source(src.file_id, decl_id)?;
|
||||
Some(Module { krate, module_id })
|
||||
Some(Module::new(krate, module_id))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<N: AstNode> {
|
||||
pub(crate) module: Module,
|
||||
ast_id: AstId<N>,
|
||||
}
|
||||
|
||||
impl<N: AstNode> PartialEq for ItemLoc<N> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.module == other.module && self.ast_id == other.ast_id
|
||||
}
|
||||
}
|
||||
impl<N: AstNode> Eq for ItemLoc<N> {}
|
||||
impl<N: AstNode> Hash for ItemLoc<N> {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.module.hash(hasher);
|
||||
self.ast_id.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: AstNode> Clone for ItemLoc<N> {
|
||||
fn clone(&self) -> ItemLoc<N> {
|
||||
ItemLoc { module: self.module, ast_id: self.ast_id }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct LocationCtx<DB> {
|
||||
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<N, DEF>(self, ast: &N) -> DEF
|
||||
where
|
||||
N: AstNode,
|
||||
DEF: AstItemDef<N>,
|
||||
{
|
||||
DEF::from_ast(self, ast)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
|
||||
|
||||
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<N>) -> 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<N> {
|
||||
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<ast::FnDef> for FunctionId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
|
||||
db.intern_function(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
|
||||
db.lookup_intern_function(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StructId(salsa::InternId);
|
||||
impl_intern_key!(StructId);
|
||||
impl AstItemDef<ast::StructDef> for StructId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
|
||||
db.intern_struct(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
|
||||
db.lookup_intern_struct(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct EnumId(salsa::InternId);
|
||||
impl_intern_key!(EnumId);
|
||||
impl AstItemDef<ast::EnumDef> for EnumId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
|
||||
db.intern_enum(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
|
||||
db.lookup_intern_enum(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ConstId(salsa::InternId);
|
||||
impl_intern_key!(ConstId);
|
||||
impl AstItemDef<ast::ConstDef> for ConstId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
|
||||
db.intern_const(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
|
||||
db.lookup_intern_const(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StaticId(salsa::InternId);
|
||||
impl_intern_key!(StaticId);
|
||||
impl AstItemDef<ast::StaticDef> for StaticId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
|
||||
db.intern_static(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
|
||||
db.lookup_intern_static(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TraitId(salsa::InternId);
|
||||
impl_intern_key!(TraitId);
|
||||
impl AstItemDef<ast::TraitDef> for TraitId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
|
||||
db.intern_trait(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
|
||||
db.lookup_intern_trait(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TypeAliasId(salsa::InternId);
|
||||
impl_intern_key!(TypeAliasId);
|
||||
impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
|
||||
db.intern_type_alias(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -182,7 +182,7 @@ impl ModuleImplBlocks {
|
|||
) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
|
||||
let mut source_map = ImplSourceMap::default();
|
||||
let crate_graph = db.crate_graph();
|
||||
let cfg_options = crate_graph.cfg_options(module.krate.crate_id());
|
||||
let cfg_options = crate_graph.cfg_options(module.id.krate);
|
||||
|
||||
let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map);
|
||||
(Arc::new(result), Arc::new(source_map))
|
||||
|
|
|
|||
|
|
@ -22,14 +22,14 @@ pub enum LangItemTarget {
|
|||
|
||||
impl LangItemTarget {
|
||||
pub(crate) fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
|
||||
match self {
|
||||
LangItemTarget::Enum(e) => e.module(db).krate(db),
|
||||
LangItemTarget::Function(f) => f.module(db).krate(db),
|
||||
LangItemTarget::ImplBlock(i) => i.module().krate(db),
|
||||
LangItemTarget::Static(s) => s.module(db).krate(db),
|
||||
LangItemTarget::Struct(s) => s.module(db).krate(db),
|
||||
LangItemTarget::Trait(t) => t.module(db).krate(db),
|
||||
}
|
||||
Some(match self {
|
||||
LangItemTarget::Enum(e) => e.module(db).krate(),
|
||||
LangItemTarget::Function(f) => f.module(db).krate(),
|
||||
LangItemTarget::ImplBlock(i) => i.module().krate(),
|
||||
LangItemTarget::Static(s) => s.module(db).krate(),
|
||||
LangItemTarget::Struct(s) => s.module(db).krate(),
|
||||
LangItemTarget::Trait(t) => t.module(db).krate(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,8 +56,9 @@ mod tests;
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::CrateModuleId;
|
||||
use once_cell::sync::Lazy;
|
||||
use ra_arena::{impl_arena_id, Arena, RawId};
|
||||
use ra_arena::Arena;
|
||||
use ra_db::{Edition, FileId};
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::ast;
|
||||
|
|
@ -115,13 +116,8 @@ impl std::ops::Index<CrateModuleId> for CrateDefMap {
|
|||
}
|
||||
}
|
||||
|
||||
/// An ID of a module, **local** to a specific crate
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub(crate) struct CrateModuleId(RawId);
|
||||
impl_arena_id!(CrateModuleId);
|
||||
|
||||
#[derive(Default, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct ModuleData {
|
||||
pub struct ModuleData {
|
||||
pub(crate) parent: Option<CrateModuleId>,
|
||||
pub(crate) children: FxHashMap<Name, CrateModuleId>,
|
||||
pub(crate) scope: ModuleScope,
|
||||
|
|
@ -335,7 +331,7 @@ impl CrateDefMap {
|
|||
PathKind::DollarCrate(krate) => {
|
||||
if krate == self.krate {
|
||||
tested_by!(macro_dollar_crate_self);
|
||||
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
|
||||
PerNs::types(Module::new(self.krate, self.root).into())
|
||||
} else {
|
||||
match krate.root_module(db) {
|
||||
Some(module) => {
|
||||
|
|
@ -346,12 +342,8 @@ impl CrateDefMap {
|
|||
}
|
||||
}
|
||||
}
|
||||
PathKind::Crate => {
|
||||
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
|
||||
}
|
||||
PathKind::Self_ => {
|
||||
PerNs::types(Module { krate: self.krate, module_id: original_module }.into())
|
||||
}
|
||||
PathKind::Crate => PerNs::types(Module::new(self.krate, self.root).into()),
|
||||
PathKind::Self_ => PerNs::types(Module::new(self.krate, original_module).into()),
|
||||
// plain import or absolute path in 2015: crate-relative with
|
||||
// fallback to extern prelude (with the simplification in
|
||||
// rust-lang/rust#57745)
|
||||
|
|
@ -377,7 +369,7 @@ impl CrateDefMap {
|
|||
}
|
||||
PathKind::Super => {
|
||||
if let Some(p) = self.modules[original_module].parent {
|
||||
PerNs::types(Module { krate: self.krate, module_id: p }.into())
|
||||
PerNs::types(Module::new(self.krate, p).into())
|
||||
} else {
|
||||
log::debug!("super path in root module");
|
||||
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
||||
|
|
@ -419,12 +411,12 @@ impl CrateDefMap {
|
|||
|
||||
curr_per_ns = match curr {
|
||||
ModuleDef::Module(module) => {
|
||||
if module.krate != self.krate {
|
||||
if module.krate() != self.krate {
|
||||
let path =
|
||||
Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ };
|
||||
log::debug!("resolving {:?} in other crate", path);
|
||||
let defp_map = db.crate_def_map(module.krate);
|
||||
let (def, s) = defp_map.resolve_path(db, module.module_id, &path);
|
||||
let defp_map = db.crate_def_map(module.krate());
|
||||
let (def, s) = defp_map.resolve_path(db, module.id.module_id, &path);
|
||||
return ResolvePathResult::with(
|
||||
def,
|
||||
ReachedFixedPoint::Yes,
|
||||
|
|
@ -433,7 +425,7 @@ impl CrateDefMap {
|
|||
}
|
||||
|
||||
// Since it is a qualified path here, it should not contains legacy macros
|
||||
match self[module.module_id].scope.get(&segment.name) {
|
||||
match self[module.id.module_id].scope.get(&segment.name) {
|
||||
Some(res) => res.def,
|
||||
_ => {
|
||||
log::debug!("path segment {:?} not found", segment.name);
|
||||
|
|
@ -511,14 +503,14 @@ impl CrateDefMap {
|
|||
fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs {
|
||||
if let Some(prelude) = self.prelude {
|
||||
let keep;
|
||||
let def_map = if prelude.krate == self.krate {
|
||||
let def_map = if prelude.krate() == self.krate {
|
||||
self
|
||||
} else {
|
||||
// Extend lifetime
|
||||
keep = db.crate_def_map(prelude.krate);
|
||||
keep = db.crate_def_map(prelude.krate());
|
||||
&keep
|
||||
};
|
||||
def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def)
|
||||
def_map[prelude.id.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def)
|
||||
} else {
|
||||
PerNs::none()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ where
|
|||
|
||||
if let Some(ModuleDef::Module(m)) = res.take_types() {
|
||||
tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use);
|
||||
self.import_all_macros_exported(current_module_id, m.krate);
|
||||
self.import_all_macros_exported(current_module_id, m.krate());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -289,11 +289,11 @@ where
|
|||
if import.is_prelude {
|
||||
tested_by!(std_prelude);
|
||||
self.def_map.prelude = Some(m);
|
||||
} else if m.krate != self.def_map.krate {
|
||||
} else if m.krate() != self.def_map.krate {
|
||||
tested_by!(glob_across_crates);
|
||||
// glob import from other crate => we can just import everything once
|
||||
let item_map = self.db.crate_def_map(m.krate);
|
||||
let scope = &item_map[m.module_id].scope;
|
||||
let item_map = self.db.crate_def_map(m.krate());
|
||||
let scope = &item_map[m.id.module_id].scope;
|
||||
|
||||
// Module scoped macros is included
|
||||
let items = scope
|
||||
|
|
@ -307,7 +307,7 @@ where
|
|||
// glob import from same crate => we do an initial
|
||||
// import, and then need to propagate any further
|
||||
// additions
|
||||
let scope = &self.def_map[m.module_id].scope;
|
||||
let scope = &self.def_map[m.id.module_id].scope;
|
||||
|
||||
// Module scoped macros is included
|
||||
let items = scope
|
||||
|
|
@ -319,7 +319,7 @@ where
|
|||
self.update(module_id, Some(import_id), &items);
|
||||
// record the glob import in case we add further items
|
||||
self.glob_imports
|
||||
.entry(m.module_id)
|
||||
.entry(m.id.module_id)
|
||||
.or_default()
|
||||
.push((module_id, import_id));
|
||||
}
|
||||
|
|
@ -523,9 +523,10 @@ where
|
|||
|
||||
// Prelude module is always considered to be `#[macro_use]`.
|
||||
if let Some(prelude_module) = self.def_collector.def_map.prelude {
|
||||
if prelude_module.krate != self.def_collector.def_map.krate {
|
||||
if prelude_module.krate() != self.def_collector.def_map.krate {
|
||||
tested_by!(prelude_is_macro_use);
|
||||
self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate);
|
||||
self.def_collector
|
||||
.import_all_macros_exported(self.module_id, prelude_module.krate());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -631,9 +632,7 @@ where
|
|||
modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone();
|
||||
modules[self.module_id].children.insert(name.clone(), res);
|
||||
let resolution = Resolution {
|
||||
def: PerNs::types(
|
||||
Module { krate: self.def_collector.def_map.krate, module_id: res }.into(),
|
||||
),
|
||||
def: PerNs::types(Module::new(self.def_collector.def_map.krate, res).into()),
|
||||
import: None,
|
||||
};
|
||||
self.def_collector.update(self.module_id, None, &[(name, resolution)]);
|
||||
|
|
@ -641,8 +640,8 @@ where
|
|||
}
|
||||
|
||||
fn define_def(&mut self, def: &raw::DefData) {
|
||||
let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id };
|
||||
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
|
||||
let module = Module::new(self.def_collector.def_map.krate, self.module_id);
|
||||
let ctx = LocationCtx::new(self.def_collector.db, module.id, self.file_id);
|
||||
|
||||
macro_rules! def {
|
||||
($kind:ident, $ast_id:ident) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
//! Name resolution.
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::CrateModuleId;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -13,7 +14,7 @@ use crate::{
|
|||
generics::GenericParams,
|
||||
impl_block::ImplBlock,
|
||||
name::{Name, SELF_PARAM, SELF_TYPE},
|
||||
nameres::{CrateDefMap, CrateModuleId, PerNs},
|
||||
nameres::{CrateDefMap, PerNs},
|
||||
path::{Path, PathKind},
|
||||
Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct,
|
||||
Trait, TypeAlias,
|
||||
|
|
@ -330,8 +331,8 @@ impl Resolver {
|
|||
for scope in &self.scopes {
|
||||
if let Scope::ModuleScope(m) = scope {
|
||||
if let Some(prelude) = m.crate_def_map.prelude() {
|
||||
let prelude_def_map = db.crate_def_map(prelude.krate);
|
||||
traits.extend(prelude_def_map[prelude.module_id].scope.traits());
|
||||
let prelude_def_map = db.crate_def_map(prelude.krate());
|
||||
traits.extend(prelude_def_map[prelude.id.module_id].scope.traits());
|
||||
}
|
||||
traits.extend(m.crate_def_map[m.module_id].scope.traits());
|
||||
}
|
||||
|
|
@ -444,10 +445,12 @@ impl Scope {
|
|||
f(name.clone(), ScopeDef::ModuleDef(*def));
|
||||
});
|
||||
if let Some(prelude) = m.crate_def_map.prelude() {
|
||||
let prelude_def_map = db.crate_def_map(prelude.krate);
|
||||
prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
|
||||
f(name.clone(), res.def.into());
|
||||
});
|
||||
let prelude_def_map = db.crate_def_map(prelude.krate());
|
||||
prelude_def_map[prelude.id.module_id].scope.entries().for_each(
|
||||
|(name, res)| {
|
||||
f(name.clone(), res.def.into());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Scope::GenericParams(gp) => {
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ fn def_with_body_from_child_node(
|
|||
) -> Option<DefWithBody> {
|
||||
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()) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use hir_def::CrateModuleId;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitRef};
|
||||
use crate::{
|
||||
db::HirDatabase,
|
||||
impl_block::{ImplBlock, ImplId},
|
||||
nameres::CrateModuleId,
|
||||
resolve::Resolver,
|
||||
ty::primitive::{FloatBitness, UncertainFloatTy, UncertainIntTy},
|
||||
ty::{Ty, TypeCtor},
|
||||
|
|
@ -50,7 +50,7 @@ impl CrateImplBlocks {
|
|||
let fingerprint = TyFingerprint::for_impl(ty);
|
||||
fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flat_map(|i| i.iter()).map(
|
||||
move |(module_id, impl_id)| {
|
||||
let module = Module { krate: self.krate, module_id: *module_id };
|
||||
let module = Module::new(self.krate, *module_id);
|
||||
ImplBlock::from_id(module, *impl_id)
|
||||
},
|
||||
)
|
||||
|
|
@ -62,7 +62,7 @@ impl CrateImplBlocks {
|
|||
) -> impl Iterator<Item = ImplBlock> + 'a {
|
||||
self.impls_by_trait.get(&tr).into_iter().flat_map(|i| i.iter()).map(
|
||||
move |(module_id, impl_id)| {
|
||||
let module = Module { krate: self.krate, module_id: *module_id };
|
||||
let module = Module::new(self.krate, *module_id);
|
||||
ImplBlock::from_id(module, *impl_id)
|
||||
},
|
||||
)
|
||||
|
|
@ -71,7 +71,7 @@ impl CrateImplBlocks {
|
|||
pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplBlock> + 'a {
|
||||
self.impls.values().chain(self.impls_by_trait.values()).flat_map(|i| i.iter()).map(
|
||||
move |(module_id, impl_id)| {
|
||||
let module = Module { krate: self.krate, module_id: *module_id };
|
||||
let module = Module::new(self.krate, *module_id);
|
||||
ImplBlock::from_id(module, *impl_id)
|
||||
},
|
||||
)
|
||||
|
|
@ -90,14 +90,14 @@ impl CrateImplBlocks {
|
|||
self.impls_by_trait
|
||||
.entry(tr.trait_)
|
||||
.or_insert_with(Vec::new)
|
||||
.push((module.module_id, impl_id));
|
||||
.push((module.id.module_id, impl_id));
|
||||
}
|
||||
} else {
|
||||
if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) {
|
||||
self.impls
|
||||
.entry(target_ty_fp)
|
||||
.or_insert_with(Vec::new)
|
||||
.push((module.module_id, impl_id));
|
||||
.push((module.id.module_id, impl_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -537,7 +537,7 @@ pub(crate) fn trait_datum_query(
|
|||
let trait_ref = trait_.trait_ref(db).subst(&bound_vars).to_chalk(db);
|
||||
let flags = chalk_rust_ir::TraitFlags {
|
||||
auto: trait_.is_auto(db),
|
||||
upstream: trait_.module(db).krate(db) != Some(krate),
|
||||
upstream: trait_.module(db).krate() != krate,
|
||||
non_enumerable: true,
|
||||
// FIXME set these flags correctly
|
||||
marker: false,
|
||||
|
|
@ -625,7 +625,7 @@ fn impl_block_datum(
|
|||
.target_trait_ref(db)
|
||||
.expect("FIXME handle unresolved impl block trait ref")
|
||||
.subst(&bound_vars);
|
||||
let impl_type = if impl_block.module().krate(db) == Some(krate) {
|
||||
let impl_type = if impl_block.module().krate() == krate {
|
||||
chalk_rust_ir::ImplType::Local
|
||||
} else {
|
||||
chalk_rust_ir::ImplType::External
|
||||
|
|
@ -775,30 +775,6 @@ fn id_to_chalk<T: InternKey>(salsa_id: T) -> chalk_ir::RawId {
|
|||
chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() }
|
||||
}
|
||||
|
||||
impl From<chalk_ir::TraitId> for crate::ids::TraitId {
|
||||
fn from(trait_id: chalk_ir::TraitId) -> Self {
|
||||
id_from_chalk(trait_id.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::ids::TraitId> for chalk_ir::TraitId {
|
||||
fn from(trait_id: crate::ids::TraitId) -> Self {
|
||||
chalk_ir::TraitId(id_to_chalk(trait_id))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<chalk_ir::TypeId> for crate::ids::TypeAliasId {
|
||||
fn from(type_id: chalk_ir::TypeId) -> Self {
|
||||
id_from_chalk(type_id.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::ids::TypeAliasId> for chalk_ir::TypeId {
|
||||
fn from(type_id: crate::ids::TypeAliasId) -> Self {
|
||||
chalk_ir::TypeId(id_to_chalk(type_id))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<chalk_ir::StructId> for crate::ids::TypeCtorId {
|
||||
fn from(struct_id: chalk_ir::StructId) -> Self {
|
||||
id_from_chalk(struct_id.0)
|
||||
|
|
|
|||
14
crates/ra_hir_def/Cargo.toml
Normal file
14
crates/ra_hir_def/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
edition = "2018"
|
||||
name = "ra_hir_def"
|
||||
version = "0.1.0"
|
||||
authors = ["rust-analyzer developers"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.5"
|
||||
|
||||
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" }
|
||||
22
crates/ra_hir_def/src/db.rs
Normal file
22
crates/ra_hir_def/src/db.rs
Normal file
|
|
@ -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<ast::FnDef>) -> crate::FunctionId;
|
||||
#[salsa::interned]
|
||||
fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId;
|
||||
#[salsa::interned]
|
||||
fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId;
|
||||
#[salsa::interned]
|
||||
fn intern_const(&self, loc: crate::ItemLoc<ast::ConstDef>) -> crate::ConstId;
|
||||
#[salsa::interned]
|
||||
fn intern_static(&self, loc: crate::ItemLoc<ast::StaticDef>) -> crate::StaticId;
|
||||
#[salsa::interned]
|
||||
fn intern_trait(&self, loc: crate::ItemLoc<ast::TraitDef>) -> crate::TraitId;
|
||||
#[salsa::interned]
|
||||
fn intern_type_alias(&self, loc: crate::ItemLoc<ast::TypeAliasDef>) -> crate::TypeAliasId;
|
||||
}
|
||||
216
crates/ra_hir_def/src/lib.rs
Normal file
216
crates/ra_hir_def/src/lib.rs
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
//! `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::{salsa, CrateId};
|
||||
use ra_syntax::{ast, AstNode, SyntaxNode};
|
||||
|
||||
use crate::db::InternDatabase;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Source<T> {
|
||||
pub file_id: HirFileId,
|
||||
pub ast: T,
|
||||
}
|
||||
|
||||
impl<T> Source<T> {
|
||||
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
|
||||
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 {
|
||||
pub krate: CrateId,
|
||||
pub module_id: CrateModuleId,
|
||||
}
|
||||
|
||||
/// An ID of a module, **local** to a specific crate
|
||||
// FIXME: rename to `LocalModuleId`.
|
||||
#[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<N: AstNode> {
|
||||
pub(crate) module: ModuleId,
|
||||
ast_id: AstId<N>,
|
||||
}
|
||||
|
||||
impl<N: AstNode> PartialEq for ItemLoc<N> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.module == other.module && self.ast_id == other.ast_id
|
||||
}
|
||||
}
|
||||
impl<N: AstNode> Eq for ItemLoc<N> {}
|
||||
impl<N: AstNode> Hash for ItemLoc<N> {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.module.hash(hasher);
|
||||
self.ast_id.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: AstNode> Clone for ItemLoc<N> {
|
||||
fn clone(&self) -> ItemLoc<N> {
|
||||
ItemLoc { module: self.module, ast_id: self.ast_id }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct LocationCtx<DB> {
|
||||
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<N, DEF>(self, ast: &N) -> DEF
|
||||
where
|
||||
N: AstNode,
|
||||
DEF: AstItemDef<N>,
|
||||
{
|
||||
DEF::from_ast(self, ast)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
|
||||
|
||||
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<N>) -> 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<N> {
|
||||
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<ast::FnDef> for FunctionId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
|
||||
db.intern_function(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
|
||||
db.lookup_intern_function(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StructId(salsa::InternId);
|
||||
impl_intern_key!(StructId);
|
||||
impl AstItemDef<ast::StructDef> for StructId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
|
||||
db.intern_struct(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
|
||||
db.lookup_intern_struct(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct EnumId(salsa::InternId);
|
||||
impl_intern_key!(EnumId);
|
||||
impl AstItemDef<ast::EnumDef> for EnumId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
|
||||
db.intern_enum(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
|
||||
db.lookup_intern_enum(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ConstId(salsa::InternId);
|
||||
impl_intern_key!(ConstId);
|
||||
impl AstItemDef<ast::ConstDef> for ConstId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
|
||||
db.intern_const(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
|
||||
db.lookup_intern_const(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StaticId(salsa::InternId);
|
||||
impl_intern_key!(StaticId);
|
||||
impl AstItemDef<ast::StaticDef> for StaticId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
|
||||
db.intern_static(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
|
||||
db.lookup_intern_static(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TraitId(salsa::InternId);
|
||||
impl_intern_key!(TraitId);
|
||||
impl AstItemDef<ast::TraitDef> for TraitId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
|
||||
db.intern_trait(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
|
||||
db.lookup_intern_trait(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TypeAliasId(salsa::InternId);
|
||||
impl_intern_key!(TypeAliasId);
|
||||
impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
|
||||
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
|
||||
db.intern_type_alias(loc)
|
||||
}
|
||||
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
|
||||
db.lookup_intern_type_alias(self)
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -27,10 +27,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
|||
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()]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue