Skip to content

Commit 9f83e56

Browse files
authored
Rollup merge of #111988 - BoxyUwU:make_tykind_debug_good, r=compiler-errors
Make `TyKind: Debug` have less verbose output Current `TyKind: Debug` impl is basically unusable for debugging, its too verbose even for verbose debugging 🤣 This PR replaces the debug logic for `TyKind` with a more manual debug impl instead of a hand expanded derived impl. This should help make #107084 more reasonable to land since the output of `Ty: Debug` will be better. This isn't a fully completed change to the `Debug` impl of `TyKind` as there's still logic from the derive macro for some variants. Some of the variants are also not consisten with the `-Zverbose` printing of `Ty`, ideally `-Zverbose` printing of `Ty` would also just defer to the debug impl instead of having lots of checks in pretty printing. I plan on fixing this in follow up PRs since it seems tricky to do in this one and its already a large PR 😅
2 parents 3fead07 + 976adf3 commit 9f83e56

File tree

9 files changed

+176
-78
lines changed

9 files changed

+176
-78
lines changed

compiler/rustc_middle/src/ty/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
115115
type FreeRegion = ty::FreeRegion;
116116
type RegionVid = ty::RegionVid;
117117
type PlaceholderRegion = ty::PlaceholderRegion;
118+
119+
fn ty_and_mut_to_parts(
120+
TypeAndMut { ty, mutbl }: TypeAndMut<'tcx>,
121+
) -> (Self::Ty, Self::Mutability) {
122+
(ty, mutbl)
123+
}
124+
125+
fn mutability_is_mut(mutbl: Self::Mutability) -> bool {
126+
mutbl.is_mut()
127+
}
118128
}
119129

120130
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
14961496
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
14971497
/// regions/types/consts within the same universe simply have an unknown relationship to one
14981498
/// another.
1499-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
1499+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
15001500
#[derive(HashStable, TyEncodable, TyDecodable)]
15011501
pub struct Placeholder<T> {
15021502
pub universe: UniverseIndex,

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

+19-43
Original file line numberDiff line numberDiff line change
@@ -685,29 +685,30 @@ pub trait PrettyPrinter<'tcx>:
685685
}
686686
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
687687
ty::Infer(infer_ty) => {
688-
let verbose = self.should_print_verbose();
688+
if self.should_print_verbose() {
689+
p!(write("{:?}", ty.kind()));
690+
return Ok(self);
691+
}
692+
689693
if let ty::TyVar(ty_vid) = infer_ty {
690694
if let Some(name) = self.ty_infer_name(ty_vid) {
691695
p!(write("{}", name))
692696
} else {
693-
if verbose {
694-
p!(write("{:?}", infer_ty))
695-
} else {
696-
p!(write("{}", infer_ty))
697-
}
697+
p!(write("{}", infer_ty))
698698
}
699699
} else {
700-
if verbose { p!(write("{:?}", infer_ty)) } else { p!(write("{}", infer_ty)) }
700+
p!(write("{}", infer_ty))
701701
}
702702
}
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 => debug_bound_var(&mut self, debruijn, bound_ty.var)?,
706+
ty::BoundTyKind::Anon => {
707+
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_ty.var)?
708+
}
707709
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
708-
true if debruijn == ty::INNERMOST => p!(write("^{}", s)),
709-
true => p!(write("^{}_{}", debruijn.index(), s)),
710-
false => p!(write("{}", s)),
710+
true => p!(write("{:?}", ty.kind())),
711+
false => p!(write("{s}")),
711712
},
712713
},
713714
ty::Adt(def, substs) => {
@@ -740,10 +741,11 @@ pub trait PrettyPrinter<'tcx>:
740741
}
741742
}
742743
ty::Placeholder(placeholder) => match placeholder.bound.kind {
743-
ty::BoundTyKind::Anon => {
744-
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound.var)?;
745-
}
746-
ty::BoundTyKind::Param(_, name) => p!(write("{}", name)),
744+
ty::BoundTyKind::Anon => p!(write("{placeholder:?}")),
745+
ty::BoundTyKind::Param(_, name) => match self.should_print_verbose() {
746+
true => p!(write("{:?}", ty.kind())),
747+
false => p!(write("{name}")),
748+
},
747749
},
748750
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
749751
// We use verbose printing in 'NO_QUERIES' mode, to
@@ -1372,11 +1374,9 @@ pub trait PrettyPrinter<'tcx>:
13721374
}
13731375

