Skip extern prelude path resolution in block def maps

This commit is contained in:
Lukas Wirth 2023-06-01 17:58:08 +02:00
parent 54e3ef658a
commit 11b937177b
4 changed files with 21 additions and 32 deletions

View File

@ -81,7 +81,7 @@ fn find_path_inner(
} }
let def_map = from.def_map(db); let def_map = from.def_map(db);
let crate_root = def_map.crate_root(db); let crate_root = def_map.crate_root();
// - if the item is a module, jump straight to module search // - if the item is a module, jump straight to module search
if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item { if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item {
let mut visited_modules = FxHashSet::default(); let mut visited_modules = FxHashSet::default();
@ -454,7 +454,7 @@ fn find_local_import_locations(
worklist.push(ancestor); worklist.push(ancestor);
} }
let def_map = def_map.crate_root(db).def_map(db); let def_map = def_map.crate_root().def_map(db);
let mut seen: FxHashSet<_> = FxHashSet::default(); let mut seen: FxHashSet<_> = FxHashSet::default();

View File

@ -431,15 +431,8 @@ pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId {
ModuleId { krate: self.krate, local_id, block } ModuleId { krate: self.krate, local_id, block }
} }
pub(crate) fn crate_root(&self, db: &dyn DefDatabase) -> ModuleId { pub(crate) fn crate_root(&self) -> ModuleId {
self.with_ancestor_maps(db, Self::ROOT, &mut |def_map, _module| { ModuleId { krate: self.krate, block: None, local_id: DefMap::ROOT }
if def_map.block.is_none() {
Some(def_map.module_id(Self::ROOT))
} else {
None
}
})
.expect("DefMap chain without root")
} }
pub(crate) fn resolve_path( pub(crate) fn resolve_path(

View File

@ -834,11 +834,7 @@ fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialRe
fn resolve_extern_crate(&self, name: &Name) -> Option<ModuleId> { fn resolve_extern_crate(&self, name: &Name) -> Option<ModuleId> {
if *name == name!(self) { if *name == name!(self) {
cov_mark::hit!(extern_crate_self_as); cov_mark::hit!(extern_crate_self_as);
let root = match self.def_map.block { Some(self.def_map.crate_root())
Some(_) => self.def_map.crate_root(self.db),
None => self.def_map.module_id(DefMap::ROOT),
};
Some(root)
} else { } else {
self.deps.get(name).map(|dep| ModuleId { self.deps.get(name).map(|dep| ModuleId {
krate: dep.crate_id, krate: dep.crate_id,

View File

@ -20,7 +20,7 @@
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
visibility::{RawVisibility, Visibility}, visibility::{RawVisibility, Visibility},
AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId,
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -74,17 +74,6 @@ pub(super) fn filter_macro(
} }
impl DefMap { impl DefMap {
pub(super) fn resolve_name_in_extern_prelude(
&self,
db: &dyn DefDatabase,
name: &Name,
) -> Option<ModuleId> {
match self.block {
Some(_) => self.crate_root(db).def_map(db).data.extern_prelude.get(name).copied(),
None => self.data.extern_prelude.get(name).copied(),
}
}
pub(crate) fn resolve_visibility( pub(crate) fn resolve_visibility(
&self, &self,
db: &dyn DefDatabase, db: &dyn DefDatabase,
@ -204,7 +193,7 @@ pub(super) fn resolve_path_fp_with_macro_single(
PathKind::DollarCrate(krate) => { PathKind::DollarCrate(krate) => {
if krate == self.krate { if krate == self.krate {
cov_mark::hit!(macro_dollar_crate_self); cov_mark::hit!(macro_dollar_crate_self);
PerNs::types(self.crate_root(db).into(), Visibility::Public) PerNs::types(self.crate_root().into(), Visibility::Public)
} else { } else {
let def_map = db.crate_def_map(krate); let def_map = db.crate_def_map(krate);
let module = def_map.module_id(Self::ROOT); let module = def_map.module_id(Self::ROOT);
@ -212,7 +201,7 @@ pub(super) fn resolve_path_fp_with_macro_single(
PerNs::types(module.into(), Visibility::Public) PerNs::types(module.into(), Visibility::Public)
} }
} }
PathKind::Crate => PerNs::types(self.crate_root(db).into(), Visibility::Public), PathKind::Crate => PerNs::types(self.crate_root().into(), Visibility::Public),
// plain import or absolute path in 2015: crate-relative with // plain import or absolute path in 2015: crate-relative with
// fallback to extern prelude (with the simplification in // fallback to extern prelude (with the simplification in
// rust-lang/rust#57745) // rust-lang/rust#57745)
@ -453,6 +442,10 @@ fn resolve_name_in_module(
}; };
let extern_prelude = || { let extern_prelude = || {
if self.block.is_some() {
// Don't resolve extern prelude in block `DefMap`s.
return PerNs::none();
}
self.data self.data
.extern_prelude .extern_prelude
.get(name) .get(name)
@ -479,13 +472,20 @@ fn resolve_name_in_crate_root_or_extern_prelude(
) -> PerNs { ) -> PerNs {
let from_crate_root = match self.block { let from_crate_root = match self.block {
Some(_) => { Some(_) => {
let def_map = self.crate_root(db).def_map(db); let def_map = self.crate_root().def_map(db);
def_map[Self::ROOT].scope.get(name) def_map[Self::ROOT].scope.get(name)
} }
None => self[Self::ROOT].scope.get(name), None => self[Self::ROOT].scope.get(name),
}; };
let from_extern_prelude = || { let from_extern_prelude = || {
self.resolve_name_in_extern_prelude(db, name) if self.block.is_some() {
// Don't resolve extern prelude in block `DefMap`s.
return PerNs::none();
}
self.data
.extern_prelude
.get(name)
.copied()
.map_or(PerNs::none(), |it| PerNs::types(it.into(), Visibility::Public)) .map_or(PerNs::none(), |it| PerNs::types(it.into(), Visibility::Public))
}; };