Skip to content

Commit e16b52d

Browse files
committed
Streamline PrintState.
`PrintState` is a trait containing code that can be used by both AST and HIR pretty-printing. But several of its methods are only used by AST printing. This commit moves those methods out of the trait and into the AST `State` impl, so they are not exposed unnecessarily. This commit also removes four unused methods: `param_to_string`, `foreign_item_to_string`, `assoc_item_to_string`, and `print_inner_attributes_inline`.
1 parent c6a9027 commit e16b52d

File tree

1 file changed

+83
-99
lines changed
  • compiler/rustc_ast_pretty/src/pprust

1 file changed

+83
-99
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+83-99
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
397397
}
398398
}
399399

400-
fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
401-
self.print_token_literal(lit.as_token_lit(), lit.span)
402-
}
403-
404-
fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
405-
self.maybe_print_comment(span.lo());
406-
self.word(token_lit.to_string())
407-
}
408-
409400
fn print_string(&mut self, st: &str, style: ast::StrStyle) {
410401
let st = match style {
411402
ast::StrStyle::Cooked => format!("\"{}\"", st.escape_debug()),
@@ -416,30 +407,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
416407
self.word(st)
417408
}
418409

419-
fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
420-
self.print_string(sym.as_str(), style);
421-
}
422-
423410
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
424411
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
425412
}
426413

427-
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
428-
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
429-
}
430-
431414
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
432415
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
433416
}
434417

435-
fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
436-
self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
437-
}
438-
439-
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
440-
self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
441-
}
442-
443418
fn print_either_attributes(
444419
&mut self,
445420
attrs: &[ast::Attribute],
@@ -463,10 +438,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
463438
printed
464439
}
465440

466-
fn print_attribute(&mut self, attr: &ast::Attribute) {
467-
self.print_attribute_inline(attr, false)
468-
}
469-
470441
fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) {
471442
if !is_inline {
472443
self.hardbreak_if_not_bol();
@@ -521,33 +492,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
521492
self.end();
522493
}
523494

524-
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
525-
match item {
526-
ast::NestedMetaItem::MetaItem(mi) => self.print_meta_item(mi),
527-
ast::NestedMetaItem::Lit(lit) => self.print_meta_item_lit(lit),
528-
}
529-
}
530-
531-
fn print_meta_item(&mut self, item: &ast::MetaItem) {
532-
self.ibox(INDENT_UNIT);
533-
match &item.kind {
534-
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
535-
ast::MetaItemKind::NameValue(value) => {
536-
self.print_path(&item.path, false, 0);
537-
self.space();
538-
self.word_space("=");
539-
self.print_meta_item_lit(value);
540-
}
541-
ast::MetaItemKind::List(items) => {
542-
self.print_path(&item.path, false, 0);
543-
self.popen();
544-
self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
545-
self.pclose();
546-
}
547-
}
548-
self.end();
549-
}
550-
551495
/// This doesn't deserve to be called "pretty" printing, but it should be
552496
/// meaning-preserving. A quick hack that might help would be to look at the
553497
/// spans embedded in the TTs to decide where to put spaces and newlines.
@@ -839,17 +783,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
839783
Self::to_string(|s| s.print_type(ty))
840784
}
841785

842-
fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
843-
Self::to_string(|s| s.print_type_bounds(bounds))
844-
}
845-
846-
fn where_bound_predicate_to_string(
847-
&self,
848-
where_bound_predicate: &ast::WhereBoundPredicate,
849-
) -> String {
850-
Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
851-
}
852-
853786
fn pat_to_string(&self, pat: &ast::Pat) -> String {
854787
Self::to_string(|s| s.print_pat(pat))
855788
}
@@ -862,14 +795,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
862795
Self::to_string(|s| s.print_meta_item_lit(lit))
863796
}
864797

865-
fn tt_to_string(&self, tt: &TokenTree) -> String {
866-
Self::to_string(|s| s.print_tt(tt, false))
867-
}
868-
869-
fn tts_to_string(&self, tokens: &TokenStream) -> String {
870-
Self::to_string(|s| s.print_tts(tokens, false))
871-
}
872-
873798
fn stmt_to_string(&self, stmt: &ast::Stmt) -> String {
874799
Self::to_string(|s| s.print_stmt(stmt))
875800
}
@@ -878,22 +803,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
878803
Self::to_string(|s| s.print_item(i))
879804
}
880805

