88
99use rustc_middle:: traits:: ChalkRustInterner as RustInterner ;
1010use rustc_middle:: ty:: subst:: { InternalSubsts , Subst , SubstsRef } ;
11- use rustc_middle:: ty:: { self , AssocItemContainer , AssocKind , TyCtxt } ;
11+ use rustc_middle:: ty:: { self , AssocItemContainer , AssocKind , TyCtxt , TypeFoldable } ;
1212
1313use rustc_hir:: def_id:: DefId ;
1414use rustc_hir:: Unsafety ;
@@ -18,11 +18,13 @@ use rustc_span::symbol::sym;
1818use std:: fmt;
1919use std:: sync:: Arc ;
2020
21- use crate :: chalk:: lowering:: LowerInto ;
21+ use crate :: chalk:: lowering:: { self , LowerInto } ;
2222
2323pub struct RustIrDatabase < ' tcx > {
2424 pub tcx : TyCtxt < ' tcx > ,
2525 pub interner : RustInterner < ' tcx > ,
26+ pub restatic_placeholder : ty:: Region < ' tcx > ,
27+ pub reempty_placeholder : ty:: Region < ' tcx > ,
2628}
2729
2830impl fmt:: Debug for RustIrDatabase < ' _ > {
@@ -31,6 +33,26 @@ impl fmt::Debug for RustIrDatabase<'_> {
3133 }
3234}
3335
36+ impl < ' tcx > RustIrDatabase < ' tcx > {
37+ fn where_clauses_for (
38+ & self ,
39+ def_id : DefId ,
40+ bound_vars : SubstsRef < ' tcx > ,
41+ ) -> Vec < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > {
42+ let predicates = self . tcx . predicates_of ( def_id) . predicates ;
43+ let mut regions_substitutor = lowering:: RegionsSubstitutor :: new (
44+ self . tcx ,
45+ self . restatic_placeholder ,
46+ self . reempty_placeholder ,
47+ ) ;
48+ predicates
49+ . iter ( )
50+ . map ( |( wc, _) | wc. subst ( self . tcx , bound_vars) )
51+ . map ( |wc| wc. fold_with ( & mut regions_substitutor) )
52+ . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( )
53+ }
54+ }
55+
3456impl < ' tcx > chalk_solve:: RustIrDatabase < RustInterner < ' tcx > > for RustIrDatabase < ' tcx > {
3557 fn interner ( & self ) -> & RustInterner < ' tcx > {
3658 & self . interner
@@ -55,11 +77,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
5577 // FIXME(chalk): this really isn't right I don't think. The functions
5678 // for GATs are a bit hard to figure out. Are these supposed to be where
5779 // clauses or bounds?
58- let predicates = self . tcx . predicates_defined_on ( def_id) . predicates ;
59- let where_clauses: Vec < _ > = predicates
60- . iter ( )
61- . map ( |( wc, _) | wc. subst ( self . tcx , & bound_vars) )
62- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
80+ let where_clauses = self . where_clauses_for ( def_id, bound_vars) ;
6381
6482 Arc :: new ( chalk_solve:: rust_ir:: AssociatedTyDatum {
6583 trait_id : chalk_ir:: TraitId ( trait_def_id) ,
@@ -81,11 +99,9 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
8199
82100 let bound_vars = bound_vars_for_item ( self . tcx , def_id) ;
83101 let binders = binders_for ( & self . interner , bound_vars) ;
84- let predicates = self . tcx . predicates_defined_on ( def_id) . predicates ;
85- let where_clauses: Vec < _ > = predicates
86- . iter ( )
87- . map ( |( wc, _) | wc. subst ( self . tcx , & bound_vars) )
88- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
102+
103+ let where_clauses = self . where_clauses_for ( def_id, bound_vars) ;
104+
89105 let associated_ty_ids: Vec < _ > = self
90106 . tcx
91107 . associated_items ( def_id)
@@ -140,12 +156,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
140156 let bound_vars = bound_vars_for_item ( self . tcx , adt_def. did ) ;
141157 let binders = binders_for ( & self . interner , bound_vars) ;
142158
143- let predicates = self . tcx . predicates_of ( adt_def. did ) . predicates ;
144- let where_clauses: Vec < _ > = predicates
145- . iter ( )
146- . map ( |( wc, _) | wc. subst ( self . tcx , bound_vars) )
147- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) )
148- . collect ( ) ;
159+ let where_clauses = self . where_clauses_for ( adt_def. did , bound_vars) ;
160+
149161 let variants: Vec < _ > = adt_def
150162 . variants
151163 . iter ( )
@@ -201,14 +213,11 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
201213 let bound_vars = bound_vars_for_item ( self . tcx , def_id) ;
202214 let binders = binders_for ( & self . interner , bound_vars) ;
203215
204- let predicates = self . tcx . predicates_defined_on ( def_id) . predicates ;
205- let where_clauses: Vec < _ > = predicates
206- . iter ( )
207- . map ( |( wc, _) | wc. subst ( self . tcx , & bound_vars) )
208- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
216+ let where_clauses = self . where_clauses_for ( def_id, bound_vars) ;
209217
210218 let sig = self . tcx . fn_sig ( def_id) ;
211219 let inputs_and_output = sig. inputs_and_output ( ) ;
220+ let inputs_and_output = inputs_and_output. subst ( self . tcx , bound_vars) ;
212221 let ( inputs_and_output, iobinders, _) = crate :: chalk:: lowering:: collect_bound_vars (
213222 & self . interner ,
214223 self . tcx ,
@@ -253,12 +262,14 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
253262
254263 let trait_ref = self . tcx . impl_trait_ref ( def_id) . expect ( "not an impl" ) ;
255264 let trait_ref = trait_ref. subst ( self . tcx , bound_vars) ;
265+ let mut regions_substitutor = lowering:: RegionsSubstitutor :: new (
266+ self . tcx ,
267+ self . restatic_placeholder ,
268+ self . reempty_placeholder ,
269+ ) ;
270+ let trait_ref = trait_ref. fold_with ( & mut regions_substitutor) ;
256271
257- let predicates = self . tcx . predicates_of ( def_id) . predicates ;
258- let where_clauses: Vec < _ > = predicates
259- . iter ( )
260- . map ( |( wc, _) | wc. subst ( self . tcx , bound_vars) )
261- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
272+ let where_clauses = self . where_clauses_for ( def_id, bound_vars) ;
262273
263274 let value = chalk_solve:: rust_ir:: ImplDatumBound {
264275 trait_ref : trait_ref. lower_into ( & self . interner ) ,
@@ -293,6 +304,12 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
293304
294305 let self_ty = trait_ref. self_ty ( ) ;
295306 let self_ty = self_ty. subst ( self . tcx , bound_vars) ;
307+ let mut regions_substitutor = lowering:: RegionsSubstitutor :: new (
308+ self . tcx ,
309+ self . restatic_placeholder ,
310+ self . reempty_placeholder ,
311+ ) ;
312+ let self_ty = self_ty. fold_with ( & mut regions_substitutor) ;
296313 let lowered_ty = self_ty. lower_into ( & self . interner ) ;
297314
298315 parameters[ 0 ] . assert_ty_ref ( & self . interner ) . could_match ( & self . interner , & lowered_ty)
@@ -370,11 +387,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
370387 ) -> Arc < chalk_solve:: rust_ir:: OpaqueTyDatum < RustInterner < ' tcx > > > {
371388 let bound_vars = bound_vars_for_item ( self . tcx , opaque_ty_id. 0 ) ;
372389 let binders = binders_for ( & self . interner , bound_vars) ;
373- let predicates = self . tcx . predicates_defined_on ( opaque_ty_id. 0 ) . predicates ;
374- let where_clauses: Vec < _ > = predicates
375- . iter ( )
376- . map ( |( wc, _) | wc. subst ( self . tcx , & bound_vars) )
377- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
390+ let where_clauses = self . where_clauses_for ( opaque_ty_id. 0 , bound_vars) ;
378391
379392 let value = chalk_solve:: rust_ir:: OpaqueTyDatumBound {
380393 bounds : chalk_ir:: Binders :: new ( binders. clone ( ) , vec ! [ ] ) ,
0 commit comments