Skip to content

Commit d118997

Browse files
committed
Remove assoc_type_by_name helper
1 parent 9bc8f1f commit d118997

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,19 +740,15 @@ impl Trait {
740740
db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
741741
}
742742

743-
pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
744-
db.trait_data(self.id).associated_type_by_name(name).map(TypeAlias::from)
745-
}
746-
747743
pub fn associated_type_by_name_including_super_traits(
748744
self,
749745
db: &impl HirDatabase,
750746
name: &Name,
751747
) -> Option<TypeAlias> {
752748
all_super_traits(db, self.id)
753749
.into_iter()
754-
.map(Trait::from)
755-
.find_map(|t| t.associated_type_by_name(db, name))
750+
.find_map(|t| db.trait_data(t).associated_type_by_name(name))
751+
.map(TypeAlias::from)
756752
}
757753

758754
pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {

crates/ra_hir/src/ty/autoderef.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::name;
1010
use log::{info, warn};
1111
use ra_db::CrateId;
1212

13-
use crate::{db::HirDatabase, Trait};
13+
use crate::db::HirDatabase;
1414

1515
use super::{
1616
traits::{InEnvironment, Solution},
@@ -49,12 +49,12 @@ fn deref_by_trait(
4949
ty: InEnvironment<&Canonical<Ty>>,
5050
) -> Option<Canonical<Ty>> {
5151
let deref_trait = match db.lang_item(krate.into(), "deref".into())? {
52-
LangItemTarget::TraitId(t) => Trait::from(t),
52+
LangItemTarget::TraitId(it) => it,
5353
_ => return None,
5454
};
55-
let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
55+
let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?;
5656

57-
let generic_params = db.generic_params(target.id.into());
57+
let generic_params = db.generic_params(target.into());
5858
if generic_params.count_params_including_parent() != 1 {
5959
// the Target type + Deref trait should only have one generic parameter,
6060
// namely Deref's Self type
@@ -69,7 +69,7 @@ fn deref_by_trait(
6969

7070
let projection = super::traits::ProjectionPredicate {
7171
ty: Ty::Bound(0),
72-
projection_ty: super::ProjectionTy { associated_ty: target.id, parameters },
72+
projection_ty: super::ProjectionTy { associated_ty: target, parameters },
7373
};
7474

7575
let obligation = super::Obligation::Projection(projection);

crates/ra_hir/src/ty/infer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::{
4343
db::HirDatabase,
4444
expr::{BindingAnnotation, Body, ExprId, PatId},
4545
ty::infer::diagnostics::InferenceDiagnostic,
46-
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef,
46+
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, VariantDef,
4747
};
4848

4949
macro_rules! ty_app {
@@ -582,20 +582,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
582582

583583
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
584584
let path = known::std_iter_into_iterator();
585-
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
586-
trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
585+
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
586+
self.db.trait_data(trait_).associated_type_by_name(&name::ITEM_TYPE).map(TypeAlias::from)
587587
}
588588

589589
fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
590590
let path = known::std_ops_try();
591-
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
592-
trait_.associated_type_by_name(self.db, &name::OK_TYPE)
591+
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
592+
self.db.trait_data(trait_).associated_type_by_name(&name::OK_TYPE).map(TypeAlias::from)
593593
}
594594

595595
fn resolve_future_future_output(&self) -> Option<TypeAlias> {
596596
let path = known::std_future_future();
597-
let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
598-
trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
597+
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
598+
self.db.trait_data(trait_).associated_type_by_name(&name::OUTPUT_TYPE).map(TypeAlias::from)
599599
}
600600

601601
fn resolve_boxed_box(&self) -> Option<AdtId> {

crates/ra_hir/src/ty/lower.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,14 @@ impl Ty {
263263
});
264264
let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from);
265265
for t in traits {
266-
if let Some(associated_ty) = t.associated_type_by_name(db, &segment.name) {
266+
if let Some(associated_ty) = db.trait_data(t.id).associated_type_by_name(&segment.name)
267+
{
267268
let substs = Substs::build_for_def(db, t.id)
268269
.push(self_ty.clone())
269270
.fill_with_unknown()
270271
.build();
271272
// FIXME handle type parameters on the segment
272-
return Ty::Projection(ProjectionTy {
273-
associated_ty: associated_ty.id,
274-
parameters: substs,
275-
});
273+
return Ty::Projection(ProjectionTy { associated_ty, parameters: substs });
276274
}
277275
}
278276
Ty::Unknown

crates/ra_hir/src/ty/traits/chalk.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chalk_ir::{
99
};
1010
use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
1111

12-
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TypeAliasId};
12+
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId};
1313
use hir_expand::name;
1414

1515
use ra_db::salsa::{InternId, InternKey};
@@ -459,7 +459,7 @@ where
459459
[super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter()
460460
{
461461
if let Some(actual_trait) = get_fn_trait(self.db, self.krate, fn_trait) {
462-
if trait_ == actual_trait {
462+
if trait_.id == actual_trait {
463463
let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait };
464464
result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db));
465465
}
@@ -661,6 +661,7 @@ fn impl_block_datum(
661661
};
662662

663663
let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses };
664+
let trait_data = db.trait_data(trait_.id);
664665
let associated_ty_value_ids = impl_block
665666
.items(db)
666667
.into_iter()
@@ -670,7 +671,7 @@ fn impl_block_datum(
670671
})
671672
.filter(|type_alias| {
672673
// don't include associated types that don't exist in the trait
673-
trait_.associated_type_by_name(db, &type_alias.name(db)).is_some()
674+
trait_data.associated_type_by_name(&type_alias.name(db)).is_some()
674675
})
675676
.map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db))
676677
.collect();
@@ -713,7 +714,7 @@ fn closure_fn_trait_impl_datum(
713714
// and don't want to return a valid value only to find out later that FnOnce
714715
// is broken
715716
let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
716-
fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?;
717+
let _output = db.trait_data(fn_once_trait).associated_type_by_name(&name::OUTPUT_TYPE)?;
717718

718719
let num_args: u16 = match &db.body(data.def.into())[data.expr] {
719720
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
@@ -735,8 +736,8 @@ fn closure_fn_trait_impl_datum(
735736
let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty);
736737

737738
let trait_ref = TraitRef {
738-
trait_,
739-
substs: Substs::build_for_def(db, trait_.id).push(self_ty).push(arg_ty).build(),
739+
trait_: trait_.into(),
740+
substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(),
740741
};
741742

742743
let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db);
@@ -783,10 +784,10 @@ fn type_alias_associated_ty_value(
783784
.target_trait_ref(db)
784785
.expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved
785786
.trait_;
786-
let assoc_ty = trait_
787-
.associated_type_by_name(db, &type_alias.name(db))
788-
.expect("assoc ty value should not exist") // validated when building the impl data as well
789-
.id;
787+
let assoc_ty = db
788+
.trait_data(trait_.id)
789+
.associated_type_by_name(&type_alias.name(db))
790+
.expect("assoc ty value should not exist"); // validated when building the impl data as well
790791
let generic_params = db.generic_params(impl_block.id.into());
791792
let bound_vars = Substs::bound_vars(&generic_params);
792793
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(
819820
let fn_once_trait =
820821
get_fn_trait(db, krate, super::FnTrait::FnOnce).expect("assoc ty value should not exist");
821822

822-
let output_ty_id = fn_once_trait
823-
.associated_type_by_name(db, &name::OUTPUT_TYPE)
824-
.expect("assoc ty value should not exist")
825-
.id;
823+
let output_ty_id = db
824+
.trait_data(fn_once_trait)
825+
.associated_type_by_name(&name::OUTPUT_TYPE)
826+
.expect("assoc ty value should not exist");
826827

827828
let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: output_ty.to_chalk(db) };
828829

@@ -834,10 +835,10 @@ fn closure_fn_trait_output_assoc_ty_value(
834835
Arc::new(value)
835836
}
836837

837-
fn get_fn_trait(db: &impl HirDatabase, krate: Crate, fn_trait: super::FnTrait) -> Option<Trait> {
838+
fn get_fn_trait(db: &impl HirDatabase, krate: Crate, fn_trait: super::FnTrait) -> Option<TraitId> {
838839
let target = db.lang_item(krate.crate_id, fn_trait.lang_item_name().into())?;
839840
match target {
840-
LangItemTarget::TraitId(t) => Some(t.into()),
841+
LangItemTarget::TraitId(t) => Some(t),
841842
_ => None,
842843
}
843844
}

0 commit comments

Comments
 (0)