mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-16 17:58:16 +00:00
Merge #7360
7360: Make public DefMap fields private r=jonas-schievink a=jonas-schievink This gives us more freedom to change the implementation for name resolution within block expressions. bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
commit
15b59fe434
10 changed files with 31 additions and 23 deletions
|
@ -90,7 +90,7 @@ impl Crate {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn root_module(self, db: &dyn HirDatabase) -> Module {
|
pub fn root_module(self, db: &dyn HirDatabase) -> Module {
|
||||||
let module_id = db.crate_def_map(self.id).root;
|
let module_id = db.crate_def_map(self.id).root();
|
||||||
Module::new(self, module_id)
|
Module::new(self, module_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,7 +302,7 @@ impl Module {
|
||||||
/// in the module tree of any target in `Cargo.toml`.
|
/// in the module tree of any target in `Cargo.toml`.
|
||||||
pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
|
pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
|
||||||
let def_map = db.crate_def_map(self.id.krate);
|
let def_map = db.crate_def_map(self.id.krate);
|
||||||
self.with_module_id(def_map.root)
|
self.with_module_id(def_map.root())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterates over all child modules.
|
/// Iterates over all child modules.
|
||||||
|
@ -1000,7 +1000,7 @@ impl MacroDef {
|
||||||
/// early, in `hir_expand`, where modules simply do not exist yet.
|
/// early, in `hir_expand`, where modules simply do not exist yet.
|
||||||
pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
|
pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
|
||||||
let krate = self.id.krate;
|
let krate = self.id.krate;
|
||||||
let module_id = db.crate_def_map(krate).root;
|
let module_id = db.crate_def_map(krate).root();
|
||||||
Some(Module::new(Crate { id: krate }, module_id))
|
Some(Module::new(Crate { id: krate }, module_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<
|
||||||
if item == ItemInNs::Types(from.into()) {
|
if item == ItemInNs::Types(from.into()) {
|
||||||
// - if the item is the module we're in, use `self`
|
// - if the item is the module we're in, use `self`
|
||||||
Some(ModPath::from_segments(PathKind::Super(0), Vec::new()))
|
Some(ModPath::from_segments(PathKind::Super(0), Vec::new()))
|
||||||
} else if let Some(parent_id) = def_map.modules[from.local_id].parent {
|
} else if let Some(parent_id) = def_map[from.local_id].parent {
|
||||||
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
||||||
if item
|
if item
|
||||||
== ItemInNs::Types(ModuleDefId::ModuleId(ModuleId {
|
== ItemInNs::Types(ModuleDefId::ModuleId(ModuleId {
|
||||||
|
@ -111,7 +111,7 @@ fn find_path_inner(
|
||||||
|
|
||||||
// - if the item is already in scope, return the name under which it is
|
// - if the item is already in scope, return the name under which it is
|
||||||
let def_map = db.crate_def_map(from.krate);
|
let def_map = db.crate_def_map(from.krate);
|
||||||
let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
|
let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope;
|
||||||
let scope_name =
|
let scope_name =
|
||||||
if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None };
|
if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None };
|
||||||
if prefixed.is_none() && scope_name.is_some() {
|
if prefixed.is_none() && scope_name.is_some() {
|
||||||
|
@ -123,7 +123,7 @@ fn find_path_inner(
|
||||||
if item
|
if item
|
||||||
== ItemInNs::Types(ModuleDefId::ModuleId(ModuleId {
|
== ItemInNs::Types(ModuleDefId::ModuleId(ModuleId {
|
||||||
krate: from.krate,
|
krate: from.krate,
|
||||||
local_id: def_map.root,
|
local_id: def_map.root(),
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
|
return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
|
||||||
|
@ -147,7 +147,7 @@ fn find_path_inner(
|
||||||
if let Some(prelude_module) = def_map.prelude {
|
if let Some(prelude_module) = def_map.prelude {
|
||||||
let prelude_def_map = db.crate_def_map(prelude_module.krate);
|
let prelude_def_map = db.crate_def_map(prelude_module.krate);
|
||||||
let prelude_scope: &crate::item_scope::ItemScope =
|
let prelude_scope: &crate::item_scope::ItemScope =
|
||||||
&prelude_def_map.modules[prelude_module.local_id].scope;
|
&prelude_def_map[prelude_module.local_id].scope;
|
||||||
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
||||||
if vis.is_visible_from(db, from) {
|
if vis.is_visible_from(db, from) {
|
||||||
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
||||||
|
@ -175,7 +175,7 @@ fn find_path_inner(
|
||||||
|
|
||||||
// - otherwise, look for modules containing (reexporting) it and import it from one of those
|
// - otherwise, look for modules containing (reexporting) it and import it from one of those
|
||||||
|
|
||||||
let crate_root = ModuleId { local_id: def_map.root, krate: from.krate };
|
let crate_root = ModuleId { local_id: def_map.root(), krate: from.krate };
|
||||||
let crate_attrs = db.attrs(crate_root.into());
|
let crate_attrs = db.attrs(crate_root.into());
|
||||||
let prefer_no_std = crate_attrs.by_key("no_std").exists();
|
let prefer_no_std = crate_attrs.by_key("no_std").exists();
|
||||||
let mut best_path = None;
|
let mut best_path = None;
|
||||||
|
@ -287,7 +287,7 @@ fn find_local_import_locations(
|
||||||
|
|
||||||
// Compute the initial worklist. We start with all direct child modules of `from` as well as all
|
// Compute the initial worklist. We start with all direct child modules of `from` as well as all
|
||||||
// of its (recursive) parent modules.
|
// of its (recursive) parent modules.
|
||||||
let data = &def_map.modules[from.local_id];
|
let data = &def_map[from.local_id];
|
||||||
let mut worklist = data
|
let mut worklist = data
|
||||||
.children
|
.children
|
||||||
.values()
|
.values()
|
||||||
|
@ -296,7 +296,7 @@ fn find_local_import_locations(
|
||||||
let mut parent = data.parent;
|
let mut parent = data.parent;
|
||||||
while let Some(p) = parent {
|
while let Some(p) = parent {
|
||||||
worklist.push(ModuleId { krate: from.krate, local_id: p });
|
worklist.push(ModuleId { krate: from.krate, local_id: p });
|
||||||
parent = def_map.modules[p].parent;
|
parent = def_map[p].parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut seen: FxHashSet<_> = FxHashSet::default();
|
let mut seen: FxHashSet<_> = FxHashSet::default();
|
||||||
|
|
|
@ -75,7 +75,7 @@ impl ImportMap {
|
||||||
|
|
||||||
// We look only into modules that are public(ly reexported), starting with the crate root.
|
// We look only into modules that are public(ly reexported), starting with the crate root.
|
||||||
let empty = ImportPath { segments: vec![] };
|
let empty = ImportPath { segments: vec![] };
|
||||||
let root = ModuleId { krate, local_id: def_map.root };
|
let root = ModuleId { krate, local_id: def_map.root() };
|
||||||
let mut worklist = vec![(root, empty)];
|
let mut worklist = vec![(root, empty)];
|
||||||
while let Some((module, mod_path)) = worklist.pop() {
|
while let Some((module, mod_path)) = worklist.pop() {
|
||||||
let ext_def_map;
|
let ext_def_map;
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl LangItems {
|
||||||
|
|
||||||
let crate_def_map = db.crate_def_map(krate);
|
let crate_def_map = db.crate_def_map(krate);
|
||||||
|
|
||||||
for (_, module_data) in crate_def_map.modules.iter() {
|
for (_, module_data) in crate_def_map.modules() {
|
||||||
for impl_def in module_data.scope.impls() {
|
for impl_def in module_data.scope.impls() {
|
||||||
lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
|
lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,8 +75,8 @@ use crate::{
|
||||||
/// Contains all top-level defs from a macro-expanded crate
|
/// Contains all top-level defs from a macro-expanded crate
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct DefMap {
|
pub struct DefMap {
|
||||||
pub root: LocalModuleId,
|
root: LocalModuleId,
|
||||||
pub modules: Arena<ModuleData>,
|
modules: Arena<ModuleData>,
|
||||||
pub(crate) krate: CrateId,
|
pub(crate) krate: CrateId,
|
||||||
/// The prelude module for this crate. This either comes from an import
|
/// The prelude module for this crate. This either comes from an import
|
||||||
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
||||||
|
@ -208,6 +208,14 @@ impl DefMap {
|
||||||
.map(|(id, _data)| id)
|
.map(|(id, _data)| id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn modules(&self) -> impl Iterator<Item = (LocalModuleId, &ModuleData)> + '_ {
|
||||||
|
self.modules.iter()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn root(&self) -> LocalModuleId {
|
||||||
|
self.root
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_path(
|
pub(crate) fn resolve_path(
|
||||||
&self,
|
&self,
|
||||||
db: &dyn DefDatabase,
|
db: &dyn DefDatabase,
|
||||||
|
|
|
@ -75,7 +75,7 @@ impl TestDB {
|
||||||
pub(crate) fn module_for_file(&self, file_id: FileId) -> crate::ModuleId {
|
pub(crate) fn module_for_file(&self, file_id: FileId) -> crate::ModuleId {
|
||||||
for &krate in self.relevant_crates(file_id).iter() {
|
for &krate in self.relevant_crates(file_id).iter() {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
for (local_id, data) in crate_def_map.modules.iter() {
|
for (local_id, data) in crate_def_map.modules() {
|
||||||
if data.origin.file_id() == Some(file_id) {
|
if data.origin.file_id() == Some(file_id) {
|
||||||
return crate::ModuleId { krate, local_id };
|
return crate::ModuleId { krate, local_id };
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ impl TestDB {
|
||||||
let crate_graph = self.crate_graph();
|
let crate_graph = self.crate_graph();
|
||||||
for krate in crate_graph.iter() {
|
for krate in crate_graph.iter() {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
for (module_id, _) in crate_def_map.modules.iter() {
|
for (module_id, _) in crate_def_map.modules() {
|
||||||
let file_id = crate_def_map[module_id].origin.file_id();
|
let file_id = crate_def_map[module_id].origin.file_id();
|
||||||
files.extend(file_id)
|
files.extend(file_id)
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ impl TestDB {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
|
|
||||||
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
|
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
|
||||||
for (module_id, module) in crate_def_map.modules.iter() {
|
for (module_id, module) in crate_def_map.modules() {
|
||||||
crate_def_map.add_diagnostics(self, module_id, &mut sink);
|
crate_def_map.add_diagnostics(self, module_id, &mut sink);
|
||||||
|
|
||||||
for decl in module.scope.declarations() {
|
for decl in module.scope.declarations() {
|
||||||
|
|
|
@ -409,7 +409,7 @@ mod tests {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
|
|
||||||
let mut fns = Vec::new();
|
let mut fns = Vec::new();
|
||||||
for (module_id, _) in crate_def_map.modules.iter() {
|
for (module_id, _) in crate_def_map.modules() {
|
||||||
for decl in crate_def_map[module_id].scope.declarations() {
|
for decl in crate_def_map[module_id].scope.declarations() {
|
||||||
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
|
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
|
||||||
validate_module_item(self, krate, decl, &mut sink);
|
validate_module_item(self, krate, decl, &mut sink);
|
||||||
|
|
|
@ -112,7 +112,7 @@ impl TraitImpls {
|
||||||
let mut impls = Self { map: FxHashMap::default() };
|
let mut impls = Self { map: FxHashMap::default() };
|
||||||
|
|
||||||
let crate_def_map = db.crate_def_map(krate);
|
let crate_def_map = db.crate_def_map(krate);
|
||||||
for (_module_id, module_data) in crate_def_map.modules.iter() {
|
for (_module_id, module_data) in crate_def_map.modules() {
|
||||||
for impl_id in module_data.scope.impls() {
|
for impl_id in module_data.scope.impls() {
|
||||||
let target_trait = match db.impl_trait(impl_id) {
|
let target_trait = match db.impl_trait(impl_id) {
|
||||||
Some(tr) => tr.value.trait_,
|
Some(tr) => tr.value.trait_,
|
||||||
|
@ -198,7 +198,7 @@ impl InherentImpls {
|
||||||
let mut map: FxHashMap<_, Vec<_>> = FxHashMap::default();
|
let mut map: FxHashMap<_, Vec<_>> = FxHashMap::default();
|
||||||
|
|
||||||
let crate_def_map = db.crate_def_map(krate);
|
let crate_def_map = db.crate_def_map(krate);
|
||||||
for (_module_id, module_data) in crate_def_map.modules.iter() {
|
for (_module_id, module_data) in crate_def_map.modules() {
|
||||||
for impl_id in module_data.scope.impls() {
|
for impl_id in module_data.scope.impls() {
|
||||||
let data = db.impl_data(impl_id);
|
let data = db.impl_data(impl_id);
|
||||||
if data.target_trait.is_some() {
|
if data.target_trait.is_some() {
|
||||||
|
|
|
@ -81,7 +81,7 @@ impl TestDB {
|
||||||
pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
|
pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
|
||||||
for &krate in self.relevant_crates(file_id).iter() {
|
for &krate in self.relevant_crates(file_id).iter() {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
for (local_id, data) in crate_def_map.modules.iter() {
|
for (local_id, data) in crate_def_map.modules() {
|
||||||
if data.origin.file_id() == Some(file_id) {
|
if data.origin.file_id() == Some(file_id) {
|
||||||
return ModuleId { krate, local_id };
|
return ModuleId { krate, local_id };
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ impl TestDB {
|
||||||
let crate_graph = self.crate_graph();
|
let crate_graph = self.crate_graph();
|
||||||
for krate in crate_graph.iter() {
|
for krate in crate_graph.iter() {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
for (module_id, _) in crate_def_map.modules.iter() {
|
for (module_id, _) in crate_def_map.modules() {
|
||||||
let file_id = crate_def_map[module_id].origin.file_id();
|
let file_id = crate_def_map[module_id].origin.file_id();
|
||||||
files.extend(file_id)
|
files.extend(file_id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,7 +191,7 @@ pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<Fil
|
||||||
|
|
||||||
let def_map = db.crate_def_map(krate);
|
let def_map = db.crate_def_map(krate);
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
let mut modules = vec![def_map.root];
|
let mut modules = vec![def_map.root()];
|
||||||
while let Some(module) = modules.pop() {
|
while let Some(module) = modules.pop() {
|
||||||
let data = &def_map[module];
|
let data = &def_map[module];
|
||||||
files.extend(data.origin.file_id());
|
files.extend(data.origin.file_id());
|
||||||
|
|
Loading…
Reference in a new issue