@@ -1241,19 +1241,76 @@ impl<'tcx> InferCtxt<'tcx> {
12411241 }
12421242 }
12431243
1244- /// Resolve any type variables found in `value` -- but only one
1245- /// level. So, if the variable `?X` is bound to some type
1246- /// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
1247- /// itself be bound to a type).
1248- ///
1249- /// Useful when you only need to inspect the outermost level of
1250- /// the type and don't care about nested types (or perhaps you
1251- /// will be resolving them as well, e.g. in a loop).
1252- pub fn shallow_resolve < T > ( & self , value : T ) -> T
1253- where
1254- T : TypeFoldable < TyCtxt < ' tcx > > ,
1255- {
1256- value. fold_with ( & mut ShallowResolver { infcx : self } )
1244+ pub fn shallow_resolve ( & self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
1245+ if let ty:: Infer ( v) = ty. kind ( ) { self . fold_infer_ty ( * v) . unwrap_or ( ty) } else { ty }
1246+ }
1247+
1248+ // This is separate from `shallow_resolve` to keep that method small and inlinable.
1249+ #[ inline( never) ]
1250+ fn fold_infer_ty ( & self , v : InferTy ) -> Option < Ty < ' tcx > > {
1251+ match v {
1252+ ty:: TyVar ( v) => {
1253+ // Not entirely obvious: if `typ` is a type variable,
1254+ // it can be resolved to an int/float variable, which
1255+ // can then be recursively resolved, hence the
1256+ // recursion. Note though that we prevent type
1257+ // variables from unifying to other type variables
1258+ // directly (though they may be embedded
1259+ // structurally), and we prevent cycles in any case,
1260+ // so this recursion should always be of very limited
1261+ // depth.
1262+ //
1263+ // Note: if these two lines are combined into one we get
1264+ // dynamic borrow errors on `self.inner`.
1265+ let known = self . inner . borrow_mut ( ) . type_variables ( ) . probe ( v) . known ( ) ;
1266+ known. map ( |t| self . shallow_resolve ( t) )
1267+ }
1268+
1269+ ty:: IntVar ( v) => self
1270+ . inner
1271+ . borrow_mut ( )
1272+ . int_unification_table ( )
1273+ . probe_value ( v)
1274+ . map ( |v| v. to_type ( self . tcx ) ) ,
1275+
1276+ ty:: FloatVar ( v) => self
1277+ . inner
1278+ . borrow_mut ( )
1279+ . float_unification_table ( )
1280+ . probe_value ( v)
1281+ . map ( |v| v. to_type ( self . tcx ) ) ,
1282+
1283+ ty:: FreshTy ( _) | ty:: FreshIntTy ( _) | ty:: FreshFloatTy ( _) => None ,
1284+ }
1285+ }
1286+
1287+ pub fn shallow_resolve_const ( & self , ct : ty:: Const < ' tcx > ) -> ty:: Const < ' tcx > {
1288+ match ct. kind ( ) {
1289+ ty:: ConstKind :: Infer ( infer_ct) => match infer_ct {
1290+ InferConst :: Var ( vid) => self
1291+ . inner
1292+ . borrow_mut ( )
1293+ . const_unification_table ( )
1294+ . probe_value ( vid)
1295+ . known ( )
1296+ . unwrap_or ( ct) ,
1297+ InferConst :: EffectVar ( vid) => self
1298+ . inner
1299+ . borrow_mut ( )
1300+ . effect_unification_table ( )
1301+ . probe_value ( vid)
1302+ . known ( )
1303+ . unwrap_or ( ct) ,
1304+ InferConst :: Fresh ( _) => ct,
1305+ } ,
1306+ ty:: ConstKind :: Param ( _)
1307+ | ty:: ConstKind :: Bound ( _, _)
1308+ | ty:: ConstKind :: Placeholder ( _)
1309+ | ty:: ConstKind :: Unevaluated ( _)
1310+ | ty:: ConstKind :: Value ( _)
1311+ | ty:: ConstKind :: Error ( _)
1312+ | ty:: ConstKind :: Expr ( _) => ct,
1313+ }
12571314 }
12581315
12591316 pub fn root_var ( & self , var : ty:: TyVid ) -> ty:: TyVid {
@@ -1764,89 +1821,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
17641821 }
17651822}
17661823
1767- struct ShallowResolver < ' a , ' tcx > {
1768- infcx : & ' a InferCtxt < ' tcx > ,
1769- }
1770-
1771- impl < ' a , ' tcx > TypeFolder < TyCtxt < ' tcx > > for ShallowResolver < ' a , ' tcx > {
1772- fn interner ( & self ) -> TyCtxt < ' tcx > {
1773- self . infcx . tcx
1774- }
1775-
1776- /// If `ty` is a type variable of some kind, resolve it one level
1777- /// (but do not resolve types found in the result). If `typ` is
1778- /// not a type variable, just return it unmodified.
1779- #[ inline]
1780- fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
1781- if let ty:: Infer ( v) = ty. kind ( ) { self . fold_infer_ty ( * v) . unwrap_or ( ty) } else { ty }
1782- }
1783-
1784- fn fold_const ( & mut self , ct : ty:: Const < ' tcx > ) -> ty:: Const < ' tcx > {
1785- match ct. kind ( ) {
1786- ty:: ConstKind :: Infer ( InferConst :: Var ( vid) ) => self
1787- . infcx
1788- . inner
1789- . borrow_mut ( )
1790- . const_unification_table ( )
1791- . probe_value ( vid)
1792- . known ( )
1793- . unwrap_or ( ct) ,
1794- ty:: ConstKind :: Infer ( InferConst :: EffectVar ( vid) ) => self
1795- . infcx
1796- . inner
1797- . borrow_mut ( )
1798- . effect_unification_table ( )
1799- . probe_value ( vid)
1800- . known ( )
1801- . unwrap_or ( ct) ,
1802- _ => ct,
1803- }
1804- }
1805- }
1806-
1807- impl < ' a , ' tcx > ShallowResolver < ' a , ' tcx > {
1808- // This is separate from `fold_ty` to keep that method small and inlinable.
1809- #[ inline( never) ]
1810- fn fold_infer_ty ( & mut self , v : InferTy ) -> Option < Ty < ' tcx > > {
1811- match v {
1812- ty:: TyVar ( v) => {
1813- // Not entirely obvious: if `typ` is a type variable,
1814- // it can be resolved to an int/float variable, which
1815- // can then be recursively resolved, hence the
1816- // recursion. Note though that we prevent type
1817- // variables from unifying to other type variables
1818- // directly (though they may be embedded
1819- // structurally), and we prevent cycles in any case,
1820- // so this recursion should always be of very limited
1821- // depth.
1822- //
1823- // Note: if these two lines are combined into one we get
1824- // dynamic borrow errors on `self.inner`.
1825- let known = self . infcx . inner . borrow_mut ( ) . type_variables ( ) . probe ( v) . known ( ) ;
1826- known. map ( |t| self . fold_ty ( t) )
1827- }
1828-
1829- ty:: IntVar ( v) => self
1830- . infcx
1831- . inner
1832- . borrow_mut ( )
1833- . int_unification_table ( )
1834- . probe_value ( v)
1835- . map ( |v| v. to_type ( self . infcx . tcx ) ) ,
1836-
1837- ty:: FloatVar ( v) => self
1838- . infcx
1839- . inner
1840- . borrow_mut ( )
1841- . float_unification_table ( )
1842- . probe_value ( v)
1843- . map ( |v| v. to_type ( self . infcx . tcx ) ) ,
1844-
1845- ty:: FreshTy ( _) | ty:: FreshIntTy ( _) | ty:: FreshFloatTy ( _) => None ,
1846- }
1847- }
1848- }
1849-
18501824impl < ' tcx > TypeTrace < ' tcx > {
18511825 pub fn span ( & self ) -> Span {
18521826 self . cause . span
0 commit comments