2019-12-20 08:44:40 -06:00
|
|
|
//! Describes items defined or visible (ie, imported) in a certain scope.
|
|
|
|
//! This is shared between modules and blocks.
|
|
|
|
|
2020-06-30 06:25:15 -05:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
2021-06-06 08:51:05 -05:00
|
|
|
use hir_expand::{name::Name, AstId, MacroCallId, MacroDefKind};
|
2019-12-20 08:43:45 -06:00
|
|
|
use once_cell::sync::Lazy;
|
2020-06-25 20:34:39 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2021-02-03 12:05:11 -06:00
|
|
|
use stdx::format_to;
|
2021-06-06 08:51:05 -05:00
|
|
|
use syntax::ast;
|
2019-12-20 08:38:17 -06:00
|
|
|
|
2019-12-25 08:00:10 -06:00
|
|
|
use crate::{
|
2021-04-06 20:12:40 -05:00
|
|
|
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId,
|
2021-03-01 12:36:34 -06:00
|
|
|
LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId,
|
2019-12-25 08:00:10 -06:00
|
|
|
};
|
2019-12-20 08:43:45 -06:00
|
|
|
|
2020-06-25 11:42:12 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub(crate) enum ImportType {
|
|
|
|
Glob,
|
|
|
|
Named,
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:34:39 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct PerNsGlobImports {
|
|
|
|
types: FxHashSet<(LocalModuleId, Name)>,
|
|
|
|
values: FxHashSet<(LocalModuleId, Name)>,
|
|
|
|
macros: FxHashSet<(LocalModuleId, Name)>,
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:43:45 -06:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
2019-12-20 08:45:12 -06:00
|
|
|
pub struct ItemScope {
|
2020-06-30 06:23:42 -05:00
|
|
|
types: FxHashMap<Name, (ModuleDefId, Visibility)>,
|
|
|
|
values: FxHashMap<Name, (ModuleDefId, Visibility)>,
|
|
|
|
macros: FxHashMap<Name, (MacroDefId, Visibility)>,
|
|
|
|
unresolved: FxHashSet<Name>,
|
|
|
|
|
2019-12-22 08:21:48 -06:00
|
|
|
defs: Vec<ModuleDefId>,
|
2019-12-20 09:55:38 -06:00
|
|
|
impls: Vec<ImplId>,
|
2021-04-06 20:12:40 -05:00
|
|
|
unnamed_consts: Vec<ConstId>,
|
2020-07-21 10:52:43 -05:00
|
|
|
/// Traits imported via `use Trait as _;`.
|
|
|
|
unnamed_trait_imports: FxHashMap<TraitId, Visibility>,
|
2019-12-20 08:44:40 -06:00
|
|
|
/// Macros visible in current module in legacy textual scope
|
2019-12-20 08:43:45 -06:00
|
|
|
///
|
2019-12-20 08:44:40 -06:00
|
|
|
/// For macros invoked by an unqualified identifier like `bar!()`, `legacy_macros` will be searched in first.
|
2019-12-20 08:43:45 -06:00
|
|
|
/// If it yields no result, then it turns to module scoped `macros`.
|
2019-12-20 08:44:40 -06:00
|
|
|
/// It macros with name qualified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
|
2019-12-20 08:43:45 -06:00
|
|
|
/// and only normal scoped `macros` will be searched in.
|
|
|
|
///
|
|
|
|
/// Note that this automatically inherit macros defined textually before the definition of module itself.
|
|
|
|
///
|
|
|
|
/// Module scoped macros will be inserted into `items` instead of here.
|
|
|
|
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
|
|
|
|
// be all resolved to the last one defined if shadowing happens.
|
2019-12-20 10:09:13 -06:00
|
|
|
legacy_macros: FxHashMap<Name, MacroDefId>,
|
2021-06-06 08:51:05 -05:00
|
|
|
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:34:06 -06:00
|
|
|
pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
|
2019-12-20 08:43:45 -06:00
|
|
|
BuiltinType::ALL
|
|
|
|
.iter()
|
2021-06-12 22:57:19 -05:00
|
|
|
.map(|(name, ty)| (name.clone(), PerNs::types((*ty).into(), Visibility::Public)))
|
2019-12-20 08:43:45 -06:00
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Shadow mode for builtin type which can be shadowed by module.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2019-12-20 08:51:43 -06:00
|
|
|
pub(crate) enum BuiltinShadowMode {
|
2020-02-21 08:34:06 -06:00
|
|
|
/// Prefer user-defined modules (or other types) over builtins.
|
2019-12-20 08:43:45 -06:00
|
|
|
Module,
|
2020-02-21 08:34:06 -06:00
|
|
|
/// Prefer builtins over user-defined modules (but not other types).
|
2019-12-20 08:43:45 -06:00
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
|
|
|
|
/// Other methods will only resolve values, types and module scoped macros only.
|
2019-12-20 08:45:12 -06:00
|
|
|
impl ItemScope {
|
2019-12-22 08:37:07 -06:00
|
|
|
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
|
2020-06-30 06:23:42 -05:00
|
|
|
// FIXME: shadowing
|
|
|
|
let keys: FxHashSet<_> = self
|
|
|
|
.types
|
|
|
|
.keys()
|
|
|
|
.chain(self.values.keys())
|
|
|
|
.chain(self.macros.keys())
|
|
|
|
.chain(self.unresolved.iter())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
keys.into_iter().map(move |name| (name, self.get(name)))
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
|
2019-12-22 08:21:48 -06:00
|
|
|
self.defs.iter().copied()
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 08:58:20 -06:00
|
|
|
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
|
|
|
|
self.impls.iter().copied()
|
|
|
|
}
|
|
|
|
|
2020-10-03 23:39:35 -05:00
|
|
|
pub fn values(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (ModuleDefId, Visibility)> + ExactSizeIterator + '_ {
|
|
|
|
self.values.values().copied()
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:11:38 -05:00
|
|
|
pub fn visibility_of(&self, def: ModuleDefId) -> Option<Visibility> {
|
2020-03-24 15:45:42 -05:00
|
|
|
self.name_of(ItemInNs::Types(def))
|
|
|
|
.or_else(|| self.name_of(ItemInNs::Values(def)))
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:33:22 -05:00
|
|
|
pub fn unnamed_consts(&self) -> impl Iterator<Item = ConstId> + '_ {
|
|
|
|
self.unnamed_consts.iter().copied()
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:43:45 -06:00
|
|
|
/// Iterate over all module scoped macros
|
2019-12-20 08:51:43 -06:00
|
|
|
pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
|
2020-06-30 06:23:42 -05:00
|
|
|
self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 08:51:43 -06:00
|
|
|
/// Iterate over all legacy textual scoped macros visible at the end of the module
|
|
|
|
pub(crate) fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
|
2019-12-20 08:43:45 -06:00
|
|
|
self.legacy_macros.iter().map(|(name, def)| (name, *def))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a name from current module scope, legacy macros are not included
|
2020-02-21 08:34:06 -06:00
|
|
|
pub(crate) fn get(&self, name: &Name) -> PerNs {
|
2020-06-30 06:23:42 -05:00
|
|
|
PerNs {
|
|
|
|
types: self.types.get(name).copied(),
|
|
|
|
values: self.values.get(name).copied(),
|
|
|
|
macros: self.macros.get(name).copied(),
|
|
|
|
}
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
|
2020-06-30 06:23:42 -05:00
|
|
|
for (name, per_ns) in self.entries() {
|
|
|
|
if let Some(vis) = item.match_with(per_ns) {
|
2019-12-30 10:31:15 -06:00
|
|
|
return Some((name, vis));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:51:43 -06:00
|
|
|
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
|
2020-07-21 10:52:43 -05:00
|
|
|
self.types
|
|
|
|
.values()
|
|
|
|
.filter_map(|(def, _)| match def {
|
|
|
|
ModuleDefId::TraitId(t) => Some(*t),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.chain(self.unnamed_trait_imports.keys().copied())
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-22 08:21:48 -06:00
|
|
|
pub(crate) fn define_def(&mut self, def: ModuleDefId) {
|
|
|
|
self.defs.push(def)
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:43:45 -06:00
|
|
|
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
|
|
|
|
self.legacy_macros.get(name).copied()
|
|
|
|
}
|
2019-12-20 09:47:22 -06:00
|
|
|
|
2019-12-20 09:55:38 -06:00
|
|
|
pub(crate) fn define_impl(&mut self, imp: ImplId) {
|
|
|
|
self.impls.push(imp)
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:12:40 -05:00
|
|
|
pub(crate) fn define_unnamed_const(&mut self, konst: ConstId) {
|
|
|
|
self.unnamed_consts.push(konst);
|
|
|
|
}
|
|
|
|
|
2019-12-20 10:09:13 -06:00
|
|
|
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
|
|
|
|
self.legacy_macros.insert(name, mac);
|
|
|
|
}
|
|
|
|
|
2021-06-06 08:51:05 -05:00
|
|
|
pub(crate) fn add_attr_macro_invoc(&mut self, item: AstId<ast::Item>, call: MacroCallId) {
|
|
|
|
self.attr_macros.insert(item, call);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn attr_macro_invocs(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
|
|
|
|
self.attr_macros.iter().map(|(k, v)| (*k, *v))
|
|
|
|
}
|
|
|
|
|
2020-07-21 10:52:43 -05:00
|
|
|
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
|
|
|
|
self.unnamed_trait_imports.get(&tr).copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_unnamed_trait(&mut self, tr: TraitId, vis: Visibility) {
|
|
|
|
self.unnamed_trait_imports.insert(tr, vis);
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:34:39 -05:00
|
|
|
pub(crate) fn push_res_with_import(
|
2020-06-25 11:42:12 -05:00
|
|
|
&mut self,
|
2020-06-25 20:34:39 -05:00
|
|
|
glob_imports: &mut PerNsGlobImports,
|
|
|
|
lookup: (LocalModuleId, Name),
|
2020-06-25 11:42:12 -05:00
|
|
|
def: PerNs,
|
|
|
|
def_import_type: ImportType,
|
|
|
|
) -> bool {
|
2019-12-20 09:47:22 -06:00
|
|
|
let mut changed = false;
|
|
|
|
|
2020-06-24 07:20:41 -05:00
|
|
|
macro_rules! check_changed {
|
2020-06-25 20:34:39 -05:00
|
|
|
(
|
|
|
|
$changed:ident,
|
2020-06-30 06:23:42 -05:00
|
|
|
( $this:ident / $def:ident ) . $field:ident,
|
2020-06-25 20:34:39 -05:00
|
|
|
$glob_imports:ident [ $lookup:ident ],
|
|
|
|
$def_import_type:ident
|
2020-06-30 06:23:42 -05:00
|
|
|
) => {{
|
|
|
|
let existing = $this.$field.entry($lookup.1.clone());
|
|
|
|
match (existing, $def.$field) {
|
|
|
|
(Entry::Vacant(entry), Some(_)) => {
|
2020-06-25 20:34:39 -05:00
|
|
|
match $def_import_type {
|
|
|
|
ImportType::Glob => {
|
|
|
|
$glob_imports.$field.insert($lookup.clone());
|
|
|
|
}
|
|
|
|
ImportType::Named => {
|
|
|
|
$glob_imports.$field.remove(&$lookup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:23:42 -05:00
|
|
|
if let Some(fld) = $def.$field {
|
|
|
|
entry.insert(fld);
|
|
|
|
}
|
2020-06-24 07:20:41 -05:00
|
|
|
$changed = true;
|
|
|
|
}
|
2020-06-30 06:23:42 -05:00
|
|
|
(Entry::Occupied(mut entry), Some(_))
|
2020-06-25 20:34:39 -05:00
|
|
|
if $glob_imports.$field.contains(&$lookup)
|
2020-06-26 21:51:13 -05:00
|
|
|
&& matches!($def_import_type, ImportType::Named) =>
|
2020-06-25 11:42:12 -05:00
|
|
|
{
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(import_shadowed);
|
2020-06-25 20:34:39 -05:00
|
|
|
$glob_imports.$field.remove(&$lookup);
|
2020-06-30 06:23:42 -05:00
|
|
|
if let Some(fld) = $def.$field {
|
|
|
|
entry.insert(fld);
|
|
|
|
}
|
2020-06-24 07:20:41 -05:00
|
|
|
$changed = true;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-06-30 06:23:42 -05:00
|
|
|
}};
|
2019-12-20 09:47:22 -06:00
|
|
|
}
|
2020-06-23 21:10:01 -05:00
|
|
|
|
2020-06-30 06:23:42 -05:00
|
|
|
check_changed!(changed, (self / def).types, glob_imports[lookup], def_import_type);
|
|
|
|
check_changed!(changed, (self / def).values, glob_imports[lookup], def_import_type);
|
|
|
|
check_changed!(changed, (self / def).macros, glob_imports[lookup], def_import_type);
|
|
|
|
|
2021-06-07 06:59:01 -05:00
|
|
|
if def.is_none() && self.unresolved.insert(lookup.1) {
|
|
|
|
changed = true;
|
2020-06-30 06:23:42 -05:00
|
|
|
}
|
2019-12-20 09:47:22 -06:00
|
|
|
|
|
|
|
changed
|
|
|
|
}
|
|
|
|
|
2020-07-21 10:52:43 -05:00
|
|
|
pub(crate) fn resolutions<'a>(&'a self) -> impl Iterator<Item = (Option<Name>, PerNs)> + 'a {
|
|
|
|
self.entries().map(|(name, res)| (Some(name.clone()), res)).chain(
|
|
|
|
self.unnamed_trait_imports
|
|
|
|
.iter()
|
|
|
|
.map(|(tr, vis)| (None, PerNs::types(ModuleDefId::TraitId(*tr), *vis))),
|
|
|
|
)
|
2019-12-25 08:00:10 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 10:09:13 -06:00
|
|
|
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
|
|
|
|
self.legacy_macros.clone()
|
|
|
|
}
|
2020-09-18 10:50:04 -05:00
|
|
|
|
|
|
|
/// Marks everything that is not a procedural macro as private to `this_module`.
|
|
|
|
pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) {
|
2020-09-28 05:51:40 -05:00
|
|
|
self.types
|
2020-09-18 10:50:04 -05:00
|
|
|
.values_mut()
|
|
|
|
.chain(self.values.values_mut())
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.chain(self.unnamed_trait_imports.values_mut())
|
2020-09-28 05:51:40 -05:00
|
|
|
.for_each(|vis| *vis = Visibility::Module(this_module));
|
2020-09-18 10:50:04 -05:00
|
|
|
|
|
|
|
for (mac, vis) in self.macros.values_mut() {
|
2021-03-18 10:11:18 -05:00
|
|
|
if let MacroDefKind::ProcMacro(..) = mac.kind {
|
2020-09-18 10:50:04 -05:00
|
|
|
// FIXME: Technically this is insufficient since reexports of proc macros are also
|
|
|
|
// forbidden. Practically nobody does that.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vis = Visibility::Module(this_module);
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 12:05:11 -06:00
|
|
|
|
|
|
|
pub(crate) fn dump(&self, buf: &mut String) {
|
|
|
|
let mut entries: Vec<_> = self.resolutions().collect();
|
|
|
|
entries.sort_by_key(|(name, _)| name.clone());
|
|
|
|
|
|
|
|
for (name, def) in entries {
|
|
|
|
format_to!(buf, "{}:", name.map_or("_".to_string(), |name| name.to_string()));
|
|
|
|
|
|
|
|
if def.types.is_some() {
|
|
|
|
buf.push_str(" t");
|
|
|
|
}
|
|
|
|
if def.values.is_some() {
|
|
|
|
buf.push_str(" v");
|
|
|
|
}
|
|
|
|
if def.macros.is_some() {
|
|
|
|
buf.push_str(" m");
|
|
|
|
}
|
|
|
|
if def.is_none() {
|
|
|
|
buf.push_str(" _");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 16:45:27 -05:00
|
|
|
|
|
|
|
pub(crate) fn shrink_to_fit(&mut self) {
|
2021-04-03 19:56:11 -05:00
|
|
|
// Exhaustive match to require handling new fields.
|
|
|
|
let Self {
|
|
|
|
types,
|
|
|
|
values,
|
|
|
|
macros,
|
|
|
|
unresolved,
|
|
|
|
defs,
|
|
|
|
impls,
|
2021-04-06 20:12:40 -05:00
|
|
|
unnamed_consts,
|
2021-04-03 19:56:11 -05:00
|
|
|
unnamed_trait_imports,
|
|
|
|
legacy_macros,
|
2021-06-06 08:51:05 -05:00
|
|
|
attr_macros,
|
2021-04-03 19:56:11 -05:00
|
|
|
} = self;
|
|
|
|
types.shrink_to_fit();
|
|
|
|
values.shrink_to_fit();
|
|
|
|
macros.shrink_to_fit();
|
|
|
|
unresolved.shrink_to_fit();
|
|
|
|
defs.shrink_to_fit();
|
|
|
|
impls.shrink_to_fit();
|
2021-04-06 20:12:40 -05:00
|
|
|
unnamed_consts.shrink_to_fit();
|
2021-04-03 19:56:11 -05:00
|
|
|
unnamed_trait_imports.shrink_to_fit();
|
|
|
|
legacy_macros.shrink_to_fit();
|
2021-06-06 08:51:05 -05:00
|
|
|
attr_macros.shrink_to_fit();
|
2021-04-03 16:45:27 -05:00
|
|
|
}
|
2019-12-20 08:43:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-25 08:00:10 -06:00
|
|
|
impl PerNs {
|
2020-05-04 12:15:27 -05:00
|
|
|
pub(crate) fn from_def(def: ModuleDefId, v: Visibility, has_constructor: bool) -> PerNs {
|
2019-12-22 08:08:57 -06:00
|
|
|
match def {
|
2019-12-25 08:00:10 -06:00
|
|
|
ModuleDefId::ModuleId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::FunctionId(_) => PerNs::values(def, v),
|
2019-12-22 08:08:57 -06:00
|
|
|
ModuleDefId::AdtId(adt) => match adt {
|
2020-05-04 12:19:54 -05:00
|
|
|
AdtId::UnionId(_) => PerNs::types(def, v),
|
2019-12-25 08:00:10 -06:00
|
|
|
AdtId::EnumId(_) => PerNs::types(def, v),
|
2020-05-04 11:17:22 -05:00
|
|
|
AdtId::StructId(_) => {
|
2020-05-05 10:01:07 -05:00
|
|
|
if has_constructor {
|
2020-05-04 11:17:22 -05:00
|
|
|
PerNs::both(def, def, v)
|
2020-05-05 10:01:07 -05:00
|
|
|
} else {
|
|
|
|
PerNs::types(def, v)
|
2020-05-04 11:17:22 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 08:08:57 -06:00
|
|
|
},
|
2019-12-25 08:00:10 -06:00
|
|
|
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v),
|
|
|
|
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v),
|
|
|
|
ModuleDefId::TraitId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::TypeAliasId(_) => PerNs::types(def, v),
|
|
|
|
ModuleDefId::BuiltinType(_) => PerNs::types(def, v),
|
2019-12-22 08:08:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
|
2019-12-30 15:51:37 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
2019-12-30 10:31:15 -06:00
|
|
|
pub enum ItemInNs {
|
|
|
|
Types(ModuleDefId),
|
|
|
|
Values(ModuleDefId),
|
|
|
|
Macros(MacroDefId),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemInNs {
|
|
|
|
fn match_with(self, per_ns: PerNs) -> Option<Visibility> {
|
|
|
|
match self {
|
|
|
|
ItemInNs::Types(def) => {
|
|
|
|
per_ns.types.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
ItemInNs::Values(def) => {
|
|
|
|
per_ns.values.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
ItemInNs::Macros(def) => {
|
|
|
|
per_ns.macros.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 14:33:25 -06:00
|
|
|
|
|
|
|
pub fn as_module_def_id(self) -> Option<ModuleDefId> {
|
|
|
|
match self {
|
2020-01-10 11:40:45 -06:00
|
|
|
ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
|
2019-12-30 14:33:25 -06:00
|
|
|
ItemInNs::Macros(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 12:30:29 -05:00
|
|
|
|
2020-06-05 06:05:19 -05:00
|
|
|
/// Returns the crate defining this item (or `None` if `self` is built-in).
|
|
|
|
pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
|
2021-03-01 12:36:34 -06:00
|
|
|
match self {
|
|
|
|
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate),
|
|
|
|
ItemInNs::Macros(id) => Some(id.krate),
|
|
|
|
}
|
2020-06-04 12:30:29 -05:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|