881-
fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String {
882-
Self::to_string(|s| s.print_assoc_item(i))
883-
}
884-
885-
fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String {
886-
Self::to_string(|s| s.print_foreign_item(i))
887-
}
888-
889806
fn path_to_string(&self, p: &ast::Path) -> String {
890807
Self::to_string(|s| s.print_path(p, false, 0))
891808
}
892809

893-
fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
894-
Self::to_string(|s| s.print_path_segment(p, false))
895-
}
896-
897810
fn vis_to_string(&self, v: &ast::Visibility) -> String {
898811
Self::to_string(|s| s.print_visibility(v))
899812
}
@@ -908,22 +821,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
908821
})
909822
}
910823

911-
fn meta_list_item_to_string(&self, li: &ast::NestedMetaItem) -> String {
912-
Self::to_string(|s| s.print_meta_list_item(li))
913-
}
914-
915824
fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
916825
Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
917826
}
918827

919-
fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
920-
Self::to_string(|s| s.print_attribute(attr))
921-
}
922-
923-
fn param_to_string(&self, arg: &ast::Param) -> String {
924-
Self::to_string(|s| s.print_param(arg, false))
925-
}
926-
927828
fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
928829
let mut printer = State::new();
929830
f(&mut printer);
@@ -1812,4 +1713,87 @@ impl<'a> State<'a> {
18121713
ast::IsAuto::No => {}
18131714
}
18141715
}
1716+
1717+
fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
1718+
self.print_token_literal(lit.as_token_lit(), lit.span)
1719+
}
1720+
1721+
fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
1722+
self.maybe_print_comment(span.lo());
1723+
self.word(token_lit.to_string())
1724+
}
1725+
1726+
fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
1727+
self.print_string(sym.as_str(), style);
1728+
}
1729+
1730+
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
1731+
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
1732+
}
1733+
1734+
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
1735+
self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
1736+
}
1737+
1738+
fn print_attribute(&mut self, attr: &ast::Attribute) {
1739+
self.print_attribute_inline(attr, false)
1740+
}
1741+
1742+
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
1743+
match item {
1744+
ast::NestedMetaItem::MetaItem(mi) => self.print_meta_item(mi),
1745+
ast::NestedMetaItem::Lit(lit) => self.print_meta_item_lit(lit),
1746+
}
1747+
}
1748+
1749+
fn print_meta_item(&mut self, item: &ast::MetaItem) {
1750+
self.ibox(INDENT_UNIT);
1751+
match &item.kind {
1752+
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
1753+
ast::MetaItemKind::NameValue(value) => {
1754+
self.print_path(&item.path, false, 0);
1755+
self.space();
1756+
self.word_space("=");
1757+
self.print_meta_item_lit(value);
1758+
}
1759+
ast::MetaItemKind::List(items) => {
1760+
self.print_path(&item.path, false, 0);
1761+
self.popen();
1762+
self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
1763+
self.pclose();
1764+
}
1765+
}
1766+
self.end();
1767+
}
1768+
1769+
pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
1770+
Self::to_string(|s| s.print_type_bounds(bounds))
1771+
}
1772+
1773+
pub(crate) fn where_bound_predicate_to_string(
1774+
&self,
1775+
where_bound_predicate: &ast::WhereBoundPredicate,
1776+
) -> String {
1777+
Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
1778+
}
1779+
1780+
pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
1781+
Self::to_string(|s| s.print_tt(tt, false))
1782+
}
1783+
1784+
pub(crate) fn tts_to_string(&self, tokens: &TokenStream) -> String {
1785+
Self::to_string(|s| s.print_tts(tokens, false))
1786+
}
1787+
1788+
pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
1789+
Self::to_string(|s| s.print_path_segment(p, false))
1790+
}
1791+
1792+
pub(crate) fn meta_list_item_to_string(&self, li: &ast::NestedMetaItem) -> String {
1793+
Self::to_string(|s| s.print_meta_list_item(li))
1794+
}
1795+
1796+
pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
1797+
Self::to_string(|s| s.print_attribute(attr))
1798+
}
18151799
}

0 commit comments

Comments
 (0)