about summary refs log tree commit diff
path: root/compiler/rustc_ast_pretty/src/pprust
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2022-05-01 20:58:24 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2022-05-11 12:43:27 +0300
commitf2b7fa484739d3f7d1303c7d42138040caaf435e (patch)
tree68782513c63135450afac341c68a6185d4ad9ec6 /compiler/rustc_ast_pretty/src/pprust
parentee6eaabdd402583ab759eb271ac69d26e06842d7 (diff)
downloadrust-f2b7fa484739d3f7d1303c7d42138040caaf435e.tar.gz
rust-f2b7fa484739d3f7d1303c7d42138040caaf435e.zip
ast: Introduce some traits to get AST node properties generically
And use them to avoid constructing some artificial `Nonterminal` tokens during expansion
Diffstat (limited to 'compiler/rustc_ast_pretty/src/pprust')
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/mod.rs32
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs8
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state/item.rs4
3 files changed, 41 insertions, 3 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/mod.rs b/compiler/rustc_ast_pretty/src/pprust/mod.rs
index ac9e7d06c4e..d2e2fd520cd 100644
--- a/compiler/rustc_ast_pretty/src/pprust/mod.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/mod.rs
@@ -4,12 +4,42 @@ mod tests;
 pub mod state;
 pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
 
-use rustc_ast as ast;
 use rustc_ast::token::{Nonterminal, Token, TokenKind};
 use rustc_ast::tokenstream::{TokenStream, TokenTree};
+use rustc_ast::{self as ast, AstDeref};
 
 use std::borrow::Cow;
 
+pub trait AstPrettyPrint {
+    fn pretty_print(&self) -> String;
+}
+
+impl<T: AstDeref<Target: AstPrettyPrint>> AstPrettyPrint for T {
+    fn pretty_print(&self) -> String {
+        self.ast_deref().pretty_print()
+    }
+}
+
+macro_rules! impl_ast_pretty_print {
+    ($($T:ty => $method:ident),+ $(,)?) => {
+        $(
+            impl AstPrettyPrint for $T {
+                fn pretty_print(&self) -> String {
+                    State::new().$method(self)
+                }
+            }
+        )+
+    };
+}
+
+impl_ast_pretty_print! {
+    ast::Item => item_to_string,
+    ast::AssocItem => assoc_item_to_string,
+    ast::ForeignItem => foreign_item_to_string,
+    ast::Expr => expr_to_string,
+    ast::Stmt => stmt_to_string,
+}
+
 pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
     State::new().nonterminal_to_string(nt)
 }
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index c02cdc29561..f520b541124 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -858,6 +858,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
         Self::to_string(|s| s.print_item(i))
     }
 
+    fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String {
+        Self::to_string(|s| s.print_assoc_item(i))
+    }
+
+    fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String {
+        Self::to_string(|s| s.print_foreign_item(i))
+    }
+
     fn generic_params_to_string(&self, generic_params: &[ast::GenericParam]) -> String {
         Self::to_string(|s| s.print_generic_params(generic_params))
     }
diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs
index 2a35dd1006e..23c5a92e352 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs
@@ -19,7 +19,7 @@ impl<'a> State<'a> {
         }
     }
 
-    fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
+    crate fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
         let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
         self.ann.pre(self, AnnNode::SubItem(id));
         self.hardbreak_if_not_bol();
@@ -496,7 +496,7 @@ impl<'a> State<'a> {
         }
     }
 
-    fn print_assoc_item(&mut self, item: &ast::AssocItem) {
+    crate fn print_assoc_item(&mut self, item: &ast::AssocItem) {
         let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
         self.ann.pre(self, AnnNode::SubItem(id));
         self.hardbreak_if_not_bol();