Skip to content

Commit 2106b3d

Browse files
committed
Add some debug formatting structs and extract more debug formatting logic.
1 parent d415364 commit 2106b3d

File tree

2 files changed

+189
-63
lines changed

2 files changed

+189
-63
lines changed

chalk-integration/src/program.rs

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
77
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, Goal, Goals, ImplId, Lifetime,
8-
Parameter, ParameterKind, ProgramClause, ProgramClauseImplication, StructId, Substitution,
9-
TraitId, Ty, TyData, TypeName,
8+
Parameter, ProgramClause, ProgramClauseImplication, StructId, Substitution, TraitId, Ty,
9+
TyData, TypeName,
1010
};
1111
use chalk_rust_ir::{
1212
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -143,10 +143,7 @@ impl tls::DebugContext for Program {
143143
fmt: &mut fmt::Formatter<'_>,
144144
) -> Result<(), fmt::Error> {
145145
let interner = self.interner();
146-
match parameter.data(interner) {
147-
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
148-
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
149-
}
146+
write!(fmt, "{:?}", parameter.data(interner).inner_debug())
150147
}
151148

152149
fn debug_goal(
@@ -155,8 +152,7 @@ impl tls::DebugContext for Program {
155152
fmt: &mut fmt::Formatter<'_>,
156153
) -> Result<(), fmt::Error> {
157154
let interner = self.interner();
158-
write!(fmt, "{:?}", goal.data(interner))?;
159-
Ok(())
155+
write!(fmt, "{:?}", goal.data(interner))
160156
}
161157

162158
fn debug_goals(
@@ -165,15 +161,7 @@ impl tls::DebugContext for Program {
165161
fmt: &mut fmt::Formatter<'_>,
166162
) -> Result<(), fmt::Error> {
167163
let interner = self.interner();
168-
write!(fmt, "(")?;
169-
for (goal, index) in goals.iter(interner).zip(0..) {
170-
if index > 0 {
171-
write!(fmt, ", ")?;
172-
}
173-
write!(fmt, "{:?}", goal)?;
174-
}
175-
write!(fmt, ")")?;
176-
Ok(())
164+
write!(fmt, "{:?}", goals.debug(interner))
177165
}
178166

179167
fn debug_program_clause_implication(
@@ -182,20 +170,7 @@ impl tls::DebugContext for Program {
182170
fmt: &mut fmt::Formatter<'_>,
183171
) -> Result<(), fmt::Error> {
184172
let interner = self.interner();
185-
write!(fmt, "{:?}", pci.consequence)?;
186-
187-
let conditions = pci.conditions.as_slice(interner);
188-
189-
let conds = conditions.len();
190-
if conds == 0 {
191-
return Ok(());
192-
}
193-
194-
write!(fmt, " :- ")?;
195-
for cond in &conditions[..conds - 1] {
196-
write!(fmt, "{:?}, ", cond)?;
197-
}
198-
write!(fmt, "{:?}", conditions[conds - 1])
173+
write!(fmt, "{:?}", pci.debug(interner))
199174
}
200175

201176
fn debug_application_ty(
@@ -204,8 +179,7 @@ impl tls::DebugContext for Program {
204179
fmt: &mut fmt::Formatter<'_>,
205180
) -> Result<(), fmt::Error> {
206181
let interner = self.interner();
207-
let ApplicationTy { name, substitution } = application_ty;
208-
write!(fmt, "{:?}{:?}", name, substitution.with_angle(interner))
182+
write!(fmt, "{:?}", application_ty.debug(interner))
209183
}
210184

211185
fn debug_substitution(
@@ -214,43 +188,16 @@ impl tls::DebugContext for Program {
214188
fmt: &mut fmt::Formatter<'_>,
215189
) -> Result<(), fmt::Error> {
216190
let interner = self.interner();
217-
let mut first = true;
218-
219-
write!(fmt, "[")?;
220-
221-
for (index, value) in substitution.iter(interner).enumerate() {
222-
if first {
223-
first = false;
224-
} else {
225-
write!(fmt, ", ")?;
226-
}
227-
228-
write!(fmt, "?{} := {:?}", index, value)?;
229-
}
230-
231-
write!(fmt, "]")?;
232-
233-
Ok(())
191+
write!(fmt, "{:?}", substitution.debug(interner))
234192
}
235193

236194
fn debug_separator_trait_ref(
237195
&self,
238-
separator_trait_ref: &SeparatorTraitRef<ChalkIr>,
196+
separator_trait_ref: &SeparatorTraitRef<'_, ChalkIr>,
239197
fmt: &mut fmt::Formatter<'_>,
240198
) -> Result<(), fmt::Error> {
241199
let interner = self.interner();
242-
let parameters = separator_trait_ref
243-
.trait_ref
244-
.substitution
245-
.parameters(interner);
246-
write!(
247-
fmt,
248-
"{:?}{}{:?}{:?}",
249-
parameters[0],
250-
separator_trait_ref.separator,
251-
separator_trait_ref.trait_ref.trait_id,
252-
Angle(&parameters[1..])
253-
)
200+
write!(fmt, "{:?}", separator_trait_ref.debug(interner))
254201
}
255202
}
256203

chalk-ir/src/debug.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,150 @@ impl<I: Interner> Debug for GoalData<I> {
195195
}
196196
}
197197

198+
pub struct GoalsDebug<'a, I: Interner> {
199+
goals: &'a Goals<I>,
200+
interner: &'a I,
201+
}
202+
203+
impl<'a, I: Interner> Debug for GoalsDebug<'a, I> {
204+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
205+
write!(fmt, "(")?;
206+
for (goal, index) in self.goals.iter(self.interner).zip(0..) {
207+
if index > 0 {
208+
write!(fmt, ", ")?;
209+
}
210+
write!(fmt, "{:?}", goal)?;
211+
}
212+
write!(fmt, ")")?;
213+
Ok(())
214+
}
215+
}
216+
217+
impl<I: Interner> Goals<I> {
218+
pub fn debug<'a>(&'a self, interner: &'a I) -> GoalsDebug<'a, I> {
219+
GoalsDebug {
220+
goals: self,
221+
interner,
222+
}
223+
}
224+
}
225+
226+
pub struct ParameterDataInnerDebug<'a, I: Interner>(&'a ParameterData<I>);
227+
228+
impl<'a, I: Interner> Debug for ParameterDataInnerDebug<'a, I> {
229+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
230+
match self.0 {
231+
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
232+
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
233+
}
234+
}
235+
}
236+
237+
impl<I: Interner> ParameterData<I> {
238+
pub fn inner_debug(&self) -> ParameterDataInnerDebug<'_, I> {
239+
ParameterDataInnerDebug(self)
240+
}
241+
}
242+
243+
pub struct ProgramClauseImplicationDebug<'a, I: Interner> {
244+
pci: &'a ProgramClauseImplication<I>,
245+
interner: &'a I,
246+
}
247+
248+
impl<'a, I: Interner> Debug for ProgramClauseImplicationDebug<'a, I> {
249+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
250+
let ProgramClauseImplicationDebug { pci, interner } = self;
251+
write!(fmt, "{:?}", pci.consequence)?;
252+
253+
let conditions = pci.conditions.as_slice(interner);
254+
255+
let conds = conditions.len();
256+
if conds == 0 {
257+
return Ok(());
258+
}
259+
260+
write!(fmt, " :- ")?;
261+
for cond in &conditions[..conds - 1] {
262+
write!(fmt, "{:?}, ", cond)?;
263+
}
264+
write!(fmt, "{:?}", conditions[conds - 1])
265+
}
266+
}
267+
268+
impl<I: Interner> ProgramClauseImplication<I> {
269+
pub fn debug<'a>(&'a self, interner: &'a I) -> ProgramClauseImplicationDebug<'a, I> {
270+
ProgramClauseImplicationDebug {
271+
pci: self,
272+
interner,
273+
}
274+
}
275+
}
276+
277+
pub struct ApplicationTyDebug<'a, I: Interner> {
278+
application_ty: &'a ApplicationTy<I>,
279+
interner: &'a I,
280+
}
281+
282+
impl<'a, I: Interner> Debug for ApplicationTyDebug<'a, I> {
283+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
284+
let ApplicationTyDebug {
285+
application_ty,
286+
interner,
287+
} = self;
288+
let ApplicationTy { name, substitution } = application_ty;
289+
write!(fmt, "{:?}{:?}", name, substitution.with_angle(interner))
290+
}
291+
}
292+
293+
impl<I: Interner> ApplicationTy<I> {
294+
pub fn debug<'a>(&'a self, interner: &'a I) -> ApplicationTyDebug<'a, I> {
295+
ApplicationTyDebug {
296+
application_ty: self,
297+
interner,
298+
}
299+
}
300+
}
301+
302+
pub struct SubstitutionDebug<'a, I: Interner> {
303+
substitution: &'a Substitution<I>,
304+
interner: &'a I,
305+
}
306+
307+
impl<'a, I: Interner> Debug for SubstitutionDebug<'a, I> {
308+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
309+
let SubstitutionDebug {
310+
substitution,
311+
interner,
312+
} = self;
313+
let mut first = true;
314+
315+
write!(fmt, "[")?;
316+
317+
for (index, value) in substitution.iter(interner).enumerate() {
318+
if first {
319+
first = false;
320+
} else {
321+
write!(fmt, ", ")?;
322+
}
323+
324+
write!(fmt, "?{} := {:?}", index, value)?;
325+
}
326+
327+
write!(fmt, "]")?;
328+
329+
Ok(())
330+
}
331+
}
332+
333+
impl<I: Interner> Substitution<I> {
334+
pub fn debug<'a>(&'a self, interner: &'a I) -> SubstitutionDebug<'a, I> {
335+
SubstitutionDebug {
336+
substitution: self,
337+
interner,
338+
}
339+
}
340+
}
341+
198342
impl Debug for PlaceholderIndex {
199343
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
200344
let PlaceholderIndex { ui, idx } = self;
@@ -231,6 +375,41 @@ pub struct SeparatorTraitRef<'me, I: Interner> {
231375
pub separator: &'me str,
232376
}
233377

378+
pub struct SeparatorTraitRefDebug<'a, 'me, I: Interner> {
379+
separator_trait_ref: &'a SeparatorTraitRef<'me, I>,
380+
interner: &'a I,
381+
}
382+
383+
impl<'a, 'me, I: Interner> Debug for SeparatorTraitRefDebug<'a, 'me, I> {
384+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
385+
let SeparatorTraitRefDebug {
386+
separator_trait_ref,
387+
interner,
388+
} = self;
389+
let parameters = separator_trait_ref
390+
.trait_ref
391+
.substitution
392+
.parameters(interner);
393+
write!(
394+
fmt,
395+
"{:?}{}{:?}{:?}",
396+
parameters[0],
397+
separator_trait_ref.separator,
398+
separator_trait_ref.trait_ref.trait_id,
399+
Angle(&parameters[1..])
400+
)
401+
}
402+
}
403+
404+
impl<'me, I: Interner> SeparatorTraitRef<'me, I> {
405+
pub fn debug<'a>(&'a self, interner: &'a I) -> SeparatorTraitRefDebug<'a, 'me, I> {
406+
SeparatorTraitRefDebug {
407+
separator_trait_ref: self,
408+
interner,
409+
}
410+
}
411+
}
412+
234413
pub struct Angle<'a, T>(pub &'a [T]);
235414

236415
impl<'a, T: Debug> Debug for Angle<'a, T> {

0 commit comments

Comments
 (0)