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.rs33
-rw-r--r--crates/ra_hir_def/src/nameres/path_resolution.rs45
2 files changed, 55 insertions, 23 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index b9f40d3dd..5b8478037 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -24,6 +24,7 @@ use crate::{
24 }, 24 },
25 path::{ModPath, PathKind}, 25 path::{ModPath, PathKind},
26 per_ns::PerNs, 26 per_ns::PerNs,
27 visibility::ResolvedVisibility,
27 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, 28 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
28 LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 29 LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
29}; 30};
@@ -214,7 +215,10 @@ where
214 // In Rust, `#[macro_export]` macros are unconditionally visible at the 215 // In Rust, `#[macro_export]` macros are unconditionally visible at the
215 // crate root, even if the parent modules is **not** visible. 216 // crate root, even if the parent modules is **not** visible.
216 if export { 217 if export {
217 self.update(self.def_map.root, &[(name, PerNs::macros(macro_))]); 218 self.update(
219 self.def_map.root,
220 &[(name, PerNs::macros(macro_, ResolvedVisibility::Public))],
221 );
218 } 222 }
219 } 223 }
220 224
@@ -351,6 +355,10 @@ where
351 let import_id = directive.import_id; 355 let import_id = directive.import_id;
352 let import = &directive.import; 356 let import = &directive.import;
353 let def = directive.status.namespaces(); 357 let def = directive.status.namespaces();
358 let vis = self
359 .def_map
360 .resolve_visibility(self.db, module_id, &directive.import.visibility)
361 .unwrap_or(ResolvedVisibility::Public);
354 362
355 if import.is_glob { 363 if import.is_glob {
356 log::debug!("glob import: {:?}", import); 364 log::debug!("glob import: {:?}", import);
@@ -365,8 +373,10 @@ where
365 let item_map = self.db.crate_def_map(m.krate); 373 let item_map = self.db.crate_def_map(m.krate);
366 let scope = &item_map[m.local_id].scope; 374 let scope = &item_map[m.local_id].scope;
367 375
376 // TODO: only use names we can see
377
368 // Module scoped macros is included 378 // Module scoped macros is included
369 let items = scope.collect_resolutions(); 379 let items = scope.collect_resolutions_with_vis(vis);
370 380
371 self.update(module_id, &items); 381 self.update(module_id, &items);
372 } else { 382 } else {
@@ -375,8 +385,10 @@ where
375 // additions 385 // additions
376 let scope = &self.def_map[m.local_id].scope; 386 let scope = &self.def_map[m.local_id].scope;
377 387
388 // TODO: only use names we can see
389
378 // Module scoped macros is included 390 // Module scoped macros is included
379 let items = scope.collect_resolutions(); 391 let items = scope.collect_resolutions_with_vis(vis);
380 392
381 self.update(module_id, &items); 393 self.update(module_id, &items);
382 // record the glob import in case we add further items 394 // record the glob import in case we add further items
@@ -396,7 +408,7 @@ where
396 .map(|(local_id, variant_data)| { 408 .map(|(local_id, variant_data)| {
397 let name = variant_data.name.clone(); 409 let name = variant_data.name.clone();
398 let variant = EnumVariantId { parent: e, local_id }; 410 let variant = EnumVariantId { parent: e, local_id };
399 let res = PerNs::both(variant.into(), variant.into()); 411 let res = PerNs::both(variant.into(), variant.into(), vis);
400 (name, res) 412 (name, res)
401 }) 413 })
402 .collect::<Vec<_>>(); 414 .collect::<Vec<_>>();
@@ -422,7 +434,7 @@ where
422 } 434 }
423 } 435 }
424 436
425 self.update(module_id, &[(name, def)]); 437 self.update(module_id, &[(name, def.with_visibility(vis))]);
426 } 438 }
427 None => tested_by!(bogus_paths), 439 None => tested_by!(bogus_paths),
428 } 440 }
@@ -701,8 +713,9 @@ where
701 modules[self.module_id].children.insert(name.clone(), res); 713 modules[self.module_id].children.insert(name.clone(), res);
702 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; 714 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
703 let def: ModuleDefId = module.into(); 715 let def: ModuleDefId = module.into();
716 let vis = ResolvedVisibility::Public; // TODO handle module visibility
704 self.def_collector.def_map.modules[self.module_id].scope.define_def(def); 717 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
705 self.def_collector.update(self.module_id, &[(name, def.into())]); 718 self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))]);
706 res 719 res
707 } 720 }
708 721
@@ -716,6 +729,7 @@ where
716 729
717 let name = def.name.clone(); 730 let name = def.name.clone();
718 let container = ContainerId::ModuleId(module); 731 let container = ContainerId::ModuleId(module);
732 let vis = &def.visibility;
719 let def: ModuleDefId = match def.kind { 733 let def: ModuleDefId = match def.kind {
720 raw::DefKind::Function(ast_id) => FunctionLoc { 734 raw::DefKind::Function(ast_id) => FunctionLoc {
721 container: container.into(), 735 container: container.into(),
@@ -761,7 +775,12 @@ where
761 .into(), 775 .into(),
762 }; 776 };
763 self.def_collector.def_map.modules[self.module_id].scope.define_def(def); 777 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
764 self.def_collector.update(self.module_id, &[(name, def.into())]) 778 let vis = self
779 .def_collector
780 .def_map
781 .resolve_visibility(self.def_collector.db, self.module_id, vis)
782 .unwrap_or(ResolvedVisibility::Public);
783 self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))])
765 } 784 }
766 785
767 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { 786 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs
index d88076aa7..a56e3f08b 100644
--- a/crates/ra_hir_def/src/nameres/path_resolution.rs
+++ b/crates/ra_hir_def/src/nameres/path_resolution.rs
@@ -62,7 +62,9 @@ impl ResolvePathResult {
62 62
63impl CrateDefMap { 63impl CrateDefMap {
64 pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { 64 pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
65 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) 65 self.extern_prelude
66 .get(name)
67 .map_or(PerNs::none(), |&it| PerNs::types(it, ResolvedVisibility::Public))
66 } 68 }
67 69
68 pub(crate) fn resolve_visibility( 70 pub(crate) fn resolve_visibility(
@@ -115,17 +117,21 @@ impl CrateDefMap {
115 PathKind::DollarCrate(krate) => { 117 PathKind::DollarCrate(krate) => {
116 if krate == self.krate { 118 if krate == self.krate {
117 tested_by!(macro_dollar_crate_self); 119 tested_by!(macro_dollar_crate_self);
118 PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) 120 PerNs::types(
121 ModuleId { krate: self.krate, local_id: self.root }.into(),
122 ResolvedVisibility::Public,
123 )
119 } else { 124 } else {
120 let def_map = db.crate_def_map(krate); 125 let def_map = db.crate_def_map(krate);
121 let module = ModuleId { krate, local_id: def_map.root }; 126 let module = ModuleId { krate, local_id: def_map.root };
122 tested_by!(macro_dollar_crate_other); 127 tested_by!(macro_dollar_crate_other);
123 PerNs::types(module.into()) 128 PerNs::types(module.into(), ResolvedVisibility::Public)
124 } 129 }
125 } 130 }
126 PathKind::Crate => { 131 PathKind::Crate => PerNs::types(
127 PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) 132 ModuleId { krate: self.krate, local_id: self.root }.into(),
128 } 133 ResolvedVisibility::Public,
134 ),
129 // plain import or absolute path in 2015: crate-relative with 135 // plain import or absolute path in 2015: crate-relative with
130 // fallback to extern prelude (with the simplification in 136 // fallback to extern prelude (with the simplification in
131 // rust-lang/rust#57745) 137 // rust-lang/rust#57745)
@@ -153,7 +159,10 @@ impl CrateDefMap {
153 let m = successors(Some(original_module), |m| self.modules[*m].parent) 159 let m = successors(Some(original_module), |m| self.modules[*m].parent)
154 .nth(lvl as usize); 160 .nth(lvl as usize);
155 if let Some(local_id) = m { 161 if let Some(local_id) = m {
156 PerNs::types(ModuleId { krate: self.krate, local_id }.into()) 162 PerNs::types(
163 ModuleId { krate: self.krate, local_id }.into(),
164 ResolvedVisibility::Public,
165 )
157 } else { 166 } else {
158 log::debug!("super path in root module"); 167 log::debug!("super path in root module");
159 return ResolvePathResult::empty(ReachedFixedPoint::Yes); 168 return ResolvePathResult::empty(ReachedFixedPoint::Yes);
@@ -167,7 +176,7 @@ impl CrateDefMap {
167 }; 176 };
168 if let Some(def) = self.extern_prelude.get(&segment) { 177 if let Some(def) = self.extern_prelude.get(&segment) {
169 log::debug!("absolute path {:?} resolved to crate {:?}", path, def); 178 log::debug!("absolute path {:?} resolved to crate {:?}", path, def);
170 PerNs::types(*def) 179 PerNs::types(*def, ResolvedVisibility::Public)
171 } else { 180 } else {
172 return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude 181 return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
173 } 182 }
@@ -175,7 +184,7 @@ impl CrateDefMap {
175 }; 184 };
176 185
177 for (i, segment) in segments { 186 for (i, segment) in segments {
178 let curr = match curr_per_ns.take_types() { 187 let (curr, vis) = match curr_per_ns.take_types_vis() {
179 Some(r) => r, 188 Some(r) => r,
180 None => { 189 None => {
181 // we still have path segments left, but the path so far 190 // we still have path segments left, but the path so far
@@ -216,11 +225,11 @@ impl CrateDefMap {
216 match enum_data.variant(&segment) { 225 match enum_data.variant(&segment) {
217 Some(local_id) => { 226 Some(local_id) => {
218 let variant = EnumVariantId { parent: e, local_id }; 227 let variant = EnumVariantId { parent: e, local_id };
219 PerNs::both(variant.into(), variant.into()) 228 PerNs::both(variant.into(), variant.into(), ResolvedVisibility::Public)
220 } 229 }
221 None => { 230 None => {
222 return ResolvePathResult::with( 231 return ResolvePathResult::with(
223 PerNs::types(e.into()), 232 PerNs::types(e.into(), vis),
224 ReachedFixedPoint::Yes, 233 ReachedFixedPoint::Yes,
225 Some(i), 234 Some(i),
226 Some(self.krate), 235 Some(self.krate),
@@ -238,7 +247,7 @@ impl CrateDefMap {
238 ); 247 );
239 248
240 return ResolvePathResult::with( 249 return ResolvePathResult::with(
241 PerNs::types(s), 250 PerNs::types(s, vis),
242 ReachedFixedPoint::Yes, 251 ReachedFixedPoint::Yes,
243 Some(i), 252 Some(i),
244 Some(self.krate), 253 Some(self.krate),
@@ -262,11 +271,15 @@ impl CrateDefMap {
262 // - current module / scope 271 // - current module / scope
263 // - extern prelude 272 // - extern prelude
264 // - std prelude 273 // - std prelude
265 let from_legacy_macro = 274 let from_legacy_macro = self[module]
266 self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); 275 .scope
276 .get_legacy_macro(name)
277 .map_or_else(PerNs::none, |m| PerNs::macros(m, ResolvedVisibility::Public));
267 let from_scope = self[module].scope.get(name, shadow); 278 let from_scope = self[module].scope.get(name, shadow);
268 let from_extern_prelude = 279 let from_extern_prelude = self
269 self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); 280 .extern_prelude
281 .get(name)
282 .map_or(PerNs::none(), |&it| PerNs::types(it, ResolvedVisibility::Public));
270 let from_prelude = self.resolve_in_prelude(db, name, shadow); 283 let from_prelude = self.resolve_in_prelude(db, name, shadow);
271 284
272 from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) 285 from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)