about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2020-01-27 18:49:05 +0100
committerOliver Scherer <github35764891676564198441@oli-obk.de>2020-03-11 09:10:49 +0100
commit02f4eeb7cb78f1cf7bcfe18db3b700b02576ca55 (patch)
treee1f165134da86f7e1148e4f25227f0e33449cbdb /src
parent02dbb35b2b6ed869f14a8aecaf9dad5c72d5cb0b (diff)
downloadrust-02f4eeb7cb78f1cf7bcfe18db3b700b02576ca55.tar.gz
rust-02f4eeb7cb78f1cf7bcfe18db3b700b02576ca55.zip
Address review comments around `type_ascribed_value`
Diffstat (limited to 'src')
-rw-r--r--src/librustc/ty/print/pretty.rs81
1 files changed, 41 insertions, 40 deletions
diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs
index 2f53b62fdec..6e960f66c7c 100644
--- a/src/librustc/ty/print/pretty.rs
+++ b/src/librustc/ty/print/pretty.rs
@@ -210,6 +210,23 @@ pub trait PrettyPrinter<'tcx>:
         Ok(self)
     }
 
+    /// Prints `{...}` around what `f` and optionally `t` print
+    fn type_ascribed_value(
+        mut self,
+        f: impl FnOnce(Self) -> Result<Self, Self::Error>,
+        t: impl FnOnce(Self) -> Result<Self, Self::Error>,
+        print_ty: bool,
+    ) -> Result<Self::Const, Self::Error> {
+        self.write_str("{")?;
+        self = f(self)?;
+        if print_ty {
+            self.write_str(": ")?;
+            self = t(self)?;
+        }
+        self.write_str("}")?;
+        Ok(self)
+    }
+
     /// Prints `<...>` around what `f` prints.
     fn generic_delimiters(
         self,
@@ -457,22 +474,6 @@ pub trait PrettyPrinter<'tcx>:
         })
     }
 
-    fn print_type_ascribed(
-        mut self,
-        f: impl FnOnce(Self) -> Result<Self, Self::Error>,
-        ty: Ty<'tcx>,
-        print_ty: bool,
-    ) -> Result<Self::Const, Self::Error> {
-        self.write_str("{")?;
-        self = f(self)?;
-        if print_ty {
-            self.write_str(": ")?;
-            self = self.print_type(ty)?;
-        }
-        self.write_str("}")?;
-        Ok(self)
-    }
-
     fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
         define_scoped_cx!(self);
 
@@ -1002,12 +1003,12 @@ pub trait PrettyPrinter<'tcx>:
             (Scalar::Raw { size: 0, .. }, _) => p!(print(ty)),
             // Nontrivial types with scalar bit representation
             (Scalar::Raw { data, size }, _) => {
-                self = self.print_type_ascribed(
+                self = self.type_ascribed_value(
                     |mut this| {
                         write!(this, "0x{:01$x}", data, size as usize * 2)?;
                         Ok(this)
                     },
-                    ty,
+                    |this| this.print_type(ty),
                     print_ty,
                 )?
             }
@@ -1027,12 +1028,12 @@ pub trait PrettyPrinter<'tcx>:
         ty: Ty<'tcx>,
         print_ty: bool,
     ) -> Result<Self::Const, Self::Error> {
-        self.print_type_ascribed(
+        self.type_ascribed_value(
             |mut this| {
                 this.write_str("pointer")?;
                 Ok(this)
             },
-            ty,
+            |this| this.print_type(ty),
             print_ty,
         )
     }
@@ -1425,6 +1426,24 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
         self.pretty_in_binder(value)
     }
 
+    fn type_ascribed_value(
+        mut self,
+        f: impl FnOnce(Self) -> Result<Self, Self::Error>,
+        t: impl FnOnce(Self) -> Result<Self, Self::Error>,
+        print_ty: bool,
+    ) -> Result<Self::Const, Self::Error> {
+        self.write_str("{")?;
+        self = f(self)?;
+        if print_ty {
+            self.write_str(": ")?;
+            let was_in_value = std::mem::replace(&mut self.in_value, false);
+            self = t(self)?;
+            self.in_value = was_in_value;
+        }
+        self.write_str("}")?;
+        Ok(self)
+    }
+
     fn generic_delimiters(
         mut self,
         f: impl FnOnce(Self) -> Result<Self, Self::Error>,
@@ -1488,7 +1507,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
         ty: Ty<'tcx>,
         print_ty: bool,
     ) -> Result<Self::Const, Self::Error> {
-        self.print_type_ascribed(
+        self.type_ascribed_value(
             |mut this| {
                 define_scoped_cx!(this);
                 if this.print_alloc_ids {
@@ -1498,28 +1517,10 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
                 }
                 Ok(this)
             },
-            ty,
+            |this| this.print_type(ty),
             print_ty,
         )
     }
-
-    fn print_type_ascribed(
-        mut self,
-        f: impl FnOnce(Self) -> Result<Self, Self::Error>,
-        ty: Ty<'tcx>,
-        print_ty: bool,
-    ) -> Result<Self::Const, Self::Error> {
-        self.write_str("{")?;
-        self = f(self)?;
-        if print_ty {
-            self.write_str(": ")?;
-            let was_in_value = std::mem::replace(&mut self.in_value, false);
-            self = self.print_type(ty)?;
-            self.in_value = was_in_value;
-        }
-        self.write_str("}")?;
-        Ok(self)
-    }
 }
 
 // HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.