Skip to content

Commit e16d71b

Browse files
committed
format Const's less verbosely
1 parent 9239760 commit e16d71b

File tree

4 files changed

+71
-41
lines changed

4 files changed

+71
-41
lines changed

compiler/rustc_middle/src/ty/consts.rs

-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::LocalDefId;
88
use rustc_macros::HashStable;
9-
use std::fmt;
109

1110
mod int;
1211
mod kind;
@@ -21,15 +20,6 @@ pub use valtree::*;
2120
#[rustc_pass_by_value]
2221
pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>);
2322

24-
impl<'tcx> fmt::Debug for Const<'tcx> {
25-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26-
// This reflects what `Const` looked liked before `Interned` was
27-
// introduced. We print it like this to avoid having to update expected
28-
// output in a lot of tests.
29-
write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind())
30-
}
31-
}
32-
3323
/// Typed constant value.
3424
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)]
3525
pub struct ConstData<'tcx> {

compiler/rustc_middle/src/ty/consts/kind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> UnevaluatedConst<'tcx> {
4242
}
4343

4444
/// Represents a constant in Rust.
45-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
45+
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
4646
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
4747
#[derive(derive_more::From)]
4848
pub enum ConstKind<'tcx> {
@@ -128,7 +128,7 @@ impl<'tcx> ConstKind<'tcx> {
128128
}
129129

130130
/// An inference variable for a const, for use in const generics.
131-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
131+
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
132132
pub enum InferConst<'tcx> {
133133
/// Infer the value of the const.
134134
Var(ty::ConstVid<'tcx>),

compiler/rustc_middle/src/ty/print/pretty.rs

+31-29
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ pub trait PrettyPrinter<'tcx>:
703703
ty::Error(_) => p!("[type error]"),
704704
ty::Param(ref param_ty) => p!(print(param_ty)),
705705
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
706-
ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?,
706+
ty::BoundTyKind::Anon => debug_bound_var(&mut self, debruijn, bound_ty.var)?,
707707
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
708708
true if debruijn == ty::INNERMOST => p!(write("^{}", s)),
709709
true => p!(write("^{}_{}", debruijn.index(), s)),
@@ -741,7 +741,7 @@ pub trait PrettyPrinter<'tcx>:
741741
}
742742
ty::Placeholder(placeholder) => match placeholder.bound.kind {
743743
ty::BoundTyKind::Anon => {
744-
self.pretty_print_placeholder_var(placeholder.universe, placeholder.bound.var)?
744+
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound.var)?;
745745
}
746746
ty::BoundTyKind::Param(_, name) => p!(write("{}", name)),
747747
},
@@ -1164,30 +1164,6 @@ pub trait PrettyPrinter<'tcx>:
11641164
traits.entry(trait_ref).or_default().extend(proj_ty);
11651165
}
11661166

1167-
fn pretty_print_bound_var(
1168-
&mut self,
1169-
debruijn: ty::DebruijnIndex,
1170-
var: ty::BoundVar,
1171-
) -> Result<(), Self::Error> {
1172-
if debruijn == ty::INNERMOST {
1173-
write!(self, "^{}", var.index())
1174-
} else {
1175-
write!(self, "^{}_{}", debruijn.index(), var.index())
1176-
}
1177-
}
1178-
1179-
fn pretty_print_placeholder_var(
1180-
&mut self,
1181-
ui: ty::UniverseIndex,
1182-
var: ty::BoundVar,
1183-
) -> Result<(), Self::Error> {
1184-
if ui == ty::UniverseIndex::ROOT {
1185-
write!(self, "!{}", var.index())
1186-
} else {
1187-
write!(self, "!{}_{}", ui.index(), var.index())
1188-
}
1189-
}
1190-
11911167
fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
11921168
None
11931169
}
@@ -1321,7 +1297,7 @@ pub trait PrettyPrinter<'tcx>:
13211297
define_scoped_cx!(self);
13221298

