about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2024-05-16 16:26:37 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2024-06-05 13:07:32 +1000
commitf7d49fdf4f16fe691561663468efc4fbcbdb6ba4 (patch)
tree6a4156ca063f1c535cb0f6d0fdff6404867694f6 /compiler
parenta330e49593ee890f9197727a3a558b6e6b37f843 (diff)
Print `token::Interpolated` with token stream pretty printing.
Instead of using AST pretty printing.

This is a step towards removing `token::Interpolated`, which will
eventually (in #124141) be replaced with a token stream within invisible
delimiters.

This changes (improves) the output of the `stringify!` macro in some
cases. This is allowed. As the `stringify!` docs say: "Note that the
expanded results of the input tokens may change in the future. You
should be careful if you rely on the output."

Test changes:

- tests/ui/macros/stringify.rs: this used to test both token stream
  pretty printing and AST pretty printing via different ways of invoking
  of `stringify!` (i.e. `$expr` vs `$tt`). But those two different
  invocations now give the same result, which is a nice consistency
  improvement. This removes the need for all the `c2*` macros. The AST
  pretty printer now has more thorough testing thanks to #125236.

- tests/ui/proc-macro/*: minor improvements where small differences
  between `INPUT (DISPLAY)` output and `DEEP-RE-COLLECTED (DISPLAY)`
  output disappear.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs25
1 files changed, 9 insertions, 16 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index 4c29ca0ca46..0576640fef8 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -870,18 +870,11 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
     }
 
     fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
-        match nt {
-            token::NtExpr(e) => self.expr_to_string(e),
-            token::NtMeta(e) => self.attr_item_to_string(e),
-            token::NtTy(e) => self.ty_to_string(e),
-            token::NtPath(e) => self.path_to_string(e),
-            token::NtItem(e) => self.item_to_string(e),
-            token::NtBlock(e) => self.block_to_string(e),
-            token::NtStmt(e) => self.stmt_to_string(e),
-            token::NtPat(e) => self.pat_to_string(e),
-            token::NtLiteral(e) => self.expr_to_string(e),
-            token::NtVis(e) => self.vis_to_string(e),
-        }
+        // We extract the token stream from the AST fragment and pretty print
+        // it, rather than using AST pretty printing, because `Nonterminal` is
+        // slated for removal in #124141. (This method will also then be
+        // removed.)
+        self.tts_to_string(&TokenStream::from_nonterminal_ast(nt))
     }
 
     /// Print the token kind precisely, without converting `$crate` into its respective crate name.
@@ -1015,6 +1008,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
         Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
     }
 
+    fn tts_to_string(&self, tokens: &TokenStream) -> String {
+        Self::to_string(|s| s.print_tts(tokens, false))
+    }
+
     fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
         let mut printer = State::new();
         f(&mut printer);
@@ -2060,10 +2057,6 @@ impl<'a> State<'a> {
         })
     }
 
-    pub(crate) fn tts_to_string(&self, tokens: &TokenStream) -> String {
-        Self::to_string(|s| s.print_tts(tokens, false))
-    }
-
     pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
         Self::to_string(|s| s.print_path_segment(p, false))
     }