Skip to content

Commit 972e1f4

Browse files
Merge #8445
8445: `hir_ty` cleanup r=flodiebold a=flodiebold Move lots of things around within `hir_ty`. Most notably, all the Chalk-related stuff moves from within `traits/` to the top-level, since Chalk isn't purely a "traits thing" anymore. Co-authored-by: Florian Diebold <[email protected]>
2 parents a6b65cf + fbe9804 commit 972e1f4

File tree

14 files changed

+355
-361
lines changed

14 files changed

+355
-361
lines changed

crates/hir_ty/src/chalk_cast.rs

-16
This file was deleted.

crates/hir_ty/src/traits/chalk.rs renamed to crates/hir_ty/src/chalk_db.rs

+87-63
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
//! Conversion code from/to Chalk.
1+
//! The implementation of `RustIrDatabase` for Chalk, which provides information
2+
//! about the code that Chalk needs.
23
use std::sync::Arc;
34

45
use log::debug;
56

6-
use chalk_ir::{fold::shift::Shift, CanonicalVarKinds};
7+
use chalk_ir::{cast::Cast, fold::shift::Shift, CanonicalVarKinds};
78
use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
89

9-
use base_db::{salsa::InternKey, CrateId};
10+
use base_db::CrateId;
1011
use hir_def::{
1112
lang_item::{lang_attr, LangItemTarget},
12-
AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId,
13+
AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId,
1314
};
1415
use hir_expand::name::name;
1516

16-
use super::ChalkContext;
1717
use crate::{
1818
db::HirDatabase,
1919
display::HirDisplay,
20-
from_assoc_type_id, make_only_type_binders,
20+
from_assoc_type_id, from_chalk_trait_id, make_only_type_binders,
21+
mapping::{from_chalk, ToChalk, TypeAliasAsValue},
2122
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
2223
to_assoc_type_id, to_chalk_trait_id,
24+
traits::ChalkContext,
2325
utils::generics,
24-
AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution,
25-
TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause,
26+
AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy,
27+
ProjectionTyExt, QuantifiedWhereClause, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder,
28+
TyExt, TyKind, WhereClause,
2629
};
27-
use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, TypeAliasAsValue};
2830

29-
pub use self::interner::Interner;
30-
pub(crate) use self::interner::*;
31-
32-
pub(super) mod tls;
33-
mod interner;
34-
mod mapping;
35-
36-
pub(crate) trait ToChalk {
37-
type Chalk;
38-
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk;
39-
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self;
40-
}
41-
42-
pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
43-
where
44-
T: ToChalk<Chalk = ChalkT>,
45-
{
46-
T::from_chalk(db, chalk)
47-
}
31+
pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
32+
pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
33+
pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>;
34+
pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>;
35+
pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
36+
37+
pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
38+
pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
39+
pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
40+
pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
41+
pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
42+
pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
43+
pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
44+
pub(crate) type Variances = chalk_ir::Variances<Interner>;
4845

4946
impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
5047
fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> {
@@ -82,7 +79,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
8279
binders: &CanonicalVarKinds<Interner>,
8380
) -> Vec<ImplId> {
8481
debug!("impls_for_trait {:?}", trait_id);
85-
let trait_: hir_def::TraitId = from_chalk(self.db, trait_id);
82+
let trait_: hir_def::TraitId = from_chalk_trait_id(trait_id);
8683

8784
let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone();
8885

@@ -164,7 +161,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
164161
Some(LangItemTarget::TraitId(trait_)) => trait_,
165162
_ => return None,
166163
};
167-
Some(trait_.to_chalk(self.db))
164+
Some(to_chalk_trait_id(trait_))
168165
}
169166

170167
fn program_clauses_for_env(
@@ -311,7 +308,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
311308
}
312309

