Skip to content

Commit ec1f459

Browse files
bors[bot]matklad
andauthored
Merge #6151
6151: Constrain ImportMap to only store simple paths r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 81d6816 + 0d3bc38 commit ec1f459

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

crates/hir_def/src/import_map.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ use std::{cmp::Ordering, fmt, hash::BuildHasherDefault, sync::Arc};
44

55
use base_db::CrateId;
66
use fst::{self, Streamer};
7+
use hir_expand::name::Name;
78
use indexmap::{map::Entry, IndexMap};
9+
use itertools::Itertools;
810
use rustc_hash::{FxHashMap, FxHasher};
911
use smallvec::SmallVec;
1012
use syntax::SmolStr;
1113

1214
use crate::{
13-
db::DefDatabase,
14-
item_scope::ItemInNs,
15-
path::{ModPath, PathKind},
16-
visibility::Visibility,
17-
AssocItemId, ModuleDefId, ModuleId, TraitId,
15+
db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
16+
ModuleId, TraitId,
1817
};
1918

2019
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
@@ -23,11 +22,28 @@ type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
2322
#[derive(Debug, Clone, Eq, PartialEq)]
2423
pub struct ImportInfo {
2524
/// A path that can be used to import the item, relative to the crate's root.
26-
pub path: ModPath,
25+
pub path: ImportPath,
2726
/// The module containing this item.
2827
pub container: ModuleId,
2928
}
3029

30+
#[derive(Debug, Clone, Eq, PartialEq)]
31+
pub struct ImportPath {
32+
pub segments: Vec<Name>,
33+
}
34+
35+
impl fmt::Display for ImportPath {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
fmt::Display::fmt(&self.segments.iter().format("::"), f)
38+
}
39+
}
40+
41+
impl ImportPath {
42+
fn len(&self) -> usize {
43+
self.segments.len()
44+
}
45+
}
46+
3147
/// A map from publicly exported items to the path needed to import/name them from a downstream
3248
/// crate.
3349
///
@@ -61,7 +77,7 @@ impl ImportMap {
6177
let mut import_map = Self::default();
6278

6379
// We look only into modules that are public(ly reexported), starting with the crate root.
64-
let empty = ModPath { kind: PathKind::Plain, segments: vec![] };
80+
let empty = ImportPath { segments: vec![] };
6581
let root = ModuleId { krate, local_id: def_map.root };
6682
let mut worklist = vec![(root, empty)];
6783
while let Some((module, mod_path)) = worklist.pop() {
@@ -152,8 +168,8 @@ impl ImportMap {
152168
}
153169

154170
/// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root.
155-
pub fn path_of(&self, item: ItemInNs) -> Option<&ModPath> {
156-
Some(&self.map.get(&item)?.path)
171+
pub fn path_of(&self, item: ItemInNs) -> Option<&ImportPath> {
172+
self.import_info_for(item).map(|it| &it.path)
157173
}
158174

159175
pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
@@ -197,7 +213,7 @@ impl fmt::Debug for ImportMap {
197213
}
198214
}
199215

200-
fn fst_path(path: &ModPath) -> String {
216+
fn fst_path(path: &ImportPath) -> String {
201217
let mut s = path.to_string();
202218
s.make_ascii_lowercase();
203219
s

0 commit comments

Comments
 (0)