@@ -382,43 +382,47 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
382382 where
383383 I :: Const : TypeSuperFoldable < I > ,
384384 {
385+ // We could canonicalize all consts with static types, but the only ones we
386+ // *really* need to worry about are the ones that we end up putting into `CanonicalVarKind`
387+ // since canonical vars can't reference other canonical vars.
388+ let ty = c
389+ . ty ( )
390+ . fold_with ( & mut RegionsToStatic { interner : self . interner ( ) , binder : ty:: INNERMOST } ) ;
385391 let kind = match c. kind ( ) {
386- ty:: ConstKind :: Infer ( i) => {
387- // FIXME: we should fold the ty too eventually
388- match i {
389- ty:: InferConst :: Var ( vid) => {
390- assert_eq ! (
391- self . infcx. root_ct_var( vid) ,
392- vid,
393- "region vid should have been resolved fully before canonicalization"
394- ) ;
395- assert_eq ! (
396- self . infcx. probe_ct_var( vid) ,
397- None ,
398- "region vid should have been resolved fully before canonicalization"
399- ) ;
400- CanonicalVarKind :: Const ( self . infcx . universe_of_ct ( vid) . unwrap ( ) , c. ty ( ) )
401- }
402- ty:: InferConst :: EffectVar ( _) => CanonicalVarKind :: Effect ,
403- ty:: InferConst :: Fresh ( _) => todo ! ( ) ,
392+ ty:: ConstKind :: Infer ( i) => match i {
393+ ty:: InferConst :: Var ( vid) => {
394+ assert_eq ! (
395+ self . infcx. root_ct_var( vid) ,
396+ vid,
397+ "region vid should have been resolved fully before canonicalization"
398+ ) ;
399+ assert_eq ! (
400+ self . infcx. probe_ct_var( vid) ,
401+ None ,
402+ "region vid should have been resolved fully before canonicalization"
403+ ) ;
404+ CanonicalVarKind :: Const ( self . infcx . universe_of_ct ( vid) . unwrap ( ) , ty)
404405 }
405- }
406+ ty:: InferConst :: EffectVar ( _) => CanonicalVarKind :: Effect ,
407+ ty:: InferConst :: Fresh ( _) => todo ! ( ) ,
408+ } ,
406409 ty:: ConstKind :: Placeholder ( placeholder) => match self . canonicalize_mode {
407410 CanonicalizeMode :: Input => CanonicalVarKind :: PlaceholderConst (
408411 PlaceholderLike :: new ( placeholder. universe ( ) , self . variables . len ( ) . into ( ) ) ,
409- c . ty ( ) ,
412+ ty ,
410413 ) ,
411414 CanonicalizeMode :: Response { .. } => {
412- CanonicalVarKind :: PlaceholderConst ( placeholder, c . ty ( ) )
415+ CanonicalVarKind :: PlaceholderConst ( placeholder, ty )
413416 }
414417 } ,
415418 ty:: ConstKind :: Param ( _) => match self . canonicalize_mode {
416419 CanonicalizeMode :: Input => CanonicalVarKind :: PlaceholderConst (
417420 PlaceholderLike :: new ( ty:: UniverseIndex :: ROOT , self . variables . len ( ) . into ( ) ) ,
418- c . ty ( ) ,
421+ ty ,
419422 ) ,
420423 CanonicalizeMode :: Response { .. } => panic ! ( "param ty in response: {c:?}" ) ,
421424 } ,
425+ // FIXME: See comment above -- we could fold the region separately or something.
422426 ty:: ConstKind :: Bound ( _, _)
423427 | ty:: ConstKind :: Unevaluated ( _)
424428 | ty:: ConstKind :: Value ( _)
@@ -435,6 +439,35 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
435439 } ) ,
436440 ) ;
437441
438- Const :: new_anon_bound ( self . interner ( ) , self . binder_index , var, c. ty ( ) )
442+ Const :: new_anon_bound ( self . interner ( ) , self . binder_index , var, ty)
443+ }
444+ }
445+
446+ struct RegionsToStatic < I > {
447+ interner : I ,
448+ binder : ty:: DebruijnIndex ,
449+ }
450+
451+ impl < I : Interner > TypeFolder < I > for RegionsToStatic < I > {
452+ fn interner ( & self ) -> I {
453+ self . interner
454+ }
455+
456+ fn fold_binder < T > ( & mut self , t : I :: Binder < T > ) -> I :: Binder < T >
457+ where
458+ T : TypeFoldable < I > ,
459+ I :: Binder < T > : TypeSuperFoldable < I > ,
460+ {
461+ self . binder . shift_in ( 1 ) ;
462+ let t = t. fold_with ( self ) ;
463+ self . binder . shift_out ( 1 ) ;
464+ t
465+ }
466+
467+ fn fold_region ( & mut self , r : I :: Region ) -> I :: Region {
468+ match r. kind ( ) {
469+ ty:: ReBound ( db, _) if self . binder > db => r,
470+ _ => Region :: new_static ( self . interner ( ) ) ,
471+ }
439472 }
440473}
0 commit comments