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