Move expr resolver to resolve

This commit is contained in:
Aleksey Kladov 2019-11-21 13:28:04 +03:00
parent 0102fb4133
commit 24964ca58e
4 changed files with 35 additions and 33 deletions

View file

@ -13,7 +13,7 @@ use crate::{
diagnostics::{MissingFields, MissingOkInTailExpr},
resolve::HasResolver,
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
Adt, DefWithBody, Function, HasBody, Name, Path, Resolver,
Adt, Function, Name, Path,
};
pub use hir_def::{
@ -27,30 +27,6 @@ pub use hir_def::{
},
};
// needs arbitrary_self_types to be a method... or maybe move to the def?
pub(crate) fn resolver_for_expr(
db: &impl HirDatabase,
owner: DefWithBody,
expr_id: ExprId,
) -> Resolver {
let scopes = owner.expr_scopes(db);
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
}
pub(crate) fn resolver_for_scope(
db: &impl HirDatabase,
owner: DefWithBody,
scope_id: Option<ScopeId>,
) -> Resolver {
let mut r = owner.resolver(db);
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.into(), Arc::clone(&scopes), scope);
}
r
}
pub(crate) struct ExprValidator<'a, 'b: 'a> {
func: Function,
infer: Arc<InferenceResult>,

View file

@ -4,6 +4,7 @@ use std::sync::Arc;
use hir_def::{
builtin_type::BuiltinType,
db::DefDatabase2,
expr::ExprId,
generics::GenericParams,
nameres::CrateDefMap,
path::{Path, PathKind},
@ -15,10 +16,10 @@ use rustc_hash::FxHashSet;
use crate::{
code_model::Crate,
db::DefDatabase,
db::{DefDatabase, HirDatabase},
expr::{ExprScopes, PatId, ScopeId},
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local,
MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock,
Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
};
#[derive(Debug, Clone, Default)]
@ -492,6 +493,30 @@ impl Scope {
}
}
// needs arbitrary_self_types to be a method... or maybe move to the def?
pub(crate) fn resolver_for_expr(
db: &impl HirDatabase,
owner: DefWithBody,
expr_id: ExprId,
) -> Resolver {
let scopes = owner.expr_scopes(db);
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
}
pub(crate) fn resolver_for_scope(
db: &impl HirDatabase,
owner: DefWithBody,
scope_id: Option<ScopeId>,
) -> Resolver {
let mut r = owner.resolver(db);
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.into(), Arc::clone(&scopes), scope);
}
r
}
pub(crate) trait HasResolver {
/// Builds a resolver for type references inside this def.
fn resolver(self, db: &impl DefDatabase) -> Resolver;

View file

@ -21,9 +21,9 @@ use ra_syntax::{
use crate::{
db::HirDatabase,
expr::{self, BodySourceMap, ExprScopes, ScopeId},
expr::{BodySourceMap, ExprScopes, ScopeId},
ids::LocationCtx,
resolve::{HasResolver, ScopeDef, TypeNs, ValueNs},
resolve::{resolver_for_scope, HasResolver, ScopeDef, 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, Static,
@ -160,7 +160,7 @@ impl SourceAnalyzer {
None => scope_for(&scopes, &source_map, node),
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
};
let resolver = expr::resolver_for_scope(db, def, scope);
let resolver = resolver_for_scope(db, def, scope);
SourceAnalyzer {
resolver,
body_owner: Some(def),

View file

@ -12,8 +12,9 @@ use hir_expand::name;
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
use crate::{
db::HirDatabase,
expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
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,
@ -186,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
Expr::Path(p) => {
// FIXME this could be more efficient...
let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr);
let resolver = resolver_for_expr(self.db, self.owner, tgt_expr);
self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
}
Expr::Continue => Ty::simple(TypeCtor::Never),