rust-analyzer/crates/ra_hir_def/src/resolver.rs

579 lines
20 KiB
Rust
Raw Normal View History

2019-11-21 12:39:09 +00:00
//! Name resolution façade.
2019-01-19 20:23:26 +00:00
use std::sync::Arc;
2019-11-21 12:39:09 +00:00
use hir_expand::{
name::{self, Name},
MacroDefId,
};
use ra_db::CrateId;
use rustc_hash::FxHashSet;
use crate::{
2019-11-21 12:24:51 +00:00
body::scope::{ExprScopes, ScopeId},
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,
2019-11-23 13:53:16 +00:00
nameres::CrateDefMap,
2019-10-30 14:24:36 +00:00
path::{Path, PathKind},
2019-11-23 13:53:16 +00:00
per_ns::PerNs,
2019-11-23 13:49:53 +00:00
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
2019-11-24 19:47:58 +00:00
GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
StructId, TraitId, TypeAliasId,
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-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> },
/// Brings `Self` in `impl` block into scope
2019-11-21 09:21:46 +00:00
ImplBlockScope(ImplId),
/// 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),
}
#[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),
GenericParam(u32),
2019-11-21 09:21:46 +00:00
AdtId(AdtId),
AdtSelfType(AdtId),
EnumVariantId(EnumVariantId),
TypeAliasId(TypeAliasId),
BuiltinType(BuiltinType),
2019-11-21 09:21:46 +00:00
TraitId(TraitId),
// Module belong to type ns, but the resolver is used when all module paths
// are fully resolved.
2019-11-21 09:21:46 +00:00
// ModuleId(ModuleId)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-11-21 12:39:09 +00:00
pub enum ResolveValueResult {
ValueNs(ValueNs),
Partial(TypeNs, usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-11-21 12:39:09 +00:00
pub enum ValueNs {
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 {
/// Resolve known trait from std, like `std::futures::Future`
2019-11-23 11:44:43 +00:00
pub fn resolve_known_trait(&self, db: &impl DefDatabase, path: &Path) -> Option<TraitId> {
let res = self.resolve_module_path(db, path).take_types()?;
match res {
2019-11-21 09:21:46 +00:00
ModuleDefId::TraitId(it) => Some(it),
_ => None,
}
}
/// Resolve known struct from std, like `std::boxed::Box`
2019-11-23 11:44:43 +00:00
pub fn resolve_known_struct(&self, db: &impl DefDatabase, path: &Path) -> Option<StructId> {
let res = self.resolve_module_path(db, path).take_types()?;
match res {
2019-11-21 09:21:46 +00:00
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
_ => None,
}
}
/// Resolve known enum from std, like `std::result::Result`
2019-11-23 11:44:43 +00:00
pub fn resolve_known_enum(&self, db: &impl DefDatabase, path: &Path) -> Option<EnumId> {
let res = self.resolve_module_path(db, path).take_types()?;
match res {
2019-11-21 09:21:46 +00:00
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
_ => None,
}
}
/// pub only for source-binder
2019-11-23 11:44:43 +00:00
pub fn resolve_module_path(&self, db: &impl DefDatabase, path: &Path) -> PerNs {
let (item_map, module) = match self.module() {
Some(it) => it,
None => return PerNs::none(),
};
let (module_res, segment_index) = item_map.resolve_path(db, module, path);
if segment_index.is_some() {
return PerNs::none();
}
module_res
}
2019-11-21 12:39:09 +00:00
pub fn resolve_path_in_type_ns(
&self,
2019-11-23 11:44:43 +00:00
db: &impl DefDatabase,
path: &Path,
) -> Option<(TypeNs, Option<usize>)> {
if path.is_type_relative() {
return None;
}
let first_name = &path.segments.first()?.name;
let skip_to_mod = path.kind != PathKind::Plain;
2019-01-19 20:23:26 +00:00
for scope in self.scopes.iter().rev() {
match scope {
Scope::ExprScope(_) => continue,
2019-11-20 17:50:34 +00:00
Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue,
2019-11-20 17:50:34 +00:00
Scope::GenericParams { params, .. } => {
if let Some(param) = params.find_by_name(first_name) {
let idx = if path.segments.len() == 1 { None } else { Some(1) };
return Some((TypeNs::GenericParam(param.idx), idx));
}
}
Scope::ImplBlockScope(impl_) => {
2019-10-30 14:19:30 +00:00
if first_name == &name::SELF_TYPE {
let idx = if path.segments.len() == 1 { None } else { Some(1) };
return Some((TypeNs::SelfType(*impl_), idx));
}
}
Scope::AdtScope(adt) => {
2019-10-30 14:19:30 +00:00
if first_name == &name::SELF_TYPE {
let idx = if path.segments.len() == 1 { None } else { Some(1) };
return Some((TypeNs::AdtSelfType(*adt), idx));
}
}
Scope::ModuleScope(m) => {
let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
let res = match module_def.take_types()? {
2019-11-21 09:21:46 +00:00
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
2019-11-21 09:21:46 +00:00
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
2019-10-31 15:45:10 +00:00
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
2019-11-21 09:21:46 +00:00
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
2019-10-31 15:45:10 +00:00
ModuleDefId::FunctionId(_)
| ModuleDefId::ConstId(_)
| ModuleDefId::StaticId(_)
| ModuleDefId::ModuleId(_) => return None,
};
return Some((res, idx));
}
2019-01-19 20:23:26 +00:00
}
}
None
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,
2019-11-23 11:44:43 +00:00
db: &impl DefDatabase,
path: &Path,
) -> 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-11-21 12:39:09 +00:00
pub fn resolve_path_in_value_ns<'p>(
&self,
2019-11-23 11:44:43 +00:00
db: &impl DefDatabase,
2019-09-11 18:01:07 +00:00
path: &'p Path,
) -> Option<ResolveValueResult> {
if path.is_type_relative() {
return None;
2019-09-11 18:01:07 +00:00
}
let n_segments = path.segments.len();
2019-10-30 14:19:30 +00:00
let tmp = name::SELF_PARAM;
let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name };
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
for scope in self.scopes.iter().rev() {
match scope {
Scope::AdtScope(_)
| Scope::ExprScope(_)
2019-11-20 17:50:34 +00:00
| Scope::GenericParams { .. }
| Scope::ImplBlockScope(_)
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())));
}
}
Scope::ExprScope(_) => continue,
2019-11-20 17:50:34 +00:00
Scope::GenericParams { params, .. } if n_segments > 1 => {
if let Some(param) = params.find_by_name(first_name) {
let ty = TypeNs::GenericParam(param.idx);
2019-09-11 18:01:07 +00:00
return Some(ResolveValueResult::Partial(ty, 1));
}
}
2019-11-20 17:50:34 +00:00
Scope::GenericParams { .. } => continue,
Scope::ImplBlockScope(impl_) if n_segments > 1 => {
2019-10-30 14:19:30 +00:00
if first_name == &name::SELF_TYPE {
let ty = TypeNs::SelfType(*impl_);
2019-09-11 18:01:07 +00:00
return Some(ResolveValueResult::Partial(ty, 1));
}
}
Scope::AdtScope(adt) if n_segments > 1 => {
2019-10-30 14:19:30 +00:00
if first_name == &name::SELF_TYPE {
let ty = TypeNs::AdtSelfType(*adt);
return Some(ResolveValueResult::Partial(ty, 1));
}
}
Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue,
Scope::ModuleScope(m) => {
let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
return match idx {
None => {
let value = match module_def.take_values()? {
2019-11-21 10:32:03 +00:00
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),
2019-10-31 15:45:10 +00:00
ModuleDefId::AdtId(AdtId::EnumId(_))
| ModuleDefId::AdtId(AdtId::UnionId(_))
| ModuleDefId::TraitId(_)
| ModuleDefId::TypeAliasId(_)
| ModuleDefId::BuiltinType(_)
| ModuleDefId::ModuleId(_) => return None,
};
2019-09-11 18:01:07 +00:00
Some(ResolveValueResult::ValueNs(value))
}
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-11 18:01:07 +00:00
Some(ResolveValueResult::Partial(ty, idx))
}
};
}
}
2019-01-26 21:52:04 +00:00
}
None
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,
2019-11-23 11:44:43 +00:00
db: &impl DefDatabase,
2019-06-08 15:38:14 +00:00
path: &Path,
) -> Option<ValueNs> {
match self.resolve_path_in_value_ns(db, path)? {
2019-09-11 18:01:07 +00:00
ResolveValueResult::ValueNs(it) => Some(it),
ResolveValueResult::Partial(..) => None,
}
}
2019-11-23 11:44:43 +00:00
pub fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &Path) -> Option<MacroDefId> {
let (item_map, module) = self.module()?;
2019-11-24 14:00:10 +00:00
item_map.resolve_path(db, module, path).0.take_macros()
}
2019-11-23 11:44:43 +00:00
pub fn process_all_names(&self, db: &impl DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
2019-02-01 22:06:57 +00:00
for scope in self.scopes.iter().rev() {
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
2019-11-23 11:44:43 +00:00
pub fn traits_in_scope(&self, db: &impl DefDatabase) -> FxHashSet<TraitId> {
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-21 09:21:46 +00:00
traits.extend(prelude_def_map[prelude.module_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());
}
}
traits
2019-03-24 16:36:15 +00:00
}
2019-11-23 13:49:53 +00:00
fn module(&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-11-21 12:39:09 +00:00
pub fn krate(&self) -> Option<CrateId> {
2019-11-24 15:05:12 +00:00
self.module().map(|t| t.0.krate)
}
2019-11-21 12:39:09 +00:00
pub fn where_predicates_in_scope<'a>(
&'a self,
) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a {
self.scopes
.iter()
.filter_map(|scope| match scope {
2019-11-20 17:50:34 +00:00
Scope::GenericParams { params, .. } => Some(params),
_ => None,
})
.flat_map(|params| params.where_predicates.iter())
}
2019-11-21 12:39:09 +00:00
pub fn generic_def(&self) -> Option<GenericDefId> {
self.scopes.iter().find_map(|scope| match scope {
2019-11-20 17:50:34 +00:00
Scope::GenericParams { def, .. } => Some(*def),
_ => None,
})
}
2019-11-21 12:39:09 +00:00
pub fn body_owner(&self) -> Option<DefWithBodyId> {
self.scopes.iter().find_map(|scope| match scope {
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 {
PerNs(PerNs),
ImplSelfType(ImplId),
AdtSelfType(AdtId),
GenericParam(u32),
Local(PatId),
}
impl Scope {
2019-11-23 11:44:43 +00:00
fn process_names(&self, db: &impl 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-03-14 09:54:03 +00:00
m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| {
f(name.clone(), ScopeDef::PerNs(res.def));
2019-02-08 11:49:43 +00:00
});
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_)));
});
2019-11-24 15:05:12 +00:00
m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| {
f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into())));
2019-02-04 21:09:56 +00:00
});
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);
prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
f(name.clone(), ScopeDef::PerNs(res.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
}
Scope::GenericParams { params, .. } => {
2019-11-20 17:50:34 +00:00
for param in params.params.iter() {
f(param.name.clone(), ScopeDef::GenericParam(param.idx))
2019-01-27 19:50:57 +00:00
}
}
Scope::ImplBlockScope(i) => {
2019-11-21 09:21:46 +00:00
f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into()));
}
Scope::AdtScope(i) => {
2019-11-21 09:21:46 +00:00
f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into()));
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| {
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?
2019-11-23 11:44:43 +00:00
pub fn resolver_for_expr(db: &impl 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(
2019-11-23 11:44:43 +00:00
db: &impl 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-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
}
2019-11-23 11:44:43 +00:00
fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver {
2019-11-21 13:00:57 +00:00
let params = db.generic_params(def);
if params.params.is_empty() {
self
} else {
self.push_scope(Scope::GenericParams { def, params })
}
}
fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
self.push_scope(Scope::ImplBlockScope(impl_block))
}
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 }))
}
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-21 12:39:09 +00:00
pub trait HasResolver {
2019-11-20 18:55:33 +00:00
/// Builds a resolver for type references inside this def.
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver;
2019-11-20 18:55:33 +00:00
}
2019-11-21 11:13:49 +00:00
impl HasResolver for ModuleId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver {
2019-11-21 11:13:49 +00:00
let def_map = db.crate_def_map(self.krate);
Resolver::default().push_module_scope(def_map, self.module_id)
2019-11-20 18:55:33 +00:00
}
}
2019-11-21 11:13:49 +00:00
impl HasResolver for TraitId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver {
2019-11-21 11:13:49 +00:00
self.module(db).resolver(db).push_generic_params_scope(db, self.into())
2019-11-20 18:55:33 +00:00
}
}
2019-11-21 12:56:27 +00:00
impl<T: Into<AdtId>> HasResolver for T {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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 {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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 DefWithBodyId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver {
2019-11-20 18:55:33 +00:00
match self {
2019-11-21 11:13:49 +00:00
DefWithBodyId::ConstId(c) => c.resolver(db),
DefWithBodyId::FunctionId(f) => f.resolver(db),
DefWithBodyId::StaticId(s) => s.resolver(db),
2019-11-20 18:55:33 +00:00
}
}
}
2019-11-21 11:13:49 +00:00
impl HasResolver for ConstId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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 {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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 {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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-11-21 11:13:49 +00:00
impl HasResolver for ContainerId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver {
2019-11-20 18:55:33 +00:00
match self {
2019-11-21 11:13:49 +00:00
ContainerId::TraitId(it) => it.resolver(db),
ContainerId::ImplId(it) => it.resolver(db),
ContainerId::ModuleId(it) => it.resolver(db),
2019-11-20 18:55:33 +00:00
}
}
}
2019-11-21 11:13:49 +00:00
impl HasResolver for GenericDefId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl 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-11-21 11:13:49 +00:00
impl HasResolver for ImplId {
2019-11-23 11:44:43 +00:00
fn resolver(self, db: &impl DefDatabase) -> Resolver {
2019-11-20 18:55:33 +00:00
self.module(db)
.resolver(db)
2019-11-21 11:13:49 +00:00
.push_generic_params_scope(db, self.into())
.push_impl_block_scope(self)
}
}