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