13231299
if self.should_print_verbose() {
1324-
p!(write("Const({:?}: {:?})", ct.kind(), ct.ty()));
1300+
p!(write("{:?}", ct));
13251301
return Ok(self);
13261302
}
13271303

@@ -1380,9 +1356,11 @@ pub trait PrettyPrinter<'tcx>:
13801356
}
13811357

13821358
ty::ConstKind::Bound(debruijn, bound_var) => {
1383-
self.pretty_print_bound_var(debruijn, bound_var)?
1359+
debug_bound_var(&mut self, debruijn, bound_var)?
13841360
}
1385-
ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
1361+
ty::ConstKind::Placeholder(placeholder) => {
1362+
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound)?;
1363+
},
13861364
// FIXME(generic_const_exprs):
13871365
// write out some legible representation of an abstract const?
13881366
ty::ConstKind::Expr(_) => p!("[const expr]"),
@@ -3067,3 +3045,27 @@ pub struct OpaqueFnEntry<'tcx> {
30673045
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
30683046
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
30693047
}
3048+
3049+
pub fn debug_bound_var<T: std::fmt::Write>(
3050+
fmt: &mut T,
3051+
debruijn: ty::DebruijnIndex,
3052+
var: ty::BoundVar,
3053+
) -> Result<(), std::fmt::Error> {
3054+
if debruijn == ty::INNERMOST {
3055+
write!(fmt, "^{}", var.index())
3056+
} else {
3057+
write!(fmt, "^{}_{}", debruijn.index(), var.index())
3058+
}
3059+
}
3060+
3061+
pub fn debug_placeholder_var<T: std::fmt::Write>(
3062+
fmt: &mut T,
3063+
universe: ty::UniverseIndex,
3064+
bound: ty::BoundVar,
3065+
) -> Result<(), std::fmt::Error> {
3066+
if universe == ty::UniverseIndex::ROOT {
3067+
write!(fmt, "!{}", bound.index())
3068+
} else {
3069+
write!(fmt, "!{}_{}", universe.index(), bound.index())
3070+
}
3071+
}

compiler/rustc_middle/src/ty/structural_impls.rs

+38
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,44 @@ impl<'tcx> fmt::Debug for AliasTy<'tcx> {
192192
}
193193
}
194194

195+
impl<'tcx> fmt::Debug for ty::InferConst<'tcx> {
196+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197+
match self {
198+
InferConst::Var(var) => write!(f, "{var:?}"),
199+
InferConst::Fresh(var) => write!(f, "Fresh({var:?})"),
200+
}
201+
}
202+
}
203+
204+
impl<'tcx> fmt::Debug for ty::Const<'tcx> {
205+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206+
// This reflects what `Const` looked liked before `Interned` was
207+
// introduced. We print it like this to avoid having to update expected
208+
// output in a lot of tests.
209+
write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind())
210+
}
211+
}
212+
213+
impl<'tcx> fmt::Debug for ty::ConstKind<'tcx> {
214+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215+
use ty::ConstKind::*;
216+
match self {
217+
Param(param) => write!(f, "{param:?}"),
218+
Infer(var) => write!(f, "{var:?}"),
219+
Bound(debruijn, var) => ty::print::debug_bound_var(f, *debruijn, *var),
220+
Placeholder(placeholder) => {
221+
ty::print::debug_placeholder_var(f, placeholder.universe, placeholder.bound)
222+
}
223+
Unevaluated(uv) => {
224+
f.debug_tuple("Unevaluated").field(&uv.substs).field(&uv.def).finish()
225+
}
226+
Value(valtree) => write!(f, "{valtree:?}"),
227+
Error(_) => write!(f, "[const error]"),
228+
Expr(expr) => write!(f, "{expr:?}"),
229+
}
230+
}
231+
}
232+
195233
///////////////////////////////////////////////////////////////////////////
196234
// Atomic structs
197235
//

0 commit comments

Comments
 (0)