Skip to content

Commit 95a968c

Browse files
Much smaller scope tree printing
1 parent 96b178b commit 95a968c

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/librustc_mir/pretty.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
144144
}
145145

146146
writeln!(w, "{}scope tree:", INDENT)?;
147-
write_scope_tree(tcx, mir, auxiliary, &scope_tree, w, None, 1)?;
147+
write_scope_tree(tcx, mir, auxiliary, &scope_tree, w, None, 1, false)?;
148+
writeln!(w, "")?;
148149

149150
writeln!(w, "}}")?;
150151
Ok(())
@@ -207,10 +208,27 @@ fn write_scope_tree(tcx: TyCtxt,
207208
scope_tree: &FnvHashMap<Option<ScopeId>, Vec<ScopeId>>,
208209
w: &mut Write,
209210
parent: Option<ScopeId>,
210-
depth: usize)
211+
depth: usize,
212+
same_line: bool)
211213
-> io::Result<()> {
212-
for &child in scope_tree.get(&parent).unwrap_or(&vec![]) {
213-
let indent = depth * INDENT.len();
214+
let indent = if same_line {
215+
0
216+
} else {
217+
depth * INDENT.len()
218+
};
219+
220+
let children = match scope_tree.get(&parent) {
221+
Some(childs) => childs,
222+
None => return Ok(()),
223+
};
224+
225+
for (index, &child) in children.iter().enumerate() {
226+
if index == 0 && same_line {
227+
// We know we're going to output a scope, so prefix it with a space to separate it from
228+
// the previous scopes on this line
229+
write!(w, " ")?;
230+
}
231+
214232
let data = &mir.scopes[child];
215233
assert_eq!(data.parent_scope, parent);
216234
write!(w, "{0:1$}{2}", "", indent, child.index())?;
@@ -223,15 +241,22 @@ fn write_scope_tree(tcx: TyCtxt,
223241
writeln!(w, "{0:1$}Extent: {2:?}", "", indent, data)?;
224242
}
225243

226-
if scope_tree.get(&Some(child)).map(Vec::is_empty).unwrap_or(true) {
227-
// No child scopes, skip the braces
228-
writeln!(w, "")?;
244+
let child_count = scope_tree.get(&Some(child)).map(Vec::len).unwrap_or(0);
245+
if child_count < 2 {
246+
// Skip the braces when there's no or only a single subscope
247+
write_scope_tree(tcx, mir, auxiliary, scope_tree, w,
248+
Some(child), depth, true)?;
229249
} else {
250+
// 2 or more child scopes? Put them in braces and on new lines.
230251
writeln!(w, " {{")?;
231252
write_scope_tree(tcx, mir, auxiliary, scope_tree, w,
232-
Some(child), depth + 1)?;
253+
Some(child), depth + 1, false)?;
233254

234-
writeln!(w, "{0:1$}}}", "", indent - INDENT.len())?;
255+
write!(w, "\n{0:1$}}}", "", depth * INDENT.len())?;
256+
}
257+
258+
if !same_line && index + 1 < children.len() {
259+
writeln!(w, "")?;
235260
}
236261
}
237262

0 commit comments

Comments
 (0)