Skip to content

Commit b78dbf4

Browse files
committed
Reuse type ascripted printing for type cast printing
1 parent 154f3f1 commit b78dbf4

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/librustc/ty/print/pretty.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,20 @@ pub trait PrettyPrinter<'tcx>:
211211
Ok(self)
212212
}
213213

214-
/// Prints `{...}` around what `f` (and optionally `t`) print
215-
fn type_ascribed_value(
214+
/// Prints `{f: t}` or `{f as t}` depending on the `cast` argument
215+
fn typed_value(
216216
mut self,
217217
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
218218
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
219+
cast: bool,
219220
) -> Result<Self::Const, Self::Error> {
220221
self.write_str("{")?;
221222
self = f(self)?;
222-
self.write_str(": ")?;
223+
if cast {
224+
self.write_str(" as ")?;
225+
} else {
226+
self.write_str(": ")?;
227+
}
223228
self = t(self)?;
224229
self.write_str("}")?;
225230
Ok(self)
@@ -990,7 +995,14 @@ pub trait PrettyPrinter<'tcx>:
990995
}
991996
// Raw pointers
992997
(Scalar::Raw { data, .. }, ty::RawPtr(_)) => {
993-
p!(write("{{0x{:x} as ", data), print(ty), write("}}"))
998+
self = self.typed_value(
999+
|mut this| {
1000+
write!(this, "0x{:x}", data)?;
1001+
Ok(this)
1002+
},
1003+
|this| this.print_type(ty),
1004+
true,
1005+
)?;
9941006
}
9951007
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
9961008
let instance = {
@@ -1025,7 +1037,7 @@ pub trait PrettyPrinter<'tcx>:
10251037
Ok(this)
10261038
};
10271039
self = if print_ty {
1028-
self.type_ascribed_value(print, |this| this.print_type(ty))?
1040+
self.typed_value(print, |this| this.print_type(ty), false)?
10291041
} else {
10301042
print(self)?
10311043
};
@@ -1047,12 +1059,13 @@ pub trait PrettyPrinter<'tcx>:
10471059
print_ty: bool,
10481060
) -> Result<Self::Const, Self::Error> {
10491061
if print_ty {
1050-
self.type_ascribed_value(
1062+
self.typed_value(
10511063
|mut this| {
10521064
this.write_str("&_")?;
10531065
Ok(this)
10541066
},
10551067
|this| this.print_type(ty),
1068+
false,
10561069
)
10571070
} else {
10581071
self.write_str("&_")?;
@@ -1449,14 +1462,19 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
14491462
self.pretty_in_binder(value)
14501463
}
14511464

1452-
fn type_ascribed_value(
1465+
fn typed_value(
14531466
mut self,
14541467
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
14551468
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
1469+
cast: bool,
14561470
) -> Result<Self::Const, Self::Error> {
14571471
self.write_str("{")?;
14581472
self = f(self)?;
1459-
self.write_str(": ")?;
1473+
if cast {
1474+
self.write_str(" as ")?;
1475+
} else {
1476+
self.write_str(": ")?;
1477+
}
14601478
let was_in_value = std::mem::replace(&mut self.in_value, false);
14611479
self = t(self)?;
14621480
self.in_value = was_in_value;
@@ -1537,7 +1555,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
15371555
Ok(this)
15381556
};
15391557
if print_ty {
1540-
self.type_ascribed_value(print, |this| this.print_type(ty))
1558+
self.typed_value(print, |this| this.print_type(ty), false)
15411559
} else {
15421560
print(self)
15431561
}

0 commit comments

Comments
 (0)