1 //! Debug impls for types.
2 
3 use std::fmt::{Debug, Display, Error, Formatter};
4 
5 use super::*;
6 
7 impl<I: Interner> Debug for TraitId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
13 impl<I: Interner> Debug for AdtId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>14     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
15         I::debug_adt_id(*self, fmt).unwrap_or_else(|| write!(fmt, "AdtId({:?})", self.0))
16     }
17 }
18 
19 impl<I: Interner> Debug for AssocTypeId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
26 impl<I: Interner> Debug for FnDefId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result27     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 
32 impl<I: Interner> Debug for ClosureId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result33     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 
38 impl<I: Interner> Debug for GeneratorId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result39     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 
45 impl<I: Interner> Debug for ForeignDefId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result46     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 
52 impl<I: Interner> Debug for Ty<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
58 impl<I: Interner> Debug for Lifetime<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
64 impl<I: Interner> Debug for Const<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
70 impl<I: Interner> Debug for ConcreteConst<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>71     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
72         write!(fmt, "{:?}", self.interned)
73     }
74 }
75 
76 impl<I: Interner> Debug for GenericArg<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>77     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
78         I::debug_generic_arg(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
79     }
80 }
81 
82 impl<I: Interner> Debug for Goal<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
88 impl<I: Interner> Debug for Goals<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
94 impl<I: Interner> Debug for ProgramClauseImplication<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
101 impl<I: Interner> Debug for ProgramClause<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
107 impl<I: Interner> Debug for ProgramClauses<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
113 impl<I: Interner> Debug for Constraints<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
119 impl<I: Interner> Debug for SeparatorTraitRef<'_, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
126 impl<I: Interner> Debug for AliasTy<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>127     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
128         I::debug_alias(self, fmt).unwrap_or_else(|| write!(fmt, "AliasTy(?)"))
129     }
130 }
131 
132 impl<I: Interner> Debug for QuantifiedWhereClauses<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
139 impl<I: Interner> Debug for ProjectionTy<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
147 impl<I: Interner> Debug for OpaqueTy<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
155 impl<I: Interner> Display for Substitution<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
161 impl<I: Interner> Debug for OpaqueTyId<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
167 impl Display for UniverseIndex {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>168     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
169         write!(fmt, "U{}", self.counter)
170     }
171 }
172 
173 impl Debug for UniverseIndex {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>174     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
175         write!(fmt, "U{}", self.counter)
176     }
177 }
178 
179 impl<I: Interner> Debug for TyData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>180     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
181         self.kind.fmt(fmt)
182     }
183 }
184 
185 impl<I: Interner> Debug for TyKind<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>186     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
187         match self {
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}}"),
229         }
230     }
231 }
232 
233 impl Debug for BoundVar {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>234     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
235         let BoundVar { debruijn, index } = self;
236         write!(fmt, "{:?}.{:?}", debruijn, index)
237     }
238 }
239 
240 impl Debug for DebruijnIndex {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>241     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
242         let DebruijnIndex { depth } = self;
243         write!(fmt, "^{}", depth)
244     }
245 }
246 
247 impl<I: Interner> Debug for DynTy<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>248     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
249         let DynTy { bounds, lifetime } = self;
250         write!(fmt, "dyn {:?} + {:?}", bounds, lifetime)
251     }
252 }
253 
254 impl Debug for InferenceVar {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>255     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
256         write!(fmt, "?{}", self.index)
257     }
258 }
259 
260 impl<I: Interner> Debug for FnSubst<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>261     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
262         write!(fmt, "{:?}", self.0)
263     }
264 }
265 
266 impl<I: Interner> Debug for FnPointer<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>267     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
268         // FIXME -- we should introduce some names or something here
269         let FnPointer {
270             num_binders,
271             substitution,
272             sig,
273         } = self;
274         write!(
275             fmt,
276             "{}{:?} for<{}> {:?}",
277             match sig.safety {
278                 Safety::Unsafe => "unsafe ",
279                 Safety::Safe => "",
280             },
281             sig.abi,
282             num_binders,
283             substitution
284         )
285     }
286 }
287 
288 impl<I: Interner> Debug for LifetimeData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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),
294             LifetimeData::Static => write!(fmt, "'static"),
295             LifetimeData::Empty(UniverseIndex::ROOT) => write!(fmt, "'<empty>"),
296             LifetimeData::Empty(universe) => write!(fmt, "'<empty:{:?}>", universe),
297             LifetimeData::Erased => write!(fmt, "'<erased>"),
298             LifetimeData::Phantom(..) => unreachable!(),
299         }
300     }
301 }
302 
303 impl<I: Interner> VariableKinds<I> {
debug(&self) -> VariableKindsDebug<'_, I>304     fn debug(&self) -> VariableKindsDebug<'_, I> {
305         VariableKindsDebug(self)
306     }
307 
308     /// Helper method for debugging variable kinds.
inner_debug<'a>(&'a self, interner: &'a I) -> VariableKindsInnerDebug<'a, I>309     pub fn inner_debug<'a>(&'a self, interner: &'a I) -> VariableKindsInnerDebug<'a, I> {
310         VariableKindsInnerDebug {
311             variable_kinds: self,
312             interner,
313         }
314     }
315 }
316 
317 struct VariableKindsDebug<'a, I: Interner>(&'a VariableKinds<I>);
318 
319 impl<'a, I: Interner> Debug for VariableKindsDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>320     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
321         I::debug_variable_kinds_with_angles(self.0, fmt)
322             .unwrap_or_else(|| write!(fmt, "{:?}", self.0.interned))
323     }
324 }
325 
326 /// Helper struct for showing debug output for `VariableKinds`.
327 pub struct VariableKindsInnerDebug<'a, I: Interner> {
328     variable_kinds: &'a VariableKinds<I>,
329     interner: &'a I,
330 }
331 
332 impl<'a, I: Interner> Debug for VariableKindsInnerDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>333     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
334         // NB: We print variable kinds as a list delimited by `<>`,
335         // like `<K1, K2, ..>`. This is because variable kind lists
336         // are always associated with binders like `forall<type> {
337         // ... }`.
338         write!(fmt, "<")?;
339         for (index, binder) in self.variable_kinds.iter(self.interner).enumerate() {
340             if index > 0 {
341                 write!(fmt, ", ")?;
342             }
343             match binder {
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")?,
347                 VariableKind::Lifetime => write!(fmt, "lifetime")?,
348                 VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty)?,
349             }
350         }
351         write!(fmt, ">")
352     }
353 }
354 
355 impl<I: Interner> Debug for ConstData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
366 impl<I: Interner> Debug for GoalData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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),
380             GoalData::SubtypeGoal(ref wc) => write!(fmt, "{:?}", wc),
381             GoalData::DomainGoal(ref wc) => write!(fmt, "{:?}", wc),
382             GoalData::CannotProve => write!(fmt, r"¯\_(ツ)_/¯"),
383         }
384     }
385 }
386 
387 /// Helper struct for showing debug output for `Goals`.
388 pub struct GoalsDebug<'a, I: Interner> {
389     goals: &'a Goals<I>,
390     interner: &'a I,
391 }
392 
393 impl<'a, I: Interner> Debug for GoalsDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
407 impl<I: Interner> Goals<I> {
408     /// Show debug output for `Goals`.
debug<'a>(&'a self, interner: &'a I) -> GoalsDebug<'a, I>409     pub fn debug<'a>(&'a self, interner: &'a I) -> GoalsDebug<'a, I> {
410         GoalsDebug {
411             goals: self,
412             interner,
413         }
414     }
415 }
416 
417 /// Helper struct for showing debug output for `GenericArgData`.
418 pub struct GenericArgDataInnerDebug<'a, I: Interner>(&'a GenericArgData<I>);
419 
420 impl<'a, I: Interner> Debug for GenericArgDataInnerDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>421     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
422         match self.0 {
423             GenericArgData::Ty(n) => write!(fmt, "{:?}", n),
424             GenericArgData::Lifetime(n) => write!(fmt, "{:?}", n),
425             GenericArgData::Const(n) => write!(fmt, "{:?}", n),
426         }
427     }
428 }
429 
430 impl<I: Interner> GenericArgData<I> {
431     /// Helper method for debugging `GenericArgData`.
inner_debug(&self) -> GenericArgDataInnerDebug<'_, I>432     pub fn inner_debug(&self) -> GenericArgDataInnerDebug<'_, I> {
433         GenericArgDataInnerDebug(self)
434     }
435 }
436 
437 /// Helper struct for showing debug output for program clause implications.
438 pub struct ProgramClauseImplicationDebug<'a, I: Interner> {
439     pci: &'a ProgramClauseImplication<I>,
440     interner: &'a I,
441 }
442 
443 impl<'a, I: Interner> Debug for ProgramClauseImplicationDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
463 impl<I: Interner> ProgramClauseImplication<I> {
464     /// Show debug output for the program clause implication.
debug<'a>(&'a self, interner: &'a I) -> ProgramClauseImplicationDebug<'a, I>465     pub fn debug<'a>(&'a self, interner: &'a I) -> ProgramClauseImplicationDebug<'a, I> {
466         ProgramClauseImplicationDebug {
467             pci: self,
468             interner,
469         }
470     }
471 }
472 
473 /// Helper struct for showing debug output for application types.
474 pub struct TyKindDebug<'a, I: Interner> {
475     ty: &'a TyKind<I>,
476     interner: &'a I,
477 }
478 
479 impl<'a, I: Interner> Debug for TyKindDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>480     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
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         }
540     }
541 }
542 
543 impl<I: Interner> TyKind<I> {
544     /// Show debug output for the application type.
debug<'a>(&'a self, interner: &'a I) -> TyKindDebug<'a, I>545     pub fn debug<'a>(&'a self, interner: &'a I) -> TyKindDebug<'a, I> {
546         TyKindDebug { ty: self, interner }
547     }
548 }
549 
550 /// Helper struct for showing debug output for substitutions.
551 pub struct SubstitutionDebug<'a, I: Interner> {
552     substitution: &'a Substitution<I>,
553     interner: &'a I,
554 }
555 
556 impl<'a, I: Interner> Debug for SubstitutionDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
582 impl<I: Interner> Substitution<I> {
583     /// Show debug output for the substitution.
debug<'a>(&'a self, interner: &'a I) -> SubstitutionDebug<'a, I>584     pub fn debug<'a>(&'a self, interner: &'a I) -> SubstitutionDebug<'a, I> {
585         SubstitutionDebug {
586             substitution: self,
587             interner,
588         }
589     }
590 }
591 
592 impl Debug for PlaceholderIndex {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>593     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
594         let PlaceholderIndex { ui, idx } = self;
595         write!(fmt, "!{}_{}", ui.counter, idx)
596     }
597 }
598 
599 impl<I: Interner> TraitRef<I> {
600     /// Returns a "Debuggable" type that prints like `P0 as Trait<P1..>`.
with_as(&self) -> impl std::fmt::Debug + '_601     pub fn with_as(&self) -> impl std::fmt::Debug + '_ {
602         SeparatorTraitRef {
603             trait_ref: self,
604             separator: " as ",
605         }
606     }
607 
608     /// Returns a "Debuggable" type that prints like `P0: Trait<P1..>`.
with_colon(&self) -> impl std::fmt::Debug + '_609     pub fn with_colon(&self) -> impl std::fmt::Debug + '_ {
610         SeparatorTraitRef {
611             trait_ref: self,
612             separator: ": ",
613         }
614     }
615 }
616 
617 impl<I: Interner> Debug for TraitRef<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>618     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
619         Debug::fmt(&self.with_as(), fmt)
620     }
621 }
622 
623 /// Trait ref with associated separator used for debug output.
624 pub struct SeparatorTraitRef<'me, I: Interner> {
625     /// The `TraitRef` itself.
626     pub trait_ref: &'me TraitRef<I>,
627 
628     /// The separator used for displaying the `TraitRef`.
629     pub separator: &'me str,
630 }
631 
632 /// Helper struct for showing debug output for the `SeperatorTraitRef`.
633 pub struct SeparatorTraitRefDebug<'a, 'me, I: Interner> {
634     separator_trait_ref: &'a SeparatorTraitRef<'me, I>,
635     interner: &'a I,
636 }
637 
638 impl<'a, 'me, I: Interner> Debug for SeparatorTraitRefDebug<'a, 'me, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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
647             .as_slice(interner);
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 
659 impl<'me, I: Interner> SeparatorTraitRef<'me, I> {
660     /// Show debug output for the `SeperatorTraitRef`.
debug<'a>(&'a self, interner: &'a I) -> SeparatorTraitRefDebug<'a, 'me, I>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 
669 impl<I: Interner> Debug for LifetimeOutlives<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>670     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
671         write!(fmt, "{:?}: {:?}", self.a, self.b)
672     }
673 }
674 
675 impl<I: Interner> Debug for TypeOutlives<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>676     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
677         write!(fmt, "{:?}: {:?}", self.ty, self.lifetime)
678     }
679 }
680 
681 /// Helper struct for showing debug output for projection types.
682 pub struct ProjectionTyDebug<'a, I: Interner> {
683     projection_ty: &'a ProjectionTy<I>,
684     interner: &'a I,
685 }
686 
687 impl<'a, I: Interner> Debug for ProjectionTyDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
702 impl<I: Interner> ProjectionTy<I> {
703     /// Show debug output for the projection type.
debug<'a>(&'a self, interner: &'a I) -> ProjectionTyDebug<'a, I>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 
712 /// Helper struct for showing debug output for opaque types.
713 pub struct OpaqueTyDebug<'a, I: Interner> {
714     opaque_ty: &'a OpaqueTy<I>,
715     interner: &'a I,
716 }
717 
718 impl<'a, I: Interner> Debug for OpaqueTyDebug<'a, I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
733 impl<I: Interner> OpaqueTy<I> {
734     /// Show debug output for the opaque type.
debug<'a>(&'a self, interner: &'a I) -> OpaqueTyDebug<'a, I>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 
743 /// Wraps debug output in angle brackets (`<>`).
744 pub struct Angle<'a, T>(pub &'a [T]);
745 
746 impl<'a, T: Debug> Debug for Angle<'a, T> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>747     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
748         if !self.0.is_empty() {
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 
763 impl<I: Interner> Debug for Normalize<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>764     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
765         write!(fmt, "Normalize({:?} -> {:?})", self.alias, self.ty)
766     }
767 }
768 
769 impl<I: Interner> Debug for AliasEq<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>770     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
771         write!(fmt, "AliasEq({:?} = {:?})", self.alias, self.ty)
772     }
773 }
774 
775 impl<I: Interner> Debug for WhereClause<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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),
780             WhereClause::LifetimeOutlives(l_o) => write!(fmt, "{:?}", l_o),
781             WhereClause::TypeOutlives(t_o) => write!(fmt, "{:?}", t_o),
782         }
783     }
784 }
785 
786 impl<I: Interner> Debug for FromEnv<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
795 impl<I: Interner> Debug for WellFormed<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
804 impl<I: Interner> Debug for DomainGoal<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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             }
817             DomainGoal::Compatible => write!(fmt, "Compatible"),
818             DomainGoal::DownstreamType(n) => write!(fmt, "DownstreamType({:?})", n),
819             DomainGoal::Reveal => write!(fmt, "Reveal"),
820             DomainGoal::ObjectSafe(n) => write!(fmt, "ObjectSafe({:?})", n),
821         }
822     }
823 }
824 
825 impl<I: Interner> Debug for EqGoal<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>826     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
827         write!(fmt, "({:?} = {:?})", self.a, self.b)
828     }
829 }
830 
831 impl<I: Interner> Debug for SubtypeGoal<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>832     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
833         write!(fmt, "({:?} <: {:?})", self.a, self.b)
834     }
835 }
836 
837 impl<T: HasInterner + Debug> Debug for Binders<T> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
848 impl<I: Interner> Debug for ProgramClauseData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>849     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
850         write!(fmt, "{:?}", self.0)
851     }
852 }
853 
854 impl<I: Interner> Debug for Environment<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>855     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
856         write!(fmt, "Env({:?})", self.clauses)
857     }
858 }
859 
860 impl<I: Interner> Debug for CanonicalVarKinds<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
867 impl<T: HasInterner + Display> Canonical<T> {
868     /// Display the canonicalized item.
display<'a>(&'a self, interner: &'a T::Interner) -> CanonicalDisplay<'a, T>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 
877 /// Helper struct for displaying canonicalized items.
878 pub struct CanonicalDisplay<'a, T: HasInterner> {
879     canonical: &'a Canonical<T>,
880     interner: &'a T::Interner,
881 }
882 
883 impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>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                 }
903                 write!(f, "?{}", pk.skip_kind())?;
904             }
905 
906             write!(f, "> {{ {} }}", value)?;
907         }
908 
909         Ok(())
910     }
911 }
912 
913 impl<I: Interner> Debug for GenericArgData<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>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 
923 impl<I: Interner> Debug for VariableKind<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>924     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
925         match self {
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"),
929             VariableKind::Lifetime => write!(fmt, "lifetime"),
930             VariableKind::Const(ty) => write!(fmt, "const: {:?}", ty),
931         }
932     }
933 }
934 
935 impl<I: Interner, T: Debug> Debug for WithKind<I, T> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>936     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
937         let value = self.skip_kind();
938         match &self.kind {
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             }
946             VariableKind::Lifetime => write!(fmt, "{:?} with kind lifetime", value),
947             VariableKind::Const(ty) => write!(fmt, "{:?} with kind {:?}", value, ty),
948         }
949     }
950 }
951 
952 impl<I: Interner> Debug for Constraint<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>953     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
954         match self {
955             Constraint::LifetimeOutlives(a, b) => write!(fmt, "{:?}: {:?}", a, b),
956             Constraint::TypeOutlives(ty, lifetime) => write!(fmt, "{:?}: {:?}", ty, lifetime),
957         }
958     }
959 }
960 
961 impl<I: Interner> Display for ConstrainedSubst<I> {
fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>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 
973 impl<I: Interner> Substitution<I> {
974     /// Displays the substitution in the form `< P0, .. Pn >`, or (if
975     /// the substitution is empty) as an empty string.
with_angle(&self, interner: &I) -> Angle<'_, GenericArg<I>>976     pub fn with_angle(&self, interner: &I) -> Angle<'_, GenericArg<I>> {
977         Angle(self.as_slice(interner))
978     }
979 }
980 
981 impl<I: Interner> Debug for Substitution<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>982     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
983         Display::fmt(self, fmt)
984     }
985 }
986 
987 impl<I: Interner> Debug for Variances<I> {
fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>988     fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
989         I::debug_variances(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
990     }
991 }
992