From 0d3bc385775a738068a5fa7138f1d21edc50d426 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 6 Oct 2020 17:04:29 +0200 Subject: Constrain ImportMap to only store simple paths --- crates/hir_def/src/import_map.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 44bfe1593..028cae2e7 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs @@ -4,17 +4,16 @@ use std::{cmp::Ordering, fmt, hash::BuildHasherDefault, sync::Arc}; use base_db::CrateId; use fst::{self, Streamer}; +use hir_expand::name::Name; use indexmap::{map::Entry, IndexMap}; +use itertools::Itertools; use rustc_hash::{FxHashMap, FxHasher}; use smallvec::SmallVec; use syntax::SmolStr; use crate::{ - db::DefDatabase, - item_scope::ItemInNs, - path::{ModPath, PathKind}, - visibility::Visibility, - AssocItemId, ModuleDefId, ModuleId, TraitId, + db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId, + ModuleId, TraitId, }; type FxIndexMap = IndexMap>; @@ -23,11 +22,28 @@ type FxIndexMap = IndexMap>; #[derive(Debug, Clone, Eq, PartialEq)] pub struct ImportInfo { /// A path that can be used to import the item, relative to the crate's root. - pub path: ModPath, + pub path: ImportPath, /// The module containing this item. pub container: ModuleId, } +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct ImportPath { + pub segments: Vec, +} + +impl fmt::Display for ImportPath { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.segments.iter().format("::"), f) + } +} + +impl ImportPath { + fn len(&self) -> usize { + self.segments.len() + } +} + /// A map from publicly exported items to the path needed to import/name them from a downstream /// crate. /// @@ -61,7 +77,7 @@ impl ImportMap { let mut import_map = Self::default(); // We look only into modules that are public(ly reexported), starting with the crate root. - let empty = ModPath { kind: PathKind::Plain, segments: vec![] }; + let empty = ImportPath { segments: vec![] }; let root = ModuleId { krate, local_id: def_map.root }; let mut worklist = vec![(root, empty)]; while let Some((module, mod_path)) = worklist.pop() { @@ -152,8 +168,8 @@ impl ImportMap { } /// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root. - pub fn path_of(&self, item: ItemInNs) -> Option<&ModPath> { - Some(&self.map.get(&item)?.path) + pub fn path_of(&self, item: ItemInNs) -> Option<&ImportPath> { + self.import_info_for(item).map(|it| &it.path) } pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> { @@ -197,7 +213,7 @@ impl fmt::Debug for ImportMap { } } -fn fst_path(path: &ModPath) -> String { +fn fst_path(path: &ImportPath) -> String { let mut s = path.to_string(); s.make_ascii_lowercase(); s -- cgit v1.2.3