@@ -4,30 +4,28 @@ use std::sync::Arc;
4
4
5
5
use log:: debug;
6
6
7
- use chalk_ir:: { fold:: shift:: Shift , CanonicalVarKinds } ;
7
+ use chalk_ir:: { cast :: Cast , fold:: shift:: Shift , CanonicalVarKinds } ;
8
8
use chalk_solve:: rust_ir:: { self , OpaqueTyDatumBound , WellKnownTrait } ;
9
9
10
- use base_db:: { salsa :: InternKey , CrateId } ;
10
+ use base_db:: CrateId ;
11
11
use hir_def:: {
12
12
lang_item:: { lang_attr, LangItemTarget } ,
13
- AssocContainerId , AssocItemId , HasModule , Lookup , TypeAliasId ,
13
+ AssocContainerId , AssocItemId , GenericDefId , HasModule , Lookup , TypeAliasId ,
14
14
} ;
15
15
use hir_expand:: name:: name;
16
16
17
17
use crate :: {
18
18
db:: HirDatabase ,
19
19
display:: HirDisplay ,
20
20
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 } ,
25
22
method_resolution:: { TyFingerprint , ALL_FLOAT_FPS , ALL_INT_FPS } ,
26
23
to_assoc_type_id, to_chalk_trait_id,
27
24
traits:: ChalkContext ,
28
25
utils:: generics,
29
26
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 ,
31
29
} ;
32
30
33
31
pub ( crate ) type AssociatedTyDatum = chalk_solve:: rust_ir:: AssociatedTyDatum < Interner > ;
@@ -39,7 +37,6 @@ pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
39
37
pub ( crate ) type AssocTypeId = chalk_ir:: AssocTypeId < Interner > ;
40
38
pub ( crate ) type TraitId = chalk_ir:: TraitId < Interner > ;
41
39
pub ( crate ) type AdtId = chalk_ir:: AdtId < Interner > ;
42
- pub ( crate ) type OpaqueTyId = chalk_ir:: OpaqueTyId < Interner > ;
43
40
pub ( crate ) type ImplId = chalk_ir:: ImplId < Interner > ;
44
41
pub ( crate ) type AssociatedTyValueId = chalk_solve:: rust_ir:: AssociatedTyValueId < Interner > ;
45
42
pub ( crate ) type AssociatedTyValue = chalk_solve:: rust_ir:: AssociatedTyValue < Interner > ;
@@ -679,38 +676,62 @@ pub(crate) fn adt_variance_query(
679
676
)
680
677
}
681
678
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
710
690
}
711
691
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 ,
715
736
}
716
737
}
0 commit comments