Skip to content

Commit 57ad454

Browse files
bors[bot]matklad
andauthored
Merge #2425
2425: Decouple r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents ac5ec2a + 3a0929f commit 57ad454

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

crates/ra_hir/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::{GenericDefId, LocalStructFieldId, TraitId, VariantId};
5+
use hir_def::{GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId};
66
use ra_arena::map::ArenaMap;
77
use ra_db::{salsa, CrateId};
88

@@ -13,7 +13,7 @@ use crate::{
1313
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
1414
ValueTyDefId,
1515
},
16-
DefWithBody, ImplBlock,
16+
DefWithBody,
1717
};
1818

1919
pub use hir_def::db::{
@@ -63,7 +63,7 @@ pub trait HirDatabase: DefDatabase {
6363
fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplBlocks>;
6464

6565
#[salsa::invoke(crate::ty::traits::impls_for_trait_query)]
66-
fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplBlock]>;
66+
fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplId]>;
6767

6868
/// This provides the Chalk trait solver instance. Because Chalk always
6969
/// works from a specific crate, this query is keyed on the crate; and

crates/ra_hir/src/ty/infer/coerce.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
//!
55
//! See: https://doc.rust-lang.org/nomicon/coercions.html
66
7-
use hir_def::{lang_item::LangItemTarget, resolver::Resolver, AdtId};
7+
use hir_def::{
8+
lang_item::LangItemTarget,
9+
resolver::{HasResolver, Resolver},
10+
AdtId,
11+
};
812
use rustc_hash::FxHashMap;
913
use test_utils::tested_by;
1014

1115
use crate::{
1216
db::HirDatabase,
13-
ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk},
17+
ty::{autoderef, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
1418
Mutability,
1519
};
1620

@@ -57,9 +61,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
5761

5862
impls
5963
.iter()
60-
.filter_map(|impl_block| {
64+
.filter_map(|&impl_id| {
65+
let impl_data = db.impl_data(impl_id);
66+
let resolver = impl_id.resolver(db);
67+
let target_ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
68+
6169
// `CoerseUnsized` has one generic parameter for the target type.
62-
let trait_ref = impl_block.target_trait_ref(db)?;
70+
let trait_ref = TraitRef::from_hir(
71+
db,
72+
&resolver,
73+
impl_data.target_trait.as_ref()?,
74+
Some(target_ty),
75+
)?;
6376
let cur_from_ty = trait_ref.substs.0.get(0)?;
6477
let cur_to_ty = trait_ref.substs.0.get(1)?;
6578

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl CrateImplBlocks {
6262
let impl_data = db.impl_data(impl_id);
6363
let resolver = impl_id.resolver(db);
6464

65-
let target_ty = { Ty::from_hir(db, &resolver, &impl_data.target_type) };
65+
let target_ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
6666

6767
match &impl_data.target_trait {
6868
Some(trait_ref) => {

crates/ra_hir/src/ty/traits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
use std::sync::{Arc, Mutex};
33

44
use chalk_ir::{cast::Cast, family::ChalkIr};
5-
use hir_def::{expr::ExprId, DefWithBodyId, TraitId};
5+
use hir_def::{expr::ExprId, DefWithBodyId, ImplId, TraitId, TypeAliasId};
66
use log::debug;
77
use ra_db::{impl_intern_key, salsa, CrateId};
88
use ra_prof::profile;
99
use rustc_hash::FxHashSet;
1010

11-
use crate::{db::HirDatabase, ImplBlock, TypeAlias};
11+
use crate::{db::HirDatabase, ImplBlock};
1212

1313
use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk};
1414

@@ -79,7 +79,7 @@ pub(crate) fn impls_for_trait_query(
7979
db: &impl HirDatabase,
8080
krate: CrateId,
8181
trait_: TraitId,
82-
) -> Arc<[ImplBlock]> {
82+
) -> Arc<[ImplId]> {
8383
let mut impls = FxHashSet::default();
8484
// We call the query recursively here. On the one hand, this means we can
8585
// reuse results from queries for different crates; on the other hand, this
@@ -90,7 +90,7 @@ pub(crate) fn impls_for_trait_query(
9090
impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
9191
}
9292
let crate_impl_blocks = db.impls_in_crate(krate);
93-
impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_).map(ImplBlock::from));
93+
impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_));
9494
impls.into_iter().collect()
9595
}
9696

@@ -317,7 +317,7 @@ impl_intern_key!(GlobalImplId);
317317
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
318318
pub enum AssocTyValue {
319319
/// A normal assoc type value from an impl block.
320-
TypeAlias(TypeAlias),
320+
TypeAlias(TypeAliasId),
321321
/// The output type of the Fn trait implementation.
322322
ClosureFnTraitImplOutput(ClosureFnTraitImplData),
323323
}

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum
1111
use ra_db::CrateId;
1212

1313
use hir_def::{
14-
lang_item::LangItemTarget, AstItemDef, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId,
14+
lang_item::LangItemTarget, resolver::HasResolver, AstItemDef, ContainerId, GenericDefId,
15+
Lookup, TraitId, TypeAliasId,
1516
};
1617
use hir_expand::name;
1718

@@ -22,7 +23,7 @@ use crate::{
2223
db::HirDatabase,
2324
ty::display::HirDisplay,
2425
ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
25-
ImplBlock, TypeAlias,
26+
ImplBlock,
2627
};
2728

2829
/// This represents a trait whose name we could not resolve.
@@ -452,7 +453,7 @@ where
452453
.impls_for_trait(self.krate, trait_.into())
453454
.iter()
454455
.copied()
455-
.map(Impl::ImplBlock)
456+
.map(|it| Impl::ImplBlock(it.into()))
456457
.map(|impl_| impl_.to_chalk(self.db))
457458
.collect();
458459

@@ -670,7 +671,7 @@ fn impl_block_datum(
670671
// don't include associated types that don't exist in the trait
671672
trait_data.associated_type_by_name(&type_alias.name(db)).is_some()
672673
})
673-
.map(|type_alias| AssocTyValue::TypeAlias(type_alias).to_chalk(db))
674+
.map(|type_alias| AssocTyValue::TypeAlias(type_alias.id).to_chalk(db))
674675
.collect();
675676
debug!("impl_datum: {:?}", impl_datum_bound);
676677
let impl_datum = ImplDatum {
@@ -773,24 +774,33 @@ pub(crate) fn associated_ty_value_query(
773774
fn type_alias_associated_ty_value(
774775
db: &impl HirDatabase,
775776
_krate: CrateId,
776-
type_alias: TypeAlias,
777+
type_alias: TypeAliasId,
777778
) -> Arc<AssociatedTyValue<ChalkIr>> {
778-
let impl_block = type_alias.impl_block(db).expect("assoc ty value should be in impl");
779-
let impl_id = Impl::ImplBlock(impl_block).to_chalk(db);
780-
let trait_ = impl_block
781-
.target_trait_ref(db)
782-
.expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved
783-
.trait_;
779+
let type_alias_data = db.type_alias_data(type_alias);
780+
let impl_id = match type_alias.lookup(db).container {
781+
ContainerId::ImplId(it) => it,
782+
_ => panic!("assoc ty value should be in impl"),
783+
};
784+
785+
let impl_data = db.impl_data(impl_id);
786+
let resolver = impl_id.resolver(db);
787+
let target_ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
788+
let target_trait = impl_data
789+
.target_trait
790+
.as_ref()
791+
.and_then(|trait_ref| TraitRef::from_hir(db, &resolver, &trait_ref, Some(target_ty)))
792+
.expect("assoc ty value should not exist"); // we don't return any assoc ty values if the impl'd trait can't be resolved
793+
784794
let assoc_ty = db
785-
.trait_data(trait_)
786-
.associated_type_by_name(&type_alias.name(db))
795+
.trait_data(target_trait.trait_)
796+
.associated_type_by_name(&type_alias_data.name)
787797
.expect("assoc ty value should not exist"); // validated when building the impl data as well
788-
let generic_params = db.generic_params(impl_block.id.into());
798+
let generic_params = db.generic_params(impl_id.into());
789799
let bound_vars = Substs::bound_vars(&generic_params);
790-
let ty = db.ty(type_alias.id.into()).subst(&bound_vars);
800+
let ty = db.ty(type_alias.into()).subst(&bound_vars);
791801
let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) };
792802
let value = chalk_rust_ir::AssociatedTyValue {
793-
impl_id,
803+
impl_id: Impl::ImplBlock(impl_id.into()).to_chalk(db),
794804
associated_ty_id: assoc_ty.to_chalk(db),
795805
value: make_binders(value_bound, bound_vars.len()),
796806
};

0 commit comments

Comments
 (0)