]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_middle/src/ty/subst.rs
Auto merge of #92130 - Kobzol:stable-hash-str, r=cjgillot
[rust.git] / compiler / rustc_middle / src / ty / subst.rs
index ffa495ce4803fba8217d5a0ddd86eb0dfb227ab6..a71181149149d648b9a9bd45f95069d74aa62366 100644 (file)
@@ -2,7 +2,7 @@
 
 use crate::mir;
 use crate::ty::codec::{TyDecoder, TyEncoder};
-use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
+use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeVisitor};
 use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
 use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
 
@@ -67,7 +67,7 @@ fn pack(self) -> GenericArg<'tcx> {
     }
 }
 
-impl fmt::Debug for GenericArg<'tcx> {
+impl<'tcx> fmt::Debug for GenericArg<'tcx> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match self.unpack() {
             GenericArgKind::Lifetime(lt) => lt.fmt(f),
@@ -153,11 +153,14 @@ fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
 }
 
 impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> {
-    fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
+    fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
+        self,
+        folder: &mut F,
+    ) -> Result<Self, F::Error> {
         match self.unpack() {
-            GenericArgKind::Lifetime(lt) => lt.fold_with(folder).map(Into::into),
-            GenericArgKind::Type(ty) => ty.fold_with(folder).map(Into::into),
-            GenericArgKind::Const(ct) => ct.fold_with(folder).map(Into::into),
+            GenericArgKind::Lifetime(lt) => lt.try_fold_with(folder).map(Into::into),
+            GenericArgKind::Type(ty) => ty.try_fold_with(folder).map(Into::into),
+            GenericArgKind::Const(ct) => ct.try_fold_with(folder).map(Into::into),
         }
     }
 
@@ -372,7 +375,10 @@ pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsR
 }
 
 impl<'tcx> TypeFoldable<'tcx> for SubstsRef<'tcx> {
-    fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
+    fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
+        self,
+        folder: &mut F,
+    ) -> Result<Self, F::Error> {
         // This code is hot enough that it's worth specializing for the most
         // common length lists, to avoid the overhead of `SmallVec` creation.
         // The match arms are in order of frequency. The 1, 2, and 0 cases are
@@ -381,12 +387,12 @@ fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F:
         // calling `intern_substs`.
         match self.len() {
             1 => {
-                let param0 = self[0].fold_with(folder)?;
+                let param0 = self[0].try_fold_with(folder)?;
                 if param0 == self[0] { Ok(self) } else { Ok(folder.tcx().intern_substs(&[param0])) }
             }
             2 => {
-                let param0 = self[0].fold_with(folder)?;
-                let param1 = self[1].fold_with(folder)?;
+                let param0 = self[0].try_fold_with(folder)?;
+                let param1 = self[1].try_fold_with(folder)?;
                 if param0 == self[0] && param1 == self[1] {
                     Ok(self)
                 } else {
@@ -396,7 +402,7 @@ fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F:
             0 => Ok(self),
             _ => {
                 let params: SmallVec<[_; 8]> =
-                    self.iter().map(|k| k.fold_with(folder)).collect::<Result<_, _>>()?;
+                    self.iter().map(|k| k.try_fold_with(folder)).collect::<Result<_, _>>()?;
                 if params[..] == self[..] {
                     Ok(self)
                 } else {
@@ -439,7 +445,7 @@ fn subst_spanned(
         span: Option<Span>,
     ) -> T {
         let mut folder = SubstFolder { tcx, substs, span, binders_passed: 0 };
-        self.fold_with(&mut folder).into_ok()
+        self.fold_with(&mut folder)
     }
 }
 
@@ -465,14 +471,14 @@ fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
     fn fold_binder<T: TypeFoldable<'tcx>>(
         &mut self,
         t: ty::Binder<'tcx, T>,
-    ) -> Result<ty::Binder<'tcx, T>, Self::Error> {
+    ) -> ty::Binder<'tcx, T> {
         self.binders_passed += 1;
-        let t = t.super_fold_with(self)?;
+        let t = t.super_fold_with(self);
         self.binders_passed -= 1;
-        Ok(t)
+        t
     }
 
-    fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
+    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
         // Note: This routine only handles regions that are bound on
         // type declarations and other outer declarations, not those
         // bound in *fn types*. Region substitution of the bound
@@ -482,7 +488,7 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self:
             ty::ReEarlyBound(data) => {
                 let rk = self.substs.get(data.index as usize).map(|k| k.unpack());
                 match rk {
-                    Some(GenericArgKind::Lifetime(lt)) => Ok(self.shift_region_through_binders(lt)),
+                    Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
                     _ => {
                         let span = self.span.unwrap_or(DUMMY_SP);
                         let msg = format!(
@@ -494,37 +500,31 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self:
                     }
                 }
             }
-            _ => Ok(r),
+            _ => r,
         }
     }
 
-    fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
+    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
         if !t.potentially_needs_subst() {
-            return Ok(t);
+            return t;
         }
 
         match *t.kind() {
-            ty::Param(p) => Ok(self.ty_for_param(p, t)),
+            ty::Param(p) => self.ty_for_param(p, t),
             _ => t.super_fold_with(self),
         }
     }
 
-    fn fold_const(
-        &mut self,
-        c: &'tcx ty::Const<'tcx>,
-    ) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
+    fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
         if let ty::ConstKind::Param(p) = c.val {
-            Ok(self.const_for_param(p, c))
+            self.const_for_param(p, c)
         } else {
             c.super_fold_with(self)
         }
     }
 
     #[inline]
-    fn fold_mir_const(
-        &mut self,
-        c: mir::ConstantKind<'tcx>,
-    ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
+    fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
         c.super_fold_with(self)
     }
 }