]> git.lizzy.rs Git - rust.git/commitdiff
Add expect_ty method to Kind
authorvarkor <github@varkor.com>
Thu, 25 Apr 2019 23:26:33 +0000 (00:26 +0100)
committervarkor <github@varkor.com>
Fri, 26 Apr 2019 19:57:13 +0000 (20:57 +0100)
src/librustc/ty/subst.rs

index b8ae0430502b5f9d5bd0c4ae8103d31d191f9493..ed3da31fb89bd53a4ff111b65c2eb008cdb4cac6 100644 (file)
@@ -123,6 +123,16 @@ pub fn unpack(self) -> UnpackedKind<'tcx> {
             }
         }
     }
+
+    /// Unpack the `Kind` as a type when it is known certainly to be a type.
+    /// This is true in cases where `Substs` is used in places where the kinds are known
+    /// to be limited (e.g. in tuples, where the only parameters are type parameters).
+    pub fn expect_ty(self) -> Ty<'tcx> {
+        match self.unpack() {
+            UnpackedKind::Type(ty) => ty,
+            _ => bug!("expected a type, but found another kind"),
+        }
+    }
 }
 
 impl<'a, 'tcx> Lift<'tcx> for Kind<'a> {
@@ -174,8 +184,7 @@ fn decode<D: Decoder>(d: &mut D) -> Result<Kind<'tcx>, D::Error> {
 
 impl<'a, 'gcx, 'tcx> InternalSubsts<'tcx> {
     /// Creates a `InternalSubsts` that maps each generic parameter to itself.
-    pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
-                             -> SubstsRef<'tcx> {
+    pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
         Self::for_item(tcx, def_id, |param, _| {
             tcx.mk_param_from_def(param)
         })