Skip to content

Commit d0227c6

Browse files
committed
Auto merge of #125174 - nnethercote:less-ast-pretty-printing, r=petrochenkov
Print `token::Interpolated` with token stream pretty printing. This is a step towards removing `token::Interpolated` (#124141). It unavoidably changes the output of the `stringify!` macro, generally for the better. r? `@petrochenkov`
2 parents 3ea5e23 + f7d49fd commit d0227c6

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
@@ -877,18 +877,11 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
877877
}
878878

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

894887
/// Print the token kind precisely, without converting `$crate` into its respective crate name.
@@ -1022,6 +1015,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
10221015
Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
10231016
}
10241017

1018+
fn tts_to_string(&self, tokens: &TokenStream) -> String {
1019+
Self::to_string(|s| s.print_tts(tokens, false))
1020+
}
1021+
10251022
fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
10261023
let mut printer = State::new();
10271024
f(&mut printer);
@@ -2068,10 +2065,6 @@ impl<'a> State<'a> {
20682065
})
20692066
}
20702067

2071-
pub(crate) fn tts_to_string(&self, tokens: &TokenStream) -> String {
2072-
Self::to_string(|s| s.print_tts(tokens, false))
2073-
}
2074-
20752068
pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
20762069
Self::to_string(|s| s.print_path_segment(p, false))
20772070
}

0 commit comments

Comments
 (0)