From dc5e5f610cf057654bce9d8fcf275ae375299f90 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Nov 2019 23:47:52 +0300 Subject: Reduce visibility --- crates/ra_hir_def/src/nameres/raw.rs | 46 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index cb47fa317..bad67d409 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -88,7 +88,7 @@ impl RawItems { (Arc::new(collector.raw_items), Arc::new(collector.source_map)) } - pub fn items(&self) -> &[RawItem] { + pub(super) fn items(&self) -> &[RawItem] { &self.items } } @@ -127,17 +127,17 @@ type Attrs = Option>; #[derive(Debug, PartialEq, Eq, Clone)] pub struct RawItem { attrs: Attrs, - pub kind: RawItemKind, + pub(super) kind: RawItemKind, } impl RawItem { - pub fn attrs(&self) -> &[Attr] { + pub(super) fn attrs(&self) -> &[Attr] { self.attrs.as_ref().map_or(&[], |it| &*it) } } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum RawItemKind { +pub(crate) enum RawItemKind { Module(Module), Import(ImportId), Def(Def), @@ -145,11 +145,11 @@ pub enum RawItemKind { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Module(RawId); +pub(crate) struct Module(RawId); impl_arena_id!(Module); #[derive(Debug, PartialEq, Eq)] -pub enum ModuleData { +pub(crate) enum ModuleData { Declaration { name: Name, ast_id: FileAstId }, Definition { name: Name, ast_id: FileAstId, items: Vec }, } @@ -160,26 +160,26 @@ impl_arena_id!(ImportId); #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { - pub path: Path, - pub alias: Option, - pub is_glob: bool, - pub is_prelude: bool, - pub is_extern_crate: bool, - pub is_macro_use: bool, + pub(super) path: Path, + pub(super) alias: Option, + pub(super) is_glob: bool, + pub(super) is_prelude: bool, + pub(super) is_extern_crate: bool, + pub(super) is_macro_use: bool, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Def(RawId); +pub(crate) struct Def(RawId); impl_arena_id!(Def); #[derive(Debug, PartialEq, Eq)] -pub struct DefData { - pub name: Name, - pub kind: DefKind, +pub(crate) struct DefData { + pub(super) name: Name, + pub(super) kind: DefKind, } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum DefKind { +pub(crate) enum DefKind { Function(FileAstId), Struct(FileAstId), Union(FileAstId), @@ -191,15 +191,15 @@ pub enum DefKind { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Macro(RawId); +pub(crate) struct Macro(RawId); impl_arena_id!(Macro); #[derive(Debug, PartialEq, Eq)] -pub struct MacroData { - pub ast_id: FileAstId, - pub path: Path, - pub name: Option, - pub export: bool, +pub(crate) struct MacroData { + pub(super) ast_id: FileAstId, + pub(super) path: Path, + pub(super) name: Option, + pub(super) export: bool, } struct RawItemsCollector { -- cgit v1.2.3 From 785887b3829e2753cefcde48f527cf829fd051f4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Nov 2019 23:53:39 +0300 Subject: Simplify There's only one call-site for the function, so it seems fine to inline --- crates/ra_hir_def/src/nameres.rs | 6 ------ crates/ra_hir_def/src/nameres/collector.rs | 6 +++++- 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index e1959cdb9..fb3ba5305 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -195,12 +195,6 @@ pub struct Resolution { pub import: Option, } -impl Resolution { - fn from_macro(macro_: MacroDefId) -> Self { - Resolution { def: PerNs::macros(macro_), import: None } - } -} - #[derive(Debug, Clone)] struct ResolvePathResult { resolved_def: PerNs, diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 3b61d9895..9ab378d54 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -182,7 +182,11 @@ where // In Rust, `#[macro_export]` macros are unconditionally visible at the // crate root, even if the parent modules is **not** visible. if export { - self.update(self.def_map.root, None, &[(name, Resolution::from_macro(macro_))]); + self.update( + self.def_map.root, + None, + &[(name, Resolution { def: PerNs::macros(macro_), import: None })], + ); } } -- cgit v1.2.3 From 53945841bea2d8016586889ccd8f8bd8c487547b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Nov 2019 00:17:17 +0300 Subject: Extract path resolution submodule --- crates/ra_hir_def/src/nameres.rs | 247 +-------------------- crates/ra_hir_def/src/nameres/collector.rs | 4 +- crates/ra_hir_def/src/nameres/path_resolution.rs | 261 +++++++++++++++++++++++ 3 files changed, 269 insertions(+), 243 deletions(-) create mode 100644 crates/ra_hir_def/src/nameres/path_resolution.rs (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index fb3ba5305..115b0264c 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -52,6 +52,7 @@ pub mod raw; pub mod per_ns; mod collector; mod mod_resolution; +mod path_resolution; #[cfg(test)] mod tests; @@ -65,14 +66,15 @@ use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; use ra_syntax::ast; use rustc_hash::{FxHashMap, FxHashSet}; -use test_utils::tested_by; use crate::{ builtin_type::BuiltinType, db::DefDatabase2, - nameres::{diagnostics::DefDiagnostic, per_ns::PerNs, raw::ImportId}, - path::{Path, PathKind}, - AdtId, AstId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, TraitId, + nameres::{ + diagnostics::DefDiagnostic, path_resolution::ResolveMode, per_ns::PerNs, raw::ImportId, + }, + path::Path, + AstId, CrateModuleId, ModuleDefId, ModuleId, TraitId, }; /// Contains all top-level defs from a macro-expanded crate @@ -195,39 +197,6 @@ pub struct Resolution { pub import: Option, } -#[derive(Debug, Clone)] -struct ResolvePathResult { - resolved_def: PerNs, - segment_index: Option, - reached_fixedpoint: ReachedFixedPoint, -} - -impl ResolvePathResult { - fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult { - ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None) - } - - fn with( - resolved_def: PerNs, - reached_fixedpoint: ReachedFixedPoint, - segment_index: Option, - ) -> ResolvePathResult { - ResolvePathResult { resolved_def, reached_fixedpoint, segment_index } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum ResolveMode { - Import, - Other, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum ReachedFixedPoint { - Yes, - No, -} - impl CrateDefMap { pub(crate) fn crate_def_map_query( // Note that this doesn't have `+ AstDatabase`! @@ -290,210 +259,6 @@ impl CrateDefMap { let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); (res.resolved_def, res.segment_index) } - - // Returns Yes if we are sure that additions to `ItemMap` wouldn't change - // the result. - fn resolve_path_fp_with_macro( - &self, - db: &impl DefDatabase2, - mode: ResolveMode, - original_module: CrateModuleId, - path: &Path, - ) -> ResolvePathResult { - let mut segments = path.segments.iter().enumerate(); - let mut curr_per_ns: PerNs = match path.kind { - PathKind::DollarCrate(krate) => { - if krate == self.krate { - tested_by!(macro_dollar_crate_self); - PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) - } else { - let def_map = db.crate_def_map(krate); - let module = ModuleId { krate, module_id: def_map.root }; - tested_by!(macro_dollar_crate_other); - PerNs::types(module.into()) - } - } - PathKind::Crate => { - PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) - } - PathKind::Self_ => { - PerNs::types(ModuleId { krate: self.krate, module_id: 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) - // FIXME there must be a nicer way to write this condition - PathKind::Plain | PathKind::Abs - if self.edition == Edition::Edition2015 - && (path.kind == PathKind::Abs || mode == ResolveMode::Import) => - { - let segment = match segments.next() { - Some((_, segment)) => segment, - None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), - }; - log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); - self.resolve_name_in_crate_root_or_extern_prelude(&segment.name) - } - PathKind::Plain => { - let segment = match segments.next() { - Some((_, segment)) => segment, - None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), - }; - log::debug!("resolving {:?} in module", segment); - self.resolve_name_in_module(db, original_module, &segment.name) - } - PathKind::Super => { - if let Some(p) = self.modules[original_module].parent { - PerNs::types(ModuleId { krate: self.krate, module_id: p }.into()) - } else { - log::debug!("super path in root module"); - return ResolvePathResult::empty(ReachedFixedPoint::Yes); - } - } - PathKind::Abs => { - // 2018-style absolute path -- only extern prelude - let segment = match segments.next() { - Some((_, segment)) => segment, - None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), - }; - if let Some(def) = self.extern_prelude.get(&segment.name) { - log::debug!("absolute path {:?} resolved to crate {:?}", path, def); - PerNs::types(*def) - } else { - return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude - } - } - PathKind::Type(_) => { - // This is handled in `infer::infer_path_expr` - // The result returned here does not matter - return ResolvePathResult::empty(ReachedFixedPoint::Yes); - } - }; - - for (i, segment) in segments { - let curr = match curr_per_ns.take_types() { - Some(r) => r, - None => { - // we still have path segments left, but the path so far - // didn't resolve in the types namespace => no resolution - // (don't break here because `curr_per_ns` might contain - // something in the value namespace, and it would be wrong - // to return that) - return ResolvePathResult::empty(ReachedFixedPoint::No); - } - }; - // resolve segment in curr - - curr_per_ns = match curr { - ModuleDefId::ModuleId(module) => { - 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); - return ResolvePathResult::with( - def, - ReachedFixedPoint::Yes, - s.map(|s| s + i), - ); - } - - // Since it is a qualified path here, it should not contains legacy macros - match self[module.module_id].scope.get(&segment.name) { - Some(res) => res.def, - _ => { - log::debug!("path segment {:?} not found", segment.name); - return ResolvePathResult::empty(ReachedFixedPoint::No); - } - } - } - ModuleDefId::AdtId(AdtId::EnumId(e)) => { - // enum variant - tested_by!(can_import_enum_variant); - let enum_data = db.enum_data(e); - match enum_data.variant(&segment.name) { - Some(local_id) => { - let variant = EnumVariantId { parent: e, local_id }; - PerNs::both(variant.into(), variant.into()) - } - None => { - return ResolvePathResult::with( - PerNs::types(e.into()), - ReachedFixedPoint::Yes, - Some(i), - ); - } - } - } - s => { - // could be an inherent method call in UFCS form - // (`Struct::method`), or some other kind of associated item - log::debug!( - "path segment {:?} resolved to non-module {:?}, but is not last", - segment.name, - curr, - ); - - return ResolvePathResult::with( - PerNs::types(s), - ReachedFixedPoint::Yes, - Some(i), - ); - } - }; - } - ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) - } - - fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { - let from_crate_root = - self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); - let from_extern_prelude = self.resolve_name_in_extern_prelude(name); - - from_crate_root.or(from_extern_prelude) - } - - fn resolve_name_in_module( - &self, - db: &impl DefDatabase2, - module: CrateModuleId, - name: &Name, - ) -> PerNs { - // Resolve in: - // - legacy scope of macro - // - current module / scope - // - extern prelude - // - std prelude - let from_legacy_macro = - self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); - let from_scope = self[module].scope.get(name).map_or_else(PerNs::none, |res| res.def); - let from_extern_prelude = - self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); - let from_prelude = self.resolve_in_prelude(db, name); - - from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) - } - - fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { - self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) - } - - fn resolve_in_prelude(&self, db: &impl DefDatabase2, name: &Name) -> PerNs { - if let Some(prelude) = self.prelude { - let keep; - let def_map = if prelude.krate == self.krate { - self - } else { - // Extend lifetime - keep = db.crate_def_map(prelude.krate); - &keep - }; - def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) - } else { - PerNs::none() - } - } } mod diagnostics { diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 9ab378d54..aacd50df8 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -14,8 +14,8 @@ use crate::{ attr::Attr, db::DefDatabase2, nameres::{ - diagnostics::DefDiagnostic, mod_resolution::ModDir, per_ns::PerNs, raw, CrateDefMap, - ModuleData, ReachedFixedPoint, Resolution, ResolveMode, + diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, + per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, }, path::{Path, PathKind}, AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs new file mode 100644 index 000000000..95692f826 --- /dev/null +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -0,0 +1,261 @@ +//! This modules implements a function to resolve a path `foo::bar::baz` to a +//! def, which is used within the name resolution. +//! +//! When name resolution is finished, the result of resolving a path is either +//! `Some(def)` or `None`. However, when we are in process of resolving imports +//! or macros, there's a third possibility: +//! +//! I can't resolve this path right now, but I might be resolve this path +//! later, when more macros are expanded. +//! +//! `ReachedFixedPoint` signals about this. + +use hir_expand::name::Name; +use ra_db::Edition; +use test_utils::tested_by; + +use crate::{ + db::DefDatabase2, + nameres::{per_ns::PerNs, CrateDefMap}, + path::{Path, PathKind}, + AdtId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum ResolveMode { + Import, + Other, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum ReachedFixedPoint { + Yes, + No, +} + +#[derive(Debug, Clone)] +pub(super) struct ResolvePathResult { + pub(super) resolved_def: PerNs, + pub(super) segment_index: Option, + pub(super) reached_fixedpoint: ReachedFixedPoint, +} + +impl ResolvePathResult { + fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult { + ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None) + } + + fn with( + resolved_def: PerNs, + reached_fixedpoint: ReachedFixedPoint, + segment_index: Option, + ) -> ResolvePathResult { + ResolvePathResult { resolved_def, reached_fixedpoint, segment_index } + } +} + +impl CrateDefMap { + pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { + self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) + } + + // Returns Yes if we are sure that additions to `ItemMap` wouldn't change + // the result. + pub(super) fn resolve_path_fp_with_macro( + &self, + db: &impl DefDatabase2, + mode: ResolveMode, + original_module: CrateModuleId, + path: &Path, + ) -> ResolvePathResult { + let mut segments = path.segments.iter().enumerate(); + let mut curr_per_ns: PerNs = match path.kind { + PathKind::DollarCrate(krate) => { + if krate == self.krate { + tested_by!(macro_dollar_crate_self); + PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) + } else { + let def_map = db.crate_def_map(krate); + let module = ModuleId { krate, module_id: def_map.root }; + tested_by!(macro_dollar_crate_other); + PerNs::types(module.into()) + } + } + PathKind::Crate => { + PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) + } + PathKind::Self_ => { + PerNs::types(ModuleId { krate: self.krate, module_id: 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) + // FIXME there must be a nicer way to write this condition + PathKind::Plain | PathKind::Abs + if self.edition == Edition::Edition2015 + && (path.kind == PathKind::Abs || mode == ResolveMode::Import) => + { + let segment = match segments.next() { + Some((_, segment)) => segment, + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), + }; + log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); + self.resolve_name_in_crate_root_or_extern_prelude(&segment.name) + } + PathKind::Plain => { + let segment = match segments.next() { + Some((_, segment)) => segment, + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), + }; + log::debug!("resolving {:?} in module", segment); + self.resolve_name_in_module(db, original_module, &segment.name) + } + PathKind::Super => { + if let Some(p) = self.modules[original_module].parent { + PerNs::types(ModuleId { krate: self.krate, module_id: p }.into()) + } else { + log::debug!("super path in root module"); + return ResolvePathResult::empty(ReachedFixedPoint::Yes); + } + } + PathKind::Abs => { + // 2018-style absolute path -- only extern prelude + let segment = match segments.next() { + Some((_, segment)) => segment, + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), + }; + if let Some(def) = self.extern_prelude.get(&segment.name) { + log::debug!("absolute path {:?} resolved to crate {:?}", path, def); + PerNs::types(*def) + } else { + return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude + } + } + PathKind::Type(_) => { + // This is handled in `infer::infer_path_expr` + // The result returned here does not matter + return ResolvePathResult::empty(ReachedFixedPoint::Yes); + } + }; + + for (i, segment) in segments { + let curr = match curr_per_ns.take_types() { + Some(r) => r, + None => { + // we still have path segments left, but the path so far + // didn't resolve in the types namespace => no resolution + // (don't break here because `curr_per_ns` might contain + // something in the value namespace, and it would be wrong + // to return that) + return ResolvePathResult::empty(ReachedFixedPoint::No); + } + }; + // resolve segment in curr + + curr_per_ns = match curr { + ModuleDefId::ModuleId(module) => { + 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); + return ResolvePathResult::with( + def, + ReachedFixedPoint::Yes, + s.map(|s| s + i), + ); + } + + // Since it is a qualified path here, it should not contains legacy macros + match self[module.module_id].scope.get(&segment.name) { + Some(res) => res.def, + _ => { + log::debug!("path segment {:?} not found", segment.name); + return ResolvePathResult::empty(ReachedFixedPoint::No); + } + } + } + ModuleDefId::AdtId(AdtId::EnumId(e)) => { + // enum variant + tested_by!(can_import_enum_variant); + let enum_data = db.enum_data(e); + match enum_data.variant(&segment.name) { + Some(local_id) => { + let variant = EnumVariantId { parent: e, local_id }; + PerNs::both(variant.into(), variant.into()) + } + None => { + return ResolvePathResult::with( + PerNs::types(e.into()), + ReachedFixedPoint::Yes, + Some(i), + ); + } + } + } + s => { + // could be an inherent method call in UFCS form + // (`Struct::method`), or some other kind of associated item + log::debug!( + "path segment {:?} resolved to non-module {:?}, but is not last", + segment.name, + curr, + ); + + return ResolvePathResult::with( + PerNs::types(s), + ReachedFixedPoint::Yes, + Some(i), + ); + } + }; + } + ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) + } + + fn resolve_name_in_module( + &self, + db: &impl DefDatabase2, + module: CrateModuleId, + name: &Name, + ) -> PerNs { + // Resolve in: + // - legacy scope of macro + // - current module / scope + // - extern prelude + // - std prelude + let from_legacy_macro = + self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); + let from_scope = self[module].scope.get(name).map_or_else(PerNs::none, |res| res.def); + let from_extern_prelude = + self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); + let from_prelude = self.resolve_in_prelude(db, name); + + from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) + } + + fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { + let from_crate_root = + self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); + let from_extern_prelude = self.resolve_name_in_extern_prelude(name); + + from_crate_root.or(from_extern_prelude) + } + + fn resolve_in_prelude(&self, db: &impl DefDatabase2, name: &Name) -> PerNs { + if let Some(prelude) = self.prelude { + let keep; + let def_map = if prelude.krate == self.krate { + self + } else { + // Extend lifetime + keep = db.crate_def_map(prelude.krate); + &keep + }; + def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) + } else { + PerNs::none() + } + } +} -- cgit v1.2.3 From 657340e078bc120d6fe4f27766d8b98d711d3daf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Nov 2019 00:23:11 +0300 Subject: Reduce visibility --- crates/ra_hir_def/src/nameres/raw.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index bad67d409..369376f30 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -125,7 +125,7 @@ impl Index for RawItems { type Attrs = Option>; #[derive(Debug, PartialEq, Eq, Clone)] -pub struct RawItem { +pub(super) struct RawItem { attrs: Attrs, pub(super) kind: RawItemKind, } @@ -137,7 +137,7 @@ impl RawItem { } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(crate) enum RawItemKind { +pub(super) enum RawItemKind { Module(Module), Import(ImportId), Def(Def), @@ -145,11 +145,11 @@ pub(crate) enum RawItemKind { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct Module(RawId); +pub(super) struct Module(RawId); impl_arena_id!(Module); #[derive(Debug, PartialEq, Eq)] -pub(crate) enum ModuleData { +pub(super) enum ModuleData { Declaration { name: Name, ast_id: FileAstId }, Definition { name: Name, ast_id: FileAstId, items: Vec }, } @@ -169,17 +169,17 @@ pub struct ImportData { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct Def(RawId); +pub(super) struct Def(RawId); impl_arena_id!(Def); #[derive(Debug, PartialEq, Eq)] -pub(crate) struct DefData { +pub(super) struct DefData { pub(super) name: Name, pub(super) kind: DefKind, } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(crate) enum DefKind { +pub(super) enum DefKind { Function(FileAstId), Struct(FileAstId), Union(FileAstId), @@ -191,11 +191,11 @@ pub(crate) enum DefKind { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct Macro(RawId); +pub(super) struct Macro(RawId); impl_arena_id!(Macro); #[derive(Debug, PartialEq, Eq)] -pub(crate) struct MacroData { +pub(super) struct MacroData { pub(super) ast_id: FileAstId, pub(super) path: Path, pub(super) name: Option, -- cgit v1.2.3 From c626a677e831e820e8a1510b198de150675c71df Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Nov 2019 00:23:19 +0300 Subject: Touch up nameres doc comment --- crates/ra_hir_def/src/nameres.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 115b0264c..d3ecabb9b 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -14,10 +14,10 @@ //! //! ## Collecting RawItems //! -//! This happens in the `raw` module, which parses a single source file into a -//! set of top-level items. Nested imports are desugared to flat imports in -//! this phase. Macro calls are represented as a triple of (Path, Option, -//! TokenTree). +//! This happens in the `raw` module, which parses a single source file into a +//! set of top-level items. Nested imports are desugared to flat imports in this +//! phase. Macro calls are represented as a triple of (Path, Option, +//! TokenTree). //! //! ## Collecting Modules //! @@ -44,10 +44,9 @@ //! Macros from other crates (including proc-macros) can be used with //! `foo::bar!` syntax. We handle them similarly to imports. There's a list of //! unexpanded macros. On every iteration, we try to resolve each macro call -//! path and, upon success, we run macro expansion and "collect module" phase -//! on the result +//! path and, upon success, we run macro expansion and "collect module" phase on +//! the result -// FIXME: review privacy of submodules pub mod raw; pub mod per_ns; mod collector; -- cgit v1.2.3