Move resolver to hir_def

This commit is contained in:
Aleksey Kladov 2019-11-21 15:39:09 +03:00
parent 00684d708b
commit 6d64798a23
14 changed files with 63 additions and 77 deletions

View file

@ -11,6 +11,7 @@ use hir_def::{
body::scope::ExprScopes,
builtin_type::BuiltinType,
nameres::per_ns::PerNs,
resolver::{HasResolver, TypeNs},
traits::TraitData,
type_ref::{Mutability, TypeRef},
ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
@ -31,7 +32,6 @@ use crate::{
AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId,
TypeAliasId,
},
resolve::{HasResolver, TypeNs},
ty::{InferenceResult, Namespace, TraitRef},
Either, HasSource, ImportId, Name, Source, Ty,
};

View file

@ -2,7 +2,7 @@
use std::sync::Arc;
use hir_def::path::known;
use hir_def::{path::known, resolver::HasResolver};
use hir_expand::diagnostics::DiagnosticSink;
use ra_syntax::ast;
use ra_syntax::AstPtr;
@ -11,7 +11,6 @@ use rustc_hash::FxHashSet;
use crate::{
db::HirDatabase,
diagnostics::{MissingFields, MissingOkInTailExpr},
resolve::HasResolver,
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
Adt, Function, Name, Path,
};

View file

@ -1,11 +1,10 @@
//! FIXME: write short doc here
use hir_def::{type_ref::TypeRef, AstItemDef};
use ra_syntax::ast::{self};
use hir_def::{resolver::HasResolver, type_ref::TypeRef, AstItemDef};
use ra_syntax::ast;
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
resolve::HasResolver,
ty::Ty,
AssocItem, Crate, HasSource, ImplBlock, Module, Source, TraitRef,
};

View file

