2019-11-21 06:39:09 -06:00
|
|
|
//! Name resolution façade.
|
2019-01-19 14:23:26 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
2019-11-21 06:39:09 -06:00
|
|
|
use hir_expand::{
|
2019-12-13 15:01:06 -06:00
|
|
|
name::{name, Name},
|
2019-11-21 06:39:09 -06:00
|
|
|
MacroDefId,
|
|
|
|
};
|
2021-08-03 09:36:06 -05:00
|
|
|
use indexmap::IndexMap;
|
2019-11-21 06:39:09 -06:00
|
|
|
use rustc_hash::FxHashSet;
|
2021-12-09 11:04:32 -06:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2019-11-21 06:39:09 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-21 06:24:51 -06:00
|
|
|
body::scope::{ExprScopes, ScopeId},
|
2019-10-31 02:51:54 -05:00
|
|
|
builtin_type::BuiltinType,
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2021-03-20 18:59:45 -05:00
|
|
|
expr::{ExprId, LabelId, PatId},
|
2019-11-21 03:21:46 -06:00
|
|
|
generics::GenericParams,
|
2021-04-04 20:50:10 -05:00
|
|
|
intern::Interned,
|
2020-02-21 08:34:06 -06:00
|
|
|
item_scope::{BuiltinShadowMode, BUILTIN_SCOPE},
|
2021-01-18 13:18:05 -06:00
|
|
|
nameres::DefMap,
|
2019-12-13 05:12:36 -06:00
|
|
|
path::{ModPath, PathKind},
|
2019-11-23 07:53:16 -06:00
|
|
|
per_ns::PerNs,
|
2019-12-26 09:00:10 -06:00
|
|
|
visibility::{RawVisibility, Visibility},
|
2021-12-07 10:31:26 -06:00
|
|
|
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId,
|
|
|
|
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
|
2021-07-19 13:36:44 -05:00
|
|
|
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
|
|
|
|
TypeParamId, VariantId,
|
2019-10-30 09:19:30 -05:00
|
|
|
};
|
2019-01-19 14:23:26 -06:00
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2019-11-21 06:39:09 -06:00
|
|
|
pub struct Resolver {
|
2021-08-02 07:27:47 -05:00
|
|
|
/// The stack of scopes, where the inner-most scope is the last item.
|
|
|
|
///
|
|
|
|
/// When using, you generally want to process the scopes in reverse order,
|
|
|
|
/// there's `scopes` *method* for that.
|
2019-01-26 15:52:04 -06:00
|
|
|
scopes: Vec<Scope>,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 02:53:48 -05:00
|
|
|
// FIXME how to store these best
|
2019-01-19 14:23:26 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 12:00:50 -06:00
|
|
|
struct ModuleItemMap {
|
2021-03-06 17:20:13 -06:00
|
|
|
def_map: Arc<DefMap>,
|
2019-11-23 07:49:53 -06:00
|
|
|
module_id: LocalModuleId,
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 12:00:50 -06:00
|
|
|
struct ExprScope {
|
2019-11-21 03:21:46 -06:00
|
|
|
owner: DefWithBodyId,
|
2019-01-19 14:23:26 -06:00
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 12:00:50 -06:00
|
|
|
enum Scope {
|
2019-01-19 14:23:26 -06:00
|
|
|
/// All the items and imported names of a module
|
|
|
|
ModuleScope(ModuleItemMap),
|
|
|
|
/// Brings the generic parameters of an item into scope
|
2021-04-04 20:50:10 -05:00
|
|
|
GenericParams { def: GenericDefId, params: Interned<GenericParams> },
|
2019-09-26 23:19:52 -05:00
|
|
|
/// Brings `Self` in `impl` block into scope
|
2020-02-29 14:24:40 -06:00
|
|
|
ImplDefScope(ImplId),
|
2019-10-08 06:25:37 -05:00
|
|
|
/// Brings `Self` in enum, struct and union definitions into scope
|
2019-11-21 03:21:46 -06:00
|
|
|
AdtScope(AdtId),
|
2019-01-19 14:23:26 -06:00
|
|
|
/// Local bindings
|
|
|
|
ExprScope(ExprScope),
|
|
|
|
}
|
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 06:39:09 -06:00
|
|
|
pub enum TypeNs {
|
2019-11-21 03:21:46 -06:00
|
|
|
SelfType(ImplId),
|
2019-12-07 13:09:53 -06:00
|
|
|
GenericParam(TypeParamId),
|
2019-11-21 03:21:46 -06:00
|
|
|
AdtId(AdtId),
|
|
|
|
AdtSelfType(AdtId),
|
2019-11-26 10:30:57 -06:00
|
|
|
// Yup, enum variants are added to the types ns, but any usage of variant as
|
|
|
|
// type is an error.
|
2019-11-21 03:21:46 -06:00
|
|
|
EnumVariantId(EnumVariantId),
|
|
|
|
TypeAliasId(TypeAliasId),
|
2019-09-12 15:35:53 -05:00
|
|
|
BuiltinType(BuiltinType),
|
2019-11-21 03:21:46 -06:00
|
|
|
TraitId(TraitId),
|
2019-09-14 09:26:03 -05:00
|
|
|
// Module belong to type ns, but the resolver is used when all module paths
|
2019-09-12 15:35:53 -05:00
|
|
|
// are fully resolved.
|
2019-11-21 03:21:46 -06:00
|
|
|
// ModuleId(ModuleId)
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 06:39:09 -06:00
|
|
|
pub enum ResolveValueResult {
|
2019-09-12 15:35:53 -05:00
|
|
|
ValueNs(ValueNs),
|
|
|
|
Partial(TypeNs, usize),
|
|
|
|
}
|
2019-09-12 12:39:10 -05:00
|
|
|
|
2019-09-14 09:26:03 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 06:39:09 -06:00
|
|
|
pub enum ValueNs {
|
2020-05-15 10:15:40 -05:00
|
|
|
ImplSelf(ImplId),
|
2019-01-30 15:41:44 -06:00
|
|
|
LocalBinding(PatId),
|
2019-11-21 04:32:03 -06:00
|
|
|
FunctionId(FunctionId),
|
|
|
|
ConstId(ConstId),
|
|
|
|
StaticId(StaticId),
|
|
|
|
StructId(StructId),
|
|
|
|
EnumVariantId(EnumVariantId),
|
2021-01-01 03:06:42 -06:00
|
|
|
GenericParam(ConstParamId),
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
2019-09-12 12:39:10 -05:00
|
|
|
/// Resolve known trait from std, like `std::futures::Future`
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn resolve_known_trait(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<TraitId> {
|
2019-11-30 09:29:21 -06:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-11-21 03:21:46 -06:00
|
|
|
ModuleDefId::TraitId(it) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known struct from std, like `std::boxed::Box`
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn resolve_known_struct(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<StructId> {
|
2019-11-30 09:29:21 -06:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-11-21 03:21:46 -06:00
|
|
|
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known enum from std, like `std::result::Result`
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn resolve_known_enum(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<EnumId> {
|
2019-11-30 09:29:21 -06:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 12:39:10 -05:00
|
|
|
match res {
|
2019-11-21 03:21:46 -06:00
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
|
2019-09-12 12:39:10 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:27:47 -05:00
|
|
|
fn scopes(&self) -> impl Iterator<Item = &Scope> {
|
|
|
|
self.scopes.iter().rev()
|
|
|
|
}
|
|
|
|
|
2019-11-30 09:29:21 -06:00
|
|
|
fn resolve_module_path(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-11-30 09:29:21 -06:00
|
|
|
shadow: BuiltinShadowMode,
|
|
|
|
) -> PerNs {
|
2020-01-10 11:40:45 -06:00
|
|
|
let (item_map, module) = match self.module_scope() {
|
2019-09-12 15:35:53 -05:00
|
|
|
Some(it) => it,
|
|
|
|
None => return PerNs::none(),
|
|
|
|
};
|
2021-06-12 22:54:16 -05:00
|
|
|
let (module_res, segment_index) = item_map.resolve_path(db, module, path, shadow);
|
2019-09-12 15:35:53 -05:00
|
|
|
if segment_index.is_some() {
|
|
|
|
return PerNs::none();
|
|
|
|
}
|
|
|
|
module_res
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn resolve_module_path_in_items(&self, db: &dyn DefDatabase, path: &ModPath) -> PerNs {
|
2019-11-30 09:29:21 -06:00
|
|
|
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
|
|
|
|
}
|
|
|
|
|
2021-07-19 13:36:44 -05:00
|
|
|
pub fn resolve_module_path_in_trait_assoc_items(
|
2021-01-19 09:43:06 -06:00
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
path: &ModPath,
|
2021-07-19 13:36:44 -05:00
|
|
|
) -> Option<PerNs> {
|
2021-01-19 09:43:06 -06:00
|
|
|
let (item_map, module) = self.module_scope()?;
|
2021-07-19 13:36:44 -05:00
|
|
|
let (module_res, idx) = item_map.resolve_path(db, module, path, BuiltinShadowMode::Module);
|
2021-01-19 09:43:06 -06:00
|
|
|
match module_res.take_types()? {
|
2021-07-19 13:36:44 -05:00
|
|
|
ModuleDefId::TraitId(it) => {
|
|
|
|
let idx = idx?;
|
|
|
|
let unresolved = &path.segments()[idx..];
|
|
|
|
let assoc = match unresolved {
|
|
|
|
[it] => it,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let &(_, assoc) = db.trait_data(it).items.iter().find(|(n, _)| n == assoc)?;
|
|
|
|
Some(match assoc {
|
|
|
|
AssocItemId::FunctionId(it) => PerNs::values(it.into(), Visibility::Public),
|
|
|
|
AssocItemId::ConstId(it) => PerNs::values(it.into(), Visibility::Public),
|
|
|
|
AssocItemId::TypeAliasId(it) => PerNs::types(it.into(), Visibility::Public),
|
|
|
|
})
|
|
|
|
}
|
2021-01-19 09:43:06 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn resolve_path_in_type_ns(
|
2019-09-12 15:35:53 -05:00
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-09-12 15:35:53 -05:00
|
|
|
) -> Option<(TypeNs, Option<usize>)> {
|
2021-02-04 13:49:24 -06:00
|
|
|
let first_name = path.segments().first()?;
|
2019-09-12 15:35:53 -05:00
|
|
|
let skip_to_mod = path.kind != PathKind::Plain;
|
2021-08-02 07:27:47 -05:00
|
|
|
for scope in self.scopes() {
|
2019-09-12 15:35:53 -05:00
|
|
|
match scope {
|
|
|
|
Scope::ExprScope(_) => continue,
|
2021-02-09 10:27:04 -06:00
|
|
|
Scope::GenericParams { .. } | Scope::ImplDefScope(_) if skip_to_mod => continue,
|
2019-09-12 15:35:53 -05:00
|
|
|
|
2019-12-07 04:50:36 -06:00
|
|
|
Scope::GenericParams { params, def } => {
|
2021-01-01 03:06:42 -06:00
|
|
|
if let Some(local_id) = params.find_type_by_name(first_name) {
|
2021-02-04 13:49:24 -06:00
|
|
|
let idx = if path.segments().len() == 1 { None } else { Some(1) };
|
2019-12-07 04:50:36 -06:00
|
|
|
return Some((
|
2019-12-07 13:09:53 -06:00
|
|
|
TypeNs::GenericParam(TypeParamId { local_id, parent: *def }),
|
2019-12-07 04:50:36 -06:00
|
|
|
idx,
|
|
|
|
));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 14:24:40 -06:00
|
|
|
Scope::ImplDefScope(impl_) => {
|
2019-12-13 15:01:06 -06:00
|
|
|
if first_name == &name![Self] {
|
2021-02-04 13:49:24 -06:00
|
|
|
let idx = if path.segments().len() == 1 { None } else { Some(1) };
|
2019-09-12 15:35:53 -05:00
|
|
|
return Some((TypeNs::SelfType(*impl_), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 23:19:52 -05:00
|
|
|
Scope::AdtScope(adt) => {
|
2019-12-13 15:01:06 -06:00
|
|
|
if first_name == &name![Self] {
|
2021-02-04 13:49:24 -06:00
|
|
|
let idx = if path.segments().len() == 1 { None } else { Some(1) };
|
2019-09-26 23:19:52 -05:00
|
|
|
return Some((TypeNs::AdtSelfType(*adt), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
Scope::ModuleScope(m) => {
|
2021-02-09 10:27:04 -06:00
|
|
|
if let Some(res) = m.resolve_path_in_type_ns(db, path) {
|
|
|
|
return Some(res);
|
2019-12-22 13:12:23 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 10:27:04 -06:00
|
|
|
None
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn resolve_path_in_type_ns_fully(
|
2019-06-01 06:34:19 -05:00
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-09-12 15:35:53 -05:00
|
|
|
) -> Option<TypeNs> {
|
|
|
|
let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?;
|
|
|
|
if unresolved.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(res)
|
2019-04-15 05:01:29 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 14:23:22 -06:00
|
|
|
pub fn resolve_visibility(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-26 08:57:14 -06:00
|
|
|
visibility: &RawVisibility,
|
2019-12-26 09:00:10 -06:00
|
|
|
) -> Option<Visibility> {
|
2019-12-24 14:23:22 -06:00
|
|
|
match visibility {
|
2019-12-26 08:57:14 -06:00
|
|
|
RawVisibility::Module(_) => {
|
2020-01-10 11:40:45 -06:00
|
|
|
let (item_map, module) = match self.module_scope() {
|
2019-12-24 16:45:14 -06:00
|
|
|
Some(it) => it,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
item_map.resolve_visibility(db, module, visibility)
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
2019-12-26 09:00:10 -06:00
|
|
|
RawVisibility::Public => Some(Visibility::Public),
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn resolve_path_in_value_ns(
|
2019-09-12 15:35:53 -05:00
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-09-16 14:38:27 -05:00
|
|
|
) -> Option<ResolveValueResult> {
|
2021-02-04 13:49:24 -06:00
|
|
|
let n_segments = path.segments().len();
|
2019-12-13 15:01:06 -06:00
|
|
|
let tmp = name![self];
|
2021-02-04 13:49:24 -06:00
|
|
|
let first_name = if path.is_self() { &tmp } else { path.segments().first()? };
|
2019-09-12 15:35:53 -05:00
|
|
|
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
|
2021-08-02 07:27:47 -05:00
|
|
|
for scope in self.scopes() {
|
2019-09-12 15:35:53 -05:00
|
|
|
match scope {
|
2019-09-26 23:19:52 -05:00
|
|
|
Scope::AdtScope(_)
|
|
|
|
| Scope::ExprScope(_)
|
2019-11-20 11:50:34 -06:00
|
|
|
| Scope::GenericParams { .. }
|
2020-02-29 14:24:40 -06:00
|
|
|
| Scope::ImplDefScope(_)
|
2019-09-12 15:35:53 -05:00
|
|
|
if skip_to_mod =>
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::ExprScope(scope) if n_segments <= 1 => {
|
|
|
|
let entry = scope
|
|
|
|
.expr_scopes
|
|
|
|
.entries(scope.scope_id)
|
|
|
|
.iter()
|
|
|
|
.find(|entry| entry.name() == first_name);
|
|
|
|
|
|
|
|
if let Some(e) = entry {
|
2019-09-11 13:01:07 -05:00
|
|
|
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(_) => continue,
|
|
|
|
|
2019-12-07 04:50:36 -06:00
|
|
|
Scope::GenericParams { params, def } if n_segments > 1 => {
|
2021-01-01 03:06:42 -06:00
|
|
|
if let Some(local_id) = params.find_type_by_name(first_name) {
|
2019-12-07 13:09:53 -06:00
|
|
|
let ty = TypeNs::GenericParam(TypeParamId { local_id, parent: *def });
|
2019-09-11 13:01:07 -05:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 03:06:42 -06:00
|
|
|
Scope::GenericParams { params, def } if n_segments == 1 => {
|
|
|
|
if let Some(local_id) = params.find_const_by_name(first_name) {
|
|
|
|
let val = ValueNs::GenericParam(ConstParamId { local_id, parent: *def });
|
|
|
|
return Some(ResolveValueResult::ValueNs(val));
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 11:50:34 -06:00
|
|
|
Scope::GenericParams { .. } => continue,
|
2019-09-12 15:35:53 -05:00
|
|
|
|
2020-05-15 10:15:40 -05:00
|
|
|
Scope::ImplDefScope(impl_) => {
|
2019-12-13 15:01:06 -06:00
|
|
|
if first_name == &name![Self] {
|
2020-05-15 10:15:40 -05:00
|
|
|
if n_segments > 1 {
|
|
|
|
let ty = TypeNs::SelfType(*impl_);
|
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
|
|
|
} else {
|
|
|
|
return Some(ResolveValueResult::ValueNs(ValueNs::ImplSelf(*impl_)));
|
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 10:15:40 -05:00
|
|
|
Scope::AdtScope(adt) => {
|
|
|
|
if n_segments == 1 {
|
|
|
|
// bare `Self` doesn't work in the value namespace in a struct/enum definition
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-13 15:01:06 -06:00
|
|
|
if first_name == &name![Self] {
|
2019-09-26 23:19:52 -05:00
|
|
|
let ty = TypeNs::AdtSelfType(*adt);
|
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:35:53 -05:00
|
|
|
|
|
|
|
Scope::ModuleScope(m) => {
|
2021-02-09 10:27:04 -06:00
|
|
|
if let Some(def) = m.resolve_path_in_value_ns(db, path) {
|
|
|
|
return Some(def);
|
2019-12-22 13:12:23 -06:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 04:04:14 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
2021-02-09 10:27:04 -06:00
|
|
|
|
|
|
|
None
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn resolve_path_in_value_ns_fully(
|
2019-06-08 10:38:14 -05:00
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-09-12 15:35:53 -05:00
|
|
|
) -> Option<ValueNs> {
|
|
|
|
match self.resolve_path_in_value_ns(db, path)? {
|
2019-09-11 13:01:07 -05:00
|
|
|
ResolveValueResult::ValueNs(it) => Some(it),
|
2019-09-16 14:38:27 -05:00
|
|
|
ResolveValueResult::Partial(..) => None,
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
2019-02-22 02:15:23 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn resolve_path_as_macro(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
|
|
|
) -> Option<MacroDefId> {
|
2020-01-10 11:40:45 -06:00
|
|
|
let (item_map, module) = self.module_scope()?;
|
2021-06-12 22:54:16 -05:00
|
|
|
item_map.resolve_path(db, module, path, BuiltinShadowMode::Other).0.take_macros()
|
2019-09-12 15:35:53 -05:00
|
|
|
}
|
|
|
|
|
2021-08-03 09:36:06 -05:00
|
|
|
/// Returns a set of names available in the current scope.
|
|
|
|
///
|
|
|
|
/// Note that this is a somewhat fuzzy concept -- internally, the compiler
|
|
|
|
/// doesn't necessary follow a strict scoping discipline. Rathe, it just
|
|
|
|
/// tells for each ident what it resolves to.
|
|
|
|
///
|
|
|
|
/// A good example is something like `str::from_utf8`. From scopes point of
|
|
|
|
/// view, this code is erroneous -- both `str` module and `str` type occupy
|
|
|
|
/// the same type namespace.
|
|
|
|
///
|
|
|
|
/// We don't try to model that super-correctly -- this functionality is
|
|
|
|
/// primarily exposed for completions.
|
|
|
|
///
|
|
|
|
/// Note that in Rust one name can be bound to several items:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// macro_rules! t { () => (()) }
|
|
|
|
/// type t = t!();
|
|
|
|
/// const t: t = t!()
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// That's why we return a multimap.
|
|
|
|
///
|
|
|
|
/// The shadowing is accounted for: in
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = 92;
|
|
|
|
/// {
|
|
|
|
/// let x = 92;
|
|
|
|
/// $0
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// there will be only one entry for `x` in the result.
|
|
|
|
///
|
|
|
|
/// The result is ordered *roughly* from the innermost scope to the
|
|
|
|
/// outermost: when the name is introduced in two namespaces in two scopes,
|
|
|
|
/// we use the position of the first scope.
|
|
|
|
pub fn names_in_scope(&self, db: &dyn DefDatabase) -> IndexMap<Name, SmallVec<[ScopeDef; 1]>> {
|
|
|
|
let mut res = ScopeNames::default();
|
2021-08-02 07:27:47 -05:00
|
|
|
for scope in self.scopes() {
|
2021-08-03 09:36:06 -05:00
|
|
|
scope.process_names(db, &mut res);
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
2021-08-03 09:36:06 -05:00
|
|
|
res.map
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-01-26 15:52:04 -06:00
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet<TraitId> {
|
2019-05-12 07:58:37 -05:00
|
|
|
let mut traits = FxHashSet::default();
|
2021-08-02 07:27:47 -05:00
|
|
|
for scope in self.scopes() {
|
2021-05-25 09:08:18 -05:00
|
|
|
match scope {
|
|
|
|
Scope::ModuleScope(m) => {
|
|
|
|
if let Some(prelude) = m.def_map.prelude() {
|
2021-03-09 11:18:35 -06:00
|
|
|
let prelude_def_map = prelude.def_map(db);
|
|
|
|
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
|
|
|
}
|
2021-05-25 09:08:18 -05:00
|
|
|
traits.extend(m.def_map[m.module_id].scope.traits());
|
|
|
|
|
|
|
|
// Add all traits that are in scope because of the containing DefMaps
|
|
|
|
m.def_map.with_ancestor_maps(db, m.module_id, &mut |def_map, module| {
|
|
|
|
if let Some(prelude) = def_map.prelude() {
|
|
|
|
let prelude_def_map = prelude.def_map(db);
|
|
|
|
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
|
|
|
}
|
|
|
|
traits.extend(def_map[module].scope.traits());
|
|
|
|
None::<()>
|
|
|
|
});
|
|
|
|
}
|
|
|
|
&Scope::ImplDefScope(impl_) => {
|
|
|
|
if let Some(target_trait) = &db.impl_data(impl_).target_trait {
|
|
|
|
if let Some(TypeNs::TraitId(trait_)) =
|
|
|
|
self.resolve_path_in_type_ns_fully(db, target_trait.path.mod_path())
|
|
|
|
{
|
|
|
|
traits.insert(trait_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
2019-05-12 07:58:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
traits
|
2019-03-24 11:36:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
fn module_scope(&self) -> Option<(&DefMap, LocalModuleId)> {
|
2021-08-02 07:27:47 -05:00
|
|
|
self.scopes().find_map(|scope| match scope {
|
2021-03-06 17:20:13 -06:00
|
|
|
Scope::ModuleScope(m) => Some((&*m.def_map, m.module_id)),
|
2019-01-29 13:49:31 -06:00
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 16:40:00 -05:00
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
pub fn module(&self) -> Option<ModuleId> {
|
|
|
|
let (def_map, local_id) = self.module_scope()?;
|
2021-01-25 08:21:33 -06:00
|
|
|
Some(def_map.module_id(local_id))
|
2019-12-31 09:17:08 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn krate(&self) -> Option<CrateId> {
|
2021-02-11 12:52:33 -06:00
|
|
|
// FIXME: can this ever be `None`?
|
2021-01-20 11:17:48 -06:00
|
|
|
self.module_scope().map(|t| t.0.krate())
|
2019-04-17 16:40:00 -05:00
|
|
|
}
|
2019-07-06 09:41:04 -05:00
|
|
|
|
2021-06-07 06:59:01 -05:00
|
|
|
pub fn where_predicates_in_scope(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = &crate::generics::WherePredicate> {
|
2021-08-02 07:27:47 -05:00
|
|
|
self.scopes()
|
2019-07-06 09:41:04 -05:00
|
|
|
.filter_map(|scope| match scope {
|
2019-11-20 11:50:34 -06:00
|
|
|
Scope::GenericParams { params, .. } => Some(params),
|
2019-07-06 09:41:04 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flat_map(|params| params.where_predicates.iter())
|
|
|
|
}
|
2019-09-22 13:01:12 -05:00
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn generic_def(&self) -> Option<GenericDefId> {
|
2021-08-02 07:27:47 -05:00
|
|
|
self.scopes().find_map(|scope| match scope {
|
2019-11-20 11:50:34 -06:00
|
|
|
Scope::GenericParams { def, .. } => Some(*def),
|
2019-09-22 13:01:12 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-11-21 06:13:46 -06:00
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn body_owner(&self) -> Option<DefWithBodyId> {
|
2021-08-02 07:27:47 -05:00
|
|
|
self.scopes().find_map(|scope| match scope {
|
2019-11-21 06:13:46 -06:00
|
|
|
Scope::ExprScope(it) => Some(it.owner),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
|
|
|
|
2021-08-03 09:36:06 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-11-21 06:39:09 -06:00
|
|
|
pub enum ScopeDef {
|
2021-08-03 09:36:06 -05:00
|
|
|
ModuleDef(ModuleDefId),
|
|
|
|
MacroDef(MacroDefId),
|
|
|
|
Unknown,
|
2019-11-21 06:13:46 -06:00
|
|
|
ImplSelfType(ImplId),
|
|
|
|
AdtSelfType(AdtId),
|
2021-01-19 13:01:49 -06:00
|
|
|
GenericParam(GenericParamId),
|
2019-11-21 06:13:46 -06:00
|
|
|
Local(PatId),
|
2021-03-20 18:59:45 -05:00
|
|
|
Label(LabelId),
|
2019-11-21 06:13:46 -06:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:35:53 -05:00
|
|
|
impl Scope {
|
2021-08-03 09:36:06 -05:00
|
|
|
fn process_names(&self, db: &dyn DefDatabase, acc: &mut ScopeNames) {
|
2019-01-27 13:50:57 -06:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-03-23 02:53:48 -05:00
|
|
|
// FIXME: should we provide `self` here?
|
2019-01-27 10:23:49 -06:00
|
|
|
// f(
|
|
|
|
// Name::self_param(),
|
|
|
|
// PerNs::types(Resolution::Def {
|
|
|
|
// def: m.module.into(),
|
|
|
|
// }),
|
|
|
|
// );
|
2021-03-06 17:20:13 -06:00
|
|
|
m.def_map[m.module_id].scope.entries().for_each(|(name, def)| {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add_per_ns(name, def);
|
2019-02-08 05:49:43 -06:00
|
|
|
});
|
2021-08-03 09:36:06 -05:00
|
|
|
m.def_map[m.module_id].scope.legacy_macros().for_each(|(name, mac)| {
|
|
|
|
acc.add(name, ScopeDef::MacroDef(mac));
|
2019-09-09 07:54:02 -05:00
|
|
|
});
|
2021-03-06 17:20:13 -06:00
|
|
|
m.def_map.extern_prelude().for_each(|(name, &def)| {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(name, ScopeDef::ModuleDef(def));
|
2019-02-04 15:09:56 -06:00
|
|
|
});
|
2020-02-21 08:34:06 -06:00
|
|
|
BUILTIN_SCOPE.iter().for_each(|(name, &def)| {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add_per_ns(name, def);
|
2020-02-21 08:34:06 -06:00
|
|
|
});
|
2021-03-06 17:20:13 -06:00
|
|
|
if let Some(prelude) = m.def_map.prelude() {
|
2021-01-22 09:31:40 -06:00
|
|
|
let prelude_def_map = prelude.def_map(db);
|
2021-08-03 09:36:06 -05:00
|
|
|
for (name, def) in prelude_def_map[prelude.local_id].scope.entries() {
|
|
|
|
acc.add_per_ns(name, def)
|
|
|
|
}
|
2019-02-13 13:53:42 -06:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
2021-08-02 07:59:28 -05:00
|
|
|
Scope::GenericParams { params, def: parent } => {
|
|
|
|
let parent = *parent;
|
2019-12-07 13:09:53 -06:00
|
|
|
for (local_id, param) in params.types.iter() {
|
2021-08-03 09:36:06 -05:00
|
|
|
if let Some(name) = ¶m.name {
|
2021-06-07 06:59:01 -05:00
|
|
|
let id = TypeParamId { parent, local_id };
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(name, ScopeDef::GenericParam(id.into()))
|
2020-01-24 12:35:09 -06:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
2021-01-19 13:01:49 -06:00
|
|
|
for (local_id, param) in params.consts.iter() {
|
2021-06-07 06:59:01 -05:00
|
|
|
let id = ConstParamId { parent, local_id };
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(¶m.name, ScopeDef::GenericParam(id.into()))
|
2021-01-19 13:01:49 -06:00
|
|
|
}
|
|
|
|
for (local_id, param) in params.lifetimes.iter() {
|
2021-06-07 06:59:01 -05:00
|
|
|
let id = LifetimeParamId { parent, local_id };
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(¶m.name, ScopeDef::GenericParam(id.into()))
|
2021-01-19 13:01:49 -06:00
|
|
|
}
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
2020-02-29 14:24:40 -06:00
|
|
|
Scope::ImplDefScope(i) => {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(&name![Self], ScopeDef::ImplSelfType(*i));
|
2019-09-26 23:19:52 -05:00
|
|
|
}
|
|
|
|
Scope::AdtScope(i) => {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(&name![Self], ScopeDef::AdtSelfType(*i));
|
2019-01-27 13:50:57 -06:00
|
|
|
}
|
2019-11-15 03:00:36 -06:00
|
|
|
Scope::ExprScope(scope) => {
|
2021-03-20 18:59:45 -05:00
|
|
|
if let Some((label, name)) = scope.expr_scopes.label(scope.scope_id) {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add(&name, ScopeDef::Label(label))
|
2021-03-20 18:59:45 -05:00
|
|
|
}
|
2019-11-15 03:00:36 -06:00
|
|
|
scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
|
2021-08-03 09:36:06 -05:00
|
|
|
acc.add_local(e.name(), e.pat());
|
2019-01-27 13:50:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 14:23:26 -06:00
|
|
|
}
|
2019-11-20 12:55:33 -06:00
|
|
|
|
2019-11-21 04:28:04 -06:00
|
|
|
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn resolver_for_expr(db: &dyn DefDatabase, owner: DefWithBodyId, expr_id: ExprId) -> Resolver {
|
2019-11-21 04:32:03 -06:00
|
|
|
let scopes = db.expr_scopes(owner);
|
2019-11-21 04:28:04 -06:00
|
|
|
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:39:09 -06:00
|
|
|
pub fn resolver_for_scope(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-21 04:32:03 -06:00
|
|
|
owner: DefWithBodyId,
|
2019-11-21 04:28:04 -06:00
|
|
|
scope_id: Option<ScopeId>,
|
|
|
|
) -> Resolver {
|
2019-11-21 05:13:49 -06:00
|
|
|
let mut r = owner.resolver(db);
|
2019-11-21 04:32:03 -06:00
|
|
|
let scopes = db.expr_scopes(owner);
|
2019-11-21 04:28:04 -06:00
|
|
|
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
2021-12-09 11:04:32 -06:00
|
|
|
r.scopes.reserve(scope_chain.len());
|
|
|
|
|
2019-11-21 04:28:04 -06:00
|
|
|
for scope in scope_chain.into_iter().rev() {
|
2021-02-09 10:27:04 -06:00
|
|
|
if let Some(block) = scopes.block(scope) {
|
|
|
|
if let Some(def_map) = db.block_def_map(block) {
|
|
|
|
let root = def_map.root();
|
|
|
|
r = r.push_module_scope(def_map, root);
|
|
|
|
// FIXME: This adds as many module scopes as there are blocks, but resolving in each
|
|
|
|
// already traverses all parents, so this is O(n²). I think we could only store the
|
|
|
|
// innermost module scope instead?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:32:03 -06:00
|
|
|
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
|
2019-11-21 04:28:04 -06:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:00:57 -06:00
|
|
|
impl Resolver {
|
|
|
|
fn push_scope(mut self, scope: Scope) -> Resolver {
|
|
|
|
self.scopes.push(scope);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
fn push_generic_params_scope(self, db: &dyn DefDatabase, def: GenericDefId) -> Resolver {
|
2019-11-21 07:00:57 -06:00
|
|
|
let params = db.generic_params(def);
|
2020-02-14 12:16:42 -06:00
|
|
|
self.push_scope(Scope::GenericParams { def, params })
|
2019-11-21 07:00:57 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 14:24:40 -06:00
|
|
|
fn push_impl_def_scope(self, impl_def: ImplId) -> Resolver {
|
|
|
|
self.push_scope(Scope::ImplDefScope(impl_def))
|
2019-11-21 07:00:57 -06:00
|
|
|
}
|
|
|
|
|
2021-03-06 17:20:13 -06:00
|
|
|
fn push_module_scope(self, def_map: Arc<DefMap>, module_id: LocalModuleId) -> Resolver {
|
|
|
|
self.push_scope(Scope::ModuleScope(ModuleItemMap { def_map, module_id }))
|
2019-11-21 07:00:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn push_expr_scope(
|
|
|
|
self,
|
|
|
|
owner: DefWithBodyId,
|
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
) -> Resolver {
|
|
|
|
self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:27:04 -06:00
|
|
|
impl ModuleItemMap {
|
2021-02-09 10:36:10 -06:00
|
|
|
fn resolve_path_in_value_ns(
|
2021-02-09 10:27:04 -06:00
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
path: &ModPath,
|
|
|
|
) -> Option<ResolveValueResult> {
|
|
|
|
let (module_def, idx) =
|
2021-06-12 22:54:16 -05:00
|
|
|
self.def_map.resolve_path_locally(db, self.module_id, path, BuiltinShadowMode::Other);
|
2021-02-09 11:40:05 -06:00
|
|
|
match idx {
|
2021-02-09 10:27:04 -06:00
|
|
|
None => {
|
|
|
|
let value = to_value_ns(module_def)?;
|
|
|
|
Some(ResolveValueResult::ValueNs(value))
|
|
|
|
}
|
|
|
|
Some(idx) => {
|
|
|
|
let ty = match module_def.take_types()? {
|
|
|
|
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
|
|
|
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
|
|
|
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
|
|
|
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDefId::ModuleId(_)
|
|
|
|
| ModuleDefId::FunctionId(_)
|
|
|
|
| ModuleDefId::EnumVariantId(_)
|
|
|
|
| ModuleDefId::ConstId(_)
|
|
|
|
| ModuleDefId::StaticId(_) => return None,
|
|
|
|
};
|
|
|
|
Some(ResolveValueResult::Partial(ty, idx))
|
|
|
|
}
|
2021-02-09 11:40:05 -06:00
|
|
|
}
|
2021-02-09 10:27:04 -06:00
|
|
|
}
|
|
|
|
|
2021-02-09 10:36:10 -06:00
|
|
|
fn resolve_path_in_type_ns(
|
2021-02-09 10:27:04 -06:00
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
path: &ModPath,
|
|
|
|
) -> Option<(TypeNs, Option<usize>)> {
|
|
|
|
let (module_def, idx) =
|
2021-06-12 22:54:16 -05:00
|
|
|
self.def_map.resolve_path_locally(db, self.module_id, path, BuiltinShadowMode::Other);
|
2021-02-09 10:27:04 -06:00
|
|
|
let res = to_type_ns(module_def)?;
|
|
|
|
Some((res, idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value_ns(per_ns: PerNs) -> Option<ValueNs> {
|
|
|
|
let res = match per_ns.take_values()? {
|
|
|
|
ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),
|
|
|
|
ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it),
|
|
|
|
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it),
|
|
|
|
ModuleDefId::ConstId(it) => ValueNs::ConstId(it),
|
|
|
|
ModuleDefId::StaticId(it) => ValueNs::StaticId(it),
|
|
|
|
|
2021-06-17 10:37:14 -05:00
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(_) | AdtId::UnionId(_))
|
2021-02-09 10:27:04 -06:00
|
|
|
| ModuleDefId::TraitId(_)
|
|
|
|
| ModuleDefId::TypeAliasId(_)
|
|
|
|
| ModuleDefId::BuiltinType(_)
|
|
|
|
| ModuleDefId::ModuleId(_) => return None,
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_type_ns(per_ns: PerNs) -> Option<TypeNs> {
|
|
|
|
let res = match per_ns.take_types()? {
|
|
|
|
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
|
|
|
ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
|
|
|
|
|
|
|
|
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
|
|
|
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
|
|
|
|
|
|
|
ModuleDefId::FunctionId(_)
|
|
|
|
| ModuleDefId::ConstId(_)
|
|
|
|
| ModuleDefId::StaticId(_)
|
|
|
|
| ModuleDefId::ModuleId(_) => return None,
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:36:06 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ScopeNames {
|
|
|
|
map: IndexMap<Name, SmallVec<[ScopeDef; 1]>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScopeNames {
|
|
|
|
fn add(&mut self, name: &Name, def: ScopeDef) {
|
|
|
|
let set = self.map.entry(name.clone()).or_default();
|
|
|
|
if !set.contains(&def) {
|
|
|
|
set.push(def)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn add_per_ns(&mut self, name: &Name, def: PerNs) {
|
|
|
|
if let Some(ty) = &def.types {
|
|
|
|
self.add(name, ScopeDef::ModuleDef(ty.0))
|
|
|
|
}
|
|
|
|
if let Some(val) = &def.values {
|
|
|
|
self.add(name, ScopeDef::ModuleDef(val.0))
|
|
|
|
}
|
|
|
|
if let Some(mac) = &def.macros {
|
|
|
|
self.add(name, ScopeDef::MacroDef(mac.0))
|
|
|
|
}
|
|
|
|
if def.is_none() {
|
|
|
|
self.add(name, ScopeDef::Unknown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn add_local(&mut self, name: &Name, pat: PatId) {
|
|
|
|
let set = self.map.entry(name.clone()).or_default();
|
|
|
|
// XXX: hack, account for local (and only local) shadowing.
|
|
|
|
//
|
|
|
|
// This should be somewhat more principled and take namespaces into
|
|
|
|
// accounts, but, alas, scoping rules are a hoax. `str` type and `str`
|
|
|
|
// module can be both available in the same scope.
|
|
|
|
if set.iter().any(|it| matches!(it, &ScopeDef::Local(_))) {
|
|
|
|
cov_mark::hit!(shadowing_shows_single_completion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set.push(ScopeDef::Local(pat))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:56:07 -06:00
|
|
|
pub trait HasResolver: Copy {
|
2019-11-20 12:55:33 -06:00
|
|
|
/// Builds a resolver for type references inside this def.
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver;
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 05:13:49 -06:00
|
|
|
impl HasResolver for ModuleId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2021-03-22 12:47:19 -05:00
|
|
|
let mut def_map = self.def_map(db);
|
2021-12-09 11:04:32 -06:00
|
|
|
let mut modules: SmallVec<[_; 2]> = smallvec![(def_map.clone(), self.local_id)];
|
2021-03-22 12:47:19 -05:00
|
|
|
while let Some(parent) = def_map.parent() {
|
|
|
|
def_map = parent.def_map(db);
|
|
|
|
modules.push((def_map.clone(), parent.local_id));
|
|
|
|
}
|
|
|
|
let mut resolver = Resolver::default();
|
2021-12-09 11:04:32 -06:00
|
|
|
resolver.scopes.reserve(modules.len());
|
2021-03-22 12:47:19 -05:00
|
|
|
for (def_map, module) in modules.into_iter().rev() {
|
|
|
|
resolver = resolver.push_module_scope(def_map, module);
|
|
|
|
}
|
|
|
|
resolver
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 05:13:49 -06:00
|
|
|
impl HasResolver for TraitId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-12 07:34:03 -06:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:56:07 -06:00
|
|
|
impl<T: Into<AdtId> + Copy> HasResolver for T {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 06:56:27 -06:00
|
|
|
let def = self.into();
|
2019-11-24 13:47:58 -06:00
|
|
|
def.module(db)
|
2019-11-20 12:55:33 -06:00
|
|
|
.resolver(db)
|
2019-11-21 06:56:27 -06:00
|
|
|
.push_generic_params_scope(db, def.into())
|
|
|
|
.push_scope(Scope::AdtScope(def))
|
2019-11-21 05:13:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for FunctionId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 05:13:49 -06:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for ConstId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 05:13:49 -06:00
|
|
|
self.lookup(db).container.resolver(db)
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 05:13:49 -06:00
|
|
|
impl HasResolver for StaticId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-24 06:13:56 -06:00
|
|
|
self.lookup(db).container.resolver(db)
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 05:13:49 -06:00
|
|
|
impl HasResolver for TypeAliasId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 05:13:49 -06:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:16:57 -06:00
|
|
|
impl HasResolver for ImplId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-12 07:09:13 -06:00
|
|
|
self.lookup(db)
|
|
|
|
.container
|
2019-12-08 05:16:57 -06:00
|
|
|
.resolver(db)
|
|
|
|
.push_generic_params_scope(db, self.into())
|
2020-02-29 14:24:40 -06:00
|
|
|
.push_impl_def_scope(self)
|
2019-12-08 05:16:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:31:26 -06:00
|
|
|
impl HasResolver for ExternBlockId {
|
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
|
|
|
// Same as parent's
|
|
|
|
self.lookup(db).container.resolver(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:16:57 -06:00
|
|
|
impl HasResolver for DefWithBodyId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-08 05:16:57 -06:00
|
|
|
match self {
|
|
|
|
DefWithBodyId::ConstId(c) => c.resolver(db),
|
|
|
|
DefWithBodyId::FunctionId(f) => f.resolver(db),
|
|
|
|
DefWithBodyId::StaticId(s) => s.resolver(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:31:26 -06:00
|
|
|
impl HasResolver for ItemContainerId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-20 12:55:33 -06:00
|
|
|
match self {
|
2021-12-07 10:31:26 -06:00
|
|
|
ItemContainerId::ModuleId(it) => it.resolver(db),
|
|
|
|
ItemContainerId::TraitId(it) => it.resolver(db),
|
|
|
|
ItemContainerId::ImplId(it) => it.resolver(db),
|
|
|
|
ItemContainerId::ExternBlockId(it) => it.resolver(db),
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 05:13:49 -06:00
|
|
|
impl HasResolver for GenericDefId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-20 12:55:33 -06:00
|
|
|
match self {
|
2019-11-21 05:13:49 -06:00
|
|
|
GenericDefId::FunctionId(inner) => inner.resolver(db),
|
|
|
|
GenericDefId::AdtId(adt) => adt.resolver(db),
|
|
|
|
GenericDefId::TraitId(inner) => inner.resolver(db),
|
|
|
|
GenericDefId::TypeAliasId(inner) => inner.resolver(db),
|
|
|
|
GenericDefId::ImplId(inner) => inner.resolver(db),
|
|
|
|
GenericDefId::EnumVariantId(inner) => inner.parent.resolver(db),
|
|
|
|
GenericDefId::ConstId(inner) => inner.resolver(db),
|
2019-11-20 12:55:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:16:57 -06:00
|
|
|
impl HasResolver for VariantId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-08 05:16:57 -06:00
|
|
|
match self {
|
|
|
|
VariantId::EnumVariantId(it) => it.parent.resolver(db),
|
|
|
|
VariantId::StructId(it) => it.resolver(db),
|
|
|
|
VariantId::UnionId(it) => it.resolver(db),
|
|
|
|
}
|
2019-11-21 05:13:49 -06:00
|
|
|
}
|
|
|
|
}
|