Skip to content

Commit f7d49fd

Browse files
committed
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.
1 parent a330e49 commit f7d49fd

File tree

7 files changed

+75
-246
lines changed

7 files changed

+75
-246
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -870,18 +870,11 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
870870
}
871871

872872
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
873-
match nt {
874-
token::NtExpr(e) => self.expr_to_string(e),
875-
token::NtMeta(e) => self.attr_item_to_string(e),
876-
token::NtTy(e) => self.ty_to_string(e),
877-
token::NtPath(e) => self.path_to_string(e),
878-
token::NtItem(e) => self.item_to_string(e),
879-
token::NtBlock(e) => self.block_to_string(e),
880-
token::NtStmt(e) => self.stmt_to_string(e),
881-
token::NtPat(e) => self.pat_to_string(e),
882-
token::NtLiteral(e) => self.expr_to_string(e),
883-
token::NtVis(e) => self.vis_to_string(e),
884-
}
873+
// We extract the token stream from the AST fragment and pretty print
874+
// it, rather than using AST pretty printing, because `Nonterminal` is
875+
// slated for removal in #124141. (This method will also then be
876+
// removed.)
877+
self.tts_to_string(&TokenStream::from_nonterminal_ast(nt))
885878
}
886879

887880
/// 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
10151008
Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
10161009
}
10171010

1011+
fn tts_to_string(&self, tokens: &TokenStream) -> String {
1012+
Self::to_string(|s| s.print_tts(tokens, false))
1013+
}
1014+
10181015
fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
10191016
let mut printer = State::new();
10201017
f(&mut printer);
@@ -2060,10 +2057,6 @@ impl<'a> State<'a> {
20602057
})
20612058
}
20622059

2063-
pub(crate) fn tts_to_string(&self, tokens: &TokenStream) -> String {
2064-
Self::to_string(|s| s.print_tts(tokens, false))
2065-
}
2066-
20672060
pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
20682061
Self::to_string(|s| s.print_path_segment(p, false))
20692062
}

0 commit comments

Comments
 (0)