Skip to content

Commit

Permalink
inspect formatter: add braces
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Mar 8, 2024
1 parent 4282576 commit 309d85b
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions compiler/rustc_middle/src/traits/solve/inspect/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ pub(super) struct ProofTreeFormatter<'a, 'b> {
f: &'a mut (dyn Write + 'b),
}

enum IndentorState {
StartWithNewline,
OnNewline,
Inline,
}

/// A formatter which adds 4 spaces of indentation to its input before
/// passing it on to its nested formatter.
///
/// We can use this for arbitrary levels of indentation by nesting it.
struct Indentor<'a, 'b> {
f: &'a mut (dyn Write + 'b),
on_newline: bool,
state: IndentorState,
}

impl Write for Indentor<'_, '_> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
for line in s.split_inclusive('\n') {
if self.on_newline {
self.f.write_str(" ")?;
match self.state {
IndentorState::StartWithNewline => self.f.write_str("\n ")?,
IndentorState::OnNewline => self.f.write_str(" ")?,
IndentorState::Inline => {}
}
self.on_newline = line.ends_with('\n');
self.state =
if line.ends_with('\n') { IndentorState::OnNewline } else { IndentorState::Inline };
self.f.write_str(line)?;
}

Expand All @@ -32,11 +41,15 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
ProofTreeFormatter { f }
}

fn nested<F, R>(&mut self, func: F) -> R
fn nested<F>(&mut self, func: F) -> std::fmt::Result
where
F: FnOnce(&mut ProofTreeFormatter<'_, '_>) -> R,
F: FnOnce(&mut ProofTreeFormatter<'_, '_>) -> std::fmt::Result,
{
func(&mut ProofTreeFormatter { f: &mut Indentor { f: self.f, on_newline: true } })
write!(self.f, " {{")?;
func(&mut ProofTreeFormatter {
f: &mut Indentor { f: self.f, state: IndentorState::StartWithNewline },
})?;
writeln!(self.f, "}}")
}

pub(super) fn format_goal_evaluation(&mut self, eval: &GoalEvaluation<'_>) -> std::fmt::Result {
Expand All @@ -47,7 +60,7 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
IsNormalizesToHack::Yes => "NORMALIZES-TO HACK GOAL",
},
};
writeln!(self.f, "{}: {:?}", goal_text, eval.uncanonicalized_goal)?;
write!(self.f, "{}: {:?}", goal_text, eval.uncanonicalized_goal)?;
self.nested(|this| this.format_canonical_goal_evaluation(&eval.evaluation))
}

Expand All @@ -69,7 +82,7 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
}
CanonicalGoalEvaluationKind::Evaluation { revisions } => {
for (n, step) in revisions.iter().enumerate() {
writeln!(self.f, "REVISION {n}")?;
write!(self.f, "REVISION {n}")?;
self.nested(|this| this.format_evaluation_step(step))?;
}
writeln!(self.f, "RESULT: {:?}", eval.result)
Expand All @@ -88,25 +101,25 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
pub(super) fn format_probe(&mut self, probe: &Probe<'_>) -> std::fmt::Result {
match &probe.kind {
ProbeKind::Root { result } => {
writeln!(self.f, "ROOT RESULT: {result:?}")
write!(self.f, "ROOT RESULT: {result:?}")
}
ProbeKind::NormalizedSelfTyAssembly => {
writeln!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:")
write!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:")
}
ProbeKind::UnsizeAssembly => {
writeln!(self.f, "ASSEMBLING CANDIDATES FOR UNSIZING:")
write!(self.f, "ASSEMBLING CANDIDATES FOR UNSIZING:")
}
ProbeKind::UpcastProjectionCompatibility => {
writeln!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
write!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
}
ProbeKind::CommitIfOk => {
writeln!(self.f, "COMMIT_IF_OK:")
write!(self.f, "COMMIT_IF_OK:")
}
ProbeKind::MiscCandidate { name, result } => {
writeln!(self.f, "CANDIDATE {name}: {result:?}")
write!(self.f, "CANDIDATE {name}: {result:?}")
}
ProbeKind::TraitCandidate { source, result } => {
writeln!(self.f, "CANDIDATE {source:?}: {result:?}")
write!(self.f, "CANDIDATE {source:?}: {result:?}")
}
}?;

Expand Down Expand Up @@ -137,7 +150,7 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
writeln!(self.f, "TRY_EVALUATE_ADDED_GOALS: {:?}", added_goals_evaluation.result)?;

for (n, iterations) in added_goals_evaluation.evaluations.iter().enumerate() {
writeln!(self.f, "ITERATION {n}")?;
write!(self.f, "ITERATION {n}")?;
self.nested(|this| {
for goal_evaluation in iterations {
this.format_goal_evaluation(goal_evaluation)?;
Expand Down

0 comments on commit 309d85b

Please sign in to comment.