Skip to content

Commit 02f4eeb

Browse files
committed
Address review comments around type_ascribed_value
1 parent 02dbb35 commit 02f4eeb

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ pub trait PrettyPrinter<'tcx>:
210210
Ok(self)
211211
}
212212

213+
/// Prints `{...}` around what `f` and optionally `t` print
214+
fn type_ascribed_value(
215+
mut self,
216+
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
217+
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
218+
print_ty: bool,
219+
) -> Result<Self::Const, Self::Error> {
220+
self.write_str("{")?;
221+
self = f(self)?;
222+
if print_ty {
223+
self.write_str(": ")?;
224+
self = t(self)?;
225+
}
226+
self.write_str("}")?;
227+
Ok(self)
228+
}
229+
213230
/// Prints `<...>` around what `f` prints.
214231
fn generic_delimiters(
215232
self,
@@ -457,22 +474,6 @@ pub trait PrettyPrinter<'tcx>:
457474
})
458475
}
459476

460-
fn print_type_ascribed(
461-
mut self,
462-
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
463-
ty: Ty<'tcx>,
464-
print_ty: bool,
465-
) -> Result<Self::Const, Self::Error> {
466-
self.write_str("{")?;
467-
self = f(self)?;
468-
if print_ty {
469-
self.write_str(": ")?;
470-
self = self.print_type(ty)?;
471-
}
472-
self.write_str("}")?;
473-
Ok(self)
474-
}
475-
476477
fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
477478
define_scoped_cx!(self);
478479

@@ -1002,12 +1003,12 @@ pub trait PrettyPrinter<'tcx>:
10021003
(Scalar::Raw { size: 0, .. }, _) => p!(print(ty)),
10031004
// Nontrivial types with scalar bit representation
10041005
(Scalar::Raw { data, size }, _) => {
1005-
self = self.print_type_ascribed(
1006+
self = self.type_ascribed_value(
10061007
|mut this| {
10071008
write!(this, "0x{:01$x}", data, size as usize * 2)?;
10081009
Ok(this)
10091010
},
1010-
ty,
1011+
|this| this.print_type(ty),
10111012
print_ty,
10121013
)?
10131014
}
@@ -1027,12 +1028,12 @@ pub trait PrettyPrinter<'tcx>:
10271028
ty: Ty<'tcx>,
10281029
print_ty: bool,
10291030
) -> Result<Self::Const, Self::Error> {
1030-
self.print_type_ascribed(
1031+
self.type_ascribed_value(
10311032
|mut this| {
10321033
this.write_str("pointer")?;
10331034
Ok(this)
10341035
},
1035-
ty,
1036+
|this| this.print_type(ty),
10361037
print_ty,
10371038
)
10381039
}
@@ -1425,6 +1426,24 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
14251426
self.pretty_in_binder(value)
14261427
}
14271428

1429+
fn type_ascribed_value(
1430+
mut self,
1431+
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
1432+
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
1433+
print_ty: bool,
1434+
) -> Result<Self::Const, Self::Error> {
1435+
self.write_str("{")?;
1436+
self = f(self)?;
1437+
if print_ty {
1438+
self.write_str(": ")?;
1439+
let was_in_value = std::mem::replace(&mut self.in_value, false);
1440+
self = t(self)?;
1441+
self.in_value = was_in_value;
1442+
}
1443+
self.write_str("}")?;
1444+
Ok(self)
1445+
}
1446+
14281447
fn generic_delimiters(
14291448
mut self,
14301449
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
@@ -1488,7 +1507,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
14881507
ty: Ty<'tcx>,
14891508
print_ty: bool,
14901509
) -> Result<Self::Const, Self::Error> {
1491-
self.print_type_ascribed(
1510+
self.type_ascribed_value(
14921511
|mut this| {
14931512
define_scoped_cx!(this);
14941513
if this.print_alloc_ids {
@@ -1498,28 +1517,10 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
14981517
}
14991518
Ok(this)
15001519
},
1501-
ty,
1520+
|this| this.print_type(ty),
15021521
print_ty,
15031522
)
15041523
}
1505-
1506-
fn print_type_ascribed(
1507-
mut self,
1508-
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
1509-
ty: Ty<'tcx>,
1510-
print_ty: bool,
1511-
) -> Result<Self::Const, Self::Error> {
1512-
self.write_str("{")?;
1513-
self = f(self)?;
1514-
if print_ty {
1515-
self.write_str(": ")?;
1516-
let was_in_value = std::mem::replace(&mut self.in_value, false);
1517-
self = self.print_type(ty)?;
1518-
self.in_value = was_in_value;
1519-
}
1520-
self.write_str("}")?;
1521-
Ok(self)
1522-
}
15231524
}
15241525

15251526
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.

0 commit comments

Comments
 (0)