@@ -23,7 +23,7 @@ use infer::InferCtxt;
2323use std:: sync:: atomic:: Ordering ;
2424use ty:: fold:: { TypeFoldable , TypeFolder } ;
2525use ty:: subst:: Kind ;
26- use ty:: { self , BoundTy , BoundVar , Lift , List , Ty , TyCtxt , TypeFlags } ;
26+ use ty:: { self , BoundVar , Lift , List , Ty , TyCtxt , TypeFlags } ;
2727
2828use rustc_data_structures:: fx:: FxHashMap ;
2929use rustc_data_structures:: indexed_vec:: Idx ;
@@ -339,20 +339,51 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
339339
340340 fn fold_ty ( & mut self , t : Ty < ' tcx > ) -> Ty < ' tcx > {
341341 match t. sty {
342- ty:: Infer ( ty:: TyVar ( _) ) => self . canonicalize_ty_var ( CanonicalTyVarKind :: General , t) ,
342+ ty:: Infer ( ty:: TyVar ( vid) ) => {
343+ match self . infcx . unwrap ( ) . probe_ty_var ( vid) {
344+ // `t` could be a float / int variable: canonicalize that instead
345+ Ok ( t) => self . fold_ty ( t) ,
346+
347+ // `TyVar(vid)` is unresolved, track its universe index in the canonicalized
348+ // result
349+ Err ( ui) => self . canonicalize_ty_var (
350+ CanonicalVarInfo {
351+ kind : CanonicalVarKind :: Ty ( CanonicalTyVarKind :: General ( ui) )
352+ } ,
353+ t
354+ )
355+ }
356+ }
343357
344- ty:: Infer ( ty:: IntVar ( _) ) => self . canonicalize_ty_var ( CanonicalTyVarKind :: Int , t) ,
358+ ty:: Infer ( ty:: IntVar ( _) ) => self . canonicalize_ty_var (
359+ CanonicalVarInfo {
360+ kind : CanonicalVarKind :: Ty ( CanonicalTyVarKind :: Int )
361+ } ,
362+ t
363+ ) ,
345364
346- ty:: Infer ( ty:: FloatVar ( _) ) => self . canonicalize_ty_var ( CanonicalTyVarKind :: Float , t) ,
365+ ty:: Infer ( ty:: FloatVar ( _) ) => self . canonicalize_ty_var (
366+ CanonicalVarInfo {
367+ kind : CanonicalVarKind :: Ty ( CanonicalTyVarKind :: Float )
368+ } ,
369+ t
370+ ) ,
347371
348372 ty:: Infer ( ty:: FreshTy ( _) )
349373 | ty:: Infer ( ty:: FreshIntTy ( _) )
350374 | ty:: Infer ( ty:: FreshFloatTy ( _) ) => {
351375 bug ! ( "encountered a fresh type during canonicalization" )
352376 }
353377
354- ty:: Bound ( bound_ty) => {
355- if bound_ty. index >= self . binder_index {
378+ ty:: Placeholder ( placeholder) => self . canonicalize_ty_var (
379+ CanonicalVarInfo {
380+ kind : CanonicalVarKind :: PlaceholderTy ( placeholder)
381+ } ,
382+ t
383+ ) ,
384+
385+ ty:: Bound ( debruijn, _) => {
386+ if debruijn >= self . binder_index {
356387 bug ! ( "escaping bound type during canonicalization" )
357388 } else {
358389 t
@@ -408,9 +439,13 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
408439 V : TypeFoldable < ' tcx > + Lift < ' gcx > ,
409440 {
410441 let needs_canonical_flags = if canonicalize_region_mode. any ( ) {
411- TypeFlags :: HAS_FREE_REGIONS | TypeFlags :: KEEP_IN_LOCAL_TCX
442+ TypeFlags :: KEEP_IN_LOCAL_TCX |
443+ TypeFlags :: HAS_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_FREE_REGIONS`
444+ TypeFlags :: HAS_TY_PLACEHOLDER
412445 } else {
413- TypeFlags :: KEEP_IN_LOCAL_TCX
446+ TypeFlags :: KEEP_IN_LOCAL_TCX |
447+ TypeFlags :: HAS_RE_PLACEHOLDER |
448+ TypeFlags :: HAS_TY_PLACEHOLDER
414449 } ;
415450
416451 let gcx = tcx. global_tcx ( ) ;
@@ -574,17 +609,14 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
574609 /// if `ty_var` is bound to anything; if so, canonicalize
575610 /// *that*. Otherwise, create a new canonical variable for
576611 /// `ty_var`.
577- fn canonicalize_ty_var ( & mut self , ty_kind : CanonicalTyVarKind , ty_var : Ty < ' tcx > ) -> Ty < ' tcx > {
612+ fn canonicalize_ty_var ( & mut self , info : CanonicalVarInfo , ty_var : Ty < ' tcx > ) -> Ty < ' tcx > {
578613 let infcx = self . infcx . expect ( "encountered ty-var without infcx" ) ;
579614 let bound_to = infcx. shallow_resolve ( ty_var) ;
580615 if bound_to != ty_var {
581616 self . fold_ty ( bound_to)
582617 } else {
583- let info = CanonicalVarInfo {
584- kind : CanonicalVarKind :: Ty ( ty_kind) ,
585- } ;
586618 let var = self . canonical_var ( info, ty_var. into ( ) ) ;
587- self . tcx ( ) . mk_ty ( ty:: Bound ( BoundTy :: new ( self . binder_index , var) ) )
619+ self . tcx ( ) . mk_ty ( ty:: Bound ( self . binder_index , var. into ( ) ) )
588620 }
589621 }
590622}
0 commit comments