Don't track imports
This commit is contained in:
parent
67ba9072fa
commit
ab7a70fb14
2 changed files with 26 additions and 48 deletions
|
@ -5,7 +5,7 @@ use hir_expand::name::Name;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId};
|
use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Eq)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub struct ItemScope {
|
pub struct ItemScope {
|
||||||
|
@ -30,7 +30,7 @@ static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
|
||||||
BuiltinType::ALL
|
BuiltinType::ALL
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, ty)| {
|
.map(|(name, ty)| {
|
||||||
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
|
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), declaration: false })
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
});
|
});
|
||||||
|
@ -53,11 +53,9 @@ impl ItemScope {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
|
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
|
||||||
self.entries()
|
self.entries().filter(|(_name, res)| res.declaration).flat_map(|(_name, res)| {
|
||||||
.filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
|
res.def.take_types().into_iter().chain(res.def.take_values().into_iter())
|
||||||
.flat_map(|per_ns| {
|
})
|
||||||
per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
|
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
|
||||||
|
@ -112,38 +110,26 @@ impl ItemScope {
|
||||||
self.legacy_macros.insert(name, mac);
|
self.legacy_macros.insert(name, mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn push_res(
|
pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, declaration: bool) -> bool {
|
||||||
&mut self,
|
|
||||||
name: Name,
|
|
||||||
res: &Resolution,
|
|
||||||
import: Option<LocalImportId>,
|
|
||||||
) -> bool {
|
|
||||||
let mut changed = false;
|
let mut changed = false;
|
||||||
let existing = self.items.entry(name.clone()).or_default();
|
let existing = self.items.entry(name.clone()).or_default();
|
||||||
|
|
||||||
if existing.def.types.is_none() && res.def.types.is_some() {
|
if existing.def.types.is_none() && res.def.types.is_some() {
|
||||||
existing.def.types = res.def.types;
|
existing.def.types = res.def.types;
|
||||||
existing.import = import.or(res.import);
|
existing.declaration |= declaration;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
if existing.def.values.is_none() && res.def.values.is_some() {
|
if existing.def.values.is_none() && res.def.values.is_some() {
|
||||||
existing.def.values = res.def.values;
|
existing.def.values = res.def.values;
|
||||||
existing.import = import.or(res.import);
|
existing.declaration |= declaration;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
if existing.def.macros.is_none() && res.def.macros.is_some() {
|
if existing.def.macros.is_none() && res.def.macros.is_some() {
|
||||||
existing.def.macros = res.def.macros;
|
existing.def.macros = res.def.macros;
|
||||||
existing.import = import.or(res.import);
|
existing.declaration |= declaration;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if existing.def.is_none()
|
|
||||||
&& res.def.is_none()
|
|
||||||
&& existing.import.is_none()
|
|
||||||
&& res.import.is_some()
|
|
||||||
{
|
|
||||||
existing.import = res.import;
|
|
||||||
}
|
|
||||||
changed
|
changed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,6 +146,5 @@ impl ItemScope {
|
||||||
pub struct Resolution {
|
pub struct Resolution {
|
||||||
/// None for unresolved
|
/// None for unresolved
|
||||||
pub def: PerNs,
|
pub def: PerNs,
|
||||||
/// ident by which this is imported into local scope.
|
pub declaration: bool,
|
||||||
pub import: Option<LocalImportId>,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -218,8 +218,7 @@ where
|
||||||
if export {
|
if export {
|
||||||
self.update(
|
self.update(
|
||||||
self.def_map.root,
|
self.def_map.root,
|
||||||
None,
|
&[(name, Resolution { def: PerNs::macros(macro_), declaration: false })],
|
||||||
&[(name, Resolution { def: PerNs::macros(macro_), import: None })],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -374,7 +373,7 @@ where
|
||||||
// Module scoped macros is included
|
// Module scoped macros is included
|
||||||
let items = scope.collect_resolutions();
|
let items = scope.collect_resolutions();
|
||||||
|
|
||||||
self.update(module_id, Some(import_id), &items);
|
self.update(module_id, &items);
|
||||||
} else {
|
} else {
|
||||||
// glob import from same crate => we do an initial
|
// glob import from same crate => we do an initial
|
||||||
// import, and then need to propagate any further
|
// import, and then need to propagate any further
|
||||||
|
@ -384,7 +383,7 @@ where
|
||||||
// Module scoped macros is included
|
// Module scoped macros is included
|
||||||
let items = scope.collect_resolutions();
|
let items = scope.collect_resolutions();
|
||||||
|
|
||||||
self.update(module_id, Some(import_id), &items);
|
self.update(module_id, &items);
|
||||||
// record the glob import in case we add further items
|
// record the glob import in case we add further items
|
||||||
let glob = self.glob_imports.entry(m.local_id).or_default();
|
let glob = self.glob_imports.entry(m.local_id).or_default();
|
||||||
if !glob.iter().any(|it| *it == (module_id, import_id)) {
|
if !glob.iter().any(|it| *it == (module_id, import_id)) {
|
||||||
|
@ -404,12 +403,12 @@ where
|
||||||
let variant = EnumVariantId { parent: e, local_id };
|
let variant = EnumVariantId { parent: e, local_id };
|
||||||
let res = Resolution {
|
let res = Resolution {
|
||||||
def: PerNs::both(variant.into(), variant.into()),
|
def: PerNs::both(variant.into(), variant.into()),
|
||||||
import: Some(import_id),
|
declaration: false,
|
||||||
};
|
};
|
||||||
(name, res)
|
(name, res)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
self.update(module_id, Some(import_id), &resolutions);
|
self.update(module_id, &resolutions);
|
||||||
}
|
}
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
|
log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
|
||||||
|
@ -431,27 +430,21 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let resolution = Resolution { def, import: Some(import_id) };
|
let resolution = Resolution { def, declaration: false };
|
||||||
self.update(module_id, Some(import_id), &[(name, resolution)]);
|
self.update(module_id, &[(name, resolution)]);
|
||||||
}
|
}
|
||||||
None => tested_by!(bogus_paths),
|
None => tested_by!(bogus_paths),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) {
|
||||||
&mut self,
|
self.update_recursive(module_id, resolutions, 0)
|
||||||
module_id: LocalModuleId,
|
|
||||||
import: Option<LocalImportId>,
|
|
||||||
resolutions: &[(Name, Resolution)],
|
|
||||||
) {
|
|
||||||
self.update_recursive(module_id, import, resolutions, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_recursive(
|
fn update_recursive(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_id: LocalModuleId,
|
module_id: LocalModuleId,
|
||||||
import: Option<LocalImportId>,
|
|
||||||
resolutions: &[(Name, Resolution)],
|
resolutions: &[(Name, Resolution)],
|
||||||
depth: usize,
|
depth: usize,
|
||||||
) {
|
) {
|
||||||
|
@ -462,7 +455,7 @@ where
|
||||||
let scope = &mut self.def_map.modules[module_id].scope;
|
let scope = &mut self.def_map.modules[module_id].scope;
|
||||||
let mut changed = false;
|
let mut changed = false;
|
||||||
for (name, res) in resolutions {
|
for (name, res) in resolutions {
|
||||||
changed |= scope.push_res(name.clone(), res, import);
|
changed |= scope.push_res(name.clone(), res, depth == 0 && res.declaration);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !changed {
|
if !changed {
|
||||||
|
@ -475,9 +468,9 @@ where
|
||||||
.flat_map(|v| v.iter())
|
.flat_map(|v| v.iter())
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
for (glob_importing_module, glob_import) in glob_imports {
|
for (glob_importing_module, _glob_import) in glob_imports {
|
||||||
// We pass the glob import so that the tracked import in those modules is that glob import
|
// We pass the glob import so that the tracked import in those modules is that glob import
|
||||||
self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1);
|
self.update_recursive(glob_importing_module, resolutions, depth + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -719,9 +712,9 @@ where
|
||||||
def: PerNs::types(
|
def: PerNs::types(
|
||||||
ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
|
ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
|
||||||
),
|
),
|
||||||
import: None,
|
declaration: true,
|
||||||
};
|
};
|
||||||
self.def_collector.update(self.module_id, None, &[(name, resolution)]);
|
self.def_collector.update(self.module_id, &[(name, resolution)]);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -791,8 +784,8 @@ where
|
||||||
PerNs::types(def.into())
|
PerNs::types(def.into())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let resolution = Resolution { def, import: None };
|
let resolution = Resolution { def, declaration: true };
|
||||||
self.def_collector.update(self.module_id, None, &[(name, resolution)])
|
self.def_collector.update(self.module_id, &[(name, resolution)])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
|
fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue