2426: Decouple r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-27 09:34:54 +00:00 committed by GitHub
commit 84bd7916ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 62 deletions

View file

@ -11,7 +11,7 @@ use hir_def::{
expr::{ExprId, PatId},
path::known,
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
DefWithBodyId,
AssocItemId, DefWithBodyId,
};
use hir_expand::{
hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source,
@ -380,7 +380,7 @@ impl SourceAnalyzer {
name,
method_resolution::LookupMode::MethodCall,
|ty, it| match it {
AssocItem::Function(f) => callback(ty, f),
AssocItemId::FunctionId(f) => callback(ty, f.into()),
_ => None,
},
)
@ -391,7 +391,7 @@ impl SourceAnalyzer {
db: &impl HirDatabase,
ty: &Type,
name: Option<&Name>,
callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
) -> Option<T> {
// There should be no inference vars in types passed here
// FIXME check that?
@ -403,7 +403,7 @@ impl SourceAnalyzer {
&self.resolver,
name,
method_resolution::LookupMode::Path,
callback,
|ty, it| callback(ty, it.into()),
)
}

View file

@ -26,7 +26,7 @@ use hir_def::{
path::known,
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{Mutability, TypeRef},
AdtId, DefWithBodyId,
AdtId, AssocItemId, DefWithBodyId,
};
use hir_expand::{diagnostics::DiagnosticSink, name};
use ra_arena::map::ArenaMap;
@ -255,8 +255,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
self.result.variant_resolutions.insert(id, variant);
}
fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItem) {
self.result.assoc_resolutions.insert(id, item);
fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId) {
self.result.assoc_resolutions.insert(id, item.into());
}
fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {

View file

@ -7,6 +7,7 @@
use hir_def::{
lang_item::LangItemTarget,
resolver::{HasResolver, Resolver},
type_ref::Mutability,
AdtId,
};
use rustc_hash::FxHashMap;
@ -15,7 +16,6 @@ use test_utils::tested_by;
use crate::{
db::HirDatabase,
ty::{autoderef, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
Mutability,
};
use super::{InEnvironment, InferTy, InferenceContext, TypeVarValue};

View file

@ -5,22 +5,21 @@ use std::sync::Arc;
use hir_def::{
builtin_type::Signedness,
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
generics::GenericParams,
path::{GenericArg, GenericArgs},
resolver::resolver_for_expr,
AdtId, ContainerId, Lookup, StructFieldId,
};
use hir_expand::name;
use hir_expand::name::{self, Name};
use crate::{
db::HirDatabase,
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
ty::{
autoderef, method_resolution, op, traits::InEnvironment, CallableDef, InferTy, IntTy,
Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
TypeWalk, Uncertain,
},
Name,
};
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};

View file

@ -3,14 +3,18 @@
use std::iter::repeat;
use std::sync::Arc;
use hir_def::{
expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
path::Path,
type_ref::Mutability,
};
use hir_expand::name::Name;
use test_utils::tested_by;
use super::{BindingMode, InferenceContext};
use crate::{
db::HirDatabase,
expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
ty::{Mutability, Substs, Ty, TypeCtor, TypeWalk},
Name, Path,
ty::{Substs, Ty, TypeCtor, TypeWalk},
};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {

View file

@ -1,14 +1,15 @@
//! Path expression resolution.
use hir_def::{
path::PathSegment,
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
path::{Path, PathSegment},
resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
AssocItemId, ContainerId, Lookup,
};
use hir_expand::name::Name;
use crate::{
db::HirDatabase,
ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId},
AssocItem, Container, Function, Name, Path,
};
use super::{ExprOrPatId, InferenceContext, TraitRef};
@ -142,31 +143,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
id: ExprOrPatId,
) -> Option<(ValueNs, Option<Substs>)> {
let trait_ = trait_ref.trait_;
let item =
self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map(
|item| match item {
AssocItem::Function(func) => {
if segment.name == func.name(self.db) {
Some(AssocItem::Function(func))
} else {
None
}
let item = self
.db
.trait_data(trait_)
.items
.iter()
.map(|(_name, id)| (*id).into())
.find_map(|item| match item {
AssocItemId::FunctionId(func) => {
if segment.name == self.db.function_data(func).name {
Some(AssocItemId::FunctionId(func))
} else {
None
}
}
AssocItem::Const(konst) => {
if konst.name(self.db).map_or(false, |n| n == segment.name) {
Some(AssocItem::Const(konst))
} else {
None
}
AssocItemId::ConstId(konst) => {
if self.db.const_data(konst).name.as_ref().map_or(false, |n| n == &segment.name)
{
Some(AssocItemId::ConstId(konst))
} else {
None
}
AssocItem::TypeAlias(_) => None,
},
)?;
}
AssocItemId::TypeAliasId(_) => None,
})?;
let def = match item {
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
AssocItem::Const(c) => ValueNs::ConstId(c.id),
AssocItem::TypeAlias(_) => unreachable!(),
AssocItemId::FunctionId(f) => ValueNs::FunctionId(f),
AssocItemId::ConstId(c) => ValueNs::ConstId(c),
AssocItemId::TypeAliasId(_) => unreachable!(),
};
let substs = Substs::build_for_def(self.db, item)
.use_parent_substs(&trait_ref.substs)
@ -196,16 +201,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Some(name),
method_resolution::LookupMode::Path,
move |_ty, item| {
let def = match item {
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
AssocItem::Const(c) => ValueNs::ConstId(c.id),
AssocItem::TypeAlias(_) => unreachable!(),
let (def, container) = match item {
AssocItemId::FunctionId(f) => {
(ValueNs::FunctionId(f), f.lookup(self.db).container)
}
AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container),
AssocItemId::TypeAliasId(_) => unreachable!(),
};
let substs = match item.container(self.db) {
Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()),
Container::Trait(t) => {
let substs = match container {
ContainerId::ImplId(_) => self.find_self_types(&def, ty.clone()),
ContainerId::TraitId(trait_) => {
// we're picking this method
let trait_substs = Substs::build_for_def(self.db, t.id)
let trait_substs = Substs::build_for_def(self.db, trait_)
.push(ty.clone())
.fill(std::iter::repeat_with(|| self.new_type_var()))
.build();
@ -214,29 +221,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
.fill_with_params()
.build();
self.obligations.push(super::Obligation::Trait(TraitRef {
trait_: t.id,
trait_,
substs: trait_substs,
}));
Some(substs)
}
ContainerId::ModuleId(_) => None,
};
self.write_assoc_resolution(id, item);
self.write_assoc_resolution(id, item.into());
Some((def, substs))
},
)
}
fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> {
if let ValueNs::FunctionId(func) = def {
let func = Function::from(*func);
if let ValueNs::FunctionId(func) = *def {
// We only do the infer if parent has generic params
let gen = self.db.generic_params(func.id.into());
let gen = self.db.generic_params(func.into());
if gen.count_parent_params() == 0 {
return None;
}
let impl_block = func.impl_block(self.db)?.target_ty(self.db);
let impl_id = match func.lookup(self.db).container {
ContainerId::ImplId(it) => it,
_ => return None,
};
let resolver = impl_id.resolver(self.db);
let impl_data = self.db.impl_data(impl_id);
let impl_block = Ty::from_hir(self.db, &resolver, &impl_data.target_type);
let impl_block_substs = impl_block.substs()?;
let actual_substs = actual_def_ty.substs()?;

View file

@ -1,12 +1,14 @@
//! Unification and canonicalization logic.
use super::{InferenceContext, Obligation};
use crate::db::HirDatabase;
use crate::ty::{
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
TypeWalk,
use crate::{
db::HirDatabase,
ty::{
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
TypeWalk,
},
util::make_mut_slice,
};
use crate::util::make_mut_slice;
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>

View file

@ -18,7 +18,7 @@ use crate::{
db::HirDatabase,
ty::primitive::{FloatBitness, Uncertain},
ty::{utils::all_super_traits, Ty, TypeCtor},
AssocItem, Function,
Function,
};
use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef};
@ -157,7 +157,7 @@ pub(crate) fn lookup_method(
) -> Option<(Ty, Function)> {
iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f
{
AssocItem::Function(f) => Some((ty.clone(), f)),
AssocItemId::FunctionId(f) => Some((ty.clone(), f.into())),
_ => None,
})
}
@ -183,7 +183,7 @@ pub(crate) fn iterate_method_candidates<T>(
resolver: &Resolver,
name: Option<&Name>,
mode: LookupMode,
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
) -> Option<T> {
let krate = resolver.krate()?;
match mode {
@ -239,7 +239,7 @@ fn iterate_trait_method_candidates<T>(
resolver: &Resolver,
name: Option<&Name>,
mode: LookupMode,
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
) -> Option<T> {
let krate = resolver.krate()?;
// FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that)
@ -285,7 +285,7 @@ fn iterate_inherent_methods<T>(
name: Option<&Name>,
mode: LookupMode,
krate: CrateId,
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
) -> Option<T> {
for krate in ty.value.def_crates(db, krate)? {
let impls = db.impls_in_crate(krate);

View file

@ -398,6 +398,16 @@ impl_froms!(
ConstId
);
impl From<AssocItemId> for GenericDefId {
fn from(item: AssocItemId) -> Self {
match item {
AssocItemId::FunctionId(f) => f.into(),
AssocItemId::ConstId(c) => c.into(),
AssocItemId::TypeAliasId(t) => t.into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AttrDefId {
ModuleId(ModuleId),