313310
fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String {
314-
let id = from_chalk(self.db, trait_id);
311+
let id = from_chalk_trait_id(trait_id);
315312
self.db.trait_data(id).name.to_string()
316313
}
317314
fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String {
@@ -416,7 +413,7 @@ pub(crate) fn trait_datum_query(
416413
trait_id: TraitId,
417414
) -> Arc<TraitDatum> {
418415
debug!("trait_datum {:?}", trait_id);
419-
let trait_: hir_def::TraitId = from_chalk(db, trait_id);
416+
let trait_ = from_chalk_trait_id(trait_id);
420417
let trait_data = db.trait_data(trait_);
421418
debug!("trait {:?} = {:?}", trait_id, trait_data.name);
422419
let generic_params = generics(db.upcast(), trait_.into());
@@ -679,38 +676,65 @@ pub(crate) fn adt_variance_query(
679676
)
680677
}
681678

682-
impl From<FnDefId> for crate::db::InternedCallableDefId {
683-
fn from(fn_def_id: FnDefId) -> Self {
684-
InternKey::from_intern_id(fn_def_id.0)
685-
}
686-
}
687-
688-
impl From<crate::db::InternedCallableDefId> for FnDefId {
689-
fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self {
690-
chalk_ir::FnDefId(callable_def_id.as_intern_id())
691-
}
692-
}
693-
694-
impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId {
695-
fn from(id: OpaqueTyId) -> Self {
696-
InternKey::from_intern_id(id.0)
697-
}
698-
}
699-
700-
impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId {
701-
fn from(id: crate::db::InternedOpaqueTyId) -> Self {
702-
chalk_ir::OpaqueTyId(id.as_intern_id())
703-
}
704-
}
705-
706-
impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId {
707-
fn from(id: chalk_ir::ClosureId<Interner>) -> Self {
708-
Self::from_intern_id(id.0)
709-
}
679+
pub(super) fn convert_where_clauses(
680+
db: &dyn HirDatabase,
681+
def: GenericDefId,
682+
substs: &Substitution,
683+
) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> {
684+
let generic_predicates = db.generic_predicates(def);
685+
let mut result = Vec::with_capacity(generic_predicates.len());
686+
for pred in generic_predicates.iter() {
687+
result.push(pred.clone().substitute(&Interner, substs));
688+
}
689+
result
710690
}
711691

712-
impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> {
713-
fn from(id: crate::db::InternedClosureId) -> Self {
714-
chalk_ir::ClosureId(id.as_intern_id())
692+
pub(super) fn generic_predicate_to_inline_bound(
693+
db: &dyn HirDatabase,
694+
pred: &QuantifiedWhereClause,
695+
self_ty: &Ty,
696+
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
697+
// An InlineBound is like a GenericPredicate, except the self type is left out.
698+
// We don't have a special type for this, but Chalk does.
699+
let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE);
700+
let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
701+
match pred {
702+
WhereClause::Implemented(trait_ref) => {
703+
if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in {
704+
// we can only convert predicates back to type bounds if they
705+
// have the expected self type
706+
return None;
707+
}
708+
let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..]
709+
.iter()
710+
.map(|ty| ty.clone().cast(&Interner))
711+
.collect();
712+
let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self };
713+
Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound)))
714+
}
715+
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
716+
if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in {
717+
return None;
718+
}
719+
let trait_ = projection_ty.trait_(db);
720+
let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..]
721+
.iter()
722+
.map(|ty| ty.clone().cast(&Interner))
723+
.collect();
724+
let alias_eq_bound = rust_ir::AliasEqBound {
725+
value: ty.clone(),
726+
trait_bound: rust_ir::TraitBound {
727+
trait_id: to_chalk_trait_id(trait_),
728+
args_no_self,
729+
},
730+
associated_ty_id: projection_ty.associated_ty_id,
731+
parameters: Vec::new(), // FIXME we don't support generic associated types yet
732+
};
733+
Some(chalk_ir::Binders::new(
734+
binders,
735+
rust_ir::InlineBound::AliasEqBound(alias_eq_bound),
736+
))
737+
}
738+
_ => None,
715739
}
716740
}

crates/hir_ty/src/db.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! FIXME: write short doc here
1+
//! The home of `HirDatabase`, which is the Salsa database containing all the
2+
//! type inference-related queries.
23
34
use std::sync::Arc;
45

@@ -10,9 +11,9 @@ use hir_def::{
1011
use la_arena::ArenaMap;
1112

1213
use crate::{
14+
chalk_db,
1315
method_resolution::{InherentImpls, TraitImpls},
14-
traits::chalk,
15-
Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, PolyFnSig,
16+
Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig,
1617
QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
1718
};
1819
use hir_expand::name::Name;
@@ -94,33 +95,38 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
9495
#[salsa::interned]
9596
fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
9697

97-
#[salsa::invoke(chalk::associated_ty_data_query)]
98-
fn associated_ty_data(&self, id: chalk::AssocTypeId) -> Arc<chalk::AssociatedTyDatum>;
98+
#[salsa::invoke(chalk_db::associated_ty_data_query)]
99+
fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>;
99100

100-
#[salsa::invoke(chalk::trait_datum_query)]
101-
fn trait_datum(&self, krate: CrateId, trait_id: chalk::TraitId) -> Arc<chalk::TraitDatum>;
101+
#[salsa::invoke(chalk_db::trait_datum_query)]
102+
fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId)
103+
-> Arc<chalk_db::TraitDatum>;
102104

