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