11use std:: collections:: VecDeque ;
22
3- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
3+ use rustc_data_structures:: fx:: FxHashSet ;
44use rustc_hir:: LangItem ;
55use rustc_hir:: def_id:: DefId ;
66use rustc_infer:: infer:: InferCtxt ;
77use rustc_infer:: traits:: PolyTraitObligation ;
88pub use rustc_infer:: traits:: util:: * ;
9- use rustc_middle:: bug;
109use rustc_middle:: ty:: fast_reject:: DeepRejectCtxt ;
1110use rustc_middle:: ty:: {
1211 self , PolyTraitPredicate , PredicatePolarity , SizedTraitKind , TraitPredicate , TraitRef , Ty ,
13- TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitableExt ,
12+ TyCtxt , TypeFoldable , TypeVisitableExt ,
1413} ;
15- pub use rustc_next_trait_solver:: placeholder:: BoundVarReplacer ;
14+ pub use rustc_next_trait_solver:: placeholder:: { BoundVarReplacer , PlaceholderReplacer } ;
1615use rustc_span:: Span ;
1716use smallvec:: { SmallVec , smallvec} ;
1817use tracing:: debug;
@@ -205,7 +204,7 @@ pub fn with_replaced_escaping_bound_vars<
205204 BoundVarReplacer :: replace_bound_vars ( infcx, universe_indices, value) ;
206205 let result = f ( value) ;
207206 PlaceholderReplacer :: replace_placeholders (
208- infcx,
207+ infcx. tcx ,
209208 mapped_regions,
210209 mapped_types,
211210 mapped_consts,
@@ -217,154 +216,6 @@ pub fn with_replaced_escaping_bound_vars<
217216 }
218217}
219218
220- /// The inverse of [`BoundVarReplacer`]: replaces placeholders with the bound vars from which they came.
221- pub struct PlaceholderReplacer < ' a , ' tcx > {
222- infcx : & ' a InferCtxt < ' tcx > ,
223- mapped_regions : FxIndexMap < ty:: PlaceholderRegion < ' tcx > , ty:: BoundRegion < ' tcx > > ,
224- mapped_types : FxIndexMap < ty:: PlaceholderType < ' tcx > , ty:: BoundTy < ' tcx > > ,
225- mapped_consts : FxIndexMap < ty:: PlaceholderConst < ' tcx > , ty:: BoundConst < ' tcx > > ,
226- universe_indices : & ' a [ Option < ty:: UniverseIndex > ] ,
227- current_index : ty:: DebruijnIndex ,
228- }
229-
230- impl < ' a , ' tcx > PlaceholderReplacer < ' a , ' tcx > {
231- pub fn replace_placeholders < T : TypeFoldable < TyCtxt < ' tcx > > > (
232- infcx : & ' a InferCtxt < ' tcx > ,
233- mapped_regions : FxIndexMap < ty:: PlaceholderRegion < ' tcx > , ty:: BoundRegion < ' tcx > > ,
234- mapped_types : FxIndexMap < ty:: PlaceholderType < ' tcx > , ty:: BoundTy < ' tcx > > ,
235- mapped_consts : FxIndexMap < ty:: PlaceholderConst < ' tcx > , ty:: BoundConst < ' tcx > > ,
236- universe_indices : & ' a [ Option < ty:: UniverseIndex > ] ,
237- value : T ,
238- ) -> T {
239- let mut replacer = PlaceholderReplacer {
240- infcx,
241- mapped_regions,
242- mapped_types,
243- mapped_consts,
244- universe_indices,
245- current_index : ty:: INNERMOST ,
246- } ;
247- value. fold_with ( & mut replacer)
248- }
249- }
250-
251- impl < ' tcx > TypeFolder < TyCtxt < ' tcx > > for PlaceholderReplacer < ' _ , ' tcx > {
252- fn cx ( & self ) -> TyCtxt < ' tcx > {
253- self . infcx . tcx
254- }
255-
256- fn fold_binder < T : TypeFoldable < TyCtxt < ' tcx > > > (
257- & mut self ,
258- t : ty:: Binder < ' tcx , T > ,
259- ) -> ty:: Binder < ' tcx , T > {
260- if !t. has_placeholders ( ) && !t. has_infer ( ) {
261- return t;
262- }
263- self . current_index . shift_in ( 1 ) ;
264- let t = t. super_fold_with ( self ) ;
265- self . current_index . shift_out ( 1 ) ;
266- t
267- }
268-
269- fn fold_region ( & mut self , r0 : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
270- let r1 = match r0. kind ( ) {
271- ty:: ReVar ( vid) => self
272- . infcx
273- . inner
274- . borrow_mut ( )
275- . unwrap_region_constraints ( )
276- . opportunistic_resolve_var ( self . infcx . tcx , vid) ,
277- _ => r0,
278- } ;
279-
280- let r2 = match r1. kind ( ) {
281- ty:: RePlaceholder ( p) => {
282- let replace_var = self . mapped_regions . get ( & p) ;
283- match replace_var {
284- Some ( replace_var) => {
285- let index = self
286- . universe_indices
287- . iter ( )
288- . position ( |u| matches ! ( u, Some ( pu) if * pu == p. universe) )
289- . unwrap_or_else ( || bug ! ( "Unexpected placeholder universe." ) ) ;
290- let db = ty:: DebruijnIndex :: from_usize (
291- self . universe_indices . len ( ) - index + self . current_index . as_usize ( ) - 1 ,
292- ) ;
293- ty:: Region :: new_bound ( self . cx ( ) , db, * replace_var)
294- }
295- None => r1,
296- }
297- }
298- _ => r1,
299- } ;
300-
301- debug ! ( ?r0, ?r1, ?r2, "fold_region" ) ;
302-
303- r2
304- }
305-
306- fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
307- let ty = self . infcx . shallow_resolve ( ty) ;
308- match * ty. kind ( ) {
309- ty:: Placeholder ( p) => {
310- let replace_var = self . mapped_types . get ( & p) ;
311- match replace_var {
312- Some ( replace_var) => {
313- let index = self
314- . universe_indices
315- . iter ( )
316- . position ( |u| matches ! ( u, Some ( pu) if * pu == p. universe) )
317- . unwrap_or_else ( || bug ! ( "Unexpected placeholder universe." ) ) ;
318- let db = ty:: DebruijnIndex :: from_usize (
319- self . universe_indices . len ( ) - index + self . current_index . as_usize ( ) - 1 ,
320- ) ;
321- Ty :: new_bound ( self . infcx . tcx , db, * replace_var)
322- }
323- None => {
324- if ty. has_infer ( ) {
325- ty. super_fold_with ( self )
326- } else {
327- ty
328- }
329- }
330- }
331- }
332-
333- _ if ty. has_placeholders ( ) || ty. has_infer ( ) => ty. super_fold_with ( self ) ,
334- _ => ty,
335- }
336- }
337-
338- fn fold_const ( & mut self , ct : ty:: Const < ' tcx > ) -> ty:: Const < ' tcx > {
339- let ct = self . infcx . shallow_resolve_const ( ct) ;
340- if let ty:: ConstKind :: Placeholder ( p) = ct. kind ( ) {
341- let replace_var = self . mapped_consts . get ( & p) ;
342- match replace_var {
343- Some ( replace_var) => {
344- let index = self
345- . universe_indices
346- . iter ( )
347- . position ( |u| matches ! ( u, Some ( pu) if * pu == p. universe) )
348- . unwrap_or_else ( || bug ! ( "Unexpected placeholder universe." ) ) ;
349- let db = ty:: DebruijnIndex :: from_usize (
350- self . universe_indices . len ( ) - index + self . current_index . as_usize ( ) - 1 ,
351- ) ;
352- ty:: Const :: new_bound ( self . infcx . tcx , db, * replace_var)
353- }
354- None => {
355- if ct. has_infer ( ) {
356- ct. super_fold_with ( self )
357- } else {
358- ct
359- }
360- }
361- }
362- } else {
363- ct. super_fold_with ( self )
364- }
365- }
366- }
367-
368219pub fn sizedness_fast_path < ' tcx > (
369220 tcx : TyCtxt < ' tcx > ,
370221 predicate : ty:: Predicate < ' tcx > ,
0 commit comments