]> git.proxmox.com Git - rustc.git/blame - vendor/chalk-ir/src/debug.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / vendor / chalk-ir / src / debug.rs
CommitLineData
f035d41b
XL
1//! Debug impls for types.
2
f9f354fc
XL
3use std::fmt::{Debug, Display, Error, Formatter};
4
5use super::*;
6
7impl<I: Interner> Debug for TraitId<I> {
8 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
9 I::debug_trait_id(*self, fmt).unwrap_or_else(|| write!(fmt, "TraitId({:?})", self.0))
10 }
11}
12
f035d41b 13impl<I: Interner> Debug for AdtId<I> {
f9f354fc 14 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 15 I::debug_adt_id(*self, fmt).unwrap_or_else(|| write!(fmt, "AdtId({:?})", self.0))
f9f354fc
XL
16 }
17}
18
19impl<I: Interner> Debug for AssocTypeId<I> {
20 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
21 I::debug_assoc_type_id(*self, fmt)
22 .unwrap_or_else(|| write!(fmt, "AssocTypeId({:?})", self.0))
23 }
24}
25
f035d41b
XL
26impl<I: Interner> Debug for FnDefId<I> {
27 fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
28 I::debug_fn_def_id(*self, fmt).unwrap_or_else(|| write!(fmt, "FnDefId({:?})", self.0))
29 }
30}
31
32impl<I: Interner> Debug for ClosureId<I> {
33 fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
34 I::debug_closure_id(*self, fmt).unwrap_or_else(|| write!(fmt, "ClosureId({:?})", self.0))
35 }
36}
37
29967ef6
XL
38impl<I: Interner> Debug for GeneratorId<I> {
39 fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
40 I::debug_generator_id(*self, fmt)
41 .unwrap_or_else(|| write!(fmt, "GeneratorId({:?})", self.0))
42 }
43}
44
1b1a35ee
XL
45impl<I: Interner> Debug for ForeignDefId<I> {
46 fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
47 I::debug_foreign_def_id(*self, fmt)
48 .unwrap_or_else(|| write!(fmt, "ForeignDefId({:?})", self.0))
49 }
50}
51
f9f354fc
XL
52impl<I: Interner> Debug for Ty<I> {
53 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
54 I::debug_ty(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
55 }
56}
57
58impl<I: Interner> Debug for Lifetime<I> {
59 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
60 I::debug_lifetime(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
61 }
62}
63
f035d41b
XL
64impl<I: Interner> Debug for Const<I> {
65 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
66 I::debug_const(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
67 }
68}
69
70impl<I: Interner> Debug for ConcreteConst<I> {
71 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
72 write!(fmt, "{:?}", self.interned)
73 }
74}
75
76impl<I: Interner> Debug for GenericArg<I> {
f9f354fc 77 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 78 I::debug_generic_arg(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
f9f354fc
XL
79 }
80}
81
82impl<I: Interner> Debug for Goal<I> {
83 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
84 I::debug_goal(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
85 }
86}
87
88impl<I: Interner> Debug for Goals<I> {
89 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
90 I::debug_goals(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
91 }
92}
93
94impl<I: Interner> Debug for ProgramClauseImplication<I> {
95 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
96 I::debug_program_clause_implication(self, fmt)
97 .unwrap_or_else(|| write!(fmt, "ProgramClauseImplication(?)"))
98 }
99}
100
101impl<I: Interner> Debug for ProgramClause<I> {
102 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
103 I::debug_program_clause(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
104 }
105}
106
107impl<I: Interner> Debug for ProgramClauses<I> {
108 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
109 I::debug_program_clauses(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
110 }
111}
112
3dfed10e
XL
113impl<I: Interner> Debug for Constraints<I> {
114 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
115 I::debug_constraints(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
116 }
117}
118
f9f354fc
XL
119impl<I: Interner> Debug for SeparatorTraitRef<'_, I> {
120 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
121 I::debug_separator_trait_ref(self, fmt)
122 .unwrap_or_else(|| write!(fmt, "SeparatorTraitRef(?)"))
123 }
124}
125
126impl<I: Interner> Debug for AliasTy<I> {
127 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
128 I::debug_alias(self, fmt).unwrap_or_else(|| write!(fmt, "AliasTy(?)"))
129 }
130}
131
132impl<I: Interner> Debug for QuantifiedWhereClauses<I> {
133 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
134 I::debug_quantified_where_clauses(self, fmt)
135 .unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
136 }
137}
138
139impl<I: Interner> Debug for ProjectionTy<I> {
140 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
141 I::debug_projection_ty(self, fmt).unwrap_or_else(|| {
142 unimplemented!("cannot format ProjectionTy without setting Program in tls")
143 })
144 }
145}
146
147impl<I: Interner> Debug for OpaqueTy<I> {
148 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
149 I::debug_opaque_ty(self, fmt).unwrap_or_else(|| {
150 unimplemented!("cannot format OpaqueTy without setting Program in tls")
151 })
152 }
153}
154
155impl<I: Interner> Display for Substitution<I> {
156 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
157 I::debug_substitution(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
158 }
159}
160
161impl<I: Interner> Debug for OpaqueTyId<I> {
162 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
163 I::debug_opaque_ty_id(*self, fmt).unwrap_or_else(|| write!(fmt, "OpaqueTyId({:?})", self.0))
164 }
165}
166
167impl Display for UniverseIndex {
168 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
169 write!(fmt, "U{}", self.counter)
170 }
171}
172
173impl Debug for UniverseIndex {
174 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
175 write!(fmt, "U{}", self.counter)
176 }
177}
178
29967ef6 179impl<I: Interner> Debug for TyData<I> {
f9f354fc 180 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
29967ef6 181 self.kind.fmt(fmt)
f9f354fc
XL
182 }
183}
184
29967ef6 185impl<I: Interner> Debug for TyKind<I> {
f9f354fc
XL
186 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
187 match self {
29967ef6
XL
188 TyKind::BoundVar(db) => write!(fmt, "{:?}", db),
189 TyKind::Dyn(clauses) => write!(fmt, "{:?}", clauses),
190 TyKind::InferenceVar(var, TyVariableKind::General) => write!(fmt, "{:?}", var),
191 TyKind::InferenceVar(var, TyVariableKind::Integer) => write!(fmt, "{:?}i", var),
192 TyKind::InferenceVar(var, TyVariableKind::Float) => write!(fmt, "{:?}f", var),
193 TyKind::Alias(alias) => write!(fmt, "{:?}", alias),
194 TyKind::Placeholder(index) => write!(fmt, "{:?}", index),
195 TyKind::Function(function) => write!(fmt, "{:?}", function),
196 TyKind::Adt(id, substitution) => write!(fmt, "{:?}<{:?}>", id, substitution),
197 TyKind::AssociatedType(assoc_ty, substitution) => {
198 write!(fmt, "{:?}<{:?}>", assoc_ty, substitution)
199 }
200 TyKind::Scalar(scalar) => write!(fmt, "{:?}", scalar),
201 TyKind::Str => write!(fmt, "Str"),
202 TyKind::Tuple(arity, substitution) => write!(fmt, "{:?}<{:?}>", arity, substitution),
203 TyKind::OpaqueType(opaque_ty, substitution) => {
204 write!(fmt, "!{:?}<{:?}>", opaque_ty, substitution)
205 }
206 TyKind::Slice(substitution) => write!(fmt, "{{slice}}<{:?}>", substitution),
207 TyKind::FnDef(fn_def, substitution) => write!(fmt, "{:?}<{:?}>", fn_def, substitution),
208 TyKind::Ref(mutability, lifetime, ty) => match mutability {
209 Mutability::Mut => write!(fmt, "(&{:?} mut {:?})", lifetime, ty),
210 Mutability::Not => write!(fmt, "(&{:?} {:?})", lifetime, ty),
211 },
212 TyKind::Raw(mutability, ty) => match mutability {
213 Mutability::Mut => write!(fmt, "(*mut {:?})", ty),
214 Mutability::Not => write!(fmt, "(*const {:?})", ty),
215 },
216 TyKind::Never => write!(fmt, "Never"),
217 TyKind::Array(ty, const_) => write!(fmt, "[{:?}; {:?}]", ty, const_),
218 TyKind::Closure(id, substitution) => {
219 write!(fmt, "{{closure:{:?}}}<{:?}>", id, substitution)
220 }
221 TyKind::Generator(generator, substitution) => {
222 write!(fmt, "{:?}<{:?}>", generator, substitution)
223 }
224 TyKind::GeneratorWitness(witness, substitution) => {
225 write!(fmt, "{:?}<{:?}>", witness, substitution)
226 }
227 TyKind::Foreign(foreign_ty) => write!(fmt, "{:?}", foreign_ty),
228 TyKind::Error => write!(fmt, "{{error}}"),
f9f354fc
XL
229 }
230 }
231}
232
233impl Debug for BoundVar {
234 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
235 let BoundVar { debruijn, index } = self;
236 write!(fmt, "{:?}.{:?}", debruijn, index)
237 }
238}
239
240impl Debug for DebruijnIndex {
241 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
242 let DebruijnIndex { depth } = self;
243 write!(fmt, "^{}", depth)
244 }
245}
246
247impl<I: Interner> Debug for DynTy<I> {
248 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b
XL
249 let DynTy { bounds, lifetime } = self;
250 write!(fmt, "dyn {:?} + {:?}", bounds, lifetime)
f9f354fc
XL
251 }
252}
253
254impl Debug for InferenceVar {
255 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
256 write!(fmt, "?{}", self.index)
257 }
258}
259
3dfed10e 260impl<I: Interner> Debug for FnPointer<I> {
f9f354fc
XL
261 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
262 // FIXME -- we should introduce some names or something here
3dfed10e 263 let FnPointer {
f9f354fc
XL
264 num_binders,
265 substitution,
1b1a35ee 266 sig,
f9f354fc 267 } = self;
3dfed10e
XL
268 write!(
269 fmt,
270 "for<{}> {:?} {:?} {:?}",
1b1a35ee 271 num_binders, sig.safety, sig.abi, substitution
3dfed10e 272 )
f9f354fc
XL
273 }
274}
275
276impl<I: Interner> Debug for LifetimeData<I> {
277 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
278 match self {
279 LifetimeData::BoundVar(db) => write!(fmt, "'{:?}", db),
280 LifetimeData::InferenceVar(var) => write!(fmt, "'{:?}", var),
281 LifetimeData::Placeholder(index) => write!(fmt, "'{:?}", index),
29967ef6 282 LifetimeData::Static => write!(fmt, "'static"),
f9f354fc
XL
283 LifetimeData::Phantom(..) => unreachable!(),
284 }
285 }
286}
287
f035d41b
XL
288impl<I: Interner> VariableKinds<I> {
289 fn debug(&self) -> VariableKindsDebug<'_, I> {
290 VariableKindsDebug(self)
f9f354fc
XL
291 }
292
f035d41b
XL
293 /// Helper method for debugging variable kinds.
294 pub fn inner_debug<'a>(&'a self, interner: &'a I) -> VariableKindsInnerDebug<'a, I> {
295 VariableKindsInnerDebug {
296 variable_kinds: self,
f9f354fc
XL
297 interner,
298 }
299 }
300}
301
f035d41b 302struct VariableKindsDebug<'a, I: Interner>(&'a VariableKinds<I>);
f9f354fc 303
f035d41b 304impl<'a, I: Interner> Debug for VariableKindsDebug<'a, I> {
f9f354fc 305 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 306 I::debug_variable_kinds_with_angles(self.0, fmt)
f9f354fc
XL
307 .unwrap_or_else(|| write!(fmt, "{:?}", self.0.interned))
308 }
309}
310
f035d41b
XL
311/// Helper struct for showing debug output for `VariableKinds`.
312pub struct VariableKindsInnerDebug<'a, I: Interner> {
313 variable_kinds: &'a VariableKinds<I>,
f9f354fc
XL
314 interner: &'a I,
315}
316
f035d41b 317impl<'a, I: Interner> Debug for VariableKindsInnerDebug<'a, I> {
f9f354fc 318 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b
XL
319 // NB: We print variable kinds as a list delimited by `<>`,
320 // like `<K1, K2, ..>`. This is because variable kind lists
f9f354fc
XL
321 // are always associated with binders like `forall<type> {
322 // ... }`.
323 write!(fmt, "<")?;
f035d41b 324 for (index, binder) in self.variable_kinds.iter(self.interner).enumerate() {
f9f354fc
XL
325 if index > 0 {
326 write!(fmt, ", ")?;
327 }
f035d41b 328 match binder {
29967ef6
XL
329 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "type")?,
330 VariableKind::Ty(TyVariableKind::Integer) => write!(fmt, "integer type")?,
331 VariableKind::Ty(TyVariableKind::Float) => write!(fmt, "float type")?,
f035d41b
XL
332 VariableKind::Lifetime => write!(fmt, "lifetime")?,
333 VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty)?,
f9f354fc
XL
334 }
335 }
336 write!(fmt, ">")
337 }
338}
339
f035d41b
XL
340impl<I: Interner> Debug for ConstData<I> {
341 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
342 match &self.value {
343 ConstValue::BoundVar(db) => write!(fmt, "{:?}", db),
344 ConstValue::InferenceVar(var) => write!(fmt, "{:?}", var),
345 ConstValue::Placeholder(index) => write!(fmt, "{:?}", index),
346 ConstValue::Concrete(evaluated) => write!(fmt, "{:?}", evaluated),
347 }
348 }
349}
350
f9f354fc
XL
351impl<I: Interner> Debug for GoalData<I> {
352 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
353 match self {
354 GoalData::Quantified(qkind, ref subgoal) => write!(
355 fmt,
356 "{:?}{:?} {{ {:?} }}",
357 qkind,
358 subgoal.binders.debug(),
359 subgoal.value
360 ),
361 GoalData::Implies(ref wc, ref g) => write!(fmt, "if ({:?}) {{ {:?} }}", wc, g),
362 GoalData::All(ref goals) => write!(fmt, "all{:?}", goals),
363 GoalData::Not(ref g) => write!(fmt, "not {{ {:?} }}", g),
364 GoalData::EqGoal(ref wc) => write!(fmt, "{:?}", wc),
365 GoalData::DomainGoal(ref wc) => write!(fmt, "{:?}", wc),
3dfed10e 366 GoalData::CannotProve => write!(fmt, r"¯\_(ツ)_/¯"),
f9f354fc
XL
367 }
368 }
369}
370
f035d41b 371/// Helper struct for showing debug output for `Goals`.
f9f354fc
XL
372pub struct GoalsDebug<'a, I: Interner> {
373 goals: &'a Goals<I>,
374 interner: &'a I,
375}
376
377impl<'a, I: Interner> Debug for GoalsDebug<'a, I> {
378 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
379 write!(fmt, "(")?;
380 for (goal, index) in self.goals.iter(self.interner).zip(0..) {
381 if index > 0 {
382 write!(fmt, ", ")?;
383 }
384 write!(fmt, "{:?}", goal)?;
385 }
386 write!(fmt, ")")?;
387 Ok(())
388 }
389}
390
391impl<I: Interner> Goals<I> {
f035d41b 392 /// Show debug output for `Goals`.
f9f354fc
XL
393 pub fn debug<'a>(&'a self, interner: &'a I) -> GoalsDebug<'a, I> {
394 GoalsDebug {
395 goals: self,
396 interner,
397 }
398 }
399}
400
f035d41b
XL
401/// Helper struct for showing debug output for `GenericArgData`.
402pub struct GenericArgDataInnerDebug<'a, I: Interner>(&'a GenericArgData<I>);
f9f354fc 403
f035d41b 404impl<'a, I: Interner> Debug for GenericArgDataInnerDebug<'a, I> {
f9f354fc
XL
405 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
406 match self.0 {
f035d41b
XL
407 GenericArgData::Ty(n) => write!(fmt, "{:?}", n),
408 GenericArgData::Lifetime(n) => write!(fmt, "{:?}", n),
409 GenericArgData::Const(n) => write!(fmt, "{:?}", n),
f9f354fc
XL
410 }
411 }
412}
413
f035d41b
XL
414impl<I: Interner> GenericArgData<I> {
415 /// Helper method for debugging `GenericArgData`.
416 pub fn inner_debug(&self) -> GenericArgDataInnerDebug<'_, I> {
417 GenericArgDataInnerDebug(self)
f9f354fc
XL
418 }
419}
420
f035d41b 421/// Helper struct for showing debug output for program clause implications.
f9f354fc
XL
422pub struct ProgramClauseImplicationDebug<'a, I: Interner> {
423 pci: &'a ProgramClauseImplication<I>,
424 interner: &'a I,
425}
426
427impl<'a, I: Interner> Debug for ProgramClauseImplicationDebug<'a, I> {
428 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
429 let ProgramClauseImplicationDebug { pci, interner } = self;
430 write!(fmt, "{:?}", pci.consequence)?;
431
432 let conditions = pci.conditions.as_slice(interner);
433
434 let conds = conditions.len();
435 if conds == 0 {
436 return Ok(());
437 }
438
439 write!(fmt, " :- ")?;
440 for cond in &conditions[..conds - 1] {
441 write!(fmt, "{:?}, ", cond)?;
442 }
443 write!(fmt, "{:?}", conditions[conds - 1])
444 }
445}
446
447impl<I: Interner> ProgramClauseImplication<I> {
f035d41b 448 /// Show debug output for the program clause implication.
f9f354fc
XL
449 pub fn debug<'a>(&'a self, interner: &'a I) -> ProgramClauseImplicationDebug<'a, I> {
450 ProgramClauseImplicationDebug {
451 pci: self,
452 interner,
453 }
454 }
455}
456
f035d41b 457/// Helper struct for showing debug output for application types.
29967ef6
XL
458pub struct TyKindDebug<'a, I: Interner> {
459 ty: &'a TyKind<I>,
f9f354fc
XL
460 interner: &'a I,
461}
462
29967ef6 463impl<'a, I: Interner> Debug for TyKindDebug<'a, I> {
f9f354fc 464 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
29967ef6
XL
465 let interner = self.interner;
466 match self.ty {
467 TyKind::BoundVar(db) => write!(fmt, "{:?}", db),
468 TyKind::Dyn(clauses) => write!(fmt, "{:?}", clauses),
469 TyKind::InferenceVar(var, TyVariableKind::General) => write!(fmt, "{:?}", var),
470 TyKind::InferenceVar(var, TyVariableKind::Integer) => write!(fmt, "{:?}i", var),
471 TyKind::InferenceVar(var, TyVariableKind::Float) => write!(fmt, "{:?}f", var),
472 TyKind::Alias(alias) => write!(fmt, "{:?}", alias),
473 TyKind::Placeholder(index) => write!(fmt, "{:?}", index),
474 TyKind::Function(function) => write!(fmt, "{:?}", function),
475 TyKind::Adt(id, substitution) => {
476 write!(fmt, "{:?}{:?}", id, substitution.with_angle(interner))
477 }
478 TyKind::AssociatedType(assoc_ty, substitution) => {
479 write!(fmt, "{:?}{:?}", assoc_ty, substitution.with_angle(interner))
480 }
481 TyKind::Scalar(scalar) => write!(fmt, "{:?}", scalar),
482 TyKind::Str => write!(fmt, "Str"),
483 TyKind::Tuple(arity, substitution) => {
484 write!(fmt, "{:?}{:?}", arity, substitution.with_angle(interner))
485 }
486 TyKind::OpaqueType(opaque_ty, substitution) => write!(
487 fmt,
488 "!{:?}{:?}",
489 opaque_ty,
490 substitution.with_angle(interner)
491 ),
492 TyKind::Slice(ty) => write!(fmt, "[{:?}]", ty),
493 TyKind::FnDef(fn_def, substitution) => {
494 write!(fmt, "{:?}{:?}", fn_def, substitution.with_angle(interner))
495 }
496 TyKind::Ref(mutability, lifetime, ty) => match mutability {
497 Mutability::Mut => write!(fmt, "(&{:?} mut {:?})", lifetime, ty),
498 Mutability::Not => write!(fmt, "(&{:?} {:?})", lifetime, ty),
499 },
500 TyKind::Raw(mutability, ty) => match mutability {
501 Mutability::Mut => write!(fmt, "(*mut {:?})", ty),
502 Mutability::Not => write!(fmt, "(*const {:?})", ty),
503 },
504 TyKind::Never => write!(fmt, "Never"),
505 TyKind::Array(ty, const_) => write!(fmt, "[{:?}; {:?}]", ty, const_),
506 TyKind::Closure(id, substitution) => write!(
507 fmt,
508 "{{closure:{:?}}}{:?}",
509 id,
510 substitution.with_angle(interner)
511 ),
512 TyKind::Generator(generator, substitution) => write!(
513 fmt,
514 "{:?}{:?}",
515 generator,
516 substitution.with_angle(interner)
517 ),
518 TyKind::GeneratorWitness(witness, substitution) => {
519 write!(fmt, "{:?}{:?}", witness, substitution.with_angle(interner))
520 }
521 TyKind::Foreign(foreign_ty) => write!(fmt, "{:?}", foreign_ty,),
522 TyKind::Error => write!(fmt, "{{error}}"),
523 }
f9f354fc
XL
524 }
525}
526
29967ef6 527impl<I: Interner> TyKind<I> {
f035d41b 528 /// Show debug output for the application type.
29967ef6
XL
529 pub fn debug<'a>(&'a self, interner: &'a I) -> TyKindDebug<'a, I> {
530 TyKindDebug { ty: self, interner }
f9f354fc
XL
531 }
532}
533
f035d41b 534/// Helper struct for showing debug output for substitutions.
f9f354fc
XL
535pub struct SubstitutionDebug<'a, I: Interner> {
536 substitution: &'a Substitution<I>,
537 interner: &'a I,
538}
539
540impl<'a, I: Interner> Debug for SubstitutionDebug<'a, I> {
541 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
542 let SubstitutionDebug {
543 substitution,
544 interner,
545 } = self;
546 let mut first = true;
547
548 write!(fmt, "[")?;
549
550 for (index, value) in substitution.iter(interner).enumerate() {
551 if first {
552 first = false;
553 } else {
554 write!(fmt, ", ")?;
555 }
556
557 write!(fmt, "?{} := {:?}", index, value)?;
558 }
559
560 write!(fmt, "]")?;
561
562 Ok(())
563 }
564}
565
566impl<I: Interner> Substitution<I> {
f035d41b 567 /// Show debug output for the substitution.
f9f354fc
XL
568 pub fn debug<'a>(&'a self, interner: &'a I) -> SubstitutionDebug<'a, I> {
569 SubstitutionDebug {
570 substitution: self,
571 interner,
572 }
573 }
574}
575
576impl Debug for PlaceholderIndex {
577 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
578 let PlaceholderIndex { ui, idx } = self;
579 write!(fmt, "!{}_{}", ui.counter, idx)
580 }
581}
582
583impl<I: Interner> TraitRef<I> {
f035d41b 584 /// Returns a "Debuggable" type that prints like `P0 as Trait<P1..>`.
f9f354fc
XL
585 pub fn with_as(&self) -> impl std::fmt::Debug + '_ {
586 SeparatorTraitRef {
587 trait_ref: self,
588 separator: " as ",
589 }
590 }
591
f035d41b 592 /// Returns a "Debuggable" type that prints like `P0: Trait<P1..>`.
f9f354fc
XL
593 pub fn with_colon(&self) -> impl std::fmt::Debug + '_ {
594 SeparatorTraitRef {
595 trait_ref: self,
596 separator: ": ",
597 }
598 }
599}
600
601impl<I: Interner> Debug for TraitRef<I> {
602 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
603 Debug::fmt(&self.with_as(), fmt)
604 }
605}
606
f035d41b 607/// Trait ref with associated separator used for debug output.
f9f354fc 608pub struct SeparatorTraitRef<'me, I: Interner> {
f035d41b 609 /// The `TraitRef` itself.
f9f354fc 610 pub trait_ref: &'me TraitRef<I>,
f035d41b
XL
611
612 /// The separator used for displaying the `TraitRef`.
f9f354fc
XL
613 pub separator: &'me str,
614}
615
f035d41b 616/// Helper struct for showing debug output for the `SeperatorTraitRef`.
f9f354fc
XL
617pub struct SeparatorTraitRefDebug<'a, 'me, I: Interner> {
618 separator_trait_ref: &'a SeparatorTraitRef<'me, I>,
619 interner: &'a I,
620}
621
622impl<'a, 'me, I: Interner> Debug for SeparatorTraitRefDebug<'a, 'me, I> {
623 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
624 let SeparatorTraitRefDebug {
625 separator_trait_ref,
626 interner,
627 } = self;
628 let parameters = separator_trait_ref
629 .trait_ref
630 .substitution
3dfed10e 631 .as_slice(interner);
f9f354fc
XL
632 write!(
633 fmt,
634 "{:?}{}{:?}{:?}",
635 parameters[0],
636 separator_trait_ref.separator,
637 separator_trait_ref.trait_ref.trait_id,
638 Angle(&parameters[1..])
639 )
640 }
641}
642
643impl<'me, I: Interner> SeparatorTraitRef<'me, I> {
f035d41b 644 /// Show debug output for the `SeperatorTraitRef`.
f9f354fc
XL
645 pub fn debug<'a>(&'a self, interner: &'a I) -> SeparatorTraitRefDebug<'a, 'me, I> {
646 SeparatorTraitRefDebug {
647 separator_trait_ref: self,
648 interner,
649 }
650 }
651}
652
f035d41b
XL
653impl<I: Interner> Debug for LifetimeOutlives<I> {
654 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
655 write!(fmt, "{:?}: {:?}", self.a, self.b)
656 }
657}
658
3dfed10e
XL
659impl<I: Interner> Debug for TypeOutlives<I> {
660 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
661 write!(fmt, "{:?}: {:?}", self.ty, self.lifetime)
662 }
663}
664
f035d41b 665/// Helper struct for showing debug output for projection types.
f9f354fc
XL
666pub struct ProjectionTyDebug<'a, I: Interner> {
667 projection_ty: &'a ProjectionTy<I>,
668 interner: &'a I,
669}
670
671impl<'a, I: Interner> Debug for ProjectionTyDebug<'a, I> {
672 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
673 let ProjectionTyDebug {
674 projection_ty,
675 interner,
676 } = self;
677 write!(
678 fmt,
679 "({:?}){:?}",
680 projection_ty.associated_ty_id,
681 projection_ty.substitution.with_angle(interner)
682 )
683 }
684}
685
686impl<I: Interner> ProjectionTy<I> {
f035d41b 687 /// Show debug output for the projection type.
f9f354fc
XL
688 pub fn debug<'a>(&'a self, interner: &'a I) -> ProjectionTyDebug<'a, I> {
689 ProjectionTyDebug {
690 projection_ty: self,
691 interner,
692 }
693 }
694}
695
f035d41b 696/// Helper struct for showing debug output for opaque types.
f9f354fc
XL
697pub struct OpaqueTyDebug<'a, I: Interner> {
698 opaque_ty: &'a OpaqueTy<I>,
699 interner: &'a I,
700}
701
702impl<'a, I: Interner> Debug for OpaqueTyDebug<'a, I> {
703 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
704 let OpaqueTyDebug {
705 opaque_ty,
706 interner,
707 } = self;
708 write!(
709 fmt,
710 "{:?}{:?}",
711 opaque_ty.opaque_ty_id,
712 opaque_ty.substitution.with_angle(interner)
713 )
714 }
715}
716
717impl<I: Interner> OpaqueTy<I> {
f035d41b 718 /// Show debug output for the opaque type.
f9f354fc
XL
719 pub fn debug<'a>(&'a self, interner: &'a I) -> OpaqueTyDebug<'a, I> {
720 OpaqueTyDebug {
721 opaque_ty: self,
722 interner,
723 }
724 }
725}
726
f035d41b 727/// Wraps debug output in angle brackets (`<>`).
f9f354fc
XL
728pub struct Angle<'a, T>(pub &'a [T]);
729
730impl<'a, T: Debug> Debug for Angle<'a, T> {
731 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
3dfed10e 732 if !self.0.is_empty() {
f9f354fc
XL
733 write!(fmt, "<")?;
734 for (index, elem) in self.0.iter().enumerate() {
735 if index > 0 {
736 write!(fmt, ", {:?}", elem)?;
737 } else {
738 write!(fmt, "{:?}", elem)?;
739 }
740 }
741 write!(fmt, ">")?;
742 }
743 Ok(())
744 }
745}
746
747impl<I: Interner> Debug for Normalize<I> {
748 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
749 write!(fmt, "Normalize({:?} -> {:?})", self.alias, self.ty)
750 }
751}
752
753impl<I: Interner> Debug for AliasEq<I> {
754 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
755 write!(fmt, "AliasEq({:?} = {:?})", self.alias, self.ty)
756 }
757}
758
759impl<I: Interner> Debug for WhereClause<I> {
760 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
761 match self {
762 WhereClause::Implemented(tr) => write!(fmt, "Implemented({:?})", tr.with_colon()),
763 WhereClause::AliasEq(a) => write!(fmt, "{:?}", a),
f035d41b 764 WhereClause::LifetimeOutlives(l_o) => write!(fmt, "{:?}", l_o),
3dfed10e 765 WhereClause::TypeOutlives(t_o) => write!(fmt, "{:?}", t_o),
f9f354fc
XL
766 }
767 }
768}
769
770impl<I: Interner> Debug for FromEnv<I> {
771 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
772 match self {
773 FromEnv::Trait(t) => write!(fmt, "FromEnv({:?})", t.with_colon()),
774 FromEnv::Ty(t) => write!(fmt, "FromEnv({:?})", t),
775 }
776 }
777}
778
779impl<I: Interner> Debug for WellFormed<I> {
780 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
781 match self {
782 WellFormed::Trait(t) => write!(fmt, "WellFormed({:?})", t.with_colon()),
783 WellFormed::Ty(t) => write!(fmt, "WellFormed({:?})", t),
784 }
785 }
786}
787
788impl<I: Interner> Debug for DomainGoal<I> {
789 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
790 match self {
791 DomainGoal::Holds(n) => write!(fmt, "{:?}", n),
792 DomainGoal::WellFormed(n) => write!(fmt, "{:?}", n),
793 DomainGoal::FromEnv(n) => write!(fmt, "{:?}", n),
794 DomainGoal::Normalize(n) => write!(fmt, "{:?}", n),
795 DomainGoal::IsLocal(n) => write!(fmt, "IsLocal({:?})", n),
796 DomainGoal::IsUpstream(n) => write!(fmt, "IsUpstream({:?})", n),
797 DomainGoal::IsFullyVisible(n) => write!(fmt, "IsFullyVisible({:?})", n),
798 DomainGoal::LocalImplAllowed(tr) => {
799 write!(fmt, "LocalImplAllowed({:?})", tr.with_colon(),)
800 }
3dfed10e 801 DomainGoal::Compatible => write!(fmt, "Compatible"),
f9f354fc 802 DomainGoal::DownstreamType(n) => write!(fmt, "DownstreamType({:?})", n),
3dfed10e 803 DomainGoal::Reveal => write!(fmt, "Reveal"),
f035d41b 804 DomainGoal::ObjectSafe(n) => write!(fmt, "ObjectSafe({:?})", n),
f9f354fc
XL
805 }
806 }
807}
808
809impl<I: Interner> Debug for EqGoal<I> {
810 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
811 write!(fmt, "({:?} = {:?})", self.a, self.b)
812 }
813}
814
815impl<T: HasInterner + Debug> Debug for Binders<T> {
816 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
817 let Binders {
818 ref binders,
819 ref value,
820 } = *self;
821 write!(fmt, "for{:?} ", binders.debug())?;
822 Debug::fmt(value, fmt)
823 }
824}
825
826impl<I: Interner> Debug for ProgramClauseData<I> {
827 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 828 write!(fmt, "{:?}", self.0)
f9f354fc
XL
829 }
830}
831
832impl<I: Interner> Debug for Environment<I> {
833 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
834 write!(fmt, "Env({:?})", self.clauses)
835 }
836}
837
838impl<I: Interner> Debug for CanonicalVarKinds<I> {
839 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
840 I::debug_canonical_var_kinds(self, fmt)
841 .unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
842 }
843}
844
845impl<T: HasInterner + Display> Canonical<T> {
f035d41b 846 /// Display the canonicalized item.
f9f354fc
XL
847 pub fn display<'a>(&'a self, interner: &'a T::Interner) -> CanonicalDisplay<'a, T> {
848 CanonicalDisplay {
849 canonical: self,
850 interner,
851 }
852 }
853}
854
f035d41b 855/// Helper struct for displaying canonicalized items.
f9f354fc
XL
856pub struct CanonicalDisplay<'a, T: HasInterner> {
857 canonical: &'a Canonical<T>,
858 interner: &'a T::Interner,
859}
860
861impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
862 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
863 let Canonical { binders, value } = self.canonical;
864 let interner = self.interner;
865 let binders = binders.as_slice(interner);
866 if binders.is_empty() {
867 // Ordinarily, we try to print all binder levels, if they
868 // are empty, but we can skip in this *particular* case
869 // because we know that `Canonical` terms are never
870 // supposed to contain free variables. In other words,
871 // all "bound variables" that appear inside the canonical
872 // value must reference values that appear in `binders`.
873 write!(f, "{}", value)?;
874 } else {
875 write!(f, "for<")?;
876
877 for (i, pk) in binders.iter().enumerate() {
878 if i > 0 {
879 write!(f, ",")?;
880 }
f035d41b 881 write!(f, "?{}", pk.skip_kind())?;
f9f354fc
XL
882 }
883
884 write!(f, "> {{ {} }}", value)?;
885 }
886
887 Ok(())
888 }
889}
890
f035d41b
XL
891impl<I: Interner> Debug for GenericArgData<I> {
892 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
893 match self {
894 GenericArgData::Ty(t) => write!(fmt, "Ty({:?})", t),
895 GenericArgData::Lifetime(l) => write!(fmt, "Lifetime({:?})", l),
896 GenericArgData::Const(c) => write!(fmt, "Const({:?})", c),
897 }
898 }
899}
900
901impl<I: Interner> Debug for VariableKind<I> {
902 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
903 match self {
29967ef6
XL
904 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "type"),
905 VariableKind::Ty(TyVariableKind::Integer) => write!(fmt, "integer type"),
906 VariableKind::Ty(TyVariableKind::Float) => write!(fmt, "float type"),
f035d41b
XL
907 VariableKind::Lifetime => write!(fmt, "lifetime"),
908 VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty),
909 }
910 }
911}
912
913impl<I: Interner, T: Debug> Debug for WithKind<I, T> {
f9f354fc 914 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b
XL
915 let value = self.skip_kind();
916 match &self.kind {
29967ef6
XL
917 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "{:?} with kind type", value),
918 VariableKind::Ty(TyVariableKind::Integer) => {
919 write!(fmt, "{:?} with kind integer type", value)
920 }
921 VariableKind::Ty(TyVariableKind::Float) => {
922 write!(fmt, "{:?} with kind float type", value)
923 }
f035d41b
XL
924 VariableKind::Lifetime => write!(fmt, "{:?} with kind lifetime", value),
925 VariableKind::Const(ty) => write!(fmt, "{:?} with kind {:?}", value, ty),
f9f354fc
XL
926 }
927 }
928}
929
930impl<I: Interner> Debug for Constraint<I> {
931 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
932 match self {
3dfed10e
XL
933 Constraint::LifetimeOutlives(a, b) => write!(fmt, "{:?}: {:?}", a, b),
934 Constraint::TypeOutlives(ty, lifetime) => write!(fmt, "{:?}: {:?}", ty, lifetime),
f9f354fc
XL
935 }
936 }
937}
938
939impl<I: Interner> Display for ConstrainedSubst<I> {
940 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
941 let ConstrainedSubst { subst, constraints } = self;
942
943 write!(
944 f,
945 "substitution {}, lifetime constraints {:?}",
946 subst, constraints,
947 )
948 }
949}
950
951impl<I: Interner> Substitution<I> {
952 /// Displays the substitution in the form `< P0, .. Pn >`, or (if
953 /// the substitution is empty) as an empty string.
f035d41b 954 pub fn with_angle(&self, interner: &I) -> Angle<'_, GenericArg<I>> {
3dfed10e 955 Angle(self.as_slice(interner))
f9f354fc
XL
956 }
957}
958
959impl<I: Interner> Debug for Substitution<I> {
960 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
961 Display::fmt(self, fmt)
962 }
963}