Skip to content

Commit 6c64870

Browse files
committed
Auto merge of #111630 - BoxyUwU:ty_const_debug_formatting, r=compiler-errors
debug format `Const`'s less verbosely Not user visible change only visible to people debugging const generics. Currently debug output for `ty::Const` is super verbose (even for `-Zverbose` lol), things like printing infer vars as `Infer(Var(?0c))` instead of just `?0c`, bound vars and placeholders not using `^0_1` or `!0_1` syntax respectively. With these changes its imo better but not perfect: `Const { ty: usize, kind: ^0_1 }` is still a lot for not much information. not entirely sure what to do about that so not dealing with it yet. Need to do formatting for `ConstKind::Expr` at some point too since rn it sucks (doesn't even print anything with `Display`) not gonna do that in this PR either. r? `@compiler-errors`
2 parents c0784db + 2a554eb commit 6c64870

File tree

7 files changed

+75
-45
lines changed

7 files changed

+75
-45
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
//

tests/mir-opt/issue_99325.main.built.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// MIR for `main` after built
22

33
| User Type Annotations
4-
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Value(Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)])) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5-
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Unevaluated(UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), substs: [] }) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
4+
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5+
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Unevaluated([], DefId(0:8 ~ issue_99325[22bb]::main::{constant#1})) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
66
|
77
fn main() -> () {
88
let mut _0: (); // return place in scope 0 at $DIR/issue_99325.rs:+0:15: +0:15

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
25-
let mut _1: [usize; Const(Value(Leaf(0x00000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
25+
let mut _1: [usize; Const { ty: usize, kind: Leaf(0x00000003) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
2626
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
2727
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
2828
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
25-
let mut _1: [usize; Const(Value(Leaf(0x0000000000000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
25+
let mut _1: [usize; Const { ty: usize, kind: Leaf(0x0000000000000003) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
2626
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
2727
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
2828
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18

0 commit comments

Comments
 (0)