From 72d8e7e69abca9f27fb3ea386a6879324741e152 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 18:00:36 +0300 Subject: Use TraitId in TraitRef --- crates/ra_hir/src/ty/infer/path.rs | 39 +++++++++++++----------- crates/ra_hir/src/ty/lower.rs | 14 ++++----- crates/ra_hir/src/ty/method_resolution.rs | 18 +++++------ crates/ra_hir/src/ty/traits/chalk.rs | 50 ++++++++++++++----------------- 4 files changed, 59 insertions(+), 62 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 6165eba4f..202fff4f3 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -143,24 +143,27 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { id: ExprOrPatId, ) -> Option<(ValueNs, Option)> { let trait_ = trait_ref.trait_; - let item = trait_.items(self.db).iter().copied().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 { + AssocItem::Function(func) => { + if segment.name == func.name(self.db) { + Some(AssocItem::Function(func)) + } else { + None + } + } - AssocItem::Const(konst) => { - if konst.name(self.db).map_or(false, |n| n == segment.name) { - Some(AssocItem::Const(konst)) - } else { - None - } - } - AssocItem::TypeAlias(_) => None, - })?; + AssocItem::Const(konst) => { + if konst.name(self.db).map_or(false, |n| n == segment.name) { + Some(AssocItem::Const(konst)) + } else { + None + } + } + AssocItem::TypeAlias(_) => None, + }, + )?; let def = match item { AssocItem::Function(f) => ValueNs::FunctionId(f.id), AssocItem::Const(c) => ValueNs::ConstId(c.id), @@ -212,7 +215,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { .fill_with_params() .build(); self.obligations.push(super::Obligation::Trait(TraitRef { - trait_: t, + trait_: t.id, substs: trait_substs, })); Some(substs) diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 805a73ff5..a7149614d 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -15,7 +15,7 @@ use hir_def::{ resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, AdtId, AstItemDef, EnumVariantId, FunctionId, GenericDefId, HasModule, LocalStructFieldId, - Lookup, StructId, VariantId, + Lookup, StructId, TraitId, VariantId, }; use ra_arena::map::ArenaMap; use ra_db::CrateId; @@ -172,7 +172,7 @@ impl Ty { let segment = &remaining_segments[0]; let associated_ty = associated_type_by_name_including_super_traits( db, - trait_ref.trait_.id, + trait_ref.trait_, &segment.name, ); match associated_ty { @@ -263,7 +263,7 @@ impl Ty { GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), _ => None, }); - let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from); + let traits = traits_from_env.flat_map(|t| all_super_traits(db, t)).map(Trait::from); for t in traits { if let Some(associated_ty) = db.trait_data(t.id).associated_type_by_name(&segment.name) { @@ -423,7 +423,7 @@ impl TraitRef { if let Some(self_ty) = explicit_self_ty { make_mut_slice(&mut substs.0)[0] = self_ty; } - TraitRef { trait_: resolved, substs } + TraitRef { trait_: resolved.id, substs } } pub(crate) fn from_hir( @@ -450,8 +450,8 @@ impl TraitRef { substs_from_path_segment(db, resolver, segment, Some(resolved.id.into()), !has_self_param) } - pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef { - let substs = Substs::identity(&db.generic_params(trait_.id.into())); + pub(crate) fn for_trait(db: &impl HirDatabase, trait_: TraitId) -> TraitRef { + let substs = Substs::identity(&db.generic_params(trait_.into())); TraitRef { trait_, substs } } @@ -510,7 +510,7 @@ fn assoc_type_bindings_from_type_bound<'a>( .flat_map(|args_and_bindings| args_and_bindings.bindings.iter()) .map(move |(name, type_ref)| { let associated_ty = - associated_type_by_name_including_super_traits(db, trait_ref.trait_.id, &name); + associated_type_by_name_including_super_traits(db, trait_ref.trait_, &name); let associated_ty = match associated_ty { None => return GenericPredicate::Error, Some(t) => t, diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 9988570e8..f1bc638ee 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -68,7 +68,7 @@ impl CrateImplBlocks { if let Some(tr) = TraitRef::from_hir(db, &resolver, &trait_ref, Some(target_ty)) { - res.impls_by_trait.entry(tr.trait_.id).or_default().push(impl_id); + res.impls_by_trait.entry(tr.trait_).or_default().push(impl_id); } } None => { @@ -249,13 +249,11 @@ fn iterate_trait_method_candidates( let traits_from_env = env .trait_predicates_for_self_ty(&ty.value) .map(|tr| tr.trait_) - .flat_map(|t| all_super_traits(db, t.id)) - .map(Trait::from); - let traits = inherent_trait - .chain(traits_from_env) - .chain(resolver.traits_in_scope(db).into_iter().map(Trait::from)); + .flat_map(|t| all_super_traits(db, t)); + let traits = + inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db).into_iter()); 'traits: for t in traits { - let data = db.trait_data(t.id); + let data = db.trait_data(t); // we'll be lazy about checking whether the type implements the // trait, but if we find out it doesn't, we'll skip the rest of the @@ -330,7 +328,7 @@ pub(crate) fn implements_trait( db: &impl HirDatabase, resolver: &Resolver, krate: Crate, - trait_: Trait, + trait_: TraitId, ) -> bool { if ty.value.inherent_trait() == Some(trait_) { // FIXME this is a bit of a hack, since Chalk should say the same thing @@ -373,11 +371,11 @@ impl Ty { fn generic_implements_goal( db: &impl HirDatabase, env: Arc, - trait_: Trait, + trait_: TraitId, self_ty: Canonical, ) -> Canonical> { let num_vars = self_ty.num_vars; - let substs = super::Substs::build_for_def(db, trait_.id) + let substs = super::Substs::build_for_def(db, trait_) .push(self_ty.value) .fill_with_bound_vars(num_vars as u32) .build(); diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 78f4b3e27..02d37dead 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -9,7 +9,9 @@ use chalk_ir::{ }; use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; -use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId}; +use hir_def::{ + lang_item::LangItemTarget, AstItemDef, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, +}; use hir_expand::name; use ra_db::salsa::{InternId, InternKey}; @@ -19,7 +21,7 @@ use crate::{ db::HirDatabase, ty::display::HirDisplay, ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, - Crate, ImplBlock, Trait, TypeAlias, + Crate, ImplBlock, TypeAlias, }; /// This represents a trait whose name we could not resolve. @@ -167,15 +169,15 @@ impl ToChalk for TraitRef { } } -impl ToChalk for Trait { +impl ToChalk for TraitId { type Chalk = chalk_ir::TraitId; fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId { - chalk_ir::TraitId(id_to_chalk(self.id)) + chalk_ir::TraitId(id_to_chalk(self)) } - fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait { - Trait { id: id_from_chalk(trait_id.0) } + fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> TraitId { + id_from_chalk(trait_id.0) } } @@ -443,10 +445,10 @@ where if trait_id == UNKNOWN_TRAIT { return Vec::new(); } - let trait_: Trait = from_chalk(self.db, trait_id); + let trait_: TraitId = from_chalk(self.db, trait_id); let mut result: Vec<_> = self .db - .impls_for_trait(self.krate, trait_) + .impls_for_trait(self.krate, trait_.into()) .iter() .copied() .map(Impl::ImplBlock) @@ -459,7 +461,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_.id == actual_trait { + if trait_ == actual_trait { let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait }; result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db)); } @@ -516,7 +518,7 @@ pub(crate) fn associated_ty_data_query( where_clauses: vec![], }; let datum = AssociatedTyDatum { - trait_id: Trait::from(trait_).to_chalk(db), + trait_id: trait_.to_chalk(db), id, name: lalrpop_intern::intern(&db.type_alias_data(type_alias).name.to_string()), binders: make_binders(bound_data, generic_params.count_params_including_parent()), @@ -548,29 +550,23 @@ pub(crate) fn trait_datum_query( associated_ty_ids: vec![], }); } - let trait_: Trait = from_chalk(db, trait_id); - debug!("trait {:?} = {:?}", trait_id, trait_.name(db)); - let generic_params = db.generic_params(trait_.id.into()); + let trait_: TraitId = from_chalk(db, trait_id); + let trait_data = db.trait_data(trait_); + debug!("trait {:?} = {:?}", trait_id, trait_data.name); + let generic_params = db.generic_params(trait_.into()); let bound_vars = Substs::bound_vars(&generic_params); let flags = chalk_rust_ir::TraitFlags { - auto: trait_.is_auto(db), - upstream: trait_.module(db).krate() != krate, + auto: trait_data.auto, + upstream: trait_.module(db).krate != krate.crate_id, non_enumerable: true, coinductive: false, // only relevant for Chalk testing // FIXME set these flags correctly marker: false, fundamental: false, }; - let where_clauses = convert_where_clauses(db, trait_.id.into(), &bound_vars); - let associated_ty_ids = trait_ - .items(db) - .into_iter() - .filter_map(|trait_item| match trait_item { - crate::AssocItem::TypeAlias(type_alias) => Some(type_alias.id), - _ => None, - }) - .map(|type_alias| type_alias.to_chalk(db)) - .collect(); + let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); + let associated_ty_ids = + trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; let trait_datum = TraitDatum { id: trait_id, @@ -661,7 +657,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 trait_data = db.trait_data(trait_); let associated_ty_value_ids = impl_block .items(db) .into_iter() @@ -785,7 +781,7 @@ fn type_alias_associated_ty_value( .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 = db - .trait_data(trait_.id) + .trait_data(trait_) .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()); -- cgit v1.2.3