2019-11-21 12:39:09 +00:00
|
|
|
//! Name resolution façade.
|
2019-01-19 20:23:26 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::CrateId;
|
2019-11-21 12:39:09 +00:00
|
|
|
use hir_expand::{
|
2019-12-13 21:01:06 +00:00
|
|
|
name::{name, Name},
|
2019-11-21 12:39:09 +00:00
|
|
|
MacroDefId,
|
|
|
|
};
|
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
|
|
|
use crate::{
|
2019-11-21 12:24:51 +00:00
|
|
|
body::scope::{ExprScopes, ScopeId},
|
2019-12-22 19:12:23 +00:00
|
|
|
body::Body,
|
2019-10-31 07:51:54 +00:00
|
|
|
builtin_type::BuiltinType,
|
2019-11-23 11:44:43 +00:00
|
|
|
db::DefDatabase,
|
2019-11-21 12:24:51 +00:00
|
|
|
expr::{ExprId, PatId},
|
2019-11-21 09:21:46 +00:00
|
|
|
generics::GenericParams,
|
2020-02-21 14:34:06 +00:00
|
|
|
item_scope::{BuiltinShadowMode, BUILTIN_SCOPE},
|
2019-12-20 14:43:45 +00:00
|
|
|
nameres::CrateDefMap,
|
2019-12-13 11:12:36 +00:00
|
|
|
path::{ModPath, PathKind},
|
2019-11-23 13:53:16 +00:00
|
|
|
per_ns::PerNs,
|
2019-12-26 15:00:10 +00:00
|
|
|
visibility::{RawVisibility, Visibility},
|
2019-12-20 11:07:23 +00:00
|
|
|
AdtId, AssocContainerId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId,
|
|
|
|
FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
|
|
|
|
StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
|
2019-10-30 14:19:30 +00:00
|
|
|
};
|
2019-01-19 20:23:26 +00:00
|
|
|
|
2019-01-23 22:08:41 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2019-11-21 12:39:09 +00:00
|
|
|
pub struct Resolver {
|
2019-12-07 10:50:36 +00:00
|
|
|
// FIXME: all usages generally call `.rev`, so maybe reverse once in consturciton?
|
2019-01-26 21:52:04 +00:00
|
|
|
scopes: Vec<Scope>,
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 07:53:48 +00:00
|
|
|
// FIXME how to store these best
|
2019-01-19 20:23:26 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 18:00:50 +00:00
|
|
|
struct ModuleItemMap {
|
2019-03-14 09:54:03 +00:00
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-11-23 13:49:53 +00:00
|
|
|
module_id: LocalModuleId,
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 18:00:50 +00:00
|
|
|
struct ExprScope {
|
2019-11-21 09:21:46 +00:00
|
|
|
owner: DefWithBodyId,
|
2019-01-19 20:23:26 +00:00
|
|
|
expr_scopes: Arc<ExprScopes>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2019-11-24 18:00:50 +00:00
|
|
|
enum Scope {
|
2019-01-19 20:23:26 +00:00
|
|
|
/// All the items and imported names of a module
|
|
|
|
ModuleScope(ModuleItemMap),
|
|
|
|
/// Brings the generic parameters of an item into scope
|
2019-11-21 09:21:46 +00:00
|
|
|
GenericParams { def: GenericDefId, params: Arc<GenericParams> },
|
2019-09-27 04:19:52 +00:00
|
|
|
/// Brings `Self` in `impl` block into scope
|
2020-02-29 20:24:40 +00:00
|
|
|
ImplDefScope(ImplId),
|
2019-10-08 11:25:37 +00:00
|
|
|
/// Brings `Self` in enum, struct and union definitions into scope
|
2019-11-21 09:21:46 +00:00
|
|
|
AdtScope(AdtId),
|
2019-01-19 20:23:26 +00:00
|
|
|
/// Local bindings
|
|
|
|
ExprScope(ExprScope),
|
2019-12-22 19:12:23 +00:00
|
|
|
/// Temporary hack to support local items.
|
|
|
|
LocalItemsScope(Arc<Body>),
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 12:39:09 +00:00
|
|
|
pub enum TypeNs {
|
2019-11-21 09:21:46 +00:00
|
|
|
SelfType(ImplId),
|
2019-12-07 19:09:53 +00:00
|
|
|
GenericParam(TypeParamId),
|
2019-11-21 09:21:46 +00:00
|
|
|
AdtId(AdtId),
|
|
|
|
AdtSelfType(AdtId),
|
2019-11-26 16:30:57 +00:00
|
|
|
// Yup, enum variants are added to the types ns, but any usage of variant as
|
|
|
|
// type is an error.
|
2019-11-21 09:21:46 +00:00
|
|
|
EnumVariantId(EnumVariantId),
|
|
|
|
TypeAliasId(TypeAliasId),
|
2019-09-12 20:35:53 +00:00
|
|
|
BuiltinType(BuiltinType),
|
2019-11-21 09:21:46 +00:00
|
|
|
TraitId(TraitId),
|
2019-09-14 14:26:03 +00:00
|
|
|
// Module belong to type ns, but the resolver is used when all module paths
|
2019-09-12 20:35:53 +00:00
|
|
|
// are fully resolved.
|
2019-11-21 09:21:46 +00:00
|
|
|
// ModuleId(ModuleId)
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 12:39:09 +00:00
|
|
|
pub enum ResolveValueResult {
|
2019-09-12 20:35:53 +00:00
|
|
|
ValueNs(ValueNs),
|
|
|
|
Partial(TypeNs, usize),
|
|
|
|
}
|
2019-09-12 17:39:10 +00:00
|
|
|
|
2019-09-14 14:26:03 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-11-21 12:39:09 +00:00
|
|
|
pub enum ValueNs {
|
2020-05-15 15:15:40 +00:00
|
|
|
ImplSelf(ImplId),
|
2019-01-30 21:41:44 +00:00
|
|
|
LocalBinding(PatId),
|
2019-11-21 10:32:03 +00:00
|
|
|
FunctionId(FunctionId),
|
|
|
|
ConstId(ConstId),
|
|
|
|
StaticId(StaticId),
|
|
|
|
StructId(StructId),
|
|
|
|
EnumVariantId(EnumVariantId),
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resolver {
|
2019-09-12 17:39:10 +00:00
|
|
|
/// Resolve known trait from std, like `std::futures::Future`
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn resolve_known_trait(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<TraitId> {
|
2019-11-30 15:29:21 +00:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::TraitId(it) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known struct from std, like `std::boxed::Box`
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn resolve_known_struct(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<StructId> {
|
2019-11-30 15:29:21 +00:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve known enum from std, like `std::result::Result`
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn resolve_known_enum(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<EnumId> {
|
2019-11-30 15:29:21 +00:00
|
|
|
let res = self.resolve_module_path(db, path, BuiltinShadowMode::Other).take_types()?;
|
2019-09-12 17:39:10 +00:00
|
|
|
match res {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
|
2019-09-12 17:39:10 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:29:21 +00:00
|
|
|
fn resolve_module_path(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-11-30 15:29:21 +00:00
|
|
|
shadow: BuiltinShadowMode,
|
|
|
|
) -> PerNs {
|
2020-01-10 17:40:45 +00:00
|
|
|
let (item_map, module) = match self.module_scope() {
|
2019-09-12 20:35:53 +00:00
|
|
|
Some(it) => it,
|
|
|
|
None => return PerNs::none(),
|
|
|
|
};
|
2019-12-13 11:12:36 +00:00
|
|
|
let (module_res, segment_index) = item_map.resolve_path(db, module, &path, shadow);
|
2019-09-12 20:35:53 +00:00
|
|
|
if segment_index.is_some() {
|
|
|
|
return PerNs::none();
|
|
|
|
}
|
|
|
|
module_res
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn resolve_module_path_in_items(&self, db: &dyn DefDatabase, path: &ModPath) -> PerNs {
|
2019-11-30 15:29:21 +00:00
|
|
|
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn resolve_path_in_type_ns(
|
2019-09-12 20:35:53 +00:00
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-09-12 20:35:53 +00:00
|
|
|
) -> Option<(TypeNs, Option<usize>)> {
|
2019-12-13 11:12:36 +00:00
|
|
|
let first_name = path.segments.first()?;
|
2019-09-12 20:35:53 +00:00
|
|
|
let skip_to_mod = path.kind != PathKind::Plain;
|
2019-01-19 20:23:26 +00:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 20:35:53 +00:00
|
|
|
match scope {
|
|
|
|
Scope::ExprScope(_) => continue,
|
2019-12-22 19:12:23 +00:00
|
|
|
Scope::GenericParams { .. }
|
2020-02-29 20:24:40 +00:00
|
|
|
| Scope::ImplDefScope(_)
|
2019-12-22 19:12:23 +00:00
|
|
|
| Scope::LocalItemsScope(_)
|
|
|
|
if skip_to_mod =>
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2019-12-07 10:50:36 +00:00
|
|
|
Scope::GenericParams { params, def } => {
|
|
|
|
if let Some(local_id) = params.find_by_name(first_name) {
|
2019-09-12 20:35:53 +00:00
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
2019-12-07 10:50:36 +00:00
|
|
|
return Some((
|
2019-12-07 19:09:53 +00:00
|
|
|
TypeNs::GenericParam(TypeParamId { local_id, parent: *def }),
|
2019-12-07 10:50:36 +00:00
|
|
|
idx,
|
|
|
|
));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 20:24:40 +00:00
|
|
|
Scope::ImplDefScope(impl_) => {
|
2019-12-13 21:01:06 +00:00
|
|
|
if first_name == &name![Self] {
|
2019-09-12 20:35:53 +00:00
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::SelfType(*impl_), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 04:19:52 +00:00
|
|
|
Scope::AdtScope(adt) => {
|
2019-12-13 21:01:06 +00:00
|
|
|
if first_name == &name![Self] {
|
2019-09-27 04:19:52 +00:00
|
|
|
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
|
|
|
return Some((TypeNs::AdtSelfType(*adt), idx));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
Scope::ModuleScope(m) => {
|
2019-11-30 15:29:21 +00:00
|
|
|
let (module_def, idx) = m.crate_def_map.resolve_path(
|
|
|
|
db,
|
|
|
|
m.module_id,
|
2019-12-13 11:12:36 +00:00
|
|
|
&path,
|
2019-11-30 15:29:21 +00:00
|
|
|
BuiltinShadowMode::Other,
|
|
|
|
);
|
2019-12-22 19:12:23 +00:00
|
|
|
let res = to_type_ns(module_def)?;
|
2019-09-12 20:35:53 +00:00
|
|
|
return Some((res, idx));
|
|
|
|
}
|
2019-12-22 19:12:23 +00:00
|
|
|
Scope::LocalItemsScope(body) => {
|
2020-02-21 14:34:06 +00:00
|
|
|
let def = body.item_scope.get(first_name);
|
2019-12-22 19:12:23 +00:00
|
|
|
if let Some(res) = to_type_ns(def) {
|
|
|
|
return Some((res, None));
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 19:12:23 +00:00
|
|
|
return None;
|
|
|
|
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)
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn resolve_path_in_type_ns_fully(
|
2019-06-01 11:34:19 +00:00
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-09-12 20:35:53 +00: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 10:01:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:23:22 +00:00
|
|
|
pub fn resolve_visibility(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-26 14:57:14 +00:00
|
|
|
visibility: &RawVisibility,
|
2019-12-26 15:00:10 +00:00
|
|
|
) -> Option<Visibility> {
|
2019-12-24 20:23:22 +00:00
|
|
|
match visibility {
|
2019-12-26 14:57:14 +00:00
|
|
|
RawVisibility::Module(_) => {
|
2020-01-10 17:40:45 +00:00
|
|
|
let (item_map, module) = match self.module_scope() {
|
2019-12-24 22:45:14 +00:00
|
|
|
Some(it) => it,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
item_map.resolve_visibility(db, module, visibility)
|
2019-12-24 20:23:22 +00:00
|
|
|
}
|
2019-12-26 15:00:10 +00:00
|
|
|
RawVisibility::Public => Some(Visibility::Public),
|
2019-12-24 20:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 11:12:36 +00:00
|
|
|
pub fn resolve_path_in_value_ns(
|
2019-09-12 20:35:53 +00:00
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-09-16 19:38:27 +00:00
|
|
|
) -> Option<ResolveValueResult> {
|
2019-09-12 20:35:53 +00:00
|
|
|
let n_segments = path.segments.len();
|
2019-12-13 21:01:06 +00:00
|
|
|
let tmp = name![self];
|
2019-12-13 11:12:36 +00:00
|
|
|
let first_name = if path.is_self() { &tmp } else { &path.segments.first()? };
|
2019-09-12 20:35:53 +00:00
|
|
|
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
|
|
|
|
for scope in self.scopes.iter().rev() {
|
|
|
|
match scope {
|
2019-09-27 04:19:52 +00:00
|
|
|
Scope::AdtScope(_)
|
|
|
|
| Scope::ExprScope(_)
|
2019-11-20 17:50:34 +00:00
|
|
|
| Scope::GenericParams { .. }
|
2020-02-29 20:24:40 +00:00
|
|
|
| Scope::ImplDefScope(_)
|
2019-12-22 19:12:23 +00:00
|
|
|
| Scope::LocalItemsScope(_)
|
2019-09-12 20:35:53 +00: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 18:01:07 +00:00
|
|
|
return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat())));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ExprScope(_) => continue,
|
|
|
|
|
2019-12-07 10:50:36 +00:00
|
|
|
Scope::GenericParams { params, def } if n_segments > 1 => {
|
|
|
|
if let Some(local_id) = params.find_by_name(first_name) {
|
2019-12-07 19:09:53 +00:00
|
|
|
let ty = TypeNs::GenericParam(TypeParamId { local_id, parent: *def });
|
2019-09-11 18:01:07 +00:00
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { .. } => continue,
|
2019-09-12 20:35:53 +00:00
|
|
|
|
2020-05-15 15:15:40 +00:00
|
|
|
Scope::ImplDefScope(impl_) => {
|
2019-12-13 21:01:06 +00:00
|
|
|
if first_name == &name![Self] {
|
2020-05-15 15:15:40 +00: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 20:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 15:15:40 +00: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 21:01:06 +00:00
|
|
|
if first_name == &name![Self] {
|
2019-09-27 04:19:52 +00:00
|
|
|
let ty = TypeNs::AdtSelfType(*adt);
|
|
|
|
return Some(ResolveValueResult::Partial(ty, 1));
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:35:53 +00:00
|
|
|
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-11-30 15:29:21 +00:00
|
|
|
let (module_def, idx) = m.crate_def_map.resolve_path(
|
|
|
|
db,
|
|
|
|
m.module_id,
|
2019-12-13 11:12:36 +00:00
|
|
|
&path,
|
2019-11-30 15:29:21 +00:00
|
|
|
BuiltinShadowMode::Other,
|
|
|
|
);
|
2019-09-12 20:35:53 +00:00
|
|
|
return match idx {
|
|
|
|
None => {
|
2019-12-22 19:12:23 +00:00
|
|
|
let value = to_value_ns(module_def)?;
|
2019-09-11 18:01:07 +00:00
|
|
|
Some(ResolveValueResult::ValueNs(value))
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
Some(idx) => {
|
|
|
|
let ty = match module_def.take_types()? {
|
2019-11-21 09:21:46 +00:00
|
|
|
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
|
|
|
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
|
|
|
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
2019-10-31 15:45:10 +00:00
|
|
|
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
|
|
|
|
|
|
|
ModuleDefId::ModuleId(_)
|
|
|
|
| ModuleDefId::FunctionId(_)
|
|
|
|
| ModuleDefId::EnumVariantId(_)
|
|
|
|
| ModuleDefId::ConstId(_)
|
|
|
|
| ModuleDefId::StaticId(_) => return None,
|
2019-09-12 20:35:53 +00:00
|
|
|
};
|
2019-09-11 18:01:07 +00:00
|
|
|
Some(ResolveValueResult::Partial(ty, idx))
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-12-22 19:12:23 +00:00
|
|
|
Scope::LocalItemsScope(body) => {
|
2020-02-21 14:34:06 +00:00
|
|
|
// we don't bother looking in the builtin scope here because there are no builtin values
|
|
|
|
let def = to_value_ns(body.item_scope.get(first_name));
|
|
|
|
|
|
|
|
if let Some(res) = def {
|
2019-12-22 19:12:23 +00:00
|
|
|
return Some(ResolveValueResult::ValueNs(res));
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 10:04:14 +00:00
|
|
|
}
|
2019-01-26 21:52:04 +00:00
|
|
|
}
|
2019-12-22 19:12:23 +00:00
|
|
|
return None;
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(_))
|
|
|
|
| ModuleDefId::AdtId(AdtId::UnionId(_))
|
|
|
|
| ModuleDefId::TraitId(_)
|
|
|
|
| ModuleDefId::TypeAliasId(_)
|
|
|
|
| ModuleDefId::BuiltinType(_)
|
|
|
|
| ModuleDefId::ModuleId(_) => return None,
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn resolve_path_in_value_ns_fully(
|
2019-06-08 15:38:14 +00:00
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-09-12 20:35:53 +00:00
|
|
|
) -> Option<ValueNs> {
|
|
|
|
match self.resolve_path_in_value_ns(db, path)? {
|
2019-09-11 18:01:07 +00:00
|
|
|
ResolveValueResult::ValueNs(it) => Some(it),
|
2019-09-16 19:38:27 +00:00
|
|
|
ResolveValueResult::Partial(..) => None,
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
2019-02-22 08:15:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 11:12:36 +00:00
|
|
|
pub fn resolve_path_as_macro(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
|
|
|
) -> Option<MacroDefId> {
|
2020-03-14 06:25:30 +00:00
|
|
|
// Search item scope legacy macro first
|
|
|
|
if let Some(def) = self.resolve_local_macro_def(path) {
|
|
|
|
return Some(def);
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:40:45 +00:00
|
|
|
let (item_map, module) = self.module_scope()?;
|
2019-12-13 11:12:36 +00:00
|
|
|
item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros()
|
2019-09-12 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn process_all_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
|
2019-02-01 22:06:57 +00:00
|
|
|
for scope in self.scopes.iter().rev() {
|
2019-09-12 20:35:53 +00:00
|
|
|
scope.process_names(db, f);
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
2019-01-26 21:52:04 +00:00
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet<TraitId> {
|
2019-05-12 12:58:37 +00:00
|
|
|
let mut traits = FxHashSet::default();
|
|
|
|
for scope in &self.scopes {
|
|
|
|
if let Scope::ModuleScope(m) = scope {
|
2019-11-24 15:05:12 +00:00
|
|
|
if let Some(prelude) = m.crate_def_map.prelude {
|
2019-10-31 15:45:10 +00:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
2019-11-27 18:31:51 +00:00
|
|
|
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
2019-03-24 16:36:15 +00:00
|
|
|
}
|
2019-11-21 09:21:46 +00:00
|
|
|
traits.extend(m.crate_def_map[m.module_id].scope.traits());
|
2019-05-12 12:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
traits
|
2019-03-24 16:36:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 17:40:45 +00:00
|
|
|
fn module_scope(&self) -> Option<(&CrateDefMap, LocalModuleId)> {
|
2019-01-29 19:49:31 +00:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
2019-03-14 09:54:03 +00:00
|
|
|
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
|
2019-01-29 19:49:31 +00:00
|
|
|
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 21:40:00 +00:00
|
|
|
|
2020-03-14 06:25:30 +00:00
|
|
|
fn resolve_local_macro_def(&self, path: &ModPath) -> Option<MacroDefId> {
|
|
|
|
let name = path.as_ident()?;
|
|
|
|
self.scopes.iter().rev().find_map(|scope| {
|
|
|
|
if let Scope::LocalItemsScope(body) = scope {
|
|
|
|
return body.item_scope.get_legacy_macro(name);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:40:45 +00:00
|
|
|
pub fn module(&self) -> Option<ModuleId> {
|
|
|
|
let (def_map, local_id) = self.module_scope()?;
|
2019-12-31 15:17:08 +00:00
|
|
|
Some(ModuleId { krate: def_map.krate, local_id })
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn krate(&self) -> Option<CrateId> {
|
2020-01-10 17:40:45 +00:00
|
|
|
self.module_scope().map(|t| t.0.krate)
|
2019-04-17 21:40:00 +00:00
|
|
|
}
|
2019-07-06 14:41:04 +00:00
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn where_predicates_in_scope<'a>(
|
2019-07-06 14:41:04 +00:00
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a {
|
|
|
|
self.scopes
|
|
|
|
.iter()
|
2019-12-07 10:50:36 +00:00
|
|
|
.rev()
|
2019-07-06 14:41:04 +00:00
|
|
|
.filter_map(|scope| match scope {
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { params, .. } => Some(params),
|
2019-07-06 14:41:04 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flat_map(|params| params.where_predicates.iter())
|
|
|
|
}
|
2019-09-22 18:01:12 +00:00
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn generic_def(&self) -> Option<GenericDefId> {
|
2019-12-07 10:50:36 +00:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
2019-11-20 17:50:34 +00:00
|
|
|
Scope::GenericParams { def, .. } => Some(*def),
|
2019-09-22 18:01:12 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-11-21 12:13:46 +00:00
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn body_owner(&self) -> Option<DefWithBodyId> {
|
2019-12-07 10:50:36 +00:00
|
|
|
self.scopes.iter().rev().find_map(|scope| match scope {
|
2019-11-21 12:13:46 +00:00
|
|
|
Scope::ExprScope(it) => Some(it.owner),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub enum ScopeDef {
|
2019-11-21 12:13:46 +00:00
|
|
|
PerNs(PerNs),
|
|
|
|
ImplSelfType(ImplId),
|
|
|
|
AdtSelfType(AdtId),
|
2019-12-07 19:09:53 +00:00
|
|
|
GenericParam(TypeParamId),
|
2019-11-21 12:13:46 +00:00
|
|
|
Local(PatId),
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:53 +00:00
|
|
|
impl Scope {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
|
2019-01-27 19:50:57 +00:00
|
|
|
match self {
|
|
|
|
Scope::ModuleScope(m) => {
|
2019-03-23 07:53:48 +00:00
|
|
|
// FIXME: should we provide `self` here?
|
2019-01-27 16:23:49 +00:00
|
|
|
// f(
|
|
|
|
// Name::self_param(),
|
|
|
|
// PerNs::types(Resolution::Def {
|
|
|
|
// def: m.module.into(),
|
|
|
|
// }),
|
|
|
|
// );
|
2019-12-22 14:37:07 +00:00
|
|
|
m.crate_def_map[m.module_id].scope.entries().for_each(|(name, def)| {
|
|
|
|
f(name.clone(), ScopeDef::PerNs(def));
|
2019-02-08 11:49:43 +00:00
|
|
|
});
|
2019-09-09 12:54:02 +00:00
|
|
|
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
|
2019-12-26 15:00:10 +00:00
|
|
|
f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_, Visibility::Public)));
|
2019-09-09 12:54:02 +00:00
|
|
|
});
|
2019-11-24 15:05:12 +00:00
|
|
|
m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| {
|
2020-02-18 12:53:02 +00:00
|
|
|
f(name.clone(), ScopeDef::PerNs(PerNs::types(def, Visibility::Public)));
|
2019-02-04 21:09:56 +00:00
|
|
|
});
|
2020-02-21 14:34:06 +00:00
|
|
|
BUILTIN_SCOPE.iter().for_each(|(name, &def)| {
|
|
|
|
f(name.clone(), ScopeDef::PerNs(def));
|
|
|
|
});
|
2019-11-24 15:05:12 +00:00
|
|
|
if let Some(prelude) = m.crate_def_map.prelude {
|
2019-10-31 15:45:10 +00:00
|
|
|
let prelude_def_map = db.crate_def_map(prelude.krate);
|
2019-12-22 14:37:07 +00:00
|
|
|
prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| {
|
|
|
|
f(name.clone(), ScopeDef::PerNs(def));
|
2019-10-31 15:45:10 +00:00
|
|
|
});
|
2019-02-13 19:53:42 +00:00
|
|
|
}
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2020-06-30 12:06:23 +00:00
|
|
|
Scope::LocalItemsScope(body) => body.item_scope.entries().for_each(|(name, def)| {
|
|
|
|
f(name.clone(), ScopeDef::PerNs(def));
|
|
|
|
}),
|
2019-12-07 10:50:36 +00:00
|
|
|
Scope::GenericParams { params, def } => {
|
2019-12-07 19:09:53 +00:00
|
|
|
for (local_id, param) in params.types.iter() {
|
2020-01-24 18:35:09 +00:00
|
|
|
if let Some(name) = ¶m.name {
|
|
|
|
f(
|
|
|
|
name.clone(),
|
|
|
|
ScopeDef::GenericParam(TypeParamId { local_id, parent: *def }),
|
|
|
|
)
|
|
|
|
}
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 20:24:40 +00:00
|
|
|
Scope::ImplDefScope(i) => {
|
2020-02-18 12:53:02 +00:00
|
|
|
f(name![Self], ScopeDef::ImplSelfType(*i));
|
2019-09-27 04:19:52 +00:00
|
|
|
}
|
|
|
|
Scope::AdtScope(i) => {
|
2020-02-18 12:53:02 +00:00
|
|
|
f(name![Self], ScopeDef::AdtSelfType(*i));
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
2019-11-15 09:00:36 +00:00
|
|
|
Scope::ExprScope(scope) => {
|
|
|
|
scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
|
2019-11-21 12:13:46 +00:00
|
|
|
f(e.name().clone(), ScopeDef::Local(e.pat()));
|
2019-01-27 19:50:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 20:23:26 +00:00
|
|
|
}
|
2019-11-20 18:55:33 +00:00
|
|
|
|
2019-11-21 10:28:04 +00:00
|
|
|
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn resolver_for_expr(db: &dyn DefDatabase, owner: DefWithBodyId, expr_id: ExprId) -> Resolver {
|
2019-11-21 10:32:03 +00:00
|
|
|
let scopes = db.expr_scopes(owner);
|
2019-11-21 10:28:04 +00:00
|
|
|
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
pub fn resolver_for_scope(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-21 10:32:03 +00:00
|
|
|
owner: DefWithBodyId,
|
2019-11-21 10:28:04 +00:00
|
|
|
scope_id: Option<ScopeId>,
|
|
|
|
) -> Resolver {
|
2019-11-21 11:13:49 +00:00
|
|
|
let mut r = owner.resolver(db);
|
2019-12-22 19:12:23 +00:00
|
|
|
r = r.push_local_items_scope(db.body(owner));
|
2019-11-21 10:32:03 +00:00
|
|
|
let scopes = db.expr_scopes(owner);
|
2019-11-21 10:28:04 +00:00
|
|
|
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
|
|
|
for scope in scope_chain.into_iter().rev() {
|
2019-11-21 10:32:03 +00:00
|
|
|
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
|
2019-11-21 10:28:04 +00:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:00:57 +00:00
|
|
|
impl Resolver {
|
|
|
|
fn push_scope(mut self, scope: Scope) -> Resolver {
|
|
|
|
self.scopes.push(scope);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
fn push_generic_params_scope(self, db: &dyn DefDatabase, def: GenericDefId) -> Resolver {
|
2019-11-21 13:00:57 +00:00
|
|
|
let params = db.generic_params(def);
|
2020-02-14 18:16:42 +00:00
|
|
|
self.push_scope(Scope::GenericParams { def, params })
|
2019-11-21 13:00:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 20:24:40 +00:00
|
|
|
fn push_impl_def_scope(self, impl_def: ImplId) -> Resolver {
|
|
|
|
self.push_scope(Scope::ImplDefScope(impl_def))
|
2019-11-21 13:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn push_module_scope(
|
|
|
|
self,
|
|
|
|
crate_def_map: Arc<CrateDefMap>,
|
2019-11-23 13:49:53 +00:00
|
|
|
module_id: LocalModuleId,
|
2019-11-21 13:00:57 +00:00
|
|
|
) -> Resolver {
|
|
|
|
self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
|
|
|
|
}
|
|
|
|
|
2019-12-22 19:12:23 +00:00
|
|
|
fn push_local_items_scope(self, body: Arc<Body>) -> Resolver {
|
|
|
|
self.push_scope(Scope::LocalItemsScope(body))
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:00:57 +00: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 }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 19:56:07 +00:00
|
|
|
pub trait HasResolver: Copy {
|
2019-11-20 18:55:33 +00:00
|
|
|
/// Builds a resolver for type references inside this def.
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver;
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 11:13:49 +00:00
|
|
|
impl HasResolver for ModuleId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 11:13:49 +00:00
|
|
|
let def_map = db.crate_def_map(self.krate);
|
2019-11-27 18:31:51 +00:00
|
|
|
Resolver::default().push_module_scope(def_map, self.local_id)
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:13:49 +00:00
|
|
|
impl HasResolver for TraitId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-12 13:34:03 +00:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 19:56:07 +00:00
|
|
|
impl<T: Into<AdtId> + Copy> HasResolver for T {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 12:56:27 +00:00
|
|
|
let def = self.into();
|
2019-11-24 19:47:58 +00:00
|
|
|
def.module(db)
|
2019-11-20 18:55:33 +00:00
|
|
|
.resolver(db)
|
2019-11-21 12:56:27 +00:00
|
|
|
.push_generic_params_scope(db, def.into())
|
|
|
|
.push_scope(Scope::AdtScope(def))
|
2019-11-21 11:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for FunctionId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 11:13:49 +00:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for ConstId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 11:13:49 +00:00
|
|
|
self.lookup(db).container.resolver(db)
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:13:49 +00:00
|
|
|
impl HasResolver for StaticId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-24 12:13:56 +00:00
|
|
|
self.lookup(db).container.resolver(db)
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:13:49 +00:00
|
|
|
impl HasResolver for TypeAliasId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-21 11:13:49 +00:00
|
|
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 11:16:57 +00:00
|
|
|
impl HasResolver for ImplId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-12 13:09:13 +00:00
|
|
|
self.lookup(db)
|
|
|
|
.container
|
2019-12-08 11:16:57 +00:00
|
|
|
.resolver(db)
|
|
|
|
.push_generic_params_scope(db, self.into())
|
2020-02-29 20:24:40 +00:00
|
|
|
.push_impl_def_scope(self)
|
2019-12-08 11:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasResolver for DefWithBodyId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-08 11:16:57 +00:00
|
|
|
match self {
|
|
|
|
DefWithBodyId::ConstId(c) => c.resolver(db),
|
|
|
|
DefWithBodyId::FunctionId(f) => f.resolver(db),
|
|
|
|
DefWithBodyId::StaticId(s) => s.resolver(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:07:23 +00:00
|
|
|
impl HasResolver for ContainerId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-20 11:07:23 +00:00
|
|
|
match self {
|
|
|
|
ContainerId::ModuleId(it) => it.resolver(db),
|
2019-12-29 13:46:24 +00:00
|
|
|
ContainerId::DefWithBodyId(it) => it.module(db).resolver(db),
|
2019-12-20 11:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 10:59:50 +00:00
|
|
|
impl HasResolver for AssocContainerId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-20 18:55:33 +00:00
|
|
|
match self {
|
2019-12-20 11:07:23 +00:00
|
|
|
AssocContainerId::ContainerId(it) => it.resolver(db),
|
2019-12-20 10:59:50 +00:00
|
|
|
AssocContainerId::TraitId(it) => it.resolver(db),
|
|
|
|
AssocContainerId::ImplId(it) => it.resolver(db),
|
2019-11-20 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:13:49 +00:00
|
|
|
impl HasResolver for GenericDefId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-11-20 18:55:33 +00:00
|
|
|
match self {
|
2019-11-21 11:13:49 +00: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 18:55:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 11:16:57 +00:00
|
|
|
impl HasResolver for VariantId {
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
|
2019-12-08 11:16:57 +00: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 11:13:49 +00:00
|
|
|
}
|
|
|
|
}
|