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, + t: impl FnOnce(Self) -> Result, + print_ty: bool, + ) -> Result { + 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, - ty: Ty<'tcx>, - print_ty: bool, - ) -> Result { - 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 { 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.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 PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> { self.pretty_in_binder(value) } + fn type_ascribed_value( + mut self, + f: impl FnOnce(Self) -> Result, + t: impl FnOnce(Self) -> Result, + print_ty: bool, + ) -> Result { + 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, @@ -1488,7 +1507,7 @@ impl PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> { ty: Ty<'tcx>, print_ty: bool, ) -> Result { - self.print_type_ascribed( + self.type_ascribed_value( |mut this| { define_scoped_cx!(this); if this.print_alloc_ids { @@ -1498,28 +1517,10 @@ impl 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, - ty: Ty<'tcx>, - print_ty: bool, - ) -> Result { - 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`.