Skip to content

Commit cf87edf

Browse files
committedFeb 15, 2020
pprust: unify extern & associated item printing
1 parent 0e0c028 commit cf87edf

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed
 

‎src/librustc_ast_pretty/pprust.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ impl<'a> State<'a> {
912912
}
913913
}
914914

915-
crate fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[ast::Attribute]) {
915+
crate fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[Attribute]) {
916916
self.print_inner_attributes(attrs);
917917
for item in &nmod.items {
918918
self.print_foreign_item(item);
@@ -1016,21 +1016,37 @@ impl<'a> State<'a> {
10161016
}
10171017

10181018
crate fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
1019+
let ast::ForeignItem { id, span, ident, attrs, kind, vis, tokens: _ } = item;
1020+
self.print_nested_item_kind(*id, *span, *ident, attrs, ast::Defaultness::Final, kind, vis);
1021+
}
1022+
1023+
fn print_nested_item_kind(
1024+
&mut self,
1025+
id: ast::NodeId,
1026+
span: Span,
1027+
ident: ast::Ident,
1028+
attrs: &[Attribute],
1029+
defaultness: ast::Defaultness,
1030+
kind: &ast::AssocItemKind,
1031+
vis: &ast::Visibility,
1032+
) {
1033+
self.ann.pre(self, AnnNode::SubItem(id));
10191034
self.hardbreak_if_not_bol();
1020-
self.maybe_print_comment(item.span.lo());
1021-
self.print_outer_attributes(&item.attrs);
1022-
match &item.kind {
1035+
self.maybe_print_comment(span.lo());
1036+
self.print_outer_attributes(attrs);
1037+
self.print_defaultness(defaultness);
1038+
match kind {
10231039
ast::ForeignItemKind::Fn(sig, gen, body) => {
1024-
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
1040+
self.print_fn_full(sig, ident, gen, vis, body.as_deref(), attrs);
10251041
}
10261042
ast::ForeignItemKind::Const(ty, body) => {
1027-
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
1043+
self.print_item_const(ident, None, ty, body.as_deref(), vis);
10281044
}
10291045
ast::ForeignItemKind::Static(ty, mutbl, body) => {
1030-
self.print_item_const(item.ident, Some(*mutbl), ty, body.as_deref(), &item.vis);
1046+
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis);
10311047
}
10321048
ast::ForeignItemKind::TyAlias(generics, bounds, ty) => {
1033-
self.print_associated_type(item.ident, generics, bounds, ty.as_deref());
1049+
self.print_associated_type(ident, generics, bounds, ty.as_deref());
10341050
}
10351051
ast::ForeignItemKind::Macro(m) => {
10361052
self.print_mac(m);
@@ -1039,6 +1055,7 @@ impl<'a> State<'a> {
10391055
}
10401056
}
10411057
}
1058+
self.ann.post(self, AnnNode::SubItem(id))
10421059
}
10431060

10441061
fn print_item_const(
@@ -1438,33 +1455,8 @@ impl<'a> State<'a> {
14381455
}
14391456

14401457
crate fn print_assoc_item(&mut self, item: &ast::AssocItem) {
1441-
self.ann.pre(self, AnnNode::SubItem(item.id));
1442-
self.hardbreak_if_not_bol();
1443-
self.maybe_print_comment(item.span.lo());
1444-
self.print_outer_attributes(&item.attrs);
1445-
self.print_defaultness(item.defaultness);
1446-
match &item.kind {
1447-
ast::AssocItemKind::Static(ty, mutbl, expr) => {
1448-
self.print_item_const(item.ident, Some(*mutbl), ty, expr.as_deref(), &item.vis);
1449-
}
1450-
ast::AssocItemKind::Const(ty, expr) => {
1451-
self.print_item_const(item.ident, None, ty, expr.as_deref(), &item.vis);
1452-
}
1453-
ast::AssocItemKind::Fn(sig, generics, body) => {
1454-
let body = body.as_deref();
1455-
self.print_fn_full(sig, item.ident, generics, &item.vis, body, &item.attrs);
1456-
}
1457-
ast::AssocItemKind::TyAlias(generics, bounds, ty) => {
1458-
self.print_associated_type(item.ident, generics, bounds, ty.as_deref());
1459-
}
1460-
ast::AssocItemKind::Macro(mac) => {
1461-
self.print_mac(mac);
1462-
if mac.args.need_semicolon() {
1463-
self.s.word(";");
1464-
}
1465-
}
1466-
}
1467-
self.ann.post(self, AnnNode::SubItem(item.id))
1458+
let ast::AssocItem { id, span, ident, attrs, defaultness, kind, vis, tokens: _ } = item;
1459+
self.print_nested_item_kind(*id, *span, *ident, attrs, *defaultness, kind, vis);
14681460
}
14691461

14701462
crate fn print_stmt(&mut self, st: &ast::Stmt) {

0 commit comments

Comments
 (0)
Please sign in to comment.