aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs36
-rw-r--r--crates/ra_hir_def/src/nameres/path_resolution.rs16
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs13
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs7
4 files changed, 38 insertions, 34 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 41becf8df..fd8245113 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -6,7 +6,7 @@
6use hir_expand::{ 6use hir_expand::{
7 builtin_macro::find_builtin_macro, 7 builtin_macro::find_builtin_macro,
8 name::{self, AsName, Name}, 8 name::{self, AsName, Name},
9 HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind, 9 HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
10}; 10};
11use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
12use ra_db::{CrateId, FileId}; 12use ra_db::{CrateId, FileId};
@@ -25,7 +25,7 @@ use crate::{
25 per_ns::PerNs, 25 per_ns::PerNs,
26 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, 26 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId,
27 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, 27 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId,
28 StructOrUnionId, TraitId, TypeAliasLoc, UnionId, 28 TraitId, TypeAliasLoc, UnionId,
29}; 29};
30 30
31pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 31pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -37,7 +37,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
37 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); 37 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
38 def_map.extern_prelude.insert( 38 def_map.extern_prelude.insert(
39 dep.as_name(), 39 dep.as_name(),
40 ModuleId { krate: dep.crate_id, module_id: dep_def_map.root }.into(), 40 ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(),
41 ); 41 );
42 42
43 // look for the prelude 43 // look for the prelude
@@ -323,7 +323,7 @@ where
323 tested_by!(glob_across_crates); 323 tested_by!(glob_across_crates);
324 // glob import from other crate => we can just import everything once 324 // glob import from other crate => we can just import everything once
325 let item_map = self.db.crate_def_map(m.krate); 325 let item_map = self.db.crate_def_map(m.krate);
326 let scope = &item_map[m.module_id].scope; 326 let scope = &item_map[m.local_id].scope;
327 327
328 // Module scoped macros is included 328 // Module scoped macros is included
329 let items = scope 329 let items = scope
@@ -337,7 +337,7 @@ where
337 // glob import from same crate => we do an initial 337 // glob import from same crate => we do an initial
338 // import, and then need to propagate any further 338 // import, and then need to propagate any further
339 // additions 339 // additions
340 let scope = &self.def_map[m.module_id].scope; 340 let scope = &self.def_map[m.local_id].scope;
341 341
342 // Module scoped macros is included 342 // Module scoped macros is included
343 let items = scope 343 let items = scope
@@ -349,7 +349,7 @@ where
349 self.update(module_id, Some(import_id), &items); 349 self.update(module_id, Some(import_id), &items);
350 // record the glob import in case we add further items 350 // record the glob import in case we add further items
351 self.glob_imports 351 self.glob_imports
352 .entry(m.module_id) 352 .entry(m.local_id)
353 .or_default() 353 .or_default()
354 .push((module_id, import_id)); 354 .push((module_id, import_id));
355 } 355 }
@@ -362,7 +362,7 @@ where
362 .variants 362 .variants
363 .iter() 363 .iter()
364 .filter_map(|(local_id, variant_data)| { 364 .filter_map(|(local_id, variant_data)| {
365 let name = variant_data.name.clone()?; 365 let name = variant_data.name.clone();
366 let variant = EnumVariantId { parent: e, local_id }; 366 let variant = EnumVariantId { parent: e, local_id };
367 let res = Resolution { 367 let res = Resolution {
368 def: PerNs::both(variant.into(), variant.into()), 368 def: PerNs::both(variant.into(), variant.into()),
@@ -480,7 +480,7 @@ where
480 ); 480 );
481 481
482 if let Some(def) = resolved_res.resolved_def.take_macros() { 482 if let Some(def) = resolved_res.resolved_def.take_macros() {
483 let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id: *ast_id }); 483 let call_id = def.as_call_id(self.db, *ast_id);
484 resolved.push((*module_id, call_id, def)); 484 resolved.push((*module_id, call_id, def));
485 res = ReachedFixedPoint::No; 485 res = ReachedFixedPoint::No;
486 return false; 486 return false;
@@ -590,7 +590,7 @@ where
590 raw::RawItemKind::Impl(imp) => { 590 raw::RawItemKind::Impl(imp) => {
591 let module = ModuleId { 591 let module = ModuleId {
592 krate: self.def_collector.def_map.krate, 592 krate: self.def_collector.def_map.krate,
593 module_id: self.module_id, 593 local_id: self.module_id,
594 }; 594 };
595 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); 595 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
596 let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); 596 let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id);
@@ -673,7 +673,7 @@ where
673 modules[self.module_id].children.insert(name.clone(), res); 673 modules[self.module_id].children.insert(name.clone(), res);
674 let resolution = Resolution { 674 let resolution = Resolution {
675 def: PerNs::types( 675 def: PerNs::types(
676 ModuleId { krate: self.def_collector.def_map.krate, module_id: res }.into(), 676 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
677 ), 677 ),
678 import: None, 678 import: None,
679 }; 679 };
@@ -682,8 +682,7 @@ where
682 } 682 }
683 683
684 fn define_def(&mut self, def: &raw::DefData) { 684 fn define_def(&mut self, def: &raw::DefData) {
685 let module = 685 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id };
686 ModuleId { krate: self.def_collector.def_map.krate, module_id: self.module_id };
687 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); 686 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
688 687
689 let name = def.name.clone(); 688 let name = def.name.clone();
@@ -698,14 +697,12 @@ where
698 PerNs::values(def.into()) 697 PerNs::values(def.into())
699 } 698 }
700 raw::DefKind::Struct(ast_id) => { 699 raw::DefKind::Struct(ast_id) => {
701 let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); 700 let id = StructId::from_ast_id(ctx, ast_id).into();
702 let s = StructId(id).into(); 701 PerNs::both(id, id)
703 PerNs::both(s, s)
704 } 702 }
705 raw::DefKind::Union(ast_id) => { 703 raw::DefKind::Union(ast_id) => {
706 let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); 704 let id = UnionId::from_ast_id(ctx, ast_id).into();
707 let u = UnionId(id).into(); 705 PerNs::both(id, id)
708 PerNs::both(u, u)
709 } 706 }
710 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), 707 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
711 raw::DefKind::Const(ast_id) => { 708 raw::DefKind::Const(ast_id) => {
@@ -775,8 +772,7 @@ where
775 if let Some(macro_def) = mac.path.as_ident().and_then(|name| { 772 if let Some(macro_def) = mac.path.as_ident().and_then(|name| {
776 self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) 773 self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
777 }) { 774 }) {
778 let macro_call_id = 775 let macro_call_id = macro_def.as_call_id(self.def_collector.db, ast_id);
779 self.def_collector.db.intern_macro(MacroCallLoc { def: macro_def, ast_id });
780 776
781 self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_def); 777 self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_def);
782 return; 778 return;
diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs
index 9455f22bb..b72c55bd1 100644
--- a/crates/ra_hir_def/src/nameres/path_resolution.rs
+++ b/crates/ra_hir_def/src/nameres/path_resolution.rs
@@ -74,19 +74,19 @@ impl CrateDefMap {
74 PathKind::DollarCrate(krate) => { 74 PathKind::DollarCrate(krate) => {
75 if krate == self.krate { 75 if krate == self.krate {
76 tested_by!(macro_dollar_crate_self); 76 tested_by!(macro_dollar_crate_self);
77 PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) 77 PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into())
78 } else { 78 } else {
79 let def_map = db.crate_def_map(krate); 79 let def_map = db.crate_def_map(krate);
80 let module = ModuleId { krate, module_id: def_map.root }; 80 let module = ModuleId { krate, local_id: def_map.root };
81 tested_by!(macro_dollar_crate_other); 81 tested_by!(macro_dollar_crate_other);
82 PerNs::types(module.into()) 82 PerNs::types(module.into())
83 } 83 }
84 } 84 }
85 PathKind::Crate => { 85 PathKind::Crate => {
86 PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) 86 PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into())
87 } 87 }
88 PathKind::Self_ => { 88 PathKind::Self_ => {
89 PerNs::types(ModuleId { krate: self.krate, module_id: original_module }.into()) 89 PerNs::types(ModuleId { krate: self.krate, local_id: original_module }.into())
90 } 90 }
91 // plain import or absolute path in 2015: crate-relative with 91 // plain import or absolute path in 2015: crate-relative with
92 // fallback to extern prelude (with the simplification in 92 // fallback to extern prelude (with the simplification in
@@ -113,7 +113,7 @@ impl CrateDefMap {
113 } 113 }
114 PathKind::Super => { 114 PathKind::Super => {
115 if let Some(p) = self.modules[original_module].parent { 115 if let Some(p) = self.modules[original_module].parent {
116 PerNs::types(ModuleId { krate: self.krate, module_id: p }.into()) 116 PerNs::types(ModuleId { krate: self.krate, local_id: p }.into())
117 } else { 117 } else {
118 log::debug!("super path in root module"); 118 log::debug!("super path in root module");
119 return ResolvePathResult::empty(ReachedFixedPoint::Yes); 119 return ResolvePathResult::empty(ReachedFixedPoint::Yes);
@@ -160,7 +160,7 @@ impl CrateDefMap {
160 Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; 160 Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ };
161 log::debug!("resolving {:?} in other crate", path); 161 log::debug!("resolving {:?} in other crate", path);
162 let defp_map = db.crate_def_map(module.krate); 162 let defp_map = db.crate_def_map(module.krate);
163 let (def, s) = defp_map.resolve_path(db, module.module_id, &path); 163 let (def, s) = defp_map.resolve_path(db, module.local_id, &path);
164 return ResolvePathResult::with( 164 return ResolvePathResult::with(
165 def, 165 def,
166 ReachedFixedPoint::Yes, 166 ReachedFixedPoint::Yes,
@@ -169,7 +169,7 @@ impl CrateDefMap {
169 } 169 }
170 170
171 // Since it is a qualified path here, it should not contains legacy macros 171 // Since it is a qualified path here, it should not contains legacy macros
172 match self[module.module_id].scope.get(&segment.name) { 172 match self[module.local_id].scope.get(&segment.name) {
173 Some(res) => res.def, 173 Some(res) => res.def,
174 _ => { 174 _ => {
175 log::debug!("path segment {:?} not found", segment.name); 175 log::debug!("path segment {:?} not found", segment.name);
@@ -254,7 +254,7 @@ impl CrateDefMap {
254 keep = db.crate_def_map(prelude.krate); 254 keep = db.crate_def_map(prelude.krate);
255 &keep 255 &keep
256 }; 256 };
257 def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) 257 def_map[prelude.local_id].scope.get(name).map_or_else(PerNs::none, |res| res.def)
258 } else { 258 } else {
259 PerNs::none() 259 PerNs::none()
260 } 260 }
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index 401af031c..6eb106094 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -176,7 +176,7 @@ pub(super) struct DefData {
176pub(super) enum DefKind { 176pub(super) enum DefKind {
177 Function(FileAstId<ast::FnDef>), 177 Function(FileAstId<ast::FnDef>),
178 Struct(FileAstId<ast::StructDef>), 178 Struct(FileAstId<ast::StructDef>),
179 Union(FileAstId<ast::StructDef>), 179 Union(FileAstId<ast::UnionDef>),
180 Enum(FileAstId<ast::EnumDef>), 180 Enum(FileAstId<ast::EnumDef>),
181 Const(FileAstId<ast::ConstDef>), 181 Const(FileAstId<ast::ConstDef>),
182 Static(FileAstId<ast::StaticDef>), 182 Static(FileAstId<ast::StaticDef>),
@@ -246,11 +246,12 @@ impl RawItemsCollector {
246 ast::ModuleItem::StructDef(it) => { 246 ast::ModuleItem::StructDef(it) => {
247 let id = self.source_ast_id_map.ast_id(&it); 247 let id = self.source_ast_id_map.ast_id(&it);
248 let name = it.name(); 248 let name = it.name();
249 if it.is_union() { 249 (DefKind::Struct(id), name)
250 (DefKind::Union(id), name) 250 }
251 } else { 251 ast::ModuleItem::UnionDef(it) => {
252 (DefKind::Struct(id), name) 252 let id = self.source_ast_id_map.ast_id(&it);
253 } 253 let name = it.name();
254 (DefKind::Union(id), name)
254 } 255 }
255 ast::ModuleItem::EnumDef(it) => { 256 ast::ModuleItem::EnumDef(it) => {
256 (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) 257 (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name())
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index f502f1cb3..87fcd617c 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -82,6 +82,12 @@ fn crate_def_map_smoke_test() {
82 82
83 //- /foo/bar.rs 83 //- /foo/bar.rs
84 pub struct Baz; 84 pub struct Baz;
85
86 union U {
87 to_be: bool,
88 not_to_be: u8,
89 }
90
85 enum E { V } 91 enum E { V }
86 ", 92 ",
87 ); 93 );
@@ -99,6 +105,7 @@ fn crate_def_map_smoke_test() {
99 ⋮crate::foo::bar 105 ⋮crate::foo::bar
100 ⋮Baz: t v 106 ⋮Baz: t v
101 ⋮E: t 107 ⋮E: t
108 ⋮U: t v
102 "###) 109 "###)
103} 110}
104 111