]> git.lizzy.rs Git - rust.git/commitdiff
Print braces only in print_ty mode
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 5 Feb 2020 10:00:52 +0000 (11:00 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 11 Mar 2020 08:10:49 +0000 (09:10 +0100)
src/librustc/ty/print/pretty.rs

index 6e960f66c7c99003033639806a044660f7d3e04e..b992e13daf0b9d7a0d3d2be1cf43d43cd97d0c5a 100644 (file)
@@ -210,19 +210,16 @@ fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Se
         Ok(self)
     }
 
-    /// Prints `{...}` around what `f` and optionally `t` print
+    /// 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(": ")?;
+        self = t(self)?;
         self.write_str("}")?;
         Ok(self)
     }
@@ -1003,14 +1000,15 @@ fn pretty_print_const_scalar(
             (Scalar::Raw { size: 0, .. }, _) => p!(print(ty)),
             // Nontrivial types with scalar bit representation
             (Scalar::Raw { data, size }, _) => {
-                self = self.type_ascribed_value(
-                    |mut this| {
-                        write!(this, "0x{:01$x}", data, size as usize * 2)?;
-                        Ok(this)
-                    },
-                    |this| this.print_type(ty),
-                    print_ty,
-                )?
+                let print = |mut this: Self| {
+                    write!(this, "0x{:01$x}", data, size as usize * 2)?;
+                    Ok(this)
+                };
+                self = if print_ty {
+                    self.type_ascribed_value(print, |this| this.print_type(ty))?
+                } else {
+                    print(self)?
+                };
             }
             // Any pointer values not covered by a branch above
             (Scalar::Ptr(p), _) => {
@@ -1023,19 +1021,23 @@ fn pretty_print_const_scalar(
     /// This is overridden for MIR printing because we only want to hide alloc ids from users, not
     /// from MIR where it is actually useful.
     fn pretty_print_const_pointer(
-        self,
+        mut self,
         _: Pointer,
         ty: Ty<'tcx>,
         print_ty: bool,
     ) -> Result<Self::Const, Self::Error> {
-        self.type_ascribed_value(
-            |mut this| {
-                this.write_str("pointer")?;
-                Ok(this)
-            },
-            |this| this.print_type(ty),
-            print_ty,
-        )
+        if print_ty {
+            self.type_ascribed_value(
+                |mut this| {
+                    this.write_str("&_")?;
+                    Ok(this)
+                },
+                |this| this.print_type(ty),
+            )
+        } else {
+            self.write_str("&_")?;
+            Ok(self)
+        }
     }
 
     fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result<Self::Const, Self::Error> {
@@ -1430,16 +1432,13 @@ 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(": ")?;
+        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)
     }
@@ -1507,19 +1506,20 @@ fn pretty_print_const_pointer(
         ty: Ty<'tcx>,
         print_ty: bool,
     ) -> Result<Self::Const, Self::Error> {
-        self.type_ascribed_value(
-            |mut this| {
-                define_scoped_cx!(this);
-                if this.print_alloc_ids {
-                    p!(write("{:?}", p));
-                } else {
-                    p!(write("pointer"));
-                }
-                Ok(this)
-            },
-            |this| this.print_type(ty),
-            print_ty,
-        )
+        let print = |mut this: Self| {
+            define_scoped_cx!(this);
+            if this.print_alloc_ids {
+                p!(write("{:?}", p));
+            } else {
+                p!(write("&_"));
+            }
+            Ok(this)
+        };
+        if print_ty {
+            self.type_ascribed_value(print, |this| this.print_type(ty))
+        } else {
+            print(self)
+        }
     }
 }