mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Decouple Resolver
This commit is contained in:
parent
612a72fc4e
commit
0102fb4133
10 changed files with 188 additions and 108 deletions
|
@ -12,8 +12,8 @@ use hir_def::{
|
|||
builtin_type::BuiltinType,
|
||||
traits::TraitData,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId,
|
||||
LocalStructFieldId, Lookup, ModuleId, UnionId,
|
||||
ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
|
||||
ModuleId, UnionId,
|
||||
};
|
||||
use hir_expand::{
|
||||
diagnostics::DiagnosticSink,
|
||||
|
@ -842,9 +842,10 @@ impl Trait {
|
|||
_ => None,
|
||||
})
|
||||
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) {
|
||||
Some(TypeNs::Trait(t)) => Some(t),
|
||||
Some(TypeNs::TraitId(t)) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.map(Trait::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
@ -871,14 +872,9 @@ impl Trait {
|
|||
|
||||
pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
|
||||
let trait_data = self.trait_data(db);
|
||||
trait_data
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
AssocItemId::TypeAliasId(t) => Some(TypeAlias::from(*t)),
|
||||
_ => None,
|
||||
})
|
||||
.find(|t| &t.name(db) == name)
|
||||
let res =
|
||||
trait_data.associated_types().map(TypeAlias::from).find(|t| &t.name(db) == name)?;
|
||||
Some(res)
|
||||
}
|
||||
|
||||
pub fn associated_type_by_name_including_super_traits(
|
||||
|
|
|
@ -46,7 +46,7 @@ pub(crate) fn resolver_for_scope(
|
|||
let scopes = owner.expr_scopes(db);
|
||||
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
||||
for scope in scope_chain.into_iter().rev() {
|
||||
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
|
||||
r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope);
|
||||
}
|
||||
r
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
|||
_ => return,
|
||||
};
|
||||
|
||||
let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum));
|
||||
let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum.into()));
|
||||
let params = match &mismatch.expected {
|
||||
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
|
||||
_ => return,
|
||||
|
|
|
@ -3,9 +3,14 @@
|
|||
//! It's unclear if we need this long-term, but it's definitelly useful while we
|
||||
//! are splitting the hir.
|
||||
|
||||
use hir_def::{AdtId, AssocItemId, DefWithBodyId, EnumVariantId, GenericDefId, ModuleDefId};
|
||||
use hir_def::{
|
||||
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId,
|
||||
TypeAliasId, UnionId,
|
||||
};
|
||||
|
||||
use crate::{Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef};
|
||||
use crate::{
|
||||
ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias,
|
||||
};
|
||||
|
||||
macro_rules! from_id {
|
||||
($(($id:path, $ty:path)),*) => {$(
|
||||
|
@ -83,6 +88,16 @@ impl From<DefWithBody> for DefWithBodyId {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<DefWithBodyId> for DefWithBody {
|
||||
fn from(def: DefWithBodyId) -> Self {
|
||||
match def {
|
||||
DefWithBodyId::FunctionId(it) => DefWithBody::Function(it.into()),
|
||||
DefWithBodyId::StaticId(it) => DefWithBody::Static(it.into()),
|
||||
DefWithBodyId::ConstId(it) => DefWithBody::Const(it.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AssocItemId> for AssocItem {
|
||||
fn from(def: AssocItemId) -> Self {
|
||||
match def {
|
||||
|
@ -122,3 +137,49 @@ impl From<GenericDefId> for GenericDef {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AdtId> for TypableDef {
|
||||
fn from(id: AdtId) -> Self {
|
||||
Adt::from(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StructId> for TypableDef {
|
||||
fn from(id: StructId) -> Self {
|
||||
AdtId::StructId(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UnionId> for TypableDef {
|
||||
fn from(id: UnionId) -> Self {
|
||||
AdtId::UnionId(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EnumId> for TypableDef {
|
||||
fn from(id: EnumId) -> Self {
|
||||
AdtId::EnumId(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EnumVariantId> for TypableDef {
|
||||
fn from(id: EnumVariantId) -> Self {
|
||||
EnumVariant::from(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TypeAliasId> for TypableDef {
|
||||
fn from(id: TypeAliasId) -> Self {
|
||||
TypeAlias::from(id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Adt> for GenericDefId {
|
||||
fn from(id: Adt) -> Self {
|
||||
match id {
|
||||
Adt::Struct(it) => it.id.into(),
|
||||
Adt::Union(it) => it.id.into(),
|
||||
Adt::Enum(it) => it.id.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,19 +3,21 @@ use std::sync::Arc;
|
|||
|
||||
use hir_def::{
|
||||
builtin_type::BuiltinType,
|
||||
db::DefDatabase2,
|
||||
generics::GenericParams,
|
||||
nameres::CrateDefMap,
|
||||
path::{Path, PathKind},
|
||||
AdtId, CrateModuleId, ModuleDefId,
|
||||
AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId,
|
||||
StructId, TraitId, TypeAliasId,
|
||||
};
|
||||
use hir_expand::name::{self, Name};
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
use crate::{
|
||||
code_model::Crate,
|
||||
db::{DefDatabase, HirDatabase},
|
||||
db::DefDatabase,
|
||||
expr::{ExprScopes, PatId, ScopeId},
|
||||
generics::{GenericParams, HasGenericParams},
|
||||
Adt, Const, Container, DefWithBody, Enum, EnumVariant, Function, GenericDef, ImplBlock, Local,
|
||||
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local,
|
||||
MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
|
||||
};
|
||||
|
||||
|
@ -33,7 +35,7 @@ pub(crate) struct ModuleItemMap {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ExprScope {
|
||||
owner: DefWithBody,
|
||||
owner: DefWithBodyId,
|
||||
expr_scopes: Arc<ExprScopes>,
|
||||
scope_id: ScopeId,
|
||||
}
|
||||
|
@ -43,28 +45,28 @@ pub(crate) enum Scope {
|
|||
/// All the items and imported names of a module
|
||||
ModuleScope(ModuleItemMap),
|
||||
/// Brings the generic parameters of an item into scope
|
||||
GenericParams { def: GenericDef, params: Arc<GenericParams> },
|
||||
GenericParams { def: GenericDefId, params: Arc<GenericParams> },
|
||||
/// Brings `Self` in `impl` block into scope
|
||||
ImplBlockScope(ImplBlock),
|
||||
ImplBlockScope(ImplId),
|
||||
/// Brings `Self` in enum, struct and union definitions into scope
|
||||
AdtScope(Adt),
|
||||
AdtScope(AdtId),
|
||||
/// Local bindings
|
||||
ExprScope(ExprScope),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub(crate) enum TypeNs {
|
||||
SelfType(ImplBlock),
|
||||
SelfType(ImplId),
|
||||
GenericParam(u32),
|
||||
Adt(Adt),
|
||||
AdtSelfType(Adt),
|
||||
EnumVariant(EnumVariant),
|
||||
TypeAlias(TypeAlias),
|
||||
AdtId(AdtId),
|
||||
AdtSelfType(AdtId),
|
||||
EnumVariantId(EnumVariantId),
|
||||
TypeAliasId(TypeAliasId),
|
||||
BuiltinType(BuiltinType),
|
||||
Trait(Trait),
|
||||
TraitId(TraitId),
|
||||
// Module belong to type ns, but the resolver is used when all module paths
|
||||
// are fully resolved.
|
||||
// Module(Module)
|
||||
// ModuleId(ModuleId)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
|
@ -85,10 +87,14 @@ pub(crate) enum ValueNs {
|
|||
|
||||
impl Resolver {
|
||||
/// Resolve known trait from std, like `std::futures::Future`
|
||||
pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option<Trait> {
|
||||
pub(crate) fn resolve_known_trait(
|
||||
&self,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<TraitId> {
|
||||
let res = self.resolve_module_path(db, path).take_types()?;
|
||||
match res {
|
||||
ModuleDefId::TraitId(it) => Some(it.into()),
|
||||
ModuleDefId::TraitId(it) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -96,27 +102,27 @@ impl Resolver {
|
|||
/// Resolve known struct from std, like `std::boxed::Box`
|
||||
pub(crate) fn resolve_known_struct(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<Struct> {
|
||||
) -> Option<StructId> {
|
||||
let res = self.resolve_module_path(db, path).take_types()?;
|
||||
match res {
|
||||
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it.into()),
|
||||
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve known enum from std, like `std::result::Result`
|
||||
pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> {
|
||||
pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option<EnumId> {
|
||||
let res = self.resolve_module_path(db, path).take_types()?;
|
||||
match res {
|
||||
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it.into()),
|
||||
ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// pub only for source-binder
|
||||
pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs {
|
||||
pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs {
|
||||
let (item_map, module) = match self.module() {
|
||||
Some(it) => it,
|
||||
None => return PerNs::none(),
|
||||
|
@ -130,7 +136,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn resolve_path_in_type_ns(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<(TypeNs, Option<usize>)> {
|
||||
if path.is_type_relative() {
|
||||
|
@ -164,13 +170,13 @@ impl Resolver {
|
|||
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()? {
|
||||
ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()),
|
||||
ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariant(it.into()),
|
||||
ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
|
||||
ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
|
||||
|
||||
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()),
|
||||
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
|
||||
ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
|
||||
|
||||
ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()),
|
||||
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
|
||||
|
||||
ModuleDefId::FunctionId(_)
|
||||
| ModuleDefId::ConstId(_)
|
||||
|
@ -186,7 +192,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn resolve_path_in_type_ns_fully(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<TypeNs> {
|
||||
let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?;
|
||||
|
@ -198,7 +204,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn resolve_path_in_value_ns<'p>(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &'p Path,
|
||||
) -> Option<ResolveValueResult> {
|
||||
if path.is_type_relative() {
|
||||
|
@ -278,9 +284,9 @@ impl Resolver {
|
|||
}
|
||||
Some(idx) => {
|
||||
let ty = match module_def.take_types()? {
|
||||
ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()),
|
||||
ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()),
|
||||
ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()),
|
||||
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(_)
|
||||
|
@ -300,7 +306,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn resolve_path_in_value_ns_fully(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<ValueNs> {
|
||||
match self.resolve_path_in_value_ns(db, path)? {
|
||||
|
@ -311,7 +317,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn resolve_path_as_macro(
|
||||
&self,
|
||||
db: &impl DefDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<MacroDef> {
|
||||
let (item_map, module) = self.module()?;
|
||||
|
@ -320,7 +326,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn process_all_names(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
f: &mut dyn FnMut(Name, ScopeDef),
|
||||
) {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
|
@ -328,16 +334,15 @@ impl Resolver {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<Trait> {
|
||||
pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet<TraitId> {
|
||||
let mut traits = FxHashSet::default();
|
||||
for scope in &self.scopes {
|
||||
if let Scope::ModuleScope(m) = scope {
|
||||
if let Some(prelude) = m.crate_def_map.prelude() {
|
||||
let prelude_def_map = db.crate_def_map(prelude.krate);
|
||||
traits
|
||||
.extend(prelude_def_map[prelude.module_id].scope.traits().map(Trait::from));
|
||||
traits.extend(prelude_def_map[prelude.module_id].scope.traits());
|
||||
}
|
||||
traits.extend(m.crate_def_map[m.module_id].scope.traits().map(Trait::from));
|
||||
traits.extend(m.crate_def_map[m.module_id].scope.traits());
|
||||
}
|
||||
}
|
||||
traits
|
||||
|
@ -367,7 +372,7 @@ impl Resolver {
|
|||
.flat_map(|params| params.where_predicates.iter())
|
||||
}
|
||||
|
||||
pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> {
|
||||
pub(crate) fn generic_def(&self) -> Option<GenericDefId> {
|
||||
self.scopes.iter().find_map(|scope| match scope {
|
||||
Scope::GenericParams { def, .. } => Some(*def),
|
||||
_ => None,
|
||||
|
@ -383,10 +388,10 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn push_generic_params_scope(
|
||||
self,
|
||||
db: &impl DefDatabase,
|
||||
def: GenericDef,
|
||||
db: &impl DefDatabase2,
|
||||
def: GenericDefId,
|
||||
) -> Resolver {
|
||||
let params = def.generic_params(db);
|
||||
let params = db.generic_params(def);
|
||||
if params.params.is_empty() {
|
||||
self
|
||||
} else {
|
||||
|
@ -394,7 +399,7 @@ impl Resolver {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver {
|
||||
pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
|
||||
self.push_scope(Scope::ImplBlockScope(impl_block))
|
||||
}
|
||||
|
||||
|
@ -408,7 +413,7 @@ impl Resolver {
|
|||
|
||||
pub(crate) fn push_expr_scope(
|
||||
self,
|
||||
owner: DefWithBody,
|
||||
owner: DefWithBodyId,
|
||||
expr_scopes: Arc<ExprScopes>,
|
||||
scope_id: ScopeId,
|
||||
) -> Resolver {
|
||||
|
@ -440,7 +445,7 @@ impl From<PerNs> for ScopeDef {
|
|||
}
|
||||
|
||||
impl Scope {
|
||||
fn process_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
|
||||
fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) {
|
||||
match self {
|
||||
Scope::ModuleScope(m) => {
|
||||
// FIXME: should we provide `self` here?
|
||||
|
@ -472,14 +477,14 @@ impl Scope {
|
|||
}
|
||||
}
|
||||
Scope::ImplBlockScope(i) => {
|
||||
f(name::SELF_TYPE, ScopeDef::ImplSelfType(*i));
|
||||
f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into()));
|
||||
}
|
||||
Scope::AdtScope(i) => {
|
||||
f(name::SELF_TYPE, ScopeDef::AdtSelfType(*i));
|
||||
f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into()));
|
||||
}
|
||||
Scope::ExprScope(scope) => {
|
||||
scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
|
||||
let local = Local { parent: scope.owner, pat_id: e.pat() };
|
||||
let local = Local { parent: scope.owner.into(), pat_id: e.pat() };
|
||||
f(e.name().clone(), ScopeDef::Local(local));
|
||||
});
|
||||
}
|
||||
|
@ -501,7 +506,7 @@ impl HasResolver for Module {
|
|||
|
||||
impl HasResolver for Trait {
|
||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
||||
self.module(db).resolver(db).push_generic_params_scope(db, self.into())
|
||||
self.module(db).resolver(db).push_generic_params_scope(db, self.id.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -511,7 +516,7 @@ impl<T: Into<Adt>> HasResolver for T {
|
|||
def.module(db)
|
||||
.resolver(db)
|
||||
.push_generic_params_scope(db, def.into())
|
||||
.push_scope(Scope::AdtScope(def))
|
||||
.push_scope(Scope::AdtScope(def.into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -520,7 +525,7 @@ impl HasResolver for Function {
|
|||
self.container(db)
|
||||
.map(|c| c.resolver(db))
|
||||
.unwrap_or_else(|| self.module(db).resolver(db))
|
||||
.push_generic_params_scope(db, self.into())
|
||||
.push_generic_params_scope(db, self.id.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -551,7 +556,7 @@ impl HasResolver for TypeAlias {
|
|||
self.container(db)
|
||||
.map(|ib| ib.resolver(db))
|
||||
.unwrap_or_else(|| self.module(db).resolver(db))
|
||||
.push_generic_params_scope(db, self.into())
|
||||
.push_generic_params_scope(db, self.id.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -582,7 +587,7 @@ impl HasResolver for ImplBlock {
|
|||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
||||
self.module(db)
|
||||
.resolver(db)
|
||||
.push_generic_params_scope(db, self.into())
|
||||
.push_impl_block_scope(self)
|
||||
.push_generic_params_scope(db, self.id.into())
|
||||
.push_impl_block_scope(self.id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,9 @@ use crate::{
|
|||
ids::LocationCtx,
|
||||
resolve::{HasResolver, ScopeDef, TypeNs, ValueNs},
|
||||
ty::method_resolution::{self, implements_trait},
|
||||
AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, GenericParam, HasBody,
|
||||
HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty,
|
||||
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
|
||||
GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static,
|
||||
Struct, Trait, Ty, TypeAlias,
|
||||
};
|
||||
|
||||
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
|
||||
|
@ -240,16 +241,18 @@ impl SourceAnalyzer {
|
|||
path: &crate::Path,
|
||||
) -> Option<PathResolution> {
|
||||
let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
|
||||
TypeNs::SelfType(it) => PathResolution::SelfType(it),
|
||||
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
|
||||
TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam {
|
||||
parent: self.resolver.generic_def().unwrap(),
|
||||
parent: self.resolver.generic_def().unwrap().into(),
|
||||
idx,
|
||||
}),
|
||||
TypeNs::AdtSelfType(it) | TypeNs::Adt(it) => PathResolution::Def(it.into()),
|
||||
TypeNs::EnumVariant(it) => PathResolution::Def(it.into()),
|
||||
TypeNs::TypeAlias(it) => PathResolution::Def(it.into()),
|
||||
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
|
||||
PathResolution::Def(Adt::from(it).into())
|
||||
}
|
||||
TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
|
||||
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
|
||||
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
|
||||
TypeNs::Trait(it) => PathResolution::Def(it.into()),
|
||||
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
|
||||
});
|
||||
let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| {
|
||||
let res = match val {
|
||||
|
@ -392,7 +395,7 @@ impl SourceAnalyzer {
|
|||
let std_future_path = known::std_future_future();
|
||||
|
||||
let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
|
||||
Some(it) => it,
|
||||
Some(it) => it.into(),
|
||||
_ => return false,
|
||||
};
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ use rustc_hash::FxHashMap;
|
|||
use hir_def::{
|
||||
path::known,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
AdtId,
|
||||
};
|
||||
use hir_expand::{diagnostics::DiagnosticSink, name};
|
||||
use ra_arena::map::ArenaMap;
|
||||
|
@ -43,7 +44,7 @@ use crate::{
|
|||
resolve::{HasResolver, Resolver, TypeNs},
|
||||
ty::infer::diagnostics::InferenceDiagnostic,
|
||||
Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
|
||||
StructField, VariantDef,
|
||||
StructField, Trait, VariantDef,
|
||||
};
|
||||
|
||||
macro_rules! ty_app {
|
||||
|
@ -518,17 +519,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
// FIXME: this should resolve assoc items as well, see this example:
|
||||
// https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521
|
||||
match resolver.resolve_path_in_type_ns_fully(self.db, &path) {
|
||||
Some(TypeNs::Adt(Adt::Struct(it))) => it.into(),
|
||||
Some(TypeNs::Adt(Adt::Union(it))) => it.into(),
|
||||
Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(),
|
||||
Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(),
|
||||
Some(TypeNs::AdtSelfType(adt)) => adt.into(),
|
||||
Some(TypeNs::EnumVariant(it)) => it.into(),
|
||||
Some(TypeNs::TypeAlias(it)) => it.into(),
|
||||
Some(TypeNs::EnumVariantId(it)) => it.into(),
|
||||
Some(TypeNs::TypeAliasId(it)) => it.into(),
|
||||
|
||||
Some(TypeNs::SelfType(_)) |
|
||||
Some(TypeNs::GenericParam(_)) |
|
||||
Some(TypeNs::BuiltinType(_)) |
|
||||
Some(TypeNs::Trait(_)) |
|
||||
Some(TypeNs::Adt(Adt::Enum(_))) |
|
||||
Some(TypeNs::TraitId(_)) |
|
||||
Some(TypeNs::AdtId(AdtId::EnumId(_))) |
|
||||
None => {
|
||||
return (Ty::Unknown, None)
|
||||
}
|
||||
|
@ -576,26 +577,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
|
||||
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
|
||||
let path = known::std_iter_into_iterator();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
|
||||
trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
|
||||
let path = known::std_ops_try();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
|
||||
trait_.associated_type_by_name(self.db, &name::OK_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_future_future_output(&self) -> Option<TypeAlias> {
|
||||
let path = known::std_future_future();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
|
||||
trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_boxed_box(&self) -> Option<Adt> {
|
||||
let path = known::std_boxed_box();
|
||||
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
|
||||
Some(Adt::Struct(struct_))
|
||||
Some(Adt::Struct(struct_.into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -94,13 +94,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
let is_before_last = remaining_segments.len() == 1;
|
||||
|
||||
match (def, is_before_last) {
|
||||
(TypeNs::Trait(trait_), true) => {
|
||||
(TypeNs::TraitId(trait_), true) => {
|
||||
let segment =
|
||||
remaining_segments.last().expect("there should be at least one segment here");
|
||||
let trait_ref = TraitRef::from_resolved_path(
|
||||
self.db,
|
||||
&self.resolver,
|
||||
trait_,
|
||||
trait_.into(),
|
||||
resolved_segment,
|
||||
None,
|
||||
);
|
||||
|
|
|
@ -28,8 +28,8 @@ use crate::{
|
|||
Adt,
|
||||
},
|
||||
util::make_mut_slice,
|
||||
Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait,
|
||||
TypeAlias, Union, VariantDef,
|
||||
Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField,
|
||||
Trait, TypeAlias, Union, VariantDef,
|
||||
};
|
||||
|
||||
// FIXME: this is only really used in `type_for_def`, which contains a bunch of
|
||||
|
@ -156,9 +156,14 @@ impl Ty {
|
|||
remaining_segments: &[PathSegment],
|
||||
) -> Ty {
|
||||
let ty = match resolution {
|
||||
TypeNs::Trait(trait_) => {
|
||||
let trait_ref =
|
||||
TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None);
|
||||
TypeNs::TraitId(trait_) => {
|
||||
let trait_ref = TraitRef::from_resolved_path(
|
||||
db,
|
||||
resolver,
|
||||
trait_.into(),
|
||||
resolved_segment,
|
||||
None,
|
||||
);
|
||||
return if remaining_segments.len() == 1 {
|
||||
let segment = &remaining_segments[0];
|
||||
match trait_ref
|
||||
|
@ -189,18 +194,18 @@ impl Ty {
|
|||
let name = resolved_segment.name.clone();
|
||||
Ty::Param { idx, name }
|
||||
}
|
||||
TypeNs::SelfType(impl_block) => impl_block.target_ty(db),
|
||||
TypeNs::AdtSelfType(adt) => adt.ty(db),
|
||||
TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db),
|
||||
TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db),
|
||||
|
||||
TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
|
||||
TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
|
||||
TypeNs::BuiltinType(it) => {
|
||||
Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into())
|
||||
}
|
||||
TypeNs::TypeAlias(it) => {
|
||||
TypeNs::TypeAliasId(it) => {
|
||||
Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into())
|
||||
}
|
||||
// FIXME: report error
|
||||
TypeNs::EnumVariant(_) => return Ty::Unknown,
|
||||
TypeNs::EnumVariantId(_) => return Ty::Unknown,
|
||||
};
|
||||
|
||||
Ty::from_type_relative_path(db, resolver, ty, remaining_segments)
|
||||
|
@ -247,7 +252,7 @@ impl Ty {
|
|||
Some(def) => def,
|
||||
None => return Ty::Unknown, // this can't actually happen
|
||||
};
|
||||
let predicates = db.generic_predicates_for_param(def, param_idx);
|
||||
let predicates = db.generic_predicates_for_param(def.into(), param_idx);
|
||||
let traits_from_env = predicates.iter().filter_map(|pred| match pred {
|
||||
GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_),
|
||||
_ => None,
|
||||
|
@ -391,11 +396,11 @@ impl TraitRef {
|
|||
explicit_self_ty: Option<Ty>,
|
||||
) -> Option<Self> {
|
||||
let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? {
|
||||
TypeNs::Trait(tr) => tr,
|
||||
TypeNs::TraitId(tr) => tr,
|
||||
_ => return None,
|
||||
};
|
||||
let segment = path.segments.last().expect("path should have at least one segment");
|
||||
Some(TraitRef::from_resolved_path(db, resolver, resolved, segment, explicit_self_ty))
|
||||
Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty))
|
||||
}
|
||||
|
||||
pub(super) fn from_resolved_path(
|
||||
|
|
|
@ -224,7 +224,9 @@ fn iterate_trait_method_candidates<T>(
|
|||
.trait_predicates_for_self_ty(&ty.value)
|
||||
.map(|tr| tr.trait_)
|
||||
.flat_map(|t| t.all_super_traits(db));
|
||||
let traits = inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db));
|
||||
let traits = inherent_trait
|
||||
.chain(traits_from_env)
|
||||
.chain(resolver.traits_in_scope(db).into_iter().map(Trait::from));
|
||||
'traits: for t in traits {
|
||||
let data = t.trait_data(db);
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ use ra_syntax::ast::{self, NameOwner};
|
|||
|
||||
use crate::{
|
||||
db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
|
||||
TypeAliasLoc,
|
||||
TypeAliasId, TypeAliasLoc,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
@ -56,4 +56,11 @@ impl TraitData {
|
|||
};
|
||||
Arc::new(TraitData { name, items, auto })
|
||||
}
|
||||
|
||||
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
|
||||
self.items.iter().filter_map(|item| match item {
|
||||
AssocItemId::TypeAliasId(t) => Some(*t),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue