Remove assoc_type_by_name helper

This commit is contained in:
Aleksey Kladov 2019-11-26 17:21:29 +03:00
parent 9bc8f1f4f8
commit d118997b93
5 changed files with 34 additions and 39 deletions

View file

@ -740,10 +740,6 @@ impl Trait {
db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
}
pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
db.trait_data(self.id).associated_type_by_name(name).map(TypeAlias::from)
}
pub fn associated_type_by_name_including_super_traits(
self,
db: &impl HirDatabase,
@ -751,8 +747,8 @@ impl Trait {
) -> Option<TypeAlias> {
all_super_traits(db, self.id)
.into_iter()
.map(Trait::from)
.find_map(|t| t.associated_type_by_name(db, name))
.find_map(|t| db.trait_data(t).associated_type_by_name(name))
.map(TypeAlias::from)
}
pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {

View file

@ -10,7 +10,7 @@ use hir_expand::name;
use log::{info, warn};
use ra_db::CrateId;
use crate::{db::HirDatabase, Trait};
use crate::db::HirDatabase;
use super::{
traits::{InEnvironment, Solution},
@ -49,12 +49,12 @@ fn deref_by_trait(
ty: InEnvironment<&Canonical<Ty>>,
) -> Option<Canonical<Ty>> {
let deref_trait = match db.lang_item(krate.into(), "deref".into())? {
LangItemTarget::TraitId(t) => Trait::from(t),
LangItemTarget::TraitId(it) => it,
_ => return None,
};
let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?;
let generic_params = db.generic_params(target.id.into());
let generic_params = db.generic_params(target.into());
if generic_params.count_params_including_parent() != 1 {
// the Target type + Deref trait should only have one generic parameter,
// namely Deref's Self type
@ -69,7 +69,7 @@ fn deref_by_trait(
let projection = super::traits::ProjectionPredicate {
ty: Ty::Bound(0),
projection_ty: super::ProjectionTy { associated_ty: target.id, parameters },
projection_ty: super::ProjectionTy { associated_ty: target, parameters },
};
let obligation = super::Obligation::Projection(projection);

View file

@ -43,7 +43,7 @@ use crate::{
db::HirDatabase,
expr::{BindingAnnotation, Body, ExprId, PatId},
ty::infer::diagnostics::InferenceDiagnostic,
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef,
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, VariantDef,
};
macro_rules! ty_app {
@ -582,20 +582,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
let path = known::std_iter_into_iterator();
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name::ITEM_TYPE).map(TypeAlias::from)
}
fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
let path = known::std_ops_try();
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
trait_.associated_type_by_name(self.db, &name::OK_TYPE)
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name::OK_TYPE).map(TypeAlias::from)
}
fn resolve_future_future_output(&self) -> Option<TypeAlias> {
let path = known::std_future_future();
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name::OUTPUT_TYPE).map(TypeAlias::from)
}
fn resolve_boxed_box(&self) -> Option<AdtId> {

View file

@ -263,16 +263,14 @@ impl Ty {
});
let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from);
for t in traits {
if let Some(associated_ty) = t.associated_type_by_name(db, &segment.name) {
if let Some(associated_ty) = db.trait_data(t.id).associated_type_by_name(&segment.name)
{
let substs = Substs::build_for_def(db, t.id)
.push(self_ty.clone())
.fill_with_unknown()
.build();
// FIXME handle type parameters on the segment
return Ty::Projection(ProjectionTy {
associated_ty: associated_ty.id,
parameters: substs,
});
return Ty::Projection(ProjectionTy { associated_ty, parameters: substs });
}
}
Ty::Unknown

View file

@ -9,7 +9,7 @@ use chalk_ir::{
};
use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TypeAliasId};
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId};
use hir_expand::name;
use ra_db::salsa::{InternId, InternKey};
@ -459,7 +459,7 @@ where
[super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter()
{
if let Some(actual_trait) = get_fn_trait(self.db, self.krate, fn_trait) {
if trait_ == actual_trait {
if trait_.id == actual_trait {
let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait };
result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db));
}
@ -661,6 +661,7 @@ fn impl_block_datum(
};
let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses };
let trait_data = db.trait_data(trait_.id);
let associated_ty_value_ids = impl_block
.items(db)
.into_iter()
@ -670,7 +671,7 @@ fn impl_block_datum(
})
.filter(|type_alias| {
// don't include associated types that don't exist in the trait
trait_.associated_type_by_name(db, &type_alias.name(db)).is_some()
trait_data.associated_type_by_name(&type_alias.name(db)).is_some()
})
.map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db))
.collect();
@ -713,7 +714,7 @@ fn closure_fn_trait_impl_datum(
// and don't want to return a valid value only to find out later that FnOnce
// is broken
let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?;
let _output = db.trait_data(fn_once_trait).associated_type_by_name(&name::OUTPUT_TYPE)?;
let num_args: u16 = match &db.body(data.def.into())[data.expr] {
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
@ -735,8 +736,8 @@ fn closure_fn_trait_impl_datum(
let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty);
let trait_ref = TraitRef {
trait_,
substs: Substs::build_for_def(db, trait_.id).push(self_ty).push(arg_ty).build(),
trait_: trait_.into(),
substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(),
};
let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db);
@ -783,10 +784,10 @@ fn type_alias_associated_ty_value(
.target_trait_ref(db)
.expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved
.trait_;
let assoc_ty = trait_
.associated_type_by_name(db, &type_alias.name(db))
.expect("assoc ty value should not exist") // validated when building the impl data as well
.id;
let assoc_ty = db
.trait_data(trait_.id)
.associated_type_by_name(&type_alias.name(db))
.expect("assoc ty value should not exist"); // validated when building the impl data as well
let generic_params = db.generic_params(impl_block.id.into());
let bound_vars = Substs::bound_vars(&generic_params);
let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars);
@ -819,10 +820,10 @@ fn closure_fn_trait_output_assoc_ty_value(
let fn_once_trait =
get_fn_trait(db, krate, super::FnTrait::FnOnce).expect("assoc ty value should not exist");
let output_ty_id = fn_once_trait
.associated_type_by_name(db, &name::OUTPUT_TYPE)
.expect("assoc ty value should not exist")
.id;
let output_ty_id = db
.trait_data(fn_once_trait)
.associated_type_by_name(&name::OUTPUT_TYPE)
.expect("assoc ty value should not exist");
let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: output_ty.to_chalk(db) };
@ -834,10 +835,10 @@ fn closure_fn_trait_output_assoc_ty_value(
Arc::new(value)
}
fn get_fn_trait(db: &impl HirDatabase, krate: Crate, fn_trait: super::FnTrait) -> Option<Trait> {
fn get_fn_trait(db: &impl HirDatabase, krate: Crate, fn_trait: super::FnTrait) -> Option<TraitId> {
let target = db.lang_item(krate.crate_id, fn_trait.lang_item_name().into())?;
match target {
LangItemTarget::TraitId(t) => Some(t.into()),
LangItemTarget::TraitId(t) => Some(t),
_ => None,
}
}