aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-30 09:27:54 +0000
committerAleksey Kladov <[email protected]>2019-10-30 09:27:54 +0000
commita136cc0653d2b4133fb6387009cfdbaf3e2cf275 (patch)
treec7693867220abfefd90acb9965d91c6fe0023d38 /crates
parent56bc874f1d14922686b26afc8793b7e57a652990 (diff)
introduce ra_hir_def
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/Cargo.toml1
-rw-r--r--crates/ra_hir/src/code_model.rs77
-rw-r--r--crates/ra_hir/src/code_model/src.rs10
-rw-r--r--crates/ra_hir/src/from_source.rs2
-rw-r--r--crates/ra_hir/src/impl_block.rs2
-rw-r--r--crates/ra_hir/src/lang_item.rs16
-rw-r--r--crates/ra_hir/src/nameres.rs36
-rw-r--r--crates/ra_hir/src/nameres/collector.rs23
-rw-r--r--crates/ra_hir/src/resolve.rs17
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs12
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs4
-rw-r--r--crates/ra_hir_def/Cargo.toml13
-rw-r--r--crates/ra_hir_def/src/lib.rs14
13 files changed, 127 insertions, 100 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index 143dae6bd..5df371bc0 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -20,6 +20,7 @@ ra_db = { path = "../ra_db" }
20mbe = { path = "../ra_mbe", package = "ra_mbe" } 20mbe = { path = "../ra_mbe", package = "ra_mbe" }
21tt = { path = "../ra_tt", package = "ra_tt" } 21tt = { path = "../ra_tt", package = "ra_tt" }
22hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } 22hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
23hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
23test_utils = { path = "../test_utils" } 24test_utils = { path = "../test_utils" }
24ra_prof = { path = "../ra_prof" } 25ra_prof = { path = "../ra_prof" }
25 26
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 8eb3c577d..3f1c36941 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -5,6 +5,7 @@ pub(crate) mod docs;
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use hir_def::{CrateModuleId, ModuleId};
8use ra_db::{CrateId, Edition, FileId}; 9use ra_db::{CrateId, Edition, FileId};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 10use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
10 11
@@ -23,7 +24,7 @@ use crate::{
23 BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64, 24 BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64,
24 U8, USIZE, 25 U8, USIZE,
25 }, 26 },
26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace}, 27 nameres::{ImportId, ModuleScope, Namespace},
27 resolve::{Resolver, Scope, TypeNs}, 28 resolve::{Resolver, Scope, TypeNs},
28 traits::TraitData, 29 traits::TraitData,
29 ty::{ 30 ty::{
@@ -67,8 +68,7 @@ impl Crate {
67 68
68 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 69 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
69 let module_id = db.crate_def_map(self).root(); 70 let module_id = db.crate_def_map(self).root();
70 let module = Module { krate: self, module_id }; 71 Some(Module::new(self, module_id))
71 Some(module)
72 } 72 }
73 73
74 pub fn edition(self, db: &impl DefDatabase) -> Edition { 74 pub fn edition(self, db: &impl DefDatabase) -> Edition {
@@ -83,8 +83,7 @@ impl Crate {
83 83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 84#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
85pub struct Module { 85pub struct Module {
86 pub(crate) krate: Crate, 86 pub(crate) id: ModuleId,
87 pub(crate) module_id: CrateModuleId,
88} 87}
89 88
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -175,12 +174,16 @@ impl ModuleSource {
175} 174}
176 175
177impl Module { 176impl Module {
177 pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
178 Module { id: ModuleId { krate: krate.crate_id, module_id: crate_module_id } }
179 }
180
178 /// Name of this module. 181 /// Name of this module.
179 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 182 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
180 let def_map = db.crate_def_map(self.krate); 183 let def_map = db.crate_def_map(self.krate());
181 let parent = def_map[self.module_id].parent?; 184 let parent = def_map[self.id.module_id].parent?;
182 def_map[parent].children.iter().find_map(|(name, module_id)| { 185 def_map[parent].children.iter().find_map(|(name, module_id)| {
183 if *module_id == self.module_id { 186 if *module_id == self.id.module_id {
184 Some(name.clone()) 187 Some(name.clone())
185 } else { 188 } else {
186 None 189 None
@@ -200,29 +203,29 @@ impl Module {
200 } 203 }
201 204
202 /// Returns the crate this module is part of. 205 /// Returns the crate this module is part of.
203 pub fn krate(self, _db: &impl DefDatabase) -> Option<Crate> { 206 pub fn krate(self) -> Crate {
204 Some(self.krate) 207 Crate { crate_id: self.id.krate }
205 } 208 }
206 209
207 /// Topmost parent of this module. Every module has a `crate_root`, but some 210 /// Topmost parent of this module. Every module has a `crate_root`, but some
208 /// might be missing `krate`. This can happen if a module's file is not included 211 /// might be missing `krate`. This can happen if a module's file is not included
209 /// in the module tree of any target in `Cargo.toml`. 212 /// in the module tree of any target in `Cargo.toml`.
210 pub fn crate_root(self, db: &impl DefDatabase) -> Module { 213 pub fn crate_root(self, db: &impl DefDatabase) -> Module {
211 let def_map = db.crate_def_map(self.krate); 214 let def_map = db.crate_def_map(self.krate());
212 self.with_module_id(def_map.root()) 215 self.with_module_id(def_map.root())
213 } 216 }
214 217
215 /// Finds a child module with the specified name. 218 /// Finds a child module with the specified name.
216 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 219 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
217 let def_map = db.crate_def_map(self.krate); 220 let def_map = db.crate_def_map(self.krate());
218 let child_id = def_map[self.module_id].children.get(name)?; 221 let child_id = def_map[self.id.module_id].children.get(name)?;
219 Some(self.with_module_id(*child_id)) 222 Some(self.with_module_id(*child_id))
220 } 223 }
221 224
222 /// Iterates over all child modules. 225 /// Iterates over all child modules.
223 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { 226 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
224 let def_map = db.crate_def_map(self.krate); 227 let def_map = db.crate_def_map(self.krate());
225 let children = def_map[self.module_id] 228 let children = def_map[self.id.module_id]
226 .children 229 .children
227 .iter() 230 .iter()
228 .map(|(_, module_id)| self.with_module_id(*module_id)) 231 .map(|(_, module_id)| self.with_module_id(*module_id))
@@ -232,8 +235,8 @@ impl Module {
232 235
233 /// Finds a parent module. 236 /// Finds a parent module.
234 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { 237 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
235 let def_map = db.crate_def_map(self.krate); 238 let def_map = db.crate_def_map(self.krate());
236 let parent_id = def_map[self.module_id].parent?; 239 let parent_id = def_map[self.id.module_id].parent?;
237 Some(self.with_module_id(parent_id)) 240 Some(self.with_module_id(parent_id))
238 } 241 }
239 242
@@ -249,11 +252,11 @@ impl Module {
249 252
250 /// Returns a `ModuleScope`: a set of items, visible in this module. 253 /// Returns a `ModuleScope`: a set of items, visible in this module.
251 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { 254 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope {
252 db.crate_def_map(self.krate)[self.module_id].scope.clone() 255 db.crate_def_map(self.krate())[self.id.module_id].scope.clone()
253 } 256 }
254 257
255 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 258 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
256 db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink); 259 db.crate_def_map(self.krate()).add_diagnostics(db, self.id.module_id, sink);
257 for decl in self.declarations(db) { 260 for decl in self.declarations(db) {
258 match decl { 261 match decl {
259 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 262 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
@@ -277,13 +280,13 @@ impl Module {
277 } 280 }
278 281
279 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { 282 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
280 let def_map = db.crate_def_map(self.krate); 283 let def_map = db.crate_def_map(self.krate());
281 Resolver::default().push_module_scope(def_map, self.module_id) 284 Resolver::default().push_module_scope(def_map, self.id.module_id)
282 } 285 }
283 286
284 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 287 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
285 let def_map = db.crate_def_map(self.krate); 288 let def_map = db.crate_def_map(self.krate());
286 def_map[self.module_id] 289 def_map[self.id.module_id]
287 .scope 290 .scope
288 .entries() 291 .entries()
289 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) 292 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
@@ -303,7 +306,7 @@ impl Module {
303 } 306 }
304 307
305 fn with_module_id(self, module_id: CrateModuleId) -> Module { 308 fn with_module_id(self, module_id: CrateModuleId) -> Module {
306 Module { module_id, krate: self.krate } 309 Module::new(self.krate(), module_id)
307 } 310 }
308} 311}
309 312
@@ -344,7 +347,7 @@ impl Struct {
344 } 347 }
345 348
346 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 349 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
347 self.module(db).krate(db) 350 Some(self.module(db).krate())
348 } 351 }
349 352
350 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 353 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -432,7 +435,7 @@ impl Enum {
432 } 435 }
433 436
434 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 437 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
435 self.module(db).krate(db) 438 Some(self.module(db).krate())
436 } 439 }
437 440
438 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 441 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -523,12 +526,14 @@ impl Adt {
523 } 526 }
524 527
525 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 528 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
526 match self { 529 Some(
527 Adt::Struct(s) => s.module(db), 530 match self {
528 Adt::Union(s) => s.module(db), 531 Adt::Struct(s) => s.module(db),
529 Adt::Enum(e) => e.module(db), 532 Adt::Union(s) => s.module(db),
530 } 533 Adt::Enum(e) => e.module(db),
531 .krate(db) 534 }
535 .krate(),
536 )
532 } 537 }
533 538
534 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { 539 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
@@ -696,7 +701,7 @@ impl Function {
696 } 701 }
697 702
698 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 703 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
699 self.module(db).krate(db) 704 Some(self.module(db).krate())
700 } 705 }
701 706
702 pub fn name(self, db: &impl HirDatabase) -> Name { 707 pub fn name(self, db: &impl HirDatabase) -> Name {
@@ -774,7 +779,7 @@ impl Const {
774 } 779 }
775 780
776 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 781 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
777 self.module(db).krate(db) 782 Some(self.module(db).krate())
778 } 783 }
779 784
780 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { 785 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
@@ -871,7 +876,7 @@ impl Static {
871 } 876 }
872 877
873 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 878 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
874 self.module(db).krate(db) 879 Some(self.module(db).krate())
875 } 880 }
876 881
877 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> { 882 pub fn data(self, db: &impl HirDatabase) -> Arc<ConstData> {
@@ -1002,7 +1007,7 @@ impl TypeAlias {
1002 } 1007 }
1003 1008
1004 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 1009 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
1005 self.module(db).krate(db) 1010 Some(self.module(db).krate())
1006 } 1011 }
1007 1012
1008 /// The containing impl block, if this is a method. 1013 /// The containing impl block, if this is a method.
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index fdae26906..8b33f25f7 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -37,9 +37,9 @@ impl<T> Source<T> {
37impl Module { 37impl Module {
38 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 38 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
39 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { 39 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
40 let def_map = db.crate_def_map(self.krate); 40 let def_map = db.crate_def_map(self.krate());
41 let decl_id = def_map[self.module_id].declaration; 41 let decl_id = def_map[self.id.module_id].declaration;
42 let file_id = def_map[self.module_id].definition; 42 let file_id = def_map[self.id.module_id].definition;
43 let ast = ModuleSource::new(db, file_id, decl_id); 43 let ast = ModuleSource::new(db, file_id, decl_id);
44 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); 44 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
45 Source { file_id, ast } 45 Source { file_id, ast }
@@ -51,8 +51,8 @@ impl Module {
51 self, 51 self,
52 db: &(impl DefDatabase + AstDatabase), 52 db: &(impl DefDatabase + AstDatabase),
53 ) -> Option<Source<ast::Module>> { 53 ) -> Option<Source<ast::Module>> {
54 let def_map = db.crate_def_map(self.krate); 54 let def_map = db.crate_def_map(self.krate());
55 let decl = def_map[self.module_id].declaration?; 55 let decl = def_map[self.id.module_id].declaration?;
56 let ast = decl.to_node(db); 56 let ast = decl.to_node(db);
57 Some(Source { file_id: decl.file_id(), ast }) 57 Some(Source { file_id: decl.file_id(), ast })
58 } 58 }
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 7954c04b2..291f2a14a 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -195,7 +195,7 @@ impl Module {
195 .find_map(|krate| { 195 .find_map(|krate| {
196 let def_map = db.crate_def_map(krate); 196 let def_map = db.crate_def_map(krate);
197 let module_id = def_map.find_module_by_source(src.file_id, decl_id)?; 197 let module_id = def_map.find_module_by_source(src.file_id, decl_id)?;
198 Some(Module { krate, module_id }) 198 Some(Module::new(krate, module_id))
199 }) 199 })
200 } 200 }
201} 201}
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index 1a5223680..fde47c264 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -182,7 +182,7 @@ impl ModuleImplBlocks {
182 ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) { 182 ) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
183 let mut source_map = ImplSourceMap::default(); 183 let mut source_map = ImplSourceMap::default();
184 let crate_graph = db.crate_graph(); 184 let crate_graph = db.crate_graph();
185 let cfg_options = crate_graph.cfg_options(module.krate.crate_id()); 185 let cfg_options = crate_graph.cfg_options(module.id.krate);
186 186
187 let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map); 187 let result = ModuleImplBlocks::collect(db, cfg_options, module, &mut source_map);
188 (Arc::new(result), Arc::new(source_map)) 188 (Arc::new(result), Arc::new(source_map))
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs
index 6c4e8ffbd..e1780ed38 100644
--- a/crates/ra_hir/src/lang_item.rs
+++ b/crates/ra_hir/src/lang_item.rs
@@ -22,14 +22,14 @@ pub enum LangItemTarget {
22 22
23impl LangItemTarget { 23impl LangItemTarget {
24 pub(crate) fn krate(&self, db: &impl HirDatabase) -> Option<Crate> { 24 pub(crate) fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
25 match self { 25 Some(match self {
26 LangItemTarget::Enum(e) => e.module(db).krate(db), 26 LangItemTarget::Enum(e) => e.module(db).krate(),
27 LangItemTarget::Function(f) => f.module(db).krate(db), 27 LangItemTarget::Function(f) => f.module(db).krate(),
28 LangItemTarget::ImplBlock(i) => i.module().krate(db), 28 LangItemTarget::ImplBlock(i) => i.module().krate(),
29 LangItemTarget::Static(s) => s.module(db).krate(db), 29 LangItemTarget::Static(s) => s.module(db).krate(),
30 LangItemTarget::Struct(s) => s.module(db).krate(db), 30 LangItemTarget::Struct(s) => s.module(db).krate(),
31 LangItemTarget::Trait(t) => t.module(db).krate(db), 31 LangItemTarget::Trait(t) => t.module(db).krate(),
32 } 32 })
33 } 33 }
34} 34}
35 35
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index 67adcfa28..b325979f5 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -56,8 +56,9 @@ mod tests;
56 56
57use std::sync::Arc; 57use std::sync::Arc;
58 58
59use hir_def::CrateModuleId;
59use once_cell::sync::Lazy; 60use once_cell::sync::Lazy;
60use ra_arena::{impl_arena_id, Arena, RawId}; 61use ra_arena::Arena;
61use ra_db::{Edition, FileId}; 62use ra_db::{Edition, FileId};
62use ra_prof::profile; 63use ra_prof::profile;
63use ra_syntax::ast; 64use ra_syntax::ast;
@@ -115,13 +116,8 @@ impl std::ops::Index<CrateModuleId> for CrateDefMap {
115 } 116 }
116} 117}
117 118
118/// An ID of a module, **local** to a specific crate
119#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
120pub(crate) struct CrateModuleId(RawId);
121impl_arena_id!(CrateModuleId);
122
123#[derive(Default, Debug, PartialEq, Eq)] 119#[derive(Default, Debug, PartialEq, Eq)]
124pub(crate) struct ModuleData { 120pub struct ModuleData {
125 pub(crate) parent: Option<CrateModuleId>, 121 pub(crate) parent: Option<CrateModuleId>,
126 pub(crate) children: FxHashMap<Name, CrateModuleId>, 122 pub(crate) children: FxHashMap<Name, CrateModuleId>,
127 pub(crate) scope: ModuleScope, 123 pub(crate) scope: ModuleScope,
@@ -335,7 +331,7 @@ impl CrateDefMap {
335 PathKind::DollarCrate(krate) => { 331 PathKind::DollarCrate(krate) => {
336 if krate == self.krate { 332 if krate == self.krate {
337 tested_by!(macro_dollar_crate_self); 333 tested_by!(macro_dollar_crate_self);
338 PerNs::types(Module { krate: self.krate, module_id: self.root }.into()) 334 PerNs::types(Module::new(self.krate, self.root).into())
339 } else { 335 } else {
340 match krate.root_module(db) { 336 match krate.root_module(db) {
341 Some(module) => { 337 Some(module) => {
@@ -346,12 +342,8 @@ impl CrateDefMap {
346 } 342 }
347 } 343 }
348 } 344 }
349 PathKind::Crate => { 345 PathKind::Crate => PerNs::types(Module::new(self.krate, self.root).into()),
350 PerNs::types(Module { krate: self.krate, module_id: self.root }.into()) 346 PathKind::Self_ => PerNs::types(Module::new(self.krate, original_module).into()),
351 }
352 PathKind::Self_ => {
353 PerNs::types(Module { krate: self.krate, module_id: original_module }.into())
354 }
355 // plain import or absolute path in 2015: crate-relative with 347 // plain import or absolute path in 2015: crate-relative with
356 // fallback to extern prelude (with the simplification in 348 // fallback to extern prelude (with the simplification in
357 // rust-lang/rust#57745) 349 // rust-lang/rust#57745)
@@ -377,7 +369,7 @@ impl CrateDefMap {
377 } 369 }
378 PathKind::Super => { 370 PathKind::Super => {
379 if let Some(p) = self.modules[original_module].parent { 371 if let Some(p) = self.modules[original_module].parent {
380 PerNs::types(Module { krate: self.krate, module_id: p }.into()) 372 PerNs::types(Module::new(self.krate, p).into())
381 } else { 373 } else {
382 log::debug!("super path in root module"); 374 log::debug!("super path in root module");
383 return ResolvePathResult::empty(ReachedFixedPoint::Yes); 375 return ResolvePathResult::empty(ReachedFixedPoint::Yes);
@@ -419,12 +411,12 @@ impl CrateDefMap {
419 411
420 curr_per_ns = match curr { 412 curr_per_ns = match curr {
421 ModuleDef::Module(module) => { 413 ModuleDef::Module(module) => {
422 if module.krate != self.krate { 414 if module.krate() != self.krate {
423 let path = 415 let path =
424 Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; 416 Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ };
425 log::debug!("resolving {:?} in other crate", path); 417 log::debug!("resolving {:?} in other crate", path);
426 let defp_map = db.crate_def_map(module.krate); 418 let defp_map = db.crate_def_map(module.krate());
427 let (def, s) = defp_map.resolve_path(db, module.module_id, &path); 419 let (def, s) = defp_map.resolve_path(db, module.id.module_id, &path);
428 return ResolvePathResult::with( 420 return ResolvePathResult::with(
429 def, 421 def,
430 ReachedFixedPoint::Yes, 422 ReachedFixedPoint::Yes,
@@ -433,7 +425,7 @@ impl CrateDefMap {
433 } 425 }
434 426
435 // Since it is a qualified path here, it should not contains legacy macros 427 // Since it is a qualified path here, it should not contains legacy macros
436 match self[module.module_id].scope.get(&segment.name) { 428 match self[module.id.module_id].scope.get(&segment.name) {
437 Some(res) => res.def, 429 Some(res) => res.def,
438 _ => { 430 _ => {
439 log::debug!("path segment {:?} not found", segment.name); 431 log::debug!("path segment {:?} not found", segment.name);
@@ -511,14 +503,14 @@ impl CrateDefMap {
511 fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs { 503 fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs {
512 if let Some(prelude) = self.prelude { 504 if let Some(prelude) = self.prelude {
513 let keep; 505 let keep;
514 let def_map = if prelude.krate == self.krate { 506 let def_map = if prelude.krate() == self.krate {
515 self 507 self
516 } else { 508 } else {
517 // Extend lifetime 509 // Extend lifetime
518 keep = db.crate_def_map(prelude.krate); 510 keep = db.crate_def_map(prelude.krate());
519 &keep 511 &keep
520 }; 512 };
521 def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) 513 def_map[prelude.id.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def)
522 } else { 514 } else {
523 PerNs::none() 515 PerNs::none()
524 } 516 }
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index dc591e8d3..dd5f9d4ba 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -212,7 +212,7 @@ where
212 212
213 if let Some(ModuleDef::Module(m)) = res.take_types() { 213 if let Some(ModuleDef::Module(m)) = res.take_types() {
214 tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); 214 tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use);
215 self.import_all_macros_exported(current_module_id, m.krate); 215 self.import_all_macros_exported(current_module_id, m.krate());
216 } 216 }
217 } 217 }
218 218
@@ -289,11 +289,11 @@ where
289 if import.is_prelude { 289 if import.is_prelude {
290 tested_by!(std_prelude); 290 tested_by!(std_prelude);
291 self.def_map.prelude = Some(m); 291 self.def_map.prelude = Some(m);
292 } else if m.krate != self.def_map.krate { 292 } else if m.krate() != self.def_map.krate {
293 tested_by!(glob_across_crates); 293 tested_by!(glob_across_crates);
294 // glob import from other crate => we can just import everything once 294 // glob import from other crate => we can just import everything once
295 let item_map = self.db.crate_def_map(m.krate); 295 let item_map = self.db.crate_def_map(m.krate());
296 let scope = &item_map[m.module_id].scope; 296 let scope = &item_map[m.id.module_id].scope;
297 297
298 // Module scoped macros is included 298 // Module scoped macros is included
299 let items = scope 299 let items = scope
@@ -307,7 +307,7 @@ where
307 // glob import from same crate => we do an initial 307 // glob import from same crate => we do an initial
308 // import, and then need to propagate any further 308 // import, and then need to propagate any further
309 // additions 309 // additions
310 let scope = &self.def_map[m.module_id].scope; 310 let scope = &self.def_map[m.id.module_id].scope;
311 311
312 // Module scoped macros is included 312 // Module scoped macros is included
313 let items = scope 313 let items = scope
@@ -319,7 +319,7 @@ where
319 self.update(module_id, Some(import_id), &items); 319 self.update(module_id, Some(import_id), &items);
320 // record the glob import in case we add further items 320 // record the glob import in case we add further items
321 self.glob_imports 321 self.glob_imports
322 .entry(m.module_id) 322 .entry(m.id.module_id)
323 .or_default() 323 .or_default()
324 .push((module_id, import_id)); 324 .push((module_id, import_id));
325 } 325 }
@@ -523,9 +523,10 @@ where
523 523
524 // Prelude module is always considered to be `#[macro_use]`. 524 // Prelude module is always considered to be `#[macro_use]`.
525 if let Some(prelude_module) = self.def_collector.def_map.prelude { 525 if let Some(prelude_module) = self.def_collector.def_map.prelude {
526 if prelude_module.krate != self.def_collector.def_map.krate { 526 if prelude_module.krate() != self.def_collector.def_map.krate {
527 tested_by!(prelude_is_macro_use); 527 tested_by!(prelude_is_macro_use);
528 self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate); 528 self.def_collector
529 .import_all_macros_exported(self.module_id, prelude_module.krate());
529 } 530 }
530 } 531 }
531 532
@@ -631,9 +632,7 @@ where
631 modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); 632 modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone();
632 modules[self.module_id].children.insert(name.clone(), res); 633 modules[self.module_id].children.insert(name.clone(), res);
633 let resolution = Resolution { 634 let resolution = Resolution {
634 def: PerNs::types( 635 def: PerNs::types(Module::new(self.def_collector.def_map.krate, res).into()),
635 Module { krate: self.def_collector.def_map.krate, module_id: res }.into(),
636 ),
637 import: None, 636 import: None,
638 }; 637 };
639 self.def_collector.update(self.module_id, None, &[(name, resolution)]); 638 self.def_collector.update(self.module_id, None, &[(name, resolution)]);
@@ -641,7 +640,7 @@ where
641 } 640 }
642 641
643 fn define_def(&mut self, def: &raw::DefData) { 642 fn define_def(&mut self, def: &raw::DefData) {
644 let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id }; 643 let module = Module::new(self.def_collector.def_map.krate, self.module_id);
645 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); 644 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
646 645
647 macro_rules! def { 646 macro_rules! def {
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 3c797c0c3..8b6269407 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -1,6 +1,7 @@
1//! Name resolution. 1//! Name resolution.
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use hir_def::CrateModuleId;
4use rustc_hash::FxHashSet; 5use rustc_hash::FxHashSet;
5 6
6use crate::{ 7use crate::{
@@ -13,7 +14,7 @@ use crate::{
13 generics::GenericParams, 14 generics::GenericParams,
14 impl_block::ImplBlock, 15 impl_block::ImplBlock,
15 name::{Name, SELF_PARAM, SELF_TYPE}, 16 name::{Name, SELF_PARAM, SELF_TYPE},
16 nameres::{CrateDefMap, CrateModuleId, PerNs}, 17 nameres::{CrateDefMap, PerNs},
17 path::{Path, PathKind}, 18 path::{Path, PathKind},
18 Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct, 19 Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct,
19 Trait, TypeAlias, 20 Trait, TypeAlias,
@@ -330,8 +331,8 @@ impl Resolver {
330 for scope in &self.scopes { 331 for scope in &self.scopes {
331 if let Scope::ModuleScope(m) = scope { 332 if let Scope::ModuleScope(m) = scope {
332 if let Some(prelude) = m.crate_def_map.prelude() { 333 if let Some(prelude) = m.crate_def_map.prelude() {
333 let prelude_def_map = db.crate_def_map(prelude.krate); 334 let prelude_def_map = db.crate_def_map(prelude.krate());
334 traits.extend(prelude_def_map[prelude.module_id].scope.traits()); 335 traits.extend(prelude_def_map[prelude.id.module_id].scope.traits());
335 } 336 }
336 traits.extend(m.crate_def_map[m.module_id].scope.traits()); 337 traits.extend(m.crate_def_map[m.module_id].scope.traits());
337 } 338 }
@@ -444,10 +445,12 @@ impl Scope {
444 f(name.clone(), ScopeDef::ModuleDef(*def)); 445 f(name.clone(), ScopeDef::ModuleDef(*def));
445 }); 446 });
446 if let Some(prelude) = m.crate_def_map.prelude() { 447 if let Some(prelude) = m.crate_def_map.prelude() {
447 let prelude_def_map = db.crate_def_map(prelude.krate); 448 let prelude_def_map = db.crate_def_map(prelude.krate());
448 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { 449 prelude_def_map[prelude.id.module_id].scope.entries().for_each(
449 f(name.clone(), res.def.into()); 450 |(name, res)| {
450 }); 451 f(name.clone(), res.def.into());
452 },
453 );
451 } 454 }
452 } 455 }
453 Scope::GenericParams(gp) => { 456 Scope::GenericParams(gp) => {
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index ad2ab560d..50583a142 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -5,13 +5,13 @@
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7use arrayvec::ArrayVec; 7use arrayvec::ArrayVec;
8use hir_def::CrateModuleId;
8use rustc_hash::FxHashMap; 9use rustc_hash::FxHashMap;
9 10
10use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitRef}; 11use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitRef};
11use crate::{ 12use crate::{
12 db::HirDatabase, 13 db::HirDatabase,
13 impl_block::{ImplBlock, ImplId}, 14 impl_block::{ImplBlock, ImplId},
14 nameres::CrateModuleId,
15 resolve::Resolver, 15 resolve::Resolver,
16 ty::primitive::{FloatBitness, UncertainFloatTy, UncertainIntTy}, 16 ty::primitive::{FloatBitness, UncertainFloatTy, UncertainIntTy},
17 ty::{Ty, TypeCtor}, 17 ty::{Ty, TypeCtor},
@@ -50,7 +50,7 @@ impl CrateImplBlocks {
50 let fingerprint = TyFingerprint::for_impl(ty); 50 let fingerprint = TyFingerprint::for_impl(ty);
51 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flat_map(|i| i.iter()).map( 51 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flat_map(|i| i.iter()).map(
52 move |(module_id, impl_id)| { 52 move |(module_id, impl_id)| {
53 let module = Module { krate: self.krate, module_id: *module_id }; 53 let module = Module::new(self.krate, *module_id);
54 ImplBlock::from_id(module, *impl_id) 54 ImplBlock::from_id(module, *impl_id)
55 }, 55 },
56 ) 56 )
@@ -62,7 +62,7 @@ impl CrateImplBlocks {
62 ) -> impl Iterator<Item = ImplBlock> + 'a { 62 ) -> impl Iterator<Item = ImplBlock> + 'a {
63 self.impls_by_trait.get(&tr).into_iter().flat_map(|i| i.iter()).map( 63 self.impls_by_trait.get(&tr).into_iter().flat_map(|i| i.iter()).map(
64 move |(module_id, impl_id)| { 64 move |(module_id, impl_id)| {
65 let module = Module { krate: self.krate, module_id: *module_id }; 65 let module = Module::new(self.krate, *module_id);
66 ImplBlock::from_id(module, *impl_id) 66 ImplBlock::from_id(module, *impl_id)
67 }, 67 },
68 ) 68 )
@@ -71,7 +71,7 @@ impl CrateImplBlocks {
71 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplBlock> + 'a { 71 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplBlock> + 'a {
72 self.impls.values().chain(self.impls_by_trait.values()).flat_map(|i| i.iter()).map( 72 self.impls.values().chain(self.impls_by_trait.values()).flat_map(|i| i.iter()).map(
73 move |(module_id, impl_id)| { 73 move |(module_id, impl_id)| {
74 let module = Module { krate: self.krate, module_id: *module_id }; 74 let module = Module::new(self.krate, *module_id);
75 ImplBlock::from_id(module, *impl_id) 75 ImplBlock::from_id(module, *impl_id)
76 }, 76 },
77 ) 77 )
@@ -90,14 +90,14 @@ impl CrateImplBlocks {
90 self.impls_by_trait 90 self.impls_by_trait
91 .entry(tr.trait_) 91 .entry(tr.trait_)
92 .or_insert_with(Vec::new) 92 .or_insert_with(Vec::new)
93 .push((module.module_id, impl_id)); 93 .push((module.id.module_id, impl_id));
94 } 94 }
95 } else { 95 } else {
96 if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) { 96 if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) {
97 self.impls 97 self.impls
98 .entry(target_ty_fp) 98 .entry(target_ty_fp)
99 .or_insert_with(Vec::new) 99 .or_insert_with(Vec::new)
100 .push((module.module_id, impl_id)); 100 .push((module.id.module_id, impl_id));
101 } 101 }
102 } 102 }
103 } 103 }
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index e18c28cf6..aec484feb 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -537,7 +537,7 @@ pub(crate) fn trait_datum_query(
537 let trait_ref = trait_.trait_ref(db).subst(&bound_vars).to_chalk(db); 537 let trait_ref = trait_.trait_ref(db).subst(&bound_vars).to_chalk(db);
538 let flags = chalk_rust_ir::TraitFlags { 538 let flags = chalk_rust_ir::TraitFlags {
539 auto: trait_.is_auto(db), 539 auto: trait_.is_auto(db),
540 upstream: trait_.module(db).krate(db) != Some(krate), 540 upstream: trait_.module(db).krate() != krate,
541 non_enumerable: true, 541 non_enumerable: true,
542 // FIXME set these flags correctly 542 // FIXME set these flags correctly
543 marker: false, 543 marker: false,
@@ -625,7 +625,7 @@ fn impl_block_datum(
625 .target_trait_ref(db) 625 .target_trait_ref(db)
626 .expect("FIXME handle unresolved impl block trait ref") 626 .expect("FIXME handle unresolved impl block trait ref")
627 .subst(&bound_vars); 627 .subst(&bound_vars);
628 let impl_type = if impl_block.module().krate(db) == Some(krate) { 628 let impl_type = if impl_block.module().krate() == krate {
629 chalk_rust_ir::ImplType::Local 629 chalk_rust_ir::ImplType::Local
630 } else { 630 } else {
631 chalk_rust_ir::ImplType::External 631 chalk_rust_ir::ImplType::External
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml
new file mode 100644
index 000000000..f22a678eb
--- /dev/null
+++ b/crates/ra_hir_def/Cargo.toml
@@ -0,0 +1,13 @@
1[package]
2edition = "2018"
3name = "ra_hir_def"
4version = "0.1.0"
5authors = ["rust-analyzer developers"]
6
7[dependencies]
8log = "0.4.5"
9
10ra_arena = { path = "../ra_arena" }
11ra_db = { path = "../ra_db" }
12ra_syntax = { path = "../ra_syntax" }
13ra_prof = { path = "../ra_prof" }
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
new file mode 100644
index 000000000..f5dd2ae6f
--- /dev/null
+++ b/crates/ra_hir_def/src/lib.rs
@@ -0,0 +1,14 @@
1use ra_arena::{impl_arena_id, RawId};
2use ra_db::CrateId;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct ModuleId {
6 pub krate: CrateId,
7 pub module_id: CrateModuleId,
8}
9
10/// An ID of a module, **local** to a specific crate
11// FIXME: rename to `LocalModuleId`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct CrateModuleId(RawId);
14impl_arena_id!(CrateModuleId);