Skip to content

Commit b78d69c

Browse files
committed
Auto merge of #15422 - Veykril:import-sources, r=Veykril
internal: Record import source IDs cc #14079
2 parents b6ee96c + 63aba76 commit b78d69c

File tree

7 files changed

+219
-184
lines changed

7 files changed

+219
-184
lines changed

crates/hir-def/src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl ExternCrateDeclData {
487487
db.crate_def_map(loc.container.krate())
488488
.extern_prelude()
489489
.find(|&(prelude_name, ..)| *prelude_name == name)
490-
.map(|(_, root)| root.krate())
490+
.map(|(_, (root, _))| root.krate())
491491
};
492492

493493
Arc::new(Self {

crates/hir-def/src/find_path.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
nameres::DefMap,
1212
path::{ModPath, PathKind},
1313
visibility::Visibility,
14-
ModuleDefId, ModuleId,
14+
CrateRootModuleId, ModuleDefId, ModuleId,
1515
};
1616

1717
/// Find a path that can be used to refer to a certain item. This can depend on
@@ -81,7 +81,7 @@ fn find_path_inner(
8181
}
8282

8383
let def_map = from.def_map(db);
84-
let crate_root = def_map.crate_root().into();
84+
let crate_root = def_map.crate_root();
8585
// - if the item is a module, jump straight to module search
8686
if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item {
8787
let mut visited_modules = FxHashSet::default();
@@ -149,7 +149,7 @@ fn find_path_for_module(
149149
db: &dyn DefDatabase,
150150
def_map: &DefMap,
151151
visited_modules: &mut FxHashSet<ModuleId>,
152-
crate_root: ModuleId,
152+
crate_root: CrateRootModuleId,
153153
from: ModuleId,
154154
module_id: ModuleId,
155155
max_len: usize,
@@ -183,7 +183,7 @@ fn find_path_for_module(
183183

184184
// - if the item is the crate root of a dependency crate, return the name from the extern prelude
185185
let root_def_map = crate_root.def_map(db);
186-
for (name, def_id) in root_def_map.extern_prelude() {
186+
for (name, (def_id, _extern_crate)) in root_def_map.extern_prelude() {
187187
if module_id == def_id {
188188
let name = scope_name.unwrap_or_else(|| name.clone());
189189

@@ -192,7 +192,7 @@ fn find_path_for_module(
192192
def_map[local_id]
193193
.scope
194194
.type_(&name)
195-
.filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id))
195+
.filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id.into()))
196196
})
197197
.is_some();
198198
let kind = if name_already_occupied_in_type_ns {
@@ -244,7 +244,7 @@ fn find_in_prelude(
244244
item: ItemInNs,
245245
from: ModuleId,
246246
) -> Option<ModPath> {
247-
let prelude_module = root_def_map.prelude()?;
247+
let (prelude_module, _) = root_def_map.prelude()?;
248248
// Preludes in block DefMaps are ignored, only the crate DefMap is searched
249249
let prelude_def_map = prelude_module.def_map(db);
250250
let prelude_scope = &prelude_def_map[prelude_module.local_id].scope;
@@ -293,7 +293,7 @@ fn calculate_best_path(
293293
db: &dyn DefDatabase,
294294
def_map: &DefMap,
295295
visited_modules: &mut FxHashSet<ModuleId>,
296-
crate_root: ModuleId,
296+
crate_root: CrateRootModuleId,
297297
max_len: usize,
298298
item: ItemInNs,
299299
from: ModuleId,

crates/hir-def/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ impl CrateRootModuleId {
109109
}
110110
}
111111

112+
impl PartialEq<ModuleId> for CrateRootModuleId {
113+
fn eq(&self, other: &ModuleId) -> bool {
114+
other.block.is_none() && other.local_id == DefMap::ROOT && self.krate == other.krate
115+
}
116+
}
117+
impl PartialEq<CrateRootModuleId> for ModuleId {
118+
fn eq(&self, other: &CrateRootModuleId) -> bool {
119+
other == self
120+
}
121+
}
122+
112123
impl From<CrateRootModuleId> for ModuleId {
113124
fn from(CrateRootModuleId { krate }: CrateRootModuleId) -> Self {
114125
ModuleId { krate, block: None, local_id: DefMap::ROOT }

crates/hir-def/src/nameres.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ use crate::{
7777
path::ModPath,
7878
per_ns::PerNs,
7979
visibility::Visibility,
80-
AstId, BlockId, BlockLoc, CrateRootModuleId, FunctionId, LocalModuleId, Lookup, MacroExpander,
81-
MacroId, ModuleId, ProcMacroId,
80+
AstId, BlockId, BlockLoc, CrateRootModuleId, ExternCrateId, FunctionId, LocalModuleId, Lookup,
81+
MacroExpander, MacroId, ModuleId, ProcMacroId, UseId,
8282
};
8383

8484
/// Contains the results of (early) name resolution.
@@ -105,10 +105,10 @@ pub struct DefMap {
105105
/// The prelude is empty for non-block DefMaps (unless `#[prelude_import]` was used,
106106
/// but that attribute is nightly and when used in a block, it affects resolution globally
107107
/// so we aren't handling this correctly anyways).
108-
prelude: Option<ModuleId>,
108+
prelude: Option<(ModuleId, Option<UseId>)>,
109109
/// `macro_use` prelude that contains macros from `#[macro_use]`'d external crates. Note that
110110
/// this contains all kinds of macro, not just `macro_rules!` macro.
111-
macro_use_prelude: FxHashMap<Name, MacroId>,
111+
macro_use_prelude: FxHashMap<Name, (MacroId, Option<ExternCrateId>)>,
112112

113113
/// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
114114
/// attributes.
@@ -125,7 +125,7 @@ pub struct DefMap {
125125
#[derive(Clone, Debug, PartialEq, Eq)]
126126
struct DefMapCrateData {
127127
/// The extern prelude which contains all root modules of external crates that are in scope.
128-
extern_prelude: FxHashMap<Name, CrateRootModuleId>,
128+
extern_prelude: FxHashMap<Name, (CrateRootModuleId, Option<ExternCrateId>)>,
129129

130130
/// Side table for resolving derive helpers.
131131
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
@@ -427,15 +427,19 @@ impl DefMap {
427427
self.block.map(|block| block.block)
428428
}
429429

430-
pub(crate) fn prelude(&self) -> Option<ModuleId> {
430+
pub(crate) fn prelude(&self) -> Option<(ModuleId, Option<UseId>)> {
431431
self.prelude
432432
}
433433

434-
pub(crate) fn extern_prelude(&self) -> impl Iterator<Item = (&Name, ModuleId)> + '_ {
435-
self.data.extern_prelude.iter().map(|(name, &def)| (name, def.into()))
434+
pub(crate) fn extern_prelude(
435+
&self,
436+
) -> impl Iterator<Item = (&Name, (CrateRootModuleId, Option<ExternCrateId>))> + '_ {
437+
self.data.extern_prelude.iter().map(|(name, &def)| (name, def))
436438
}
437439

438-
pub(crate) fn macro_use_prelude(&self) -> impl Iterator<Item = (&Name, MacroId)> + '_ {
440+
pub(crate) fn macro_use_prelude(
441+
&self,
442+
) -> impl Iterator<Item = (&Name, (MacroId, Option<ExternCrateId>))> + '_ {
439443
self.macro_use_prelude.iter().map(|(name, &def)| (name, def))
440444
}
441445

0 commit comments

Comments
 (0)