@ -38,7 +38,6 @@ mod impl_block;
mod expr;
mod lang_item;
pub mod generics;
mod resolve;
pub mod diagnostics;
mod util;
@ -52,8 +51,6 @@ mod test_db;
#[cfg(test)]
mod marks;
use crate::resolve::Resolver;
pub use crate::{
code_model::{
attrs::{AttrDef, Attrs},

View file

@ -10,6 +10,7 @@ use std::sync::Arc;
use hir_def::{
expr::{ExprId, PatId},
path::known,
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
DefWithBodyId,
};
use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
@ -24,11 +25,10 @@ use crate::{
db::HirDatabase,
expr::{BodySourceMap, ExprScopes, ScopeId},
ids::LocationCtx,
resolve::{self, resolver_for_scope, HasResolver, TypeNs, ValueNs},
ty::method_resolution::{self, implements_trait},
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef,
Static, Struct, Trait, Ty, TypeAlias,
GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, ScopeDef, Static,
Struct, Trait, Ty, TypeAlias,
};
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@ -317,14 +317,14 @@ impl SourceAnalyzer {
pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
self.resolver.process_all_names(db, &mut |name, def| {
let def = match def {
resolve::ScopeDef::PerNs(it) => it.into(),
resolve::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
resolve::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
resolve::ScopeDef::GenericParam(idx) => {
resolver::ScopeDef::PerNs(it) => it.into(),
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
resolver::ScopeDef::GenericParam(idx) => {
let parent = self.resolver.generic_def().unwrap().into();
ScopeDef::GenericParam(GenericParam { parent, idx })
}
resolve::ScopeDef::Local(pat_id) => {
resolver::ScopeDef::Local(pat_id) => {
let parent = self.resolver.body_owner().unwrap().into();
ScopeDef::Local(Local { parent, pat_id })
}

View file

@ -5,11 +5,12 @@
use std::iter::successors;
use hir_def::resolver::Resolver;
use hir_expand::name;
use log::{info, warn};
use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk};
use crate::{db::HirDatabase, generics::HasGenericParams, Resolver};
use crate::{db::HirDatabase, generics::HasGenericParams};
const AUTODEREF_RECURSION_LIMIT: usize = 10;

View file

@ -23,6 +23,7 @@ use rustc_hash::FxHashMap;
use hir_def::{
path::known,
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{Mutability, TypeRef},
AdtId, DefWithBodyId,
};
@ -41,7 +42,6 @@ use crate::{
code_model::TypeAlias,
db::HirDatabase,
expr::{BindingAnnotation, Body, ExprId, PatId},
resolve::{HasResolver, Resolver, TypeNs},
ty::infer::diagnostics::InferenceDiagnostic,
Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
StructField, Trait, VariantDef,

View file

@ -4,19 +4,19 @@
//!
//! See: https://doc.rust-lang.org/nomicon/coercions.html
use hir_def::resolver::Resolver;
use rustc_hash::FxHashMap;
use test_utils::tested_by;
use super::{InferTy, InferenceContext, TypeVarValue};
use crate::{
db::HirDatabase,
lang_item::LangItemTarget,
resolve::Resolver,
ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk},
Adt, Mutability,
};
use super::{InferTy, InferenceContext, TypeVarValue};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
/// Unify two types, but may coerce the first one to the second one
/// using "implicit coercion rules" if needed.

View file

@ -6,15 +6,14 @@ use std::sync::Arc;
use hir_def::{
builtin_type::Signedness,
path::{GenericArg, GenericArgs},
resolver::resolver_for_expr,
};
use hir_expand::name;
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
use crate::{
db::HirDatabase,
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
generics::{GenericParams, HasGenericParams},
resolve::resolver_for_expr,
ty::{
autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace,
Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
@ -23,6 +22,8 @@ use crate::{
Adt, Name,
};
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let ty = self.infer_expr_inner(tgt_expr, expected);

View file

@ -1,16 +1,19 @@
//! Path expression resolution.
use hir_def::path::PathSegment;
use hir_def::{
path::PathSegment,
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
};
use super::{ExprOrPatId, InferenceContext, TraitRef};
use crate::{
db::HirDatabase,
generics::HasGenericParams,
resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
AssocItem, Container, Function, Name, Path,
};
use super::{ExprOrPatId, InferenceContext, TraitRef};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub(super) fn infer_path(
&mut self,

View file

@ -11,6 +11,7 @@ use std::sync::Arc;
use hir_def::{
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
path::{GenericArg, PathSegment},
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TypeBound, TypeRef},
GenericDefId,
};
@ -23,7 +24,6 @@ use crate::{
db::HirDatabase,
generics::HasGenericParams,
generics::{GenericDef, WherePredicate},
resolve::{HasResolver, Resolver, TypeNs},
ty::{
primitive::{FloatTy, IntTy, Uncertain},
Adt,

View file

@ -5,11 +5,11 @@
use std::sync::Arc;
use arrayvec::ArrayVec;
use hir_def::resolver::Resolver;
use rustc_hash::FxHashMap;
use crate::{
db::HirDatabase,
resolve::Resolver,
ty::primitive::{FloatBitness, Uncertain},
ty::{Ty, TypeCtor},
AssocItem, Crate, Function, ImplBlock, Module, Mutability, Name, Trait,

View file

@ -19,6 +19,7 @@ pub mod expr;
pub mod body;
pub mod generics;
pub mod traits;
pub mod resolver;
#[cfg(test)]
mod test_db;

View file

@ -1,7 +1,14 @@
//! Name resolution.
//! Name resolution façade.
use std::sync::Arc;
use hir_def::{
use hir_expand::{
name::{self, Name},
MacroDefId,
};
use ra_db::CrateId;
use rustc_hash::FxHashSet;
use crate::{
body::scope::{ExprScopes, ScopeId},
builtin_type::BuiltinType,
db::DefDatabase2,
@ -13,15 +20,9 @@ use hir_def::{
FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, UnionId,
};
use hir_expand::{
name::{self, Name},
MacroDefId,
};
use ra_db::CrateId;
use rustc_hash::FxHashSet;
#[derive(Debug, Clone, Default)]
pub(crate) struct Resolver {
pub struct Resolver {
scopes: Vec<Scope>,
}
@ -54,7 +55,7 @@ pub(crate) enum Scope {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum TypeNs {
pub enum TypeNs {
SelfType(ImplId),
GenericParam(u32),
AdtId(AdtId),
@ -69,13 +70,13 @@ pub(crate) enum TypeNs {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ResolveValueResult {
pub enum ResolveValueResult {
ValueNs(ValueNs),
Partial(TypeNs, usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ValueNs {
pub enum ValueNs {
LocalBinding(PatId),
FunctionId(FunctionId),
ConstId(ConstId),
@ -86,11 +87,7 @@ pub(crate) enum ValueNs {
impl Resolver {
/// Resolve known trait from std, like `std::futures::Future`
pub(crate) fn resolve_known_trait(
&self,
db: &impl DefDatabase2,
path: &Path,
) -> Option<TraitId> {
pub 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),
@ -99,11 +96,7 @@ impl Resolver {
}
/// Resolve known struct from std, like `std::boxed::Box`
pub(crate) fn resolve_known_struct(
&self,
db: &impl DefDatabase2,
path: &Path,
) -> Option<StructId> {
pub fn resolve_known_struct(&self, db: &impl DefDatabase2, path: &Path) -> Option<StructId> {
let res = self.resolve_module_path(db, path).take_types()?;
match res {
ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
@ -112,7 +105,7 @@ impl Resolver {
}
/// Resolve known enum from std, like `std::result::Result`
pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option<EnumId> {
pub 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),
@ -121,7 +114,7 @@ impl Resolver {
}
/// pub only for source-binder
pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs {
pub 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(),
@ -133,7 +126,7 @@ impl Resolver {
module_res
}
pub(crate) fn resolve_path_in_type_ns(
pub fn resolve_path_in_type_ns(
&self,
db: &impl DefDatabase2,
path: &Path,
@ -189,7 +182,7 @@ impl Resolver {
None
}
pub(crate) fn resolve_path_in_type_ns_fully(
pub fn resolve_path_in_type_ns_fully(
&self,
db: &impl DefDatabase2,
path: &Path,
@ -201,7 +194,7 @@ impl Resolver {
Some(res)
}
pub(crate) fn resolve_path_in_value_ns<'p>(
pub fn resolve_path_in_value_ns<'p>(
&self,
db: &impl DefDatabase2,
path: &'p Path,
@ -301,7 +294,7 @@ impl Resolver {
None
}
pub(crate) fn resolve_path_in_value_ns_fully(
pub fn resolve_path_in_value_ns_fully(
&self,
db: &impl DefDatabase2,
path: &Path,
@ -312,26 +305,18 @@ impl Resolver {
}
}
pub(crate) fn resolve_path_as_macro(
&self,
db: &impl DefDatabase2,
path: &Path,
) -> Option<MacroDefId> {
pub fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> {
let (item_map, module) = self.module()?;
item_map.resolve_path(db, module, path).0.get_macros()
}
pub(crate) fn process_all_names(
&self,
db: &impl DefDatabase2,
f: &mut dyn FnMut(Name, ScopeDef),
) {
pub fn process_all_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) {
for scope in self.scopes.iter().rev() {
scope.process_names(db, f);
}
}
pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet<TraitId> {
pub 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 {
@ -353,11 +338,11 @@ impl Resolver {
})
}
pub(crate) fn krate(&self) -> Option<CrateId> {
pub fn krate(&self) -> Option<CrateId> {
self.module().map(|t| t.0.krate())
}
pub(crate) fn where_predicates_in_scope<'a>(
pub fn where_predicates_in_scope<'a>(
&'a self,
) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a {
self.scopes
@ -369,14 +354,14 @@ impl Resolver {
.flat_map(|params| params.where_predicates.iter())
}
pub(crate) fn generic_def(&self) -> Option<GenericDefId> {
pub fn generic_def(&self) -> Option<GenericDefId> {
self.scopes.iter().find_map(|scope| match scope {
Scope::GenericParams { def, .. } => Some(*def),
_ => None,
})
}
pub(crate) fn body_owner(&self) -> Option<DefWithBodyId> {
pub fn body_owner(&self) -> Option<DefWithBodyId> {
self.scopes.iter().find_map(|scope| match scope {
Scope::ExprScope(it) => Some(it.owner),
_ => None,
@ -425,7 +410,7 @@ impl Resolver {
}
}
pub(crate) enum ScopeDef {
pub enum ScopeDef {
PerNs(PerNs),
ImplSelfType(ImplId),
AdtSelfType(AdtId),
@ -481,7 +466,7 @@ impl Scope {
}
// needs arbitrary_self_types to be a method... or maybe move to the def?
pub(crate) fn resolver_for_expr(
pub fn resolver_for_expr(
db: &impl DefDatabase2,
owner: DefWithBodyId,
expr_id: ExprId,
@ -490,7 +475,7 @@ pub(crate) fn resolver_for_expr(
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
}
pub(crate) fn resolver_for_scope(
pub fn resolver_for_scope(
db: &impl DefDatabase2,
owner: DefWithBodyId,
scope_id: Option<ScopeId>,
@ -504,7 +489,7 @@ pub(crate) fn resolver_for_scope(
r
}
pub(crate) trait HasResolver {
pub trait HasResolver {
/// Builds a resolver for type references inside this def.
fn resolver(self, db: &impl DefDatabase2) -> Resolver;
}
@ -600,7 +585,7 @@ impl HasResolver for ContainerId {
}
impl HasResolver for GenericDefId {
fn resolver(self, db: &impl DefDatabase2) -> crate::Resolver {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
match self {
GenericDefId::FunctionId(inner) => inner.resolver(db),
GenericDefId::AdtId(adt) => adt.resolver(db),