Skip to content

Commit 9fba7cf

Browse files
committed
Move some more stuff to better places
1 parent 5ca481b commit 9fba7cf

File tree

2 files changed

+93
-100
lines changed

2 files changed

+93
-100
lines changed

crates/hir_ty/src/chalk_db.rs

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@ use std::sync::Arc;
44

55
use log::debug;
66

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

10-
use base_db::{salsa::InternKey, CrateId};
10+
use base_db::CrateId;
1111
use hir_def::{
1212
lang_item::{lang_attr, LangItemTarget},
13-
AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId,
13+
AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId,
1414
};
1515
use hir_expand::name::name;
1616

1717
use crate::{
1818
db::HirDatabase,
1919
display::HirDisplay,
2020
from_assoc_type_id, make_only_type_binders,
21-
mapping::{
22-
convert_where_clauses, from_chalk, generic_predicate_to_inline_bound, ToChalk,
23-
TypeAliasAsValue,
24-
},
21+
mapping::{from_chalk, ToChalk, TypeAliasAsValue},
2522
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
2623
to_assoc_type_id, to_chalk_trait_id,
2724
traits::ChalkContext,
2825
utils::generics,
2926
AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy,
30-
Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause,
27+
ProjectionTyExt, QuantifiedWhereClause, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder,
28+
TyExt, TyKind, WhereClause,
3129
};
3230

3331
pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
@@ -39,7 +37,6 @@ pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
3937
pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
4038
pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
4139
pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
42-
pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
4340
pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
4441
pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
4542
pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
@@ -679,38 +676,62 @@ 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 { trait_id: trait_.to_chalk(db), args_no_self },
727+
associated_ty_id: projection_ty.associated_ty_id,
728+
parameters: Vec::new(), // FIXME we don't support generic associated types yet
729+
};
730+
Some(chalk_ir::Binders::new(
731+
binders,
732+
rust_ir::InlineBound::AliasEqBound(alias_eq_bound),
733+
))
734+
}
735+
_ => None,
715736
}
716737
}

crates/hir_ty/src/mapping.rs

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
//! Chalk (in both directions); plus some helper functions for more specialized
44
//! conversions.
55
6-
use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex};
76
use chalk_solve::rust_ir;
87

98
use base_db::salsa::InternKey;
10-
use hir_def::{GenericDefId, TypeAliasId};
9+
use hir_def::TypeAliasId;
1110

12-
use crate::{
13-
chalk_db, db::HirDatabase, AliasEq, AliasTy, CallableDefId, FnDefId, Interner, ProjectionTyExt,
14-
QuantifiedWhereClause, Substitution, Ty, WhereClause,
15-
};
11+
use crate::{chalk_db, db::HirDatabase, CallableDefId, FnDefId, Interner, OpaqueTyId};
1612

1713
pub(crate) trait ToChalk {
1814
type Chalk;
@@ -80,62 +76,38 @@ impl ToChalk for TypeAliasAsValue {
8076
}
8177
}
8278

83-
pub(super) fn convert_where_clauses(
84-
db: &dyn HirDatabase,
85-
def: GenericDefId,
86-
substs: &Substitution,
87-
) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> {
88-
let generic_predicates = db.generic_predicates(def);
89-
let mut result = Vec::with_capacity(generic_predicates.len());
90-
for pred in generic_predicates.iter() {
91-
result.push(pred.clone().substitute(&Interner, substs));
79+
impl From<FnDefId> for crate::db::InternedCallableDefId {
80+
fn from(fn_def_id: FnDefId) -> Self {
81+
InternKey::from_intern_id(fn_def_id.0)
9282
}
93-
result
9483
}
9584

96-
pub(super) fn generic_predicate_to_inline_bound(
97-
db: &dyn HirDatabase,
98-
pred: &QuantifiedWhereClause,
99-
self_ty: &Ty,
100-
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
101-
// An InlineBound is like a GenericPredicate, except the self type is left out.
102-
// We don't have a special type for this, but Chalk does.
103-
let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE);
104-
let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
105-
match pred {
106-
WhereClause::Implemented(trait_ref) => {
107-
if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in {
108-
// we can only convert predicates back to type bounds if they
109-
// have the expected self type
110-
return None;
111-
}
112-
let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..]
113-
.iter()
114-
.map(|ty| ty.clone().cast(&Interner))
115-
.collect();
116-
let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self };
117-
Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound)))
118-
}
119-
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
120-
if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in {
121-
return None;
122-
}
123-
let trait_ = projection_ty.trait_(db);
124-
let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..]
125-
.iter()
126-
.map(|ty| ty.clone().cast(&Interner))
127-
.collect();
128-
let alias_eq_bound = rust_ir::AliasEqBound {
129-
value: ty.clone(),
130-
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
131-
associated_ty_id: projection_ty.associated_ty_id,
132-
parameters: Vec::new(), // FIXME we don't support generic associated types yet
133-
};
134-
Some(chalk_ir::Binders::new(
135-
binders,
136-
rust_ir::InlineBound::AliasEqBound(alias_eq_bound),
137-
))
138-
}
139-
_ => None,
85+
impl From<crate::db::InternedCallableDefId> for FnDefId {
86+
fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self {
87+
chalk_ir::FnDefId(callable_def_id.as_intern_id())
88+
}
89+
}
90+
91+
impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId {
92+
fn from(id: OpaqueTyId) -> Self {
93+
InternKey::from_intern_id(id.0)
94+
}
95+
}
96+
97+
impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId {
98+
fn from(id: crate::db::InternedOpaqueTyId) -> Self {
99+
chalk_ir::OpaqueTyId(id.as_intern_id())
100+
}
101+
}
102+
103+
impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId {
104+
fn from(id: chalk_ir::ClosureId<Interner>) -> Self {
105+
Self::from_intern_id(id.0)
106+
}
107+
}
108+
109+
impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> {
110+
fn from(id: crate::db::InternedClosureId) -> Self {
111+
chalk_ir::ClosureId(id.as_intern_id())
140112
}
141113
}

0 commit comments

Comments
 (0)