Generalize typed value printing and use for undef printing

This commit is contained in:
Oliver Scherer 2020-02-26 19:36:10 +01:00
parent cc9ca640c2
commit 2e91065a6f
2 changed files with 18 additions and 24 deletions

View File

@ -216,15 +216,11 @@ pub trait PrettyPrinter<'tcx>:
mut self,
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
cast: bool,
conversion: &str,
) -> Result<Self::Const, Self::Error> {
self.write_str("{")?;
self = f(self)?;
if cast {
self.write_str(" as ")?;
} else {
self.write_str(": ")?;
}
self.write_str(conversion)?;
self = t(self)?;
self.write_str("}")?;
Ok(self)
@ -1008,7 +1004,7 @@ pub trait PrettyPrinter<'tcx>:
Ok(this)
},
|this| this.print_type(ty),
true,
" as ",
)?;
}
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
@ -1019,7 +1015,7 @@ pub trait PrettyPrinter<'tcx>:
self = self.typed_value(
|this| this.print_value_path(instance.def_id(), instance.substs),
|this| this.print_type(ty),
true,
" as ",
)?;
}
// For function type zsts just printing the type is enough
@ -1048,7 +1044,7 @@ pub trait PrettyPrinter<'tcx>:
Ok(this)
};
self = if print_ty {
self.typed_value(print, |this| this.print_type(ty), false)?
self.typed_value(print, |this| this.print_type(ty), ": ")?
} else {
print(self)?
};
@ -1076,7 +1072,7 @@ pub trait PrettyPrinter<'tcx>:
Ok(this)
},
|this| this.print_type(ty),
false,
": ",
)
} else {
self.write_str("&_")?;
@ -1477,15 +1473,11 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
mut self,
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
cast: bool,
conversion: &str,
) -> Result<Self::Const, Self::Error> {
self.write_str("{")?;
self = f(self)?;
if cast {
self.write_str(" as ")?;
} else {
self.write_str(": ")?;
}
self.write_str(conversion)?;
let was_in_value = std::mem::replace(&mut self.in_value, false);
self = t(self)?;
self.in_value = was_in_value;
@ -1566,7 +1558,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
Ok(this)
};
if print_ty {
self.typed_value(print, |this| this.print_type(ty), false)
self.typed_value(print, |this| this.print_type(ty), ": ")
} else {
print(self)
}

View File

@ -98,7 +98,7 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// Helper function for printing a scalar to a FmtPrinter
fn p<'a, 'tcx, F: std::fmt::Write, Tag>(
mut cx: FmtPrinter<'a, 'tcx, F>,
cx: FmtPrinter<'a, 'tcx, F>,
s: ScalarMaybeUndef<Tag>,
ty: Ty<'tcx>,
) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
@ -106,12 +106,14 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
ScalarMaybeUndef::Scalar(s) => {
cx.pretty_print_const_scalar(s.erase_tag(), ty, true)
}
ScalarMaybeUndef::Undef => {
cx.write_str("{undef ")?;
cx = cx.print_type(ty)?;
cx.write_str("}")?;
Ok(cx)
}
ScalarMaybeUndef::Undef => cx.typed_value(
|mut this| {
this.write_str("{undef ")?;
Ok(this)
},
|this| this.print_type(ty),
" ",
),
}
}
ty::tls::with(|tcx| {