Skip to content

Commit 4aae835

Browse files
committed
rustc: always print nested nodes where a HIR map is available.
1 parent f64e73b commit 4aae835

File tree

24 files changed

+245
-378
lines changed

24 files changed

+245
-378
lines changed

src/librustc/hir/map/mod.rs

+46-25
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use syntax_pos::Span;
2626

2727
use hir::*;
2828
use hir::intravisit::Visitor;
29-
use hir::print as pprust;
29+
use hir::print::Nested;
3030

3131
use arena::TypedArena;
3232
use std::cell::RefCell;
@@ -842,6 +842,10 @@ impl<'ast> Map<'ast> {
842842
pub fn node_to_user_string(&self, id: NodeId) -> String {
843843
node_id_to_string(self, id, false)
844844
}
845+
846+
pub fn node_to_pretty_string(&self, id: NodeId) -> String {
847+
print::to_string(self, |s| s.print_node(self.get(id)))
848+
}
845849
}
846850

847851
pub struct NodesMatchingSuffix<'a, 'ast:'a> {
@@ -1004,13 +1008,23 @@ pub fn map_decoded_body<'ast>(map: &Map<'ast>,
10041008
&ii.body
10051009
}
10061010

1007-
pub trait NodePrinter {
1008-
fn print_node(&mut self, node: &Node) -> io::Result<()>;
1011+
/// Identical to the `PpAnn` implementation for `hir::Crate`,
1012+
/// except it avoids creating a dependency on the whole crate.
1013+
impl<'ast> print::PpAnn for Map<'ast> {
1014+
fn nested(&self, state: &mut print::State, nested: print::Nested) -> io::Result<()> {
1015+
match nested {
1016+
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
1017+
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
1018+
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
1019+
Nested::Body(id) => state.print_expr(&self.body(id).value),
1020+
Nested::BodyArgPat(id, i) => state.print_pat(&self.body(id).arguments[i].pat)
1021+
}
1022+
}
10091023
}
10101024

1011-
impl<'a> NodePrinter for pprust::State<'a> {
1012-
fn print_node(&mut self, node: &Node) -> io::Result<()> {
1013-
match *node {
1025+
impl<'a> print::State<'a> {
1026+
pub fn print_node(&mut self, node: Node) -> io::Result<()> {
1027+
match node {
10141028
NodeItem(a) => self.print_item(&a),
10151029
NodeForeignItem(a) => self.print_foreign_item(&a),
10161030
NodeTraitItem(a) => self.print_trait_item(a),
@@ -1020,16 +1034,24 @@ impl<'a> NodePrinter for pprust::State<'a> {
10201034
NodeStmt(a) => self.print_stmt(&a),
10211035
NodeTy(a) => self.print_type(&a),
10221036
NodeTraitRef(a) => self.print_trait_ref(&a),
1037+
NodeLocal(a) |
10231038
NodePat(a) => self.print_pat(&a),
1024-
NodeBlock(a) => self.print_block(&a),
1039+
NodeBlock(a) => {
1040+
use syntax::print::pprust::PrintState;
1041+
1042+
// containing cbox, will be closed by print-block at }
1043+
self.cbox(print::indent_unit)?;
1044+
// head-ibox, will be closed by print-block after {
1045+
self.ibox(0)?;
1046+
self.print_block(&a)
1047+
}
10251048
NodeLifetime(a) => self.print_lifetime(&a),
10261049
NodeVisibility(a) => self.print_visibility(&a),
10271050
NodeTyParam(_) => bug!("cannot print TyParam"),
10281051
NodeField(_) => bug!("cannot print StructField"),
10291052
// these cases do not carry enough information in the
10301053
// ast_map to reconstruct their full structure for pretty
10311054
// printing.
1032-
NodeLocal(_) => bug!("cannot print isolated Local"),
10331055
NodeStructCtor(_) => bug!("cannot print isolated StructCtor"),
10341056
}
10351057
}
@@ -1110,33 +1132,32 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11101132
field.name,
11111133
path_str(), id_str)
11121134
}
1113-
Some(NodeExpr(ref expr)) => {
1114-
format!("expr {}{}", pprust::expr_to_string(&expr), id_str)
1135+
Some(NodeExpr(_)) => {
1136+
format!("expr {}{}", map.node_to_pretty_string(id), id_str)
11151137
}
1116-
Some(NodeStmt(ref stmt)) => {
1117-
format!("stmt {}{}", pprust::stmt_to_string(&stmt), id_str)
1138+
Some(NodeStmt(_)) => {
1139+
format!("stmt {}{}", map.node_to_pretty_string(id), id_str)
11181140
}
1119-
Some(NodeTy(ref ty)) => {
1120-
format!("type {}{}", pprust::ty_to_string(&ty), id_str)
1141+
Some(NodeTy(_)) => {
1142+
format!("type {}{}", map.node_to_pretty_string(id), id_str)
11211143
}
1122-
Some(NodeTraitRef(ref tr)) => {
1123-
format!("trait_ref {}{}", pprust::path_to_string(&tr.path), id_str)
1144+
Some(NodeTraitRef(_)) => {
1145+
format!("trait_ref {}{}", map.node_to_pretty_string(id), id_str)
11241146
}
1125-
Some(NodeLocal(ref pat)) => {
1126-
format!("local {}{}", pprust::pat_to_string(&pat), id_str)
1147+
Some(NodeLocal(_)) => {
1148+
format!("local {}{}", map.node_to_pretty_string(id), id_str)
11271149
}
1128-
Some(NodePat(ref pat)) => {
1129-
format!("pat {}{}", pprust::pat_to_string(&pat), id_str)
1150+
Some(NodePat(_)) => {
1151+
format!("pat {}{}", map.node_to_pretty_string(id), id_str)
11301152
}
1131-
Some(NodeBlock(ref block)) => {
1132-
format!("block {}{}", pprust::block_to_string(&block), id_str)
1153+
Some(NodeBlock(_)) => {
1154+
format!("block {}{}", map.node_to_pretty_string(id), id_str)
11331155
}
11341156
Some(NodeStructCtor(_)) => {
11351157
format!("struct_ctor {}{}", path_str(), id_str)
11361158
}
1137-
Some(NodeLifetime(ref l)) => {
1138-
format!("lifetime {}{}",
1139-
pprust::lifetime_to_string(&l), id_str)
1159+
Some(NodeLifetime(_)) => {
1160+
format!("lifetime {}{}", map.node_to_pretty_string(id), id_str)
11401161
}
11411162
Some(NodeTyParam(ref ty_param)) => {
11421163
format!("typaram {:?}{}", ty_param, id_str)

src/librustc/hir/mod.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl fmt::Debug for Lifetime {
8585
write!(f,
8686
"lifetime({}: {})",
8787
self.id,
88-
print::lifetime_to_string(self))
88+
print::to_string(print::NO_ANN, |s| s.print_lifetime(self)))
8989
}
9090
}
9191

@@ -117,13 +117,8 @@ impl Path {
117117

118118
impl fmt::Debug for Path {
119119
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120-
write!(f, "path({})", print::path_to_string(self))
121-
}
122-
}
123-
124-
impl fmt::Display for Path {
125-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126-
write!(f, "{}", print::path_to_string(self))
120+
write!(f, "path({})",
121+
print::to_string(print::NO_ANN, |s| s.print_path(self, false)))
127122
}
128123
}
129124

@@ -510,7 +505,8 @@ pub struct Pat {
510505

511506
impl fmt::Debug for Pat {
512507
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
513-
write!(f, "pat({}: {})", self.id, print::pat_to_string(self))
508+
write!(f, "pat({}: {})", self.id,
509+
print::to_string(print::NO_ANN, |s| s.print_pat(self)))
514510
}
515511
}
516512

@@ -762,7 +758,7 @@ impl fmt::Debug for Stmt_ {
762758
write!(f,
763759
"stmt({}: {})",
764760
spanned.node.id(),
765-
print::stmt_to_string(&spanned))
761+
print::to_string(print::NO_ANN, |s| s.print_stmt(&spanned)))
766762
}
767763
}
768764

@@ -891,7 +887,8 @@ pub struct Expr {
891887

892888
impl fmt::Debug for Expr {
893889
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
894-
write!(f, "expr({}: {})", self.id, print::expr_to_string(self))
890+
write!(f, "expr({}: {})", self.id,
891+
print::to_string(print::NO_ANN, |s| s.print_expr(self)))
895892
}
896893
}
897894

@@ -1015,12 +1012,6 @@ pub enum QPath {
10151012
TypeRelative(P<Ty>, P<PathSegment>)
10161013
}
10171014

1018-
impl fmt::Display for QPath {
1019-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1020-
write!(f, "{}", print::qpath_to_string(self))
1021-
}
1022-
}
1023-
10241015
/// Hints at the original code for a `match _ { .. }`
10251016
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
10261017
pub enum MatchSource {
@@ -1177,7 +1168,8 @@ pub struct Ty {
11771168

11781169
impl fmt::Debug for Ty {
11791170
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1180-
write!(f, "type({})", print::ty_to_string(self))
1171+
write!(f, "type({})",
1172+
print::to_string(print::NO_ANN, |s| s.print_type(self)))
11811173
}
11821174
}
11831175

0 commit comments

Comments
 (0)