13741376
ty::ConstKind::Bound(debruijn, bound_var) => {
1375-
debug_bound_var(&mut self, debruijn, bound_var)?
1377+
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_var)?
13761378
}
1377-
ty::ConstKind::Placeholder(placeholder) => {
1378-
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound)?;
1379-
},
1379+
ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
13801380
// FIXME(generic_const_exprs):
13811381
// write out some legible representation of an abstract const?
13821382
ty::ConstKind::Expr(_) => p!("{{const expr}}"),
@@ -3065,27 +3065,3 @@ pub struct OpaqueFnEntry<'tcx> {
30653065
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
30663066
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
30673067
}
3068-
3069-
pub fn debug_bound_var<T: std::fmt::Write>(
3070-
fmt: &mut T,
3071-
debruijn: ty::DebruijnIndex,
3072-
var: ty::BoundVar,
3073-
) -> Result<(), std::fmt::Error> {
3074-
if debruijn == ty::INNERMOST {
3075-
write!(fmt, "^{}", var.index())
3076-
} else {
3077-
write!(fmt, "^{}_{}", debruijn.index(), var.index())
3078-
}
3079-
}
3080-
3081-
pub fn debug_placeholder_var<T: std::fmt::Write>(
3082-
fmt: &mut T,
3083-
universe: ty::UniverseIndex,
3084-
bound: ty::BoundVar,
3085-
) -> Result<(), std::fmt::Error> {
3086-
if universe == ty::UniverseIndex::ROOT {
3087-
write!(fmt, "!{}", bound.index())
3088-
} else {
3089-
write!(fmt, "!{}_{}", universe.index(), bound.index())
3090-
}
3091-
}

compiler/rustc_middle/src/ty/structural_impls.rs

+52-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,35 @@ impl fmt::Debug for ty::FreeRegion {
8888

8989
impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
9090
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91-
write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
91+
let ty::FnSig { inputs_and_output: _, c_variadic, unsafety, abi } = self;
92+
93+
write!(f, "{}", unsafety.prefix_str())?;
94+
match abi {
95+
rustc_target::spec::abi::Abi::Rust => (),
96+
abi => write!(f, "extern \"{abi:?}\" ")?,
97+
};
98+
99+
write!(f, "fn(")?;
100+
let inputs = self.inputs();
101+
match inputs.len() {
102+
0 if *c_variadic => write!(f, "...)")?,
103+
0 => write!(f, ")")?,
104+
_ => {
105+
for ty in &self.inputs()[0..(self.inputs().len() - 1)] {
106+
write!(f, "{ty:?}, ")?;
107+
}
108+
write!(f, "{:?}", self.inputs().last().unwrap())?;
109+
if *c_variadic {
110+
write!(f, "...")?;
111+
}
112+
write!(f, ")")?;
113+
}
114+
}
115+
116+
match self.output().kind() {
117+
ty::Tuple(list) if list.is_empty() => Ok(()),
118+
_ => write!(f, " -> {:?}", self.output()),
119+
}
92120
}
93121
}
94122

@@ -216,20 +244,37 @@ impl<'tcx> fmt::Debug for ty::ConstKind<'tcx> {
216244
match self {
217245
Param(param) => write!(f, "{param:?}"),
218246
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-
}
247+
Bound(debruijn, var) => rustc_type_ir::debug_bound_var(f, *debruijn, *var),
248+
Placeholder(placeholder) => write!(f, "{placeholder:?}"),
223249
Unevaluated(uv) => {
224250
f.debug_tuple("Unevaluated").field(&uv.substs).field(&uv.def).finish()
225251
}
226252
Value(valtree) => write!(f, "{valtree:?}"),
227-
Error(_) => write!(f, "[const error]"),
253+
Error(_) => write!(f, "{{const error}}"),
228254
Expr(expr) => write!(f, "{expr:?}"),
229255
}
230256
}
231257
}
232258

