Move impls to ItemScope

This commit is contained in:
Aleksey Kladov 2019-12-20 15:58:20 +01:00
parent 030e540ad1
commit 1b8ce5b37b
10 changed files with 16 additions and 13 deletions

View File

@ -221,7 +221,7 @@ impl Module {
pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> {
let def_map = db.crate_def_map(self.id.krate);
def_map[self.id.local_id].impls.iter().copied().map(ImplBlock::from).collect()
def_map[self.id.local_id].scope.impls().map(ImplBlock::from).collect()
}
pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module {

View File

@ -80,7 +80,7 @@ impl ChildBySource for ModuleId {
module_data.scope.declarations().for_each(|item| add_module_def(db, &mut res, item));
for &impl_ in module_data.impls.iter() {
for &impl_ in module_data.scope.impls.iter() {
let src = impl_.lookup(db).source(db);
res[keys::IMPL].insert(src, impl_)
}

View File

@ -5,11 +5,12 @@ use hir_expand::name::Name;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use crate::{per_ns::PerNs, BuiltinType, LocalImportId, MacroDefId, ModuleDefId, TraitId};
use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId};
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ItemScope {
pub(crate) items: FxHashMap<Name, Resolution>,
pub(crate) impls: Vec<ImplId>,
/// Macros visible in current module in legacy textual scope
///
/// For macros invoked by an unqualified identifier like `bar!()`, `legacy_macros` will be searched in first.
@ -59,6 +60,10 @@ impl ItemScope {
})
}
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
self.impls.iter().copied()
}
/// Iterate over all module scoped macros
pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
self.items

View File

@ -81,7 +81,7 @@ impl LangItems {
// Look for impl targets
let def_map = db.crate_def_map(module.krate);
let module_data = &def_map[module.local_id];
for &impl_block in module_data.impls.iter() {
for &impl_block in module_data.scope.impls.iter() {
self.collect_lang_item(db, impl_block, LangItemTarget::ImplBlockId)
}

View File

@ -73,7 +73,7 @@ use crate::{
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
path::ModPath,
per_ns::PerNs,
AstId, ImplId, LocalModuleId, ModuleDefId, ModuleId,
AstId, LocalModuleId, ModuleDefId, ModuleId,
};
/// Contains all top-level defs from a macro-expanded crate
@ -169,8 +169,6 @@ pub struct ModuleData {
/// Where does this module come from?
pub origin: ModuleOrigin,
pub impls: Vec<ImplId>,
}
impl CrateDefMap {

View File

@ -667,7 +667,7 @@ where
let impl_id =
ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
.intern(self.def_collector.db);
self.def_collector.def_map.modules[self.module_id].impls.push(impl_id)
self.def_collector.def_map.modules[self.module_id].scope.impls.push(impl_id)
}
}
}

View File

@ -610,7 +610,7 @@ fn expand_derive() {
struct Foo;
",
);
assert_eq!(map.modules[map.root].impls.len(), 1);
assert_eq!(map.modules[map.root].scope.impls().len(), 1);
}
#[test]
@ -622,5 +622,5 @@ fn expand_multiple_derive() {
struct Foo;
",
);
assert_eq!(map.modules[map.root].impls.len(), 2);
assert_eq!(map.modules[map.root].scope.impls().len(), 2);
}

View File

@ -58,7 +58,7 @@ impl CrateImplBlocks {
let crate_def_map = db.crate_def_map(krate);
for (_module_id, module_data) in crate_def_map.modules.iter() {
for &impl_id in module_data.impls.iter() {
for impl_id in module_data.scope.impls() {
match db.impl_trait(impl_id) {
Some(tr) => {
res.impls_by_trait.entry(tr.trait_).or_default().push(impl_id);

View File

@ -98,7 +98,7 @@ impl TestDB {
}
}
for &impl_id in crate_def_map[module_id].impls.iter() {
for impl_id in crate_def_map[module_id].scope.impls() {
let impl_data = self.impl_data(impl_id);
for item in impl_data.items.iter() {
if let AssocItemId::FunctionId(f) = item {

View File

@ -182,7 +182,7 @@ fn visit_module(
_ => (),
}
}
for &impl_id in crate_def_map[module_id].impls.iter() {
for impl_id in crate_def_map[module_id].scope.impls() {
let impl_data = db.impl_data(impl_id);
for &item in impl_data.items.iter() {
match item {