Skip to content

Commit 2e91065

Browse files
committed
Generalize typed value printing and use for undef printing
1 parent cc9ca64 commit 2e91065

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/librustc/ty/print/pretty.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,11 @@ pub trait PrettyPrinter<'tcx>:
216216
mut self,
217217
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
218218
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
219-
cast: bool,
219+
conversion: &str,
220220
) -> Result<Self::Const, Self::Error> {
221221
self.write_str("{")?;
222222
self = f(self)?;
223-
if cast {
224-
self.write_str(" as ")?;
225-
} else {
226-
self.write_str(": ")?;
227-
}
223+
self.write_str(conversion)?;
228224
self = t(self)?;
229225
self.write_str("}")?;
230226
Ok(self)
@@ -1008,7 +1004,7 @@ pub trait PrettyPrinter<'tcx>:
10081004
Ok(this)
10091005
},
10101006
|this| this.print_type(ty),
1011-
true,
1007+
" as ",
10121008
)?;
10131009
}
10141010
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
@@ -1019,7 +1015,7 @@ pub trait PrettyPrinter<'tcx>:
10191015
self = self.typed_value(
10201016
|this| this.print_value_path(instance.def_id(), instance.substs),
10211017
|this| this.print_type(ty),
1022-
true,
1018+
" as ",
10231019
)?;
10241020
}
10251021
// For function type zsts just printing the type is enough
@@ -1048,7 +1044,7 @@ pub trait PrettyPrinter<'tcx>:
10481044
Ok(this)
10491045
};
10501046
self = if print_ty {
1051-
self.typed_value(print, |this| this.print_type(ty), false)?
1047+
self.typed_value(print, |this| this.print_type(ty), ": ")?
10521048
} else {
10531049
print(self)?
10541050
};
@@ -1076,7 +1072,7 @@ pub trait PrettyPrinter<'tcx>:
10761072
Ok(this)
10771073
},
10781074
|this| this.print_type(ty),
1079-
false,
1075+
": ",
10801076
)
10811077
} else {
10821078
self.write_str("&_")?;
@@ -1477,15 +1473,11 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
14771473
mut self,
14781474
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
14791475
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
1480-
cast: bool,
1476+
conversion: &str,
14811477
) -> Result<Self::Const, Self::Error> {
14821478
self.write_str("{")?;
14831479
self = f(self)?;
1484-
if cast {
1485-
self.write_str(" as ")?;
1486-
} else {
1487-
self.write_str(": ")?;
1488-
}
1480+
self.write_str(conversion)?;
14891481
let was_in_value = std::mem::replace(&mut self.in_value, false);
14901482
self = t(self)?;
14911483
self.in_value = was_in_value;
@@ -1566,7 +1558,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
15661558
Ok(this)
15671559
};
15681560
if print_ty {
1569-
self.typed_value(print, |this| this.print_type(ty), false)
1561+
self.typed_value(print, |this| this.print_type(ty), ": ")
15701562
} else {
15711563
print(self)
15721564
}

src/librustc_mir/interpret/operand.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,22 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
9898
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9999
/// Helper function for printing a scalar to a FmtPrinter
100100
fn p<'a, 'tcx, F: std::fmt::Write, Tag>(
101-
mut cx: FmtPrinter<'a, 'tcx, F>,
101+
cx: FmtPrinter<'a, 'tcx, F>,
102102
s: ScalarMaybeUndef<Tag>,
103103
ty: Ty<'tcx>,
104104
) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
105105
match s {
106106
ScalarMaybeUndef::Scalar(s) => {
107107
cx.pretty_print_const_scalar(s.erase_tag(), ty, true)
108108
}
109-
ScalarMaybeUndef::Undef => {
110-
cx.write_str("{undef ")?;
111-
cx = cx.print_type(ty)?;
112-
cx.write_str("}")?;
113-
Ok(cx)
114-
}
109+
ScalarMaybeUndef::Undef => cx.typed_value(
110+
|mut this| {
111+
this.write_str("{undef ")?;
112+
Ok(this)
113+
},
114+
|this| this.print_type(ty),
115+
" ",
116+
),
115117
}
116118
}
117119
ty::tls::with(|tcx| {

0 commit comments

Comments
 (0)