mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Decouple
This commit is contained in:
parent
eb53aa37a3
commit
00684d708b
@ -9,10 +9,16 @@ use hir_def::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef,
|
||||
ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
|
||||
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 {
|
||||
($(($id:path, $ty:path)),*) => {$(
|
||||
impl From<$id> for $ty {
|
||||
|
@ -2,25 +2,23 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::{
|
||||
body::scope::{ExprScopes, ScopeId},
|
||||
builtin_type::BuiltinType,
|
||||
db::DefDatabase2,
|
||||
expr::ExprId,
|
||||
expr::{ExprId, PatId},
|
||||
generics::GenericParams,
|
||||
nameres::CrateDefMap,
|
||||
nameres::{per_ns::PerNs, CrateDefMap},
|
||||
path::{Path, PathKind},
|
||||
AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
|
||||
FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
|
||||
TypeAliasId, UnionId,
|
||||
};
|
||||
use hir_expand::name::{self, Name};
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
use crate::{
|
||||
code_model::Crate,
|
||||
db::HirDatabase,
|
||||
expr::{ExprScopes, PatId, ScopeId},
|
||||
DefWithBody, GenericDef, MacroDef, PerNs,
|
||||
use hir_expand::{
|
||||
name::{self, Name},
|
||||
MacroDefId,
|
||||
};
|
||||
use ra_db::CrateId;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct Resolver {
|
||||
@ -318,9 +316,9 @@ impl Resolver {
|
||||
&self,
|
||||
db: &impl DefDatabase2,
|
||||
path: &Path,
|
||||
) -> Option<MacroDef> {
|
||||
) -> Option<MacroDefId> {
|
||||
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(
|
||||
@ -355,8 +353,8 @@ impl Resolver {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn krate(&self) -> Option<Crate> {
|
||||
self.module().map(|t| Crate { crate_id: t.0.krate() })
|
||||
pub(crate) fn krate(&self) -> Option<CrateId> {
|
||||
self.module().map(|t| t.0.krate())
|
||||
}
|
||||
|
||||
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?
|
||||
pub(crate) fn resolver_for_expr(
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
owner: DefWithBodyId,
|
||||
expr_id: ExprId,
|
||||
) -> Resolver {
|
||||
@ -493,7 +491,7 @@ pub(crate) fn resolver_for_expr(
|
||||
}
|
||||
|
||||
pub(crate) fn resolver_for_scope(
|
||||
db: &impl HirDatabase,
|
||||
db: &impl DefDatabase2,
|
||||
owner: DefWithBodyId,
|
||||
scope_id: Option<ScopeId>,
|
||||
) -> Resolver {
|
||||
@ -623,15 +621,3 @@ impl HasResolver for ImplId {
|
||||
.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)
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ use std::sync::Arc;
|
||||
use hir_def::{
|
||||
expr::{ExprId, PatId},
|
||||
path::known,
|
||||
DefWithBodyId,
|
||||
};
|
||||
use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
|
||||
use ra_syntax::{
|
||||
@ -51,7 +52,9 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
|
||||
},
|
||||
_ => match node.value.kind() {
|
||||
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
|
||||
_ => None
|
||||
@ -232,7 +235,7 @@ impl SourceAnalyzer {
|
||||
) -> Option<MacroDef> {
|
||||
// This must be a normal source file rather than macro file.
|
||||
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(
|
||||
@ -275,7 +278,9 @@ impl SourceAnalyzer {
|
||||
.take_types()
|
||||
.map(|it| PathResolution::Def(it.into()));
|
||||
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 };
|
||||
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(
|
||||
|
@ -39,7 +39,7 @@ fn deref_by_trait(
|
||||
ty: &Canonical<Ty>,
|
||||
) -> Option<Canonical<Ty>> {
|
||||
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,
|
||||
_ => return None,
|
||||
};
|
||||
@ -71,7 +71,7 @@ fn deref_by_trait(
|
||||
|
||||
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 {
|
||||
Solution::Unique(vars) => {
|
||||
|
@ -24,7 +24,7 @@ use rustc_hash::FxHashMap;
|
||||
use hir_def::{
|
||||
path::known,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
AdtId,
|
||||
AdtId, DefWithBodyId,
|
||||
};
|
||||
use hir_expand::{diagnostics::DiagnosticSink, name};
|
||||
use ra_arena::map::ArenaMap;
|
||||
@ -65,7 +65,7 @@ mod coerce;
|
||||
/// The entry point of type inference.
|
||||
pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
|
||||
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);
|
||||
|
||||
match def {
|
||||
@ -378,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
for obligation in obligations {
|
||||
let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone());
|
||||
let canonicalized = self.canonicalizer().canonicalize_obligation(in_env);
|
||||
let solution =
|
||||
self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone());
|
||||
let solution = self
|
||||
.db
|
||||
.trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone());
|
||||
|
||||
match solution {
|
||||
Some(Solution::Unique(substs)) => {
|
||||
|
@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
resolver: &Resolver,
|
||||
) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
|
||||
let krate = resolver.krate().unwrap();
|
||||
let impls = match db.lang_item(krate, "coerce_unsized".into()) {
|
||||
Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_),
|
||||
let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) {
|
||||
Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_),
|
||||
_ => return FxHashMap::default(),
|
||||
};
|
||||
|
||||
|
@ -12,6 +12,7 @@ use hir_def::{
|
||||
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
|
||||
path::{GenericArg, PathSegment},
|
||||
type_ref::{TypeBound, TypeRef},
|
||||
GenericDefId,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@ -574,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query(
|
||||
def: GenericDef,
|
||||
param_idx: u32,
|
||||
) -> Arc<[GenericPredicate]> {
|
||||
let resolver = def.resolver(db);
|
||||
let resolver = GenericDefId::from(def).resolver(db);
|
||||
resolver
|
||||
.where_predicates_in_scope()
|
||||
// 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,
|
||||
def: GenericDef,
|
||||
) -> Arc<[GenericPredicate]> {
|
||||
let resolver = def.resolver(db);
|
||||
let resolver = GenericDefId::from(def).resolver(db);
|
||||
resolver
|
||||
.where_predicates_in_scope()
|
||||
.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
|
||||
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 defaults = generic_params
|
||||
|
@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates<T>(
|
||||
// rustc does an autoderef and then autoref again).
|
||||
|
||||
for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) {
|
||||
if let Some(result) =
|
||||
iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback)
|
||||
{
|
||||
if let Some(result) = iterate_inherent_methods(
|
||||
&derefed_ty,
|
||||
db,
|
||||
name,
|
||||
mode,
|
||||
krate.into(),
|
||||
&mut callback,
|
||||
) {
|
||||
return Some(result);
|
||||
}
|
||||
if let Some(result) = iterate_trait_method_candidates(
|
||||
@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates<T>(
|
||||
LookupMode::Path => {
|
||||
// No autoderef for path lookups
|
||||
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);
|
||||
}
|
||||
@ -240,7 +245,7 @@ fn iterate_trait_method_candidates<T>(
|
||||
}
|
||||
if !known_implemented {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user