Skip to content

Commit 0d1c9c0

Browse files
varkoryodaldevoid
andcommitted
Pretty printing for const generics
Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
1 parent cbf5d22 commit 0d1c9c0

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

src/librustc/util/ppaux.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr};
88
use crate::ty::{Param, Bound, RawPtr, Ref, Never, Tuple};
99
use crate::ty::{Closure, Generator, GeneratorWitness, Foreign, Projection, Opaque};
1010
use crate::ty::{Placeholder, UnnormalizedProjection, Dynamic, Int, Uint, Infer};
11-
use crate::ty::{self, Ty, TyCtxt, TypeFoldable, GenericParamCount, GenericParamDefKind};
11+
use crate::ty::{self, Ty, TyCtxt, TypeFoldable, GenericParamCount, GenericParamDefKind, ParamConst};
12+
use crate::mir::interpret::ConstValue;
1213
use crate::util::nodemap::FxHashSet;
1314

1415
use std::cell::Cell;
@@ -478,6 +479,7 @@ impl PrintContext {
478479
GenericParamDefKind::Type { has_default, .. } => {
479480
Some((param.def_id, has_default))
480481
}
482+
GenericParamDefKind::Const => None, // FIXME(const_generics:defaults)
481483
}).peekable();
482484
let has_default = {
483485
let has_default = type_params.peek().map(|(_, has_default)| has_default);
@@ -571,6 +573,14 @@ impl PrintContext {
571573
)?;
572574
}
573575

576+
// FIXME(const_generics::defaults)
577+
let consts = substs.consts();
578+
579+
for ct in consts {
580+
start_or_continue(f, "<", ", ")?;
581+
ct.print_display(f, self)?;
582+
}
583+
574584
start_or_continue(f, "", ">")?;
575585

576586
// For values, also print their name and type parameters.
@@ -763,7 +773,8 @@ impl fmt::Debug for ty::GenericParamDef {
763773
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
764774
let type_name = match self.kind {
765775
ty::GenericParamDefKind::Lifetime => "Lifetime",
766-
ty::GenericParamDefKind::Type {..} => "Type",
776+
ty::GenericParamDefKind::Type { .. } => "Type",
777+
ty::GenericParamDefKind::Const => "Const",
767778
};
768779
write!(f, "{}({}, {:?}, {})",
769780
type_name,
@@ -1088,6 +1099,12 @@ impl fmt::Debug for ty::TyVid {
10881099
}
10891100
}
10901101

1102+
impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
1103+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1104+
write!(f, "_#{}f", self.index)
1105+
}
1106+
}
1107+
10911108
impl fmt::Debug for ty::IntVid {
10921109
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10931110
write!(f, "_#{}i", self.index)
@@ -1448,7 +1465,12 @@ define_print! {
14481465
write!(f, "_")?;
14491466
}
14501467
ty::LazyConst::Evaluated(c) => ty::tls::with(|tcx| {
1451-
write!(f, "{}", c.unwrap_usize(tcx))
1468+
match c.val {
1469+
ConstValue::Infer(..) => write!(f, "_"),
1470+
ConstValue::Param(ParamConst { name, .. }) =>
1471+
write!(f, "{}", name),
1472+
_ => write!(f, "{}", c.unwrap_usize(tcx)),
1473+
}
14521474
})?,
14531475
}
14541476
write!(f, "]")
@@ -1472,6 +1494,37 @@ define_print! {
14721494
}
14731495
}
14741496

1497+
define_print! {
1498+
('tcx) ConstValue<'tcx>, (self, f, cx) {
1499+
display {
1500+
match self {
1501+
ConstValue::Infer(..) => write!(f, "_"),
1502+
ConstValue::Param(ParamConst { name, .. }) => write!(f, "{}", name),
1503+
_ => write!(f, "{:?}", self),
1504+
}
1505+
}
1506+
}
1507+
}
1508+
1509+
define_print! {
1510+
('tcx) ty::Const<'tcx>, (self, f, cx) {
1511+
display {
1512+
write!(f, "{} : {}", self.val, self.ty)
1513+
}
1514+
}
1515+
}
1516+
1517+
define_print! {
1518+
('tcx) ty::LazyConst<'tcx>, (self, f, cx) {
1519+
display {
1520+
match self {
1521+
ty::LazyConst::Unevaluated(..) => write!(f, "_ : _"),
1522+
ty::LazyConst::Evaluated(c) => write!(f, "{}", c),
1523+
}
1524+
}
1525+
}
1526+
}
1527+
14751528
define_print! {
14761529
() ty::ParamTy, (self, f, cx) {
14771530
display {
@@ -1483,6 +1536,17 @@ define_print! {
14831536
}
14841537
}
14851538

1539+
define_print! {
1540+
() ty::ParamConst, (self, f, cx) {
1541+
display {
1542+
write!(f, "{}", self.name)
1543+
}
1544+
debug {
1545+
write!(f, "{}/#{}", self.name, self.index)
1546+
}
1547+
}
1548+
}
1549+
14861550
define_print! {
14871551
('tcx, T: Print + fmt::Debug, U: Print + fmt::Debug) ty::OutlivesPredicate<T, U>,
14881552
(self, f, cx) {

0 commit comments

Comments
 (0)