259+
impl fmt::Debug for ty::BoundTy {
260+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261+
match self.kind {
262+
ty::BoundTyKind::Anon => write!(f, "{:?}", self.var),
263+
ty::BoundTyKind::Param(_, sym) => write!(f, "{sym:?}"),
264+
}
265+
}
266+
}
267+
268+
impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
269+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270+
if self.universe == ty::UniverseIndex::ROOT {
271+
write!(f, "!{:?}", self.bound)
272+
} else {
273+
write!(f, "!{}_{:?}", self.universe.index(), self.bound)
274+
}
275+
}
276+
}
277+
233278
///////////////////////////////////////////////////////////////////////////
234279
// Atomic structs
235280
//
@@ -294,6 +339,7 @@ TrivialTypeTraversalAndLiftImpls! {
294339
crate::ty::AliasRelationDirection,
295340
crate::ty::Placeholder<crate::ty::BoundRegion>,
296341
crate::ty::Placeholder<crate::ty::BoundTy>,
342+
crate::ty::Placeholder<ty::BoundVar>,
297343
crate::ty::ClosureKind,
298344
crate::ty::FreeRegion,
299345
crate::ty::InferTy,
@@ -310,7 +356,6 @@ TrivialTypeTraversalAndLiftImpls! {
310356
interpret::Scalar,
311357
rustc_target::abi::Size,
312358
ty::BoundVar,
313-
ty::Placeholder<ty::BoundVar>,
314359
}
315360

316361
TrivialTypeTraversalAndLiftImpls! {

compiler/rustc_middle/src/ty/sty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,11 @@ impl Atom for RegionVid {
15111511

15121512
rustc_index::newtype_index! {
15131513
#[derive(HashStable)]
1514+
#[debug_format = "{}"]
15141515
pub struct BoundVar {}
15151516
}
15161517

1517-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
1518+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
15181519
#[derive(HashStable)]
15191520
pub struct BoundTy {
15201521
pub var: BoundVar,

compiler/rustc_type_ir/src/lib.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub trait Interner: Sized {
5252
type PolyFnSig: Clone + Debug + Hash + Ord;
5353
type ListBinderExistentialPredicate: Clone + Debug + Hash + Ord;
5454
type BinderListTy: Clone + Debug + Hash + Ord;
55-
type ListTy: Clone + Debug + Hash + Ord;
55+
type ListTy: Clone + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
5656
type AliasTy: Clone + Debug + Hash + Ord;
5757
type ParamTy: Clone + Debug + Hash + Ord;
5858
type BoundTy: Clone + Debug + Hash + Ord;
@@ -67,6 +67,9 @@ pub trait Interner: Sized {
6767
type FreeRegion: Clone + Debug + Hash + Ord;
6868
type RegionVid: Clone + Debug + Hash + Ord;
6969
type PlaceholderRegion: Clone + Debug + Hash + Ord;
70+
71+
fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Self::Mutability);
72+
fn mutability_is_mut(mutbl: Self::Mutability) -> bool;
7073
}
7174

7275
/// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter`
@@ -390,7 +393,19 @@ impl DebruijnIndex {
390393
}
391394
}
392395

393-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
396+
pub fn debug_bound_var<T: std::fmt::Write>(
397+
fmt: &mut T,
398+
debruijn: DebruijnIndex,
399+
var: impl std::fmt::Debug,
400+
) -> Result<(), std::fmt::Error> {
401+
if debruijn == INNERMOST {
402+
write!(fmt, "^{:?}", var)
403+
} else {
404+
write!(fmt, "^{}_{:?}", debruijn.index(), var)
405+
}
406+
}
407+
408+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
394409
#[derive(Encodable, Decodable, HashStable_Generic)]
395410
pub enum IntTy {
396411
Isize,
@@ -448,7 +463,7 @@ impl IntTy {
448463
}
449464
}
450465

451-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
466+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
452467
#[derive(Encodable, Decodable, HashStable_Generic)]
453468
pub enum UintTy {
454469
Usize,
@@ -506,7 +521,7 @@ impl UintTy {
506521
}
507522
}
508523

509-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
524+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
510525
#[derive(Encodable, Decodable, HashStable_Generic)]
511526
pub enum FloatTy {
512527
F32,

compiler/rustc_type_ir/src/structural_impls.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
55
use crate::fold::{FallibleTypeFolder, TypeFoldable};
66
use crate::visit::{TypeVisitable, TypeVisitor};
7-
use crate::Interner;
7+
use crate::{FloatTy, IntTy, Interner, UintTy};
88
use rustc_data_structures::functor::IdFunctor;
99
use rustc_data_structures::sync::Lrc;
1010
use rustc_index::{Idx, IndexVec};
1111

12+
use core::fmt;
1213
use std::ops::ControlFlow;
1314

1415
///////////////////////////////////////////////////////////////////////////
@@ -163,3 +164,21 @@ impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix
163164
self.iter().try_for_each(|t| t.visit_with(visitor))
164165
}
165166
}
167+
168+
impl fmt::Debug for IntTy {
169+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170+
write!(f, "{}", self.name_str())
171+
}
172+
}
173+
174+
impl fmt::Debug for UintTy {
175+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176+
write!(f, "{}", self.name_str())
177+
}
178+
}
179+
180+
impl fmt::Debug for FloatTy {
181+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182+
write!(f, "{}", self.name_str())
183+
}
184+
}

0 commit comments

Comments
 (0)