This commit is contained in:
Aleksey Kladov 2019-11-21 15:24:51 +03:00
parent eb53aa37a3
commit 00684d708b
8 changed files with 53 additions and 49 deletions

View File

@ -9,10 +9,16 @@ use hir_def::{
}; };
use crate::{ use crate::{
ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef, ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
ModuleDef, Static, TypeAlias, ModuleDef, Static, TypeAlias,
}; };
impl From<ra_db::CrateId> for Crate {
fn from(crate_id: ra_db::CrateId) -> Self {
Crate { crate_id }
}
}
macro_rules! from_id { macro_rules! from_id {
($(($id:path, $ty:path)),*) => {$( ($(($id:path, $ty:path)),*) => {$(
impl From<$id> for $ty { impl From<$id> for $ty {

View File

@ -2,25 +2,23 @@
use std::sync::Arc; use std::sync::Arc;
use hir_def::{ use hir_def::{
body::scope::{ExprScopes, ScopeId},
builtin_type::BuiltinType, builtin_type::BuiltinType,
db::DefDatabase2, db::DefDatabase2,
expr::ExprId, expr::{ExprId, PatId},
generics::GenericParams, generics::GenericParams,
nameres::CrateDefMap, nameres::{per_ns::PerNs, CrateDefMap},
path::{Path, PathKind}, path::{Path, PathKind},
AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, UnionId, TypeAliasId, UnionId,
}; };
use hir_expand::name::{self, Name}; use hir_expand::{
use rustc_hash::FxHashSet; name::{self, Name},
MacroDefId,
use crate::{
code_model::Crate,
db::HirDatabase,
expr::{ExprScopes, PatId, ScopeId},
DefWithBody, GenericDef, MacroDef, PerNs,
}; };
use ra_db::CrateId;
use rustc_hash::FxHashSet;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub(crate) struct Resolver { pub(crate) struct Resolver {
@ -318,9 +316,9 @@ impl Resolver {
&self, &self,
db: &impl DefDatabase2, db: &impl DefDatabase2,
path: &Path, path: &Path,
) -> Option<MacroDef> { ) -> Option<MacroDefId> {
let (item_map, module) = self.module()?; let (item_map, module) = self.module()?;
item_map.resolve_path(db, module, path).0.get_macros().map(MacroDef::from) item_map.resolve_path(db, module, path).0.get_macros()
} }
pub(crate) fn process_all_names( pub(crate) fn process_all_names(
@ -355,8 +353,8 @@ impl Resolver {
}) })
} }
pub(crate) fn krate(&self) -> Option<Crate> { pub(crate) fn krate(&self) -> Option<CrateId> {
self.module().map(|t| Crate { crate_id: t.0.krate() }) self.module().map(|t| t.0.krate())
} }
pub(crate) fn where_predicates_in_scope<'a>( pub(crate) fn where_predicates_in_scope<'a>(
@ -484,7 +482,7 @@ impl Scope {
// needs arbitrary_self_types to be a method... or maybe move to the def? // needs arbitrary_self_types to be a method... or maybe move to the def?
pub(crate) fn resolver_for_expr( pub(crate) fn resolver_for_expr(
db: &impl HirDatabase, db: &impl DefDatabase2,
owner: DefWithBodyId, owner: DefWithBodyId,
expr_id: ExprId, expr_id: ExprId,
) -> Resolver { ) -> Resolver {
@ -493,7 +491,7 @@ pub(crate) fn resolver_for_expr(
} }
pub(crate) fn resolver_for_scope( pub(crate) fn resolver_for_scope(
db: &impl HirDatabase, db: &impl DefDatabase2,
owner: DefWithBodyId, owner: DefWithBodyId,
scope_id: Option<ScopeId>, scope_id: Option<ScopeId>,
) -> Resolver { ) -> Resolver {
@ -623,15 +621,3 @@ impl HasResolver for ImplId {
.push_impl_block_scope(self) .push_impl_block_scope(self)
} }
} }
impl HasResolver for GenericDef {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
GenericDefId::from(self).resolver(db)
}
}
impl HasResolver for DefWithBody {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
DefWithBodyId::from(self).resolver(db)
}
}

View File

@ -10,6 +10,7 @@ use std::sync::Arc;
use hir_def::{ use hir_def::{
expr::{ExprId, PatId}, expr::{ExprId, PatId},
path::known, path::known,
DefWithBodyId,
}; };
use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
use ra_syntax::{ use ra_syntax::{
@ -51,7 +52,9 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
}, },
_ => match node.value.kind() { _ => match node.value.kind() {
FN_DEF | CONST_DEF | STATIC_DEF => { FN_DEF | CONST_DEF | STATIC_DEF => {
Some(def_with_body_from_child_node(db, node)?.resolver(db)) let def = def_with_body_from_child_node(db, node)?;
let def = DefWithBodyId::from(def);
Some(def.resolver(db))
} }
// FIXME add missing cases // FIXME add missing cases
_ => None _ => None
@ -232,7 +235,7 @@ impl SourceAnalyzer {
) -> Option<MacroDef> { ) -> Option<MacroDef> {
// This must be a normal source file rather than macro file. // This must be a normal source file rather than macro file.
let path = macro_call.path().and_then(Path::from_ast)?; let path = macro_call.path().and_then(Path::from_ast)?;
self.resolver.resolve_path_as_macro(db, &path) self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into())
} }
pub fn resolve_hir_path( pub fn resolve_hir_path(
@ -275,7 +278,9 @@ impl SourceAnalyzer {
.take_types() .take_types()
.map(|it| PathResolution::Def(it.into())); .map(|it| PathResolution::Def(it.into()));
types.or(values).or(items).or_else(|| { types.or(values).or(items).or_else(|| {
self.resolver.resolve_path_as_macro(db, &path).map(|def| PathResolution::Macro(def)) self.resolver
.resolve_path_as_macro(db, &path)
.map(|def| PathResolution::Macro(def.into()))
}) })
} }
@ -420,7 +425,7 @@ impl SourceAnalyzer {
}; };
let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 }; let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 };
implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait)
} }
pub fn expand( pub fn expand(

View File

@ -39,7 +39,7 @@ fn deref_by_trait(
ty: &Canonical<Ty>, ty: &Canonical<Ty>,
) -> Option<Canonical<Ty>> { ) -> Option<Canonical<Ty>> {
let krate = resolver.krate()?; let krate = resolver.krate()?;
let deref_trait = match db.lang_item(krate, "deref".into())? { let deref_trait = match db.lang_item(krate.into(), "deref".into())? {
crate::lang_item::LangItemTarget::Trait(t) => t, crate::lang_item::LangItemTarget::Trait(t) => t,
_ => return None, _ => return None,
}; };
@ -71,7 +71,7 @@ fn deref_by_trait(
let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env };
let solution = db.trait_solve(krate, canonical)?; let solution = db.trait_solve(krate.into(), canonical)?;
match &solution { match &solution {
Solution::Unique(vars) => { Solution::Unique(vars) => {

View File

@ -24,7 +24,7 @@ use rustc_hash::FxHashMap;
use hir_def::{ use hir_def::{
path::known, path::known,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, AdtId, DefWithBodyId,
}; };
use hir_expand::{diagnostics::DiagnosticSink, name}; use hir_expand::{diagnostics::DiagnosticSink, name};
use ra_arena::map::ArenaMap; use ra_arena::map::ArenaMap;
@ -65,7 +65,7 @@ mod coerce;
/// The entry point of type inference. /// The entry point of type inference.
pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
let _p = profile("infer_query"); let _p = profile("infer_query");
let resolver = def.resolver(db); let resolver = DefWithBodyId::from(def).resolver(db);
let mut ctx = InferenceContext::new(db, def, resolver); let mut ctx = InferenceContext::new(db, def, resolver);
match def { match def {
@ -378,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
for obligation in obligations { for obligation in obligations {
let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone());
let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); let canonicalized = self.canonicalizer().canonicalize_obligation(in_env);
let solution = let solution = self
self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); .db
.trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone());
match solution { match solution {
Some(Solution::Unique(substs)) => { Some(Solution::Unique(substs)) => {

View File

@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
resolver: &Resolver, resolver: &Resolver,
) -> FxHashMap<(TypeCtor, TypeCtor), usize> { ) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
let krate = resolver.krate().unwrap(); let krate = resolver.krate().unwrap();
let impls = match db.lang_item(krate, "coerce_unsized".into()) { let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) {
Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_), Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_),
_ => return FxHashMap::default(), _ => return FxHashMap::default(),
}; };

View File

@ -12,6 +12,7 @@ use hir_def::{
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
path::{GenericArg, PathSegment}, path::{GenericArg, PathSegment},
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
GenericDefId,
}; };
use super::{ use super::{
@ -574,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query(
def: GenericDef, def: GenericDef,
param_idx: u32, param_idx: u32,
) -> Arc<[GenericPredicate]> { ) -> Arc<[GenericPredicate]> {
let resolver = def.resolver(db); let resolver = GenericDefId::from(def).resolver(db);
resolver resolver
.where_predicates_in_scope() .where_predicates_in_scope()
// we have to filter out all other predicates *first*, before attempting to lower them // we have to filter out all other predicates *first*, before attempting to lower them
@ -600,7 +601,7 @@ pub(crate) fn generic_predicates_query(
db: &impl HirDatabase, db: &impl HirDatabase,
def: GenericDef, def: GenericDef,
) -> Arc<[GenericPredicate]> { ) -> Arc<[GenericPredicate]> {
let resolver = def.resolver(db); let resolver = GenericDefId::from(def).resolver(db);
resolver resolver
.where_predicates_in_scope() .where_predicates_in_scope()
.flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred))
@ -609,7 +610,7 @@ pub(crate) fn generic_predicates_query(
/// Resolve the default type params from generics /// Resolve the default type params from generics
pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs {
let resolver = def.resolver(db); let resolver = GenericDefId::from(def).resolver(db);
let generic_params = def.generic_params(db); let generic_params = def.generic_params(db);
let defaults = generic_params let defaults = generic_params

View File

@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates<T>(
// rustc does an autoderef and then autoref again). // rustc does an autoderef and then autoref again).
for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) {
if let Some(result) = if let Some(result) = iterate_inherent_methods(
iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback) &derefed_ty,
{ db,
name,
mode,
krate.into(),
&mut callback,
) {
return Some(result); return Some(result);
} }
if let Some(result) = iterate_trait_method_candidates( if let Some(result) = iterate_trait_method_candidates(
@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates<T>(
LookupMode::Path => { LookupMode::Path => {
// No autoderef for path lookups // No autoderef for path lookups
if let Some(result) = if let Some(result) =
iterate_inherent_methods(&ty, db, name, mode, krate, &mut callback) iterate_inherent_methods(&ty, db, name, mode, krate.into(), &mut callback)
{ {
return Some(result); return Some(result);
} }
@ -240,7 +245,7 @@ fn iterate_trait_method_candidates<T>(
} }
if !known_implemented { if !known_implemented {
let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); let goal = generic_implements_goal(db, env.clone(), t, ty.clone());
if db.trait_solve(krate, goal).is_none() { if db.trait_solve(krate.into(), goal).is_none() {
continue 'traits; continue 'traits;
} }
} }