103-
#[salsa::invoke(chalk::struct_datum_query)]
104-
fn struct_datum(&self, krate: CrateId, struct_id: chalk::AdtId) -> Arc<chalk::StructDatum>;
105+
#[salsa::invoke(chalk_db::struct_datum_query)]
106+
fn struct_datum(
107+
&self,
108+
krate: CrateId,
109+
struct_id: chalk_db::AdtId,
110+
) -> Arc<chalk_db::StructDatum>;
105111

106-
#[salsa::invoke(crate::traits::chalk::impl_datum_query)]
107-
fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
112+
#[salsa::invoke(chalk_db::impl_datum_query)]
113+
fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>;
108114

109-
#[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
110-
fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>;
115+
#[salsa::invoke(chalk_db::fn_def_datum_query)]
116+
fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>;
111117

112-
#[salsa::invoke(crate::traits::chalk::fn_def_variance_query)]
113-
fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances;
118+
#[salsa::invoke(chalk_db::fn_def_variance_query)]
119+
fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk_db::Variances;
114120

115-
#[salsa::invoke(crate::traits::chalk::adt_variance_query)]
116-
fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances;
121+
#[salsa::invoke(chalk_db::adt_variance_query)]
122+
fn adt_variance(&self, krate: CrateId, adt_id: chalk_db::AdtId) -> chalk_db::Variances;
117123

118-
#[salsa::invoke(crate::traits::chalk::associated_ty_value_query)]
124+
#[salsa::invoke(chalk_db::associated_ty_value_query)]
119125
fn associated_ty_value(
120126
&self,
121127
krate: CrateId,
122-
id: chalk::AssociatedTyValueId,
123-
) -> Arc<chalk::AssociatedTyValue>;
128+
id: chalk_db::AssociatedTyValueId,
129+
) -> Arc<chalk_db::AssociatedTyValue>;
124130

125131
#[salsa::invoke(crate::traits::trait_solve_query)]
126132
fn trait_solve(
@@ -129,12 +135,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
129135
goal: crate::Canonical<crate::InEnvironment<crate::DomainGoal>>,
130136
) -> Option<crate::Solution>;
131137

132-
#[salsa::invoke(crate::traits::chalk::program_clauses_for_chalk_env_query)]
138+
#[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)]
133139
fn program_clauses_for_chalk_env(
134140
&self,
135141
krate: CrateId,
136-
env: chalk_ir::Environment<chalk::Interner>,
137-
) -> chalk_ir::ProgramClauses<chalk::Interner>;
142+
env: chalk_ir::Environment<Interner>,
143+
) -> chalk_ir::ProgramClauses<Interner>;
138144
}
139145

140146
fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {

crates/hir_ty/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! FIXME: write short doc here
1+
//! Type inference-based diagnostics.
22
mod expr;
33
mod match_check;
44
mod unsafe_check;

crates/hir_ty/src/display.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! FIXME: write short doc here
1+
//! The `HirDisplay` trait, which serves two purposes: Turning various bits from
2+
//! HIR back into source code, and just displaying them for debugging/testing
3+
//! purposes.
24
35
use std::{
46
array,
@@ -20,11 +22,11 @@ use hir_expand::name::Name;
2022

2123
use crate::{
2224
const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id,
23-
from_placeholder_idx, lt_from_placeholder_idx, primitive, subst_prefix, to_assoc_type_id,
24-
traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId,
25-
CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime,
26-
LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt,
27-
QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
25+
from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix,
26+
to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const,
27+
ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData,
28+
LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause,
29+
Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
2830
};
2931

3032
pub struct HirFormatter<'a> {

crates/hir_ty/src/infer/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ use syntax::ast::RangeOp;
1717
use crate::{
1818
autoderef, dummy_usize_const,
1919
lower::lower_to_chalk_mutability,
20+
mapping::from_chalk,
2021
method_resolution, op,
2122
primitive::{self, UintTy},
2223
static_lifetime, to_chalk_trait_id,
23-
traits::{chalk::from_chalk, FnTrait},
24+
traits::FnTrait,
2425
utils::{generics, Generics},
2526
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
2627
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind,

0 commit comments

Comments
 (0)