diff options
author | Aleksey Kladov <[email protected]> | 2019-11-26 14:21:29 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-26 14:44:43 +0000 |
commit | d118997b9318b750676a7fe5b8896219f98d9e6e (patch) | |
tree | 0ec33a8c39d0815d288a575dc4986f8029f5f4d1 /crates/ra_hir/src | |
parent | 9bc8f1f4f8d7bded19517205f8522a0110204f51 (diff) |
Remove assoc_type_by_name helper
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/autoderef.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/traits/chalk.rs | 33 |
5 files changed, 34 insertions, 39 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9e7a1deec..50e6409b1 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -740,10 +740,6 @@ impl Trait { | |||
740 | db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() | 740 | db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() |
741 | } | 741 | } |
742 | 742 | ||
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 | |||
747 | pub fn associated_type_by_name_including_super_traits( | 743 | pub fn associated_type_by_name_including_super_traits( |
748 | self, | 744 | self, |
749 | db: &impl HirDatabase, | 745 | db: &impl HirDatabase, |
@@ -751,8 +747,8 @@ impl Trait { | |||
751 | ) -> Option<TypeAlias> { | 747 | ) -> Option<TypeAlias> { |
752 | all_super_traits(db, self.id) | 748 | all_super_traits(db, self.id) |
753 | .into_iter() | 749 | .into_iter() |
754 | .map(Trait::from) | 750 | .find_map(|t| db.trait_data(t).associated_type_by_name(name)) |
755 | .find_map(|t| t.associated_type_by_name(db, name)) | 751 | .map(TypeAlias::from) |
756 | } | 752 | } |
757 | 753 | ||
758 | pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef { | 754 | pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef { |
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 9e7593b8b..ae68234ac 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs | |||
@@ -10,7 +10,7 @@ use hir_expand::name; | |||
10 | use log::{info, warn}; | 10 | use log::{info, warn}; |
11 | use ra_db::CrateId; | 11 | use ra_db::CrateId; |
12 | 12 | ||
13 | use crate::{db::HirDatabase, Trait}; | 13 | use crate::db::HirDatabase; |
14 | 14 | ||
15 | use super::{ | 15 | use super::{ |
16 | traits::{InEnvironment, Solution}, | 16 | traits::{InEnvironment, Solution}, |
@@ -49,12 +49,12 @@ fn deref_by_trait( | |||
49 | ty: InEnvironment<&Canonical<Ty>>, | 49 | ty: InEnvironment<&Canonical<Ty>>, |
50 | ) -> Option<Canonical<Ty>> { | 50 | ) -> Option<Canonical<Ty>> { |
51 | let deref_trait = match db.lang_item(krate.into(), "deref".into())? { | 51 | let deref_trait = match db.lang_item(krate.into(), "deref".into())? { |
52 | LangItemTarget::TraitId(t) => Trait::from(t), | 52 | LangItemTarget::TraitId(it) => it, |
53 | _ => return None, | 53 | _ => return None, |
54 | }; | 54 | }; |
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)?; |
56 | 56 | ||
57 | let generic_params = db.generic_params(target.id.into()); | 57 | let generic_params = db.generic_params(target.into()); |
58 | if generic_params.count_params_including_parent() != 1 { | 58 | if generic_params.count_params_including_parent() != 1 { |
59 | // the Target type + Deref trait should only have one generic parameter, | 59 | // the Target type + Deref trait should only have one generic parameter, |
60 | // namely Deref's Self type | 60 | // namely Deref's Self type |
@@ -69,7 +69,7 @@ fn deref_by_trait( | |||
69 | 69 | ||
70 | let projection = super::traits::ProjectionPredicate { | 70 | let projection = super::traits::ProjectionPredicate { |
71 | ty: Ty::Bound(0), | 71 | ty: Ty::Bound(0), |
72 | projection_ty: super::ProjectionTy { associated_ty: target.id, parameters }, | 72 | projection_ty: super::ProjectionTy { associated_ty: target, parameters }, |
73 | }; | 73 | }; |
74 | 74 | ||
75 | let obligation = super::Obligation::Projection(projection); | 75 | let obligation = super::Obligation::Projection(projection); |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index fce45321d..b023ae690 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -43,7 +43,7 @@ use crate::{ | |||
43 | db::HirDatabase, | 43 | db::HirDatabase, |
44 | expr::{BindingAnnotation, Body, ExprId, PatId}, | 44 | expr::{BindingAnnotation, Body, ExprId, PatId}, |
45 | ty::infer::diagnostics::InferenceDiagnostic, | 45 | 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, |
47 | }; | 47 | }; |
48 | 48 | ||
49 | macro_rules! ty_app { | 49 | macro_rules! ty_app { |
@@ -582,20 +582,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
582 | 582 | ||
583 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { | 583 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { |
584 | let path = known::std_iter_into_iterator(); | 584 | let path = known::std_iter_into_iterator(); |
585 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); | 585 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
586 | trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) | 586 | self.db.trait_data(trait_).associated_type_by_name(&name::ITEM_TYPE).map(TypeAlias::from) |
587 | } | 587 | } |
588 | 588 | ||
589 | fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { | 589 | fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { |
590 | let path = known::std_ops_try(); | 590 | let path = known::std_ops_try(); |
591 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); | 591 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
592 | trait_.associated_type_by_name(self.db, &name::OK_TYPE) | 592 | self.db.trait_data(trait_).associated_type_by_name(&name::OK_TYPE).map(TypeAlias::from) |
593 | } | 593 | } |
594 | 594 | ||
595 | fn resolve_future_future_output(&self) -> Option<TypeAlias> { | 595 | fn resolve_future_future_output(&self) -> Option<TypeAlias> { |
596 | let path = known::std_future_future(); | 596 | let path = known::std_future_future(); |
597 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); | 597 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
598 | trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) | 598 | self.db.trait_data(trait_).associated_type_by_name(&name::OUTPUT_TYPE).map(TypeAlias::from) |
599 | } | 599 | } |
600 | 600 | ||
601 | fn resolve_boxed_box(&self) -> Option<AdtId> { | 601 | fn resolve_boxed_box(&self) -> Option<AdtId> { |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 9e3afabe0..0ac7920bb 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -263,16 +263,14 @@ impl Ty { | |||
263 | }); | 263 | }); |
264 | let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from); | 264 | let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from); |
265 | for t in traits { | 265 | 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 | { | ||
267 | let substs = Substs::build_for_def(db, t.id) | 268 | let substs = Substs::build_for_def(db, t.id) |
268 | .push(self_ty.clone()) | 269 | .push(self_ty.clone()) |
269 | .fill_with_unknown() | 270 | .fill_with_unknown() |
270 | .build(); | 271 | .build(); |
271 | // FIXME handle type parameters on the segment | 272 | // FIXME handle type parameters on the segment |
272 | return Ty::Projection(ProjectionTy { | 273 | return Ty::Projection(ProjectionTy { associated_ty, parameters: substs }); |
273 | associated_ty: associated_ty.id, | ||
274 | parameters: substs, | ||
275 | }); | ||
276 | } | 274 | } |
277 | } | 275 | } |
278 | Ty::Unknown | 276 | Ty::Unknown |
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 06388a3ce..78f4b3e27 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs | |||
@@ -9,7 +9,7 @@ use chalk_ir::{ | |||
9 | }; | 9 | }; |
10 | use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; | 10 | use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; |
11 | 11 | ||
12 | use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TypeAliasId}; | 12 | use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId}; |
13 | use hir_expand::name; | 13 | use hir_expand::name; |
14 | 14 | ||
15 | use ra_db::salsa::{InternId, InternKey}; | 15 | use ra_db::salsa::{InternId, InternKey}; |
@@ -459,7 +459,7 @@ where | |||
459 | [super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter() | 459 | [super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter() |
460 | { | 460 | { |
461 | if let Some(actual_trait) = get_fn_trait(self.db, self.krate, fn_trait) { | 461 | 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 { |
463 | let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait }; | 463 | let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait }; |
464 | result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db)); | 464 | result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db)); |
465 | } | 465 | } |
@@ -661,6 +661,7 @@ fn impl_block_datum( | |||
661 | }; | 661 | }; |
662 | 662 | ||
663 | let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses }; | 663 | let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses }; |
664 | let trait_data = db.trait_data(trait_.id); | ||
664 | let associated_ty_value_ids = impl_block | 665 | let associated_ty_value_ids = impl_block |
665 | .items(db) | 666 | .items(db) |
666 | .into_iter() | 667 | .into_iter() |
@@ -670,7 +671,7 @@ fn impl_block_datum( | |||
670 | }) | 671 | }) |
671 | .filter(|type_alias| { | 672 | .filter(|type_alias| { |
672 | // don't include associated types that don't exist in the trait | 673 | // 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() |
674 | }) | 675 | }) |
675 | .map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db)) | 676 | .map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db)) |
676 | .collect(); | 677 | .collect(); |
@@ -713,7 +714,7 @@ fn closure_fn_trait_impl_datum( | |||
713 | // and don't want to return a valid value only to find out later that FnOnce | 714 | // and don't want to return a valid value only to find out later that FnOnce |
714 | // is broken | 715 | // is broken |
715 | let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?; | 716 | 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)?; |
717 | 718 | ||
718 | let num_args: u16 = match &db.body(data.def.into())[data.expr] { | 719 | let num_args: u16 = match &db.body(data.def.into())[data.expr] { |
719 | crate::expr::Expr::Lambda { args, .. } => args.len() as u16, | 720 | crate::expr::Expr::Lambda { args, .. } => args.len() as u16, |
@@ -735,8 +736,8 @@ fn closure_fn_trait_impl_datum( | |||
735 | let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); | 736 | let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); |
736 | 737 | ||
737 | let trait_ref = TraitRef { | 738 | let trait_ref = TraitRef { |
738 | trait_, | 739 | trait_: trait_.into(), |
739 | substs: Substs::build_for_def(db, trait_.id).push(self_ty).push(arg_ty).build(), | 740 | substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(), |
740 | }; | 741 | }; |
741 | 742 | ||
742 | let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db); | 743 | let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db); |
@@ -783,10 +784,10 @@ fn type_alias_associated_ty_value( | |||
783 | .target_trait_ref(db) | 784 | .target_trait_ref(db) |
784 | .expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved | 785 | .expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved |
785 | .trait_; | 786 | .trait_; |
786 | let assoc_ty = trait_ | 787 | let assoc_ty = db |
787 | .associated_type_by_name(db, &type_alias.name(db)) | 788 | .trait_data(trait_.id) |
788 | .expect("assoc ty value should not exist") // validated when building the impl data as well | 789 | .associated_type_by_name(&type_alias.name(db)) |
789 | .id; | 790 | .expect("assoc ty value should not exist"); // validated when building the impl data as well |
790 | let generic_params = db.generic_params(impl_block.id.into()); | 791 | let generic_params = db.generic_params(impl_block.id.into()); |
791 | let bound_vars = Substs::bound_vars(&generic_params); | 792 | let bound_vars = Substs::bound_vars(&generic_params); |
792 | let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars); | 793 | 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( | |||
819 | let fn_once_trait = | 820 | let fn_once_trait = |
820 | get_fn_trait(db, krate, super::FnTrait::FnOnce).expect("assoc ty value should not exist"); | 821 | get_fn_trait(db, krate, super::FnTrait::FnOnce).expect("assoc ty value should not exist"); |
821 | 822 | ||
822 | let output_ty_id = fn_once_trait | 823 | let output_ty_id = db |
823 | .associated_type_by_name(db, &name::OUTPUT_TYPE) | 824 | .trait_data(fn_once_trait) |
824 | .expect("assoc ty value should not exist") | 825 | .associated_type_by_name(&name::OUTPUT_TYPE) |
825 | .id; | 826 | .expect("assoc ty value should not exist"); |
826 | 827 | ||
827 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: output_ty.to_chalk(db) }; | 828 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: output_ty.to_chalk(db) }; |
828 | 829 | ||
@@ -834,10 +835,10 @@ fn closure_fn_trait_output_assoc_ty_value( | |||
834 | Arc::new(value) | 835 | Arc::new(value) |
835 | } | 836 | } |
836 | 837 | ||
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> { |
838 | let target = db.lang_item(krate.crate_id, fn_trait.lang_item_name().into())?; | 839 | let target = db.lang_item(krate.crate_id, fn_trait.lang_item_name().into())?; |
839 | match target { | 840 | match target { |
840 | LangItemTarget::TraitId(t) => Some(t.into()), | 841 | LangItemTarget::TraitId(t) => Some(t), |
841 | _ => None, | 842 | _ => None, |
842 | } | 843 | } |
843 | } | 844 | } |