]> git.proxmox.com Git - rustc.git/blame - vendor/chalk-ir-0.55.0/src/debug.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / vendor / chalk-ir-0.55.0 / 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
5869c6ff
XL
260impl<I: Interner> Debug for FnSubst<I> {
261 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
262 write!(fmt, "{:?}", self.0)
263 }
264}
265
3dfed10e 266impl<I: Interner> Debug for FnPointer<I> {
f9f354fc
XL
267 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
268 // FIXME -- we should introduce some names or something here
3dfed10e 269 let FnPointer {
f9f354fc
XL
270 num_binders,
271 substitution,
1b1a35ee 272 sig,
f9f354fc 273 } = self;
3dfed10e
XL
274 write!(
275 fmt,
5869c6ff
XL
276 "{}{:?} for<{}> {:?}",
277 match sig.safety {
278 Safety::Unsafe => "unsafe ",
279 Safety::Safe => "",
280 },
281 sig.abi,
282 num_binders,
283 substitution
3dfed10e 284 )
f9f354fc
XL
285 }
286}
287
288impl<I: Interner> Debug for LifetimeData<I> {
289 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
290 match self {
291 LifetimeData::BoundVar(db) => write!(fmt, "'{:?}", db),
292 LifetimeData::InferenceVar(var) => write!(fmt, "'{:?}", var),
293 LifetimeData::Placeholder(index) => write!(fmt, "'{:?}", index),
29967ef6 294 LifetimeData::Static => write!(fmt, "'static"),
5869c6ff
XL
295 LifetimeData::Empty(UniverseIndex::ROOT) => write!(fmt, "'<empty>"),
296 LifetimeData::Empty(universe) => write!(fmt, "'<empty:{:?}>", universe),
297 LifetimeData::Erased => write!(fmt, "'<erased>"),
f9f354fc
XL
298 LifetimeData::Phantom(..) => unreachable!(),
299 }
300 }
301}
302
f035d41b
XL
303impl<I: Interner> VariableKinds<I> {
304 fn debug(&self) -> VariableKindsDebug<'_, I> {
305 VariableKindsDebug(self)
f9f354fc
XL
306 }
307
f035d41b
XL
308 /// Helper method for debugging variable kinds.
309 pub fn inner_debug<'a>(&'a self, interner: &'a I) -> VariableKindsInnerDebug<'a, I> {
310 VariableKindsInnerDebug {
311 variable_kinds: self,
f9f354fc
XL
312 interner,
313 }
314 }
315}
316
f035d41b 317struct VariableKindsDebug<'a, I: Interner>(&'a VariableKinds<I>);
f9f354fc 318
f035d41b 319impl<'a, I: Interner> Debug for VariableKindsDebug<'a, I> {
f9f354fc 320 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 321 I::debug_variable_kinds_with_angles(self.0, fmt)
f9f354fc
XL
322 .unwrap_or_else(|| write!(fmt, "{:?}", self.0.interned))
323 }
324}
325
f035d41b
XL
326/// Helper struct for showing debug output for `VariableKinds`.
327pub struct VariableKindsInnerDebug<'a, I: Interner> {
328 variable_kinds: &'a VariableKinds<I>,
f9f354fc
XL
329 interner: &'a I,
330}
331
f035d41b 332impl<'a, I: Interner> Debug for VariableKindsInnerDebug<'a, I> {
f9f354fc 333 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b
XL
334 // NB: We print variable kinds as a list delimited by `<>`,
335 // like `<K1, K2, ..>`. This is because variable kind lists
f9f354fc
XL
336 // are always associated with binders like `forall<type> {
337 // ... }`.
338 write!(fmt, "<")?;
f035d41b 339 for (index, binder) in self.variable_kinds.iter(self.interner).enumerate() {
f9f354fc
XL
340 if index > 0 {
341 write!(fmt, ", ")?;
342 }
f035d41b 343 match binder {
29967ef6
XL
344 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "type")?,
345 VariableKind::Ty(TyVariableKind::Integer) => write!(fmt, "integer type")?,
346 VariableKind::Ty(TyVariableKind::Float) => write!(fmt, "float type")?,
f035d41b
XL
347 VariableKind::Lifetime => write!(fmt, "lifetime")?,
348 VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty)?,
f9f354fc
XL
349 }
350 }
351 write!(fmt, ">")
352 }
353}
354
f035d41b
XL
355impl<I: Interner> Debug for ConstData<I> {
356 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
357 match &self.value {
358 ConstValue::BoundVar(db) => write!(fmt, "{:?}", db),
359 ConstValue::InferenceVar(var) => write!(fmt, "{:?}", var),
360 ConstValue::Placeholder(index) => write!(fmt, "{:?}", index),
361 ConstValue::Concrete(evaluated) => write!(fmt, "{:?}", evaluated),
362 }
363 }
364}
365
f9f354fc
XL
366impl<I: Interner> Debug for GoalData<I> {
367 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
368 match self {
369 GoalData::Quantified(qkind, ref subgoal) => write!(
370 fmt,
371 "{:?}{:?} {{ {:?} }}",
372 qkind,
373 subgoal.binders.debug(),
374 subgoal.value
375 ),
376 GoalData::Implies(ref wc, ref g) => write!(fmt, "if ({:?}) {{ {:?} }}", wc, g),
377 GoalData::All(ref goals) => write!(fmt, "all{:?}", goals),
378 GoalData::Not(ref g) => write!(fmt, "not {{ {:?} }}", g),
379 GoalData::EqGoal(ref wc) => write!(fmt, "{:?}", wc),
5869c6ff 380 GoalData::SubtypeGoal(ref wc) => write!(fmt, "{:?}", wc),
f9f354fc 381 GoalData::DomainGoal(ref wc) => write!(fmt, "{:?}", wc),
3dfed10e 382 GoalData::CannotProve => write!(fmt, r"¯\_(ツ)_/¯"),
f9f354fc
XL
383 }
384 }
385}
386
f035d41b 387/// Helper struct for showing debug output for `Goals`.
f9f354fc
XL
388pub struct GoalsDebug<'a, I: Interner> {
389 goals: &'a Goals<I>,
390 interner: &'a I,
391}
392
393impl<'a, I: Interner> Debug for GoalsDebug<'a, I> {
394 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
395 write!(fmt, "(")?;
396 for (goal, index) in self.goals.iter(self.interner).zip(0..) {
397 if index > 0 {
398 write!(fmt, ", ")?;
399 }
400 write!(fmt, "{:?}", goal)?;
401 }
402 write!(fmt, ")")?;
403 Ok(())
404 }
405}
406
407impl<I: Interner> Goals<I> {
f035d41b 408 /// Show debug output for `Goals`.
f9f354fc
XL
409 pub fn debug<'a>(&'a self, interner: &'a I) -> GoalsDebug<'a, I> {
410 GoalsDebug {
411 goals: self,
412 interner,
413 }
414 }
415}
416
f035d41b
XL
417/// Helper struct for showing debug output for `GenericArgData`.
418pub struct GenericArgDataInnerDebug<'a, I: Interner>(&'a GenericArgData<I>);
f9f354fc 419
f035d41b 420impl<'a, I: Interner> Debug for GenericArgDataInnerDebug<'a, I> {
f9f354fc
XL
421 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
422 match self.0 {
f035d41b
XL
423 GenericArgData::Ty(n) => write!(fmt, "{:?}", n),
424 GenericArgData::Lifetime(n) => write!(fmt, "{:?}", n),
425 GenericArgData::Const(n) => write!(fmt, "{:?}", n),
f9f354fc
XL
426 }
427 }
428}
429
f035d41b
XL
430impl<I: Interner> GenericArgData<I> {
431 /// Helper method for debugging `GenericArgData`.
432 pub fn inner_debug(&self) -> GenericArgDataInnerDebug<'_, I> {
433 GenericArgDataInnerDebug(self)
f9f354fc
XL
434 }
435}
436
f035d41b 437/// Helper struct for showing debug output for program clause implications.
f9f354fc
XL
438pub struct ProgramClauseImplicationDebug<'a, I: Interner> {
439 pci: &'a ProgramClauseImplication<I>,
440 interner: &'a I,
441}
442
443impl<'a, I: Interner> Debug for ProgramClauseImplicationDebug<'a, I> {
444 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
445 let ProgramClauseImplicationDebug { pci, interner } = self;
446 write!(fmt, "{:?}", pci.consequence)?;
447
448 let conditions = pci.conditions.as_slice(interner);
449
450 let conds = conditions.len();
451 if conds == 0 {
452 return Ok(());
453 }
454
455 write!(fmt, " :- ")?;
456 for cond in &conditions[..conds - 1] {
457 write!(fmt, "{:?}, ", cond)?;
458 }
459 write!(fmt, "{:?}", conditions[conds - 1])
460 }
461}
462
463impl<I: Interner> ProgramClauseImplication<I> {
f035d41b 464 /// Show debug output for the program clause implication.
f9f354fc
XL
465 pub fn debug<'a>(&'a self, interner: &'a I) -> ProgramClauseImplicationDebug<'a, I> {
466 ProgramClauseImplicationDebug {
467 pci: self,
468 interner,
469 }
470 }
471}
472
f035d41b 473/// Helper struct for showing debug output for application types.
29967ef6
XL
474pub struct TyKindDebug<'a, I: Interner> {
475 ty: &'a TyKind<I>,
f9f354fc
XL
476 interner: &'a I,
477}
478
29967ef6 479impl<'a, I: Interner> Debug for TyKindDebug<'a, I> {
f9f354fc 480 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
29967ef6
XL
481 let interner = self.interner;
482 match self.ty {
483 TyKind::BoundVar(db) => write!(fmt, "{:?}", db),
484 TyKind::Dyn(clauses) => write!(fmt, "{:?}", clauses),
485 TyKind::InferenceVar(var, TyVariableKind::General) => write!(fmt, "{:?}", var),
486 TyKind::InferenceVar(var, TyVariableKind::Integer) => write!(fmt, "{:?}i", var),
487 TyKind::InferenceVar(var, TyVariableKind::Float) => write!(fmt, "{:?}f", var),
488 TyKind::Alias(alias) => write!(fmt, "{:?}", alias),
489 TyKind::Placeholder(index) => write!(fmt, "{:?}", index),
490 TyKind::Function(function) => write!(fmt, "{:?}", function),
491 TyKind::Adt(id, substitution) => {
492 write!(fmt, "{:?}{:?}", id, substitution.with_angle(interner))
493 }
494 TyKind::AssociatedType(assoc_ty, substitution) => {
495 write!(fmt, "{:?}{:?}", assoc_ty, substitution.with_angle(interner))
496 }
497 TyKind::Scalar(scalar) => write!(fmt, "{:?}", scalar),
498 TyKind::Str => write!(fmt, "Str"),
499 TyKind::Tuple(arity, substitution) => {
500 write!(fmt, "{:?}{:?}", arity, substitution.with_angle(interner))
501 }
502 TyKind::OpaqueType(opaque_ty, substitution) => write!(
503 fmt,
504 "!{:?}{:?}",
505 opaque_ty,
506 substitution.with_angle(interner)
507 ),
508 TyKind::Slice(ty) => write!(fmt, "[{:?}]", ty),
509 TyKind::FnDef(fn_def, substitution) => {
510 write!(fmt, "{:?}{:?}", fn_def, substitution.with_angle(interner))
511 }
512 TyKind::Ref(mutability, lifetime, ty) => match mutability {
513 Mutability::Mut => write!(fmt, "(&{:?} mut {:?})", lifetime, ty),
514 Mutability::Not => write!(fmt, "(&{:?} {:?})", lifetime, ty),
515 },
516 TyKind::Raw(mutability, ty) => match mutability {
517 Mutability::Mut => write!(fmt, "(*mut {:?})", ty),
518 Mutability::Not => write!(fmt, "(*const {:?})", ty),
519 },
520 TyKind::Never => write!(fmt, "Never"),
521 TyKind::Array(ty, const_) => write!(fmt, "[{:?}; {:?}]", ty, const_),
522 TyKind::Closure(id, substitution) => write!(
523 fmt,
524 "{{closure:{:?}}}{:?}",
525 id,
526 substitution.with_angle(interner)
527 ),
528 TyKind::Generator(generator, substitution) => write!(
529 fmt,
530 "{:?}{:?}",
531 generator,
532 substitution.with_angle(interner)
533 ),
534 TyKind::GeneratorWitness(witness, substitution) => {
535 write!(fmt, "{:?}{:?}", witness, substitution.with_angle(interner))
536 }
537 TyKind::Foreign(foreign_ty) => write!(fmt, "{:?}", foreign_ty,),
538 TyKind::Error => write!(fmt, "{{error}}"),
539 }
f9f354fc
XL
540 }
541}
542
29967ef6 543impl<I: Interner> TyKind<I> {
f035d41b 544 /// Show debug output for the application type.
29967ef6
XL
545 pub fn debug<'a>(&'a self, interner: &'a I) -> TyKindDebug<'a, I> {
546 TyKindDebug { ty: self, interner }
f9f354fc
XL
547 }
548}
549
f035d41b 550/// Helper struct for showing debug output for substitutions.
f9f354fc
XL
551pub struct SubstitutionDebug<'a, I: Interner> {
552 substitution: &'a Substitution<I>,
553 interner: &'a I,
554}
555
556impl<'a, I: Interner> Debug for SubstitutionDebug<'a, I> {
557 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
558 let SubstitutionDebug {
559 substitution,
560 interner,
561 } = self;
562 let mut first = true;
563
564 write!(fmt, "[")?;
565
566 for (index, value) in substitution.iter(interner).enumerate() {
567 if first {
568 first = false;
569 } else {
570 write!(fmt, ", ")?;
571 }
572
573 write!(fmt, "?{} := {:?}", index, value)?;
574 }
575
576 write!(fmt, "]")?;
577
578 Ok(())
579 }
580}
581
582impl<I: Interner> Substitution<I> {
f035d41b 583 /// Show debug output for the substitution.
f9f354fc
XL
584 pub fn debug<'a>(&'a self, interner: &'a I) -> SubstitutionDebug<'a, I> {
585 SubstitutionDebug {
586 substitution: self,
587 interner,
588 }
589 }
590}
591
592impl Debug for PlaceholderIndex {
593 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
594 let PlaceholderIndex { ui, idx } = self;
595 write!(fmt, "!{}_{}", ui.counter, idx)
596 }
597}
598
599impl<I: Interner> TraitRef<I> {
f035d41b 600 /// Returns a "Debuggable" type that prints like `P0 as Trait<P1..>`.
f9f354fc
XL
601 pub fn with_as(&self) -> impl std::fmt::Debug + '_ {
602 SeparatorTraitRef {
603 trait_ref: self,
604 separator: " as ",
605 }
606 }
607
f035d41b 608 /// Returns a "Debuggable" type that prints like `P0: Trait<P1..>`.
f9f354fc
XL
609 pub fn with_colon(&self) -> impl std::fmt::Debug + '_ {
610 SeparatorTraitRef {
611 trait_ref: self,
612 separator: ": ",
613 }
614 }
615}
616
617impl<I: Interner> Debug for TraitRef<I> {
618 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
619 Debug::fmt(&self.with_as(), fmt)
620 }
621}
622
f035d41b 623/// Trait ref with associated separator used for debug output.
f9f354fc 624pub struct SeparatorTraitRef<'me, I: Interner> {
f035d41b 625 /// The `TraitRef` itself.
f9f354fc 626 pub trait_ref: &'me TraitRef<I>,
f035d41b
XL
627
628 /// The separator used for displaying the `TraitRef`.
f9f354fc
XL
629 pub separator: &'me str,
630}
631
f035d41b 632/// Helper struct for showing debug output for the `SeperatorTraitRef`.
f9f354fc
XL
633pub struct SeparatorTraitRefDebug<'a, 'me, I: Interner> {
634 separator_trait_ref: &'a SeparatorTraitRef<'me, I>,
635 interner: &'a I,
636}
637
638impl<'a, 'me, I: Interner> Debug for SeparatorTraitRefDebug<'a, 'me, I> {
639 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
640 let SeparatorTraitRefDebug {
641 separator_trait_ref,
642 interner,
643 } = self;
644 let parameters = separator_trait_ref
645 .trait_ref
646 .substitution
3dfed10e 647 .as_slice(interner);
f9f354fc
XL
648 write!(
649 fmt,
650 "{:?}{}{:?}{:?}",
651 parameters[0],
652 separator_trait_ref.separator,
653 separator_trait_ref.trait_ref.trait_id,
654 Angle(&parameters[1..])
655 )
656 }
657}
658
659impl<'me, I: Interner> SeparatorTraitRef<'me, I> {
f035d41b 660 /// Show debug output for the `SeperatorTraitRef`.
f9f354fc
XL
661 pub fn debug<'a>(&'a self, interner: &'a I) -> SeparatorTraitRefDebug<'a, 'me, I> {
662 SeparatorTraitRefDebug {
663 separator_trait_ref: self,
664 interner,
665 }
666 }
667}
668
f035d41b
XL
669impl<I: Interner> Debug for LifetimeOutlives<I> {
670 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
671 write!(fmt, "{:?}: {:?}", self.a, self.b)
672 }
673}
674
3dfed10e
XL
675impl<I: Interner> Debug for TypeOutlives<I> {
676 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
677 write!(fmt, "{:?}: {:?}", self.ty, self.lifetime)
678 }
679}
680
f035d41b 681/// Helper struct for showing debug output for projection types.
f9f354fc
XL
682pub struct ProjectionTyDebug<'a, I: Interner> {
683 projection_ty: &'a ProjectionTy<I>,
684 interner: &'a I,
685}
686
687impl<'a, I: Interner> Debug for ProjectionTyDebug<'a, I> {
688 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
689 let ProjectionTyDebug {
690 projection_ty,
691 interner,
692 } = self;
693 write!(
694 fmt,
695 "({:?}){:?}",
696 projection_ty.associated_ty_id,
697 projection_ty.substitution.with_angle(interner)
698 )
699 }
700}
701
702impl<I: Interner> ProjectionTy<I> {
f035d41b 703 /// Show debug output for the projection type.
f9f354fc
XL
704 pub fn debug<'a>(&'a self, interner: &'a I) -> ProjectionTyDebug<'a, I> {
705 ProjectionTyDebug {
706 projection_ty: self,
707 interner,
708 }
709 }
710}
711
f035d41b 712/// Helper struct for showing debug output for opaque types.
f9f354fc
XL
713pub struct OpaqueTyDebug<'a, I: Interner> {
714 opaque_ty: &'a OpaqueTy<I>,
715 interner: &'a I,
716}
717
718impl<'a, I: Interner> Debug for OpaqueTyDebug<'a, I> {
719 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
720 let OpaqueTyDebug {
721 opaque_ty,
722 interner,
723 } = self;
724 write!(
725 fmt,
726 "{:?}{:?}",
727 opaque_ty.opaque_ty_id,
728 opaque_ty.substitution.with_angle(interner)
729 )
730 }
731}
732
733impl<I: Interner> OpaqueTy<I> {
f035d41b 734 /// Show debug output for the opaque type.
f9f354fc
XL
735 pub fn debug<'a>(&'a self, interner: &'a I) -> OpaqueTyDebug<'a, I> {
736 OpaqueTyDebug {
737 opaque_ty: self,
738 interner,
739 }
740 }
741}
742
f035d41b 743/// Wraps debug output in angle brackets (`<>`).
f9f354fc
XL
744pub struct Angle<'a, T>(pub &'a [T]);
745
746impl<'a, T: Debug> Debug for Angle<'a, T> {
747 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
3dfed10e 748 if !self.0.is_empty() {
f9f354fc
XL
749 write!(fmt, "<")?;
750 for (index, elem) in self.0.iter().enumerate() {
751 if index > 0 {
752 write!(fmt, ", {:?}", elem)?;
753 } else {
754 write!(fmt, "{:?}", elem)?;
755 }
756 }
757 write!(fmt, ">")?;
758 }
759 Ok(())
760 }
761}
762
763impl<I: Interner> Debug for Normalize<I> {
764 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
765 write!(fmt, "Normalize({:?} -> {:?})", self.alias, self.ty)
766 }
767}
768
769impl<I: Interner> Debug for AliasEq<I> {
770 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
771 write!(fmt, "AliasEq({:?} = {:?})", self.alias, self.ty)
772 }
773}
774
775impl<I: Interner> Debug for WhereClause<I> {
776 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
777 match self {
778 WhereClause::Implemented(tr) => write!(fmt, "Implemented({:?})", tr.with_colon()),
779 WhereClause::AliasEq(a) => write!(fmt, "{:?}", a),
f035d41b 780 WhereClause::LifetimeOutlives(l_o) => write!(fmt, "{:?}", l_o),
3dfed10e 781 WhereClause::TypeOutlives(t_o) => write!(fmt, "{:?}", t_o),
f9f354fc
XL
782 }
783 }
784}
785
786impl<I: Interner> Debug for FromEnv<I> {
787 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
788 match self {
789 FromEnv::Trait(t) => write!(fmt, "FromEnv({:?})", t.with_colon()),
790 FromEnv::Ty(t) => write!(fmt, "FromEnv({:?})", t),
791 }
792 }
793}
794
795impl<I: Interner> Debug for WellFormed<I> {
796 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
797 match self {
798 WellFormed::Trait(t) => write!(fmt, "WellFormed({:?})", t.with_colon()),
799 WellFormed::Ty(t) => write!(fmt, "WellFormed({:?})", t),
800 }
801 }
802}
803
804impl<I: Interner> Debug for DomainGoal<I> {
805 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
806 match self {
807 DomainGoal::Holds(n) => write!(fmt, "{:?}", n),
808 DomainGoal::WellFormed(n) => write!(fmt, "{:?}", n),
809 DomainGoal::FromEnv(n) => write!(fmt, "{:?}", n),
810 DomainGoal::Normalize(n) => write!(fmt, "{:?}", n),
811 DomainGoal::IsLocal(n) => write!(fmt, "IsLocal({:?})", n),
812 DomainGoal::IsUpstream(n) => write!(fmt, "IsUpstream({:?})", n),
813 DomainGoal::IsFullyVisible(n) => write!(fmt, "IsFullyVisible({:?})", n),
814 DomainGoal::LocalImplAllowed(tr) => {
815 write!(fmt, "LocalImplAllowed({:?})", tr.with_colon(),)
816 }
3dfed10e 817 DomainGoal::Compatible => write!(fmt, "Compatible"),
f9f354fc 818 DomainGoal::DownstreamType(n) => write!(fmt, "DownstreamType({:?})", n),
3dfed10e 819 DomainGoal::Reveal => write!(fmt, "Reveal"),
f035d41b 820 DomainGoal::ObjectSafe(n) => write!(fmt, "ObjectSafe({:?})", n),
f9f354fc
XL
821 }
822 }
823}
824
825impl<I: Interner> Debug for EqGoal<I> {
826 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
827 write!(fmt, "({:?} = {:?})", self.a, self.b)
828 }
829}
830
5869c6ff
XL
831impl<I: Interner> Debug for SubtypeGoal<I> {
832 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
833 write!(fmt, "({:?} <: {:?})", self.a, self.b)
834 }
835}
836
f9f354fc
XL
837impl<T: HasInterner + Debug> Debug for Binders<T> {
838 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
839 let Binders {
840 ref binders,
841 ref value,
842 } = *self;
843 write!(fmt, "for{:?} ", binders.debug())?;
844 Debug::fmt(value, fmt)
845 }
846}
847
848impl<I: Interner> Debug for ProgramClauseData<I> {
849 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b 850 write!(fmt, "{:?}", self.0)
f9f354fc
XL
851 }
852}
853
854impl<I: Interner> Debug for Environment<I> {
855 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
856 write!(fmt, "Env({:?})", self.clauses)
857 }
858}
859
860impl<I: Interner> Debug for CanonicalVarKinds<I> {
861 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
862 I::debug_canonical_var_kinds(self, fmt)
863 .unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
864 }
865}
866
867impl<T: HasInterner + Display> Canonical<T> {
f035d41b 868 /// Display the canonicalized item.
f9f354fc
XL
869 pub fn display<'a>(&'a self, interner: &'a T::Interner) -> CanonicalDisplay<'a, T> {
870 CanonicalDisplay {
871 canonical: self,
872 interner,
873 }
874 }
875}
876
f035d41b 877/// Helper struct for displaying canonicalized items.
f9f354fc
XL
878pub struct CanonicalDisplay<'a, T: HasInterner> {
879 canonical: &'a Canonical<T>,
880 interner: &'a T::Interner,
881}
882
883impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
884 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
885 let Canonical { binders, value } = self.canonical;
886 let interner = self.interner;
887 let binders = binders.as_slice(interner);
888 if binders.is_empty() {
889 // Ordinarily, we try to print all binder levels, if they
890 // are empty, but we can skip in this *particular* case
891 // because we know that `Canonical` terms are never
892 // supposed to contain free variables. In other words,
893 // all "bound variables" that appear inside the canonical
894 // value must reference values that appear in `binders`.
895 write!(f, "{}", value)?;
896 } else {
897 write!(f, "for<")?;
898
899 for (i, pk) in binders.iter().enumerate() {
900 if i > 0 {
901 write!(f, ",")?;
902 }
f035d41b 903 write!(f, "?{}", pk.skip_kind())?;
f9f354fc
XL
904 }
905
906 write!(f, "> {{ {} }}", value)?;
907 }
908
909 Ok(())
910 }
911}
912
f035d41b
XL
913impl<I: Interner> Debug for GenericArgData<I> {
914 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
915 match self {
916 GenericArgData::Ty(t) => write!(fmt, "Ty({:?})", t),
917 GenericArgData::Lifetime(l) => write!(fmt, "Lifetime({:?})", l),
918 GenericArgData::Const(c) => write!(fmt, "Const({:?})", c),
919 }
920 }
921}
922
923impl<I: Interner> Debug for VariableKind<I> {
924 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
925 match self {
29967ef6
XL
926 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "type"),
927 VariableKind::Ty(TyVariableKind::Integer) => write!(fmt, "integer type"),
928 VariableKind::Ty(TyVariableKind::Float) => write!(fmt, "float type"),
f035d41b
XL
929 VariableKind::Lifetime => write!(fmt, "lifetime"),
930 VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty),
931 }
932 }
933}
934
935impl<I: Interner, T: Debug> Debug for WithKind<I, T> {
f9f354fc 936 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
f035d41b
XL
937 let value = self.skip_kind();
938 match &self.kind {
29967ef6
XL
939 VariableKind::Ty(TyVariableKind::General) => write!(fmt, "{:?} with kind type", value),
940 VariableKind::Ty(TyVariableKind::Integer) => {
941 write!(fmt, "{:?} with kind integer type", value)
942 }
943 VariableKind::Ty(TyVariableKind::Float) => {
944 write!(fmt, "{:?} with kind float type", value)
945 }
f035d41b
XL
946 VariableKind::Lifetime => write!(fmt, "{:?} with kind lifetime", value),
947 VariableKind::Const(ty) => write!(fmt, "{:?} with kind {:?}", value, ty),
f9f354fc
XL
948 }
949 }
950}
951
952impl<I: Interner> Debug for Constraint<I> {
953 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
954 match self {
3dfed10e
XL
955 Constraint::LifetimeOutlives(a, b) => write!(fmt, "{:?}: {:?}", a, b),
956 Constraint::TypeOutlives(ty, lifetime) => write!(fmt, "{:?}: {:?}", ty, lifetime),
f9f354fc
XL
957 }
958 }
959}
960
961impl<I: Interner> Display for ConstrainedSubst<I> {
962 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
963 let ConstrainedSubst { subst, constraints } = self;
964
965 write!(
966 f,
967 "substitution {}, lifetime constraints {:?}",
968 subst, constraints,
969 )
970 }
971}
972
973impl<I: Interner> Substitution<I> {
974 /// Displays the substitution in the form `< P0, .. Pn >`, or (if
975 /// the substitution is empty) as an empty string.
f035d41b 976 pub fn with_angle(&self, interner: &I) -> Angle<'_, GenericArg<I>> {
3dfed10e 977 Angle(self.as_slice(interner))
f9f354fc
XL
978 }
979}
980
981impl<I: Interner> Debug for Substitution<I> {
982 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
983 Display::fmt(self, fmt)
984 }
985}
5869c6ff
XL
986
987impl<I: Interner> Debug for Variances<I> {
988 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
989 I::debug_variances(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
990 }
991}