Skip to content

Commit 309d85b

Browse files
committed
inspect formatter: add braces
1 parent 4282576 commit 309d85b

File tree

1 file changed

+30
-17
lines changed
  • compiler/rustc_middle/src/traits/solve/inspect

1 file changed

+30
-17
lines changed

compiler/rustc_middle/src/traits/solve/inspect/format.rs

+30-17
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ pub(super) struct ProofTreeFormatter<'a, 'b> {
44
f: &'a mut (dyn Write + 'b),
55
}
66

7+
enum IndentorState {
8+
StartWithNewline,
9+
OnNewline,
10+
Inline,
11+
}
12+
713
/// A formatter which adds 4 spaces of indentation to its input before
814
/// passing it on to its nested formatter.
915
///
1016
/// We can use this for arbitrary levels of indentation by nesting it.
1117
struct Indentor<'a, 'b> {
1218
f: &'a mut (dyn Write + 'b),
13-
on_newline: bool,
19+
state: IndentorState,
1420
}
1521

1622
impl Write for Indentor<'_, '_> {
1723
fn write_str(&mut self, s: &str) -> std::fmt::Result {
1824
for line in s.split_inclusive('\n') {
19-
if self.on_newline {
20-
self.f.write_str(" ")?;
25+
match self.state {
26+
IndentorState::StartWithNewline => self.f.write_str("\n ")?,
27+
IndentorState::OnNewline => self.f.write_str(" ")?,
28+
IndentorState::Inline => {}
2129
}
22-
self.on_newline = line.ends_with('\n');
30+
self.state =
31+
if line.ends_with('\n') { IndentorState::OnNewline } else { IndentorState::Inline };
2332
self.f.write_str(line)?;
2433
}
2534

@@ -32,11 +41,15 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
3241
ProofTreeFormatter { f }
3342
}
3443

35-
fn nested<F, R>(&mut self, func: F) -> R
44+
fn nested<F>(&mut self, func: F) -> std::fmt::Result
3645
where
37-
F: FnOnce(&mut ProofTreeFormatter<'_, '_>) -> R,
46+
F: FnOnce(&mut ProofTreeFormatter<'_, '_>) -> std::fmt::Result,
3847
{
39-
func(&mut ProofTreeFormatter { f: &mut Indentor { f: self.f, on_newline: true } })
48+
write!(self.f, " {{")?;
49+
func(&mut ProofTreeFormatter {
50+
f: &mut Indentor { f: self.f, state: IndentorState::StartWithNewline },
51+
})?;
52+
writeln!(self.f, "}}")
4053
}
4154

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

@@ -69,7 +82,7 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
6982
}
7083
CanonicalGoalEvaluationKind::Evaluation { revisions } => {
7184
for (n, step) in revisions.iter().enumerate() {
72-
writeln!(self.f, "REVISION {n}")?;
85+
write!(self.f, "REVISION {n}")?;
7386
self.nested(|this| this.format_evaluation_step(step))?;
7487
}
7588
writeln!(self.f, "RESULT: {:?}", eval.result)
@@ -88,25 +101,25 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
88101
pub(super) fn format_probe(&mut self, probe: &Probe<'_>) -> std::fmt::Result {
89102
match &probe.kind {
90103
ProbeKind::Root { result } => {
91-
writeln!(self.f, "ROOT RESULT: {result:?}")
104+
write!(self.f, "ROOT RESULT: {result:?}")
92105
}
93106
ProbeKind::NormalizedSelfTyAssembly => {
94-
writeln!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:")
107+
write!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:")
95108
}
96109
ProbeKind::UnsizeAssembly => {
97-
writeln!(self.f, "ASSEMBLING CANDIDATES FOR UNSIZING:")
110+
write!(self.f, "ASSEMBLING CANDIDATES FOR UNSIZING:")
98111
}
99112
ProbeKind::UpcastProjectionCompatibility => {
100-
writeln!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
113+
write!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
101114
}
102115
ProbeKind::CommitIfOk => {
103-
writeln!(self.f, "COMMIT_IF_OK:")
116+
write!(self.f, "COMMIT_IF_OK:")
104117
}
105118
ProbeKind::MiscCandidate { name, result } => {
106-
writeln!(self.f, "CANDIDATE {name}: {result:?}")
119+
write!(self.f, "CANDIDATE {name}: {result:?}")
107120
}
108121
ProbeKind::TraitCandidate { source, result } => {
109-
writeln!(self.f, "CANDIDATE {source:?}: {result:?}")
122+
write!(self.f, "CANDIDATE {source:?}: {result:?}")
110123
}
111124
}?;
112125

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

139152
for (n, iterations) in added_goals_evaluation.evaluations.iter().enumerate() {
140-
writeln!(self.f, "ITERATION {n}")?;
153+
write!(self.f, "ITERATION {n}")?;
141154
self.nested(|this| {
142155
for goal_evaluation in iterations {
143156
this.format_goal_evaluation(goal_evaluation)?;

0 commit comments

Comments
 (0)