Skip to content

Commit e31fc9d

Browse files
committed
Add node_to_user_string, node_to_string variant that drops id from output.
1 parent f5b795d commit e31fc9d

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/libsyntax/ast_map/mod.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,11 @@ impl<'ast> Map<'ast> {
551551
}
552552

553553
pub fn node_to_string(&self, id: NodeId) -> String {
554-
node_id_to_string(self, id)
554+
node_id_to_string(self, id, true)
555+
}
556+
557+
pub fn node_to_user_string(&self, id: NodeId) -> String {
558+
node_id_to_string(self, id, false)
555559
}
556560
}
557561

@@ -1028,7 +1032,10 @@ impl<'a> NodePrinter for pprust::State<'a> {
10281032
}
10291033
}
10301034

1031-
fn node_id_to_string(map: &Map, id: NodeId) -> String {
1035+
fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1036+
let id_str = format!(" (id={})", id);
1037+
let id_str = if include_id { id_str.as_slice() } else { "" };
1038+
10321039
match map.find(id) {
10331040
Some(NodeItem(item)) => {
10341041
let path_str = map.path_to_str_with_ident(id, item.ident);
@@ -1045,82 +1052,82 @@ fn node_id_to_string(map: &Map, id: NodeId) -> String {
10451052
ItemImpl(..) => "impl",
10461053
ItemMac(..) => "macro"
10471054
};
1048-
format!("{} {} (id={})", item_str, path_str, id)
1055+
format!("{} {}{}", item_str, path_str, id_str)
10491056
}
10501057
Some(NodeForeignItem(item)) => {
10511058
let path_str = map.path_to_str_with_ident(id, item.ident);
1052-
format!("foreign item {} (id={})", path_str, id)
1059+
format!("foreign item {}{}", path_str, id_str)
10531060
}
10541061
Some(NodeImplItem(ref ii)) => {
10551062
match **ii {
10561063
MethodImplItem(ref m) => {
10571064
match m.node {
10581065
MethDecl(ident, _, _, _, _, _, _, _) =>
1059-
format!("method {} in {} (id={})",
1066+
format!("method {} in {}{}",
10601067
token::get_ident(ident),
1061-
map.path_to_string(id), id),
1068+
map.path_to_string(id), id_str),
10621069
MethMac(ref mac) =>
1063-
format!("method macro {} (id={})",
1064-
pprust::mac_to_string(mac), id)
1070+
format!("method macro {}{}",
1071+
pprust::mac_to_string(mac), id_str)
10651072
}
10661073
}
10671074
TypeImplItem(ref t) => {
1068-
format!("typedef {} in {} (id={})",
1075+
format!("typedef {} in {}{}",
10691076
token::get_ident(t.ident),
10701077
map.path_to_string(id),
1071-
id)
1078+
id_str)
10721079
}
10731080
}
10741081
}
10751082
Some(NodeTraitItem(ref tm)) => {
10761083
match **tm {
10771084
RequiredMethod(_) | ProvidedMethod(_) => {
10781085
let m = ast_util::trait_item_to_ty_method(&**tm);
1079-
format!("method {} in {} (id={})",
1086+
format!("method {} in {}{}",
10801087
token::get_ident(m.ident),
10811088
map.path_to_string(id),
1082-
id)
1089+
id_str)
10831090
}
10841091
TypeTraitItem(ref t) => {
1085-
format!("type item {} in {} (id={})",
1092+
format!("type item {} in {}{}",
10861093
token::get_ident(t.ty_param.ident),
10871094
map.path_to_string(id),
1088-
id)
1095+
id_str)
10891096
}
10901097
}
10911098
}
10921099
Some(NodeVariant(ref variant)) => {
1093-
format!("variant {} in {} (id={})",
1100+
format!("variant {} in {}{}",
10941101
token::get_ident(variant.node.name),
1095-
map.path_to_string(id), id)
1102+
map.path_to_string(id), id_str)
10961103
}
10971104
Some(NodeExpr(ref expr)) => {
1098-
format!("expr {} (id={})", pprust::expr_to_string(&**expr), id)
1105+
format!("expr {}{}", pprust::expr_to_string(&**expr), id_str)
10991106
}
11001107
Some(NodeStmt(ref stmt)) => {
1101-
format!("stmt {} (id={})", pprust::stmt_to_string(&**stmt), id)
1108+
format!("stmt {}{}", pprust::stmt_to_string(&**stmt), id_str)
11021109
}
11031110
Some(NodeArg(ref pat)) => {
1104-
format!("arg {} (id={})", pprust::pat_to_string(&**pat), id)
1111+
format!("arg {}{}", pprust::pat_to_string(&**pat), id_str)
11051112
}
11061113
Some(NodeLocal(ref pat)) => {
1107-
format!("local {} (id={})", pprust::pat_to_string(&**pat), id)
1114+
format!("local {}{}", pprust::pat_to_string(&**pat), id_str)
11081115
}
11091116
Some(NodePat(ref pat)) => {
1110-
format!("pat {} (id={})", pprust::pat_to_string(&**pat), id)
1117+
format!("pat {}{}", pprust::pat_to_string(&**pat), id_str)
11111118
}
11121119
Some(NodeBlock(ref block)) => {
1113-
format!("block {} (id={})", pprust::block_to_string(&**block), id)
1120+
format!("block {}{}", pprust::block_to_string(&**block), id_str)
11141121
}
11151122
Some(NodeStructCtor(_)) => {
1116-
format!("struct_ctor {} (id={})", map.path_to_string(id), id)
1123+
format!("struct_ctor {}{}", map.path_to_string(id), id_str)
11171124
}
11181125
Some(NodeLifetime(ref l)) => {
1119-
format!("lifetime {} (id={})",
1120-
pprust::lifetime_to_string(&**l), id)
1126+
format!("lifetime {}{}",
1127+
pprust::lifetime_to_string(&**l), id_str)
11211128
}
11221129
None => {
1123-
format!("unknown node (id={})", id)
1130+
format!("unknown node{}", id_str)
11241131
}
11251132
}
11261133
}

0 commit comments

Comments
 (0)