Skip to content

Commit d0227c6

Browse files
committedJun 11, 2024
Auto merge of rust-lang#125174 - nnethercote:less-ast-pretty-printing, r=petrochenkov
Print `token::Interpolated` with token stream pretty printing. This is a step towards removing `token::Interpolated` (rust-lang#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
}

‎tests/ui/macros/stringify.rs

+52-207
Large diffs are not rendered by default.

‎tests/ui/macros/trace_faulty_macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! test {
4646
(let $p:pat = $e:expr) => {test!(($p,$e))};
4747
// this should be expr
4848
// vvv
49-
(($p:pat, $e:pat)) => {let $p = $e;}; //~ ERROR expected expression, found pattern `1 + 1`
49+
(($p:pat, $e:pat)) => {let $p = $e;}; //~ ERROR expected expression, found pattern `1+1`
5050
}
5151

5252
fn foo() {

‎tests/ui/macros/trace_faulty_macros.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LL | my_recursive_macro!();
5050
= note: expanding `my_recursive_macro! { }`
5151
= note: to `my_recursive_macro! ();`
5252

53-
error: expected expression, found pattern `A { a: a, b: 0, c: _, .. }`
53+
error: expected expression, found pattern `A { a : a, b : 0, c : _, .. }`
5454
--> $DIR/trace_faulty_macros.rs:16:9
5555
|
5656
LL | $a
@@ -69,7 +69,7 @@ LL | #[derive(Debug)]
6969
LL | fn use_derive_macro_as_attr() {}
7070
| -------------------------------- not a `struct`, `enum` or `union`
7171

72-
error: expected expression, found pattern `1 + 1`
72+
error: expected expression, found pattern `1+1`
7373
--> $DIR/trace_faulty_macros.rs:49:37
7474
|
7575
LL | (let $p:pat = $e:expr) => {test!(($p,$e))};
@@ -96,7 +96,7 @@ LL | let a = pat_macro!();
9696
= note: expanding `pat_macro! { }`
9797
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. });`
9898
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
99-
= note: to `A { a: a, b: 0, c: _, .. }`
99+
= note: to `A { a : a, b : 0, c : _, .. }`
100100

101101
note: trace_macro
102102
--> $DIR/trace_faulty_macros.rs:53:5
@@ -105,9 +105,9 @@ LL | test!(let x = 1+1);
105105
| ^^^^^^^^^^^^^^^^^^
106106
|
107107
= note: expanding `test! { let x = 1+1 }`
108-
= note: to `test! ((x, 1 + 1))`
109-
= note: expanding `test! { (x, 1 + 1) }`
110-
= note: to `let x = 1 + 1;`
108+
= note: to `test! ((x, 1+1))`
109+
= note: expanding `test! { (x, 1+1) }`
110+
= note: to `let x = 1+1;`
111111

112112
error: aborting due to 5 previous errors
113113

‎tests/ui/proc-macro/capture-macro-rules-invoke.stdout

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
1111
span: $DIR/capture-macro-rules-invoke.rs:21:21: 21:26 (#3),
1212
},
1313
]
14-
PRINT-BANG INPUT (DISPLAY): 1 + 1, { "a" }, let a = 1;, String, my_name, 'a, my_val = 30,
15-
std::option::Option, pub(in some::path) , [a b c], -30
16-
PRINT-BANG RE-COLLECTED (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
14+
PRINT-BANG INPUT (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
1715
std::option::Option, pub(in some::path), [a b c], -30
1816
PRINT-BANG DEEP-RE-COLLECTED (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
1917
std :: option :: Option, pub(in some :: path), [a b c], - 30

‎tests/ui/proc-macro/expr-stmt-nonterminal-tokens.stdout

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = #[allow(warnings)] 0; 0 }, }
2-
PRINT-DERIVE DEEP-RE-COLLECTED (DISPLAY): enum E { V = { let _ = #[allow(warnings)] #[allow(warnings)] 0; 0 }, }
1+
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = #[allow(warnings)] #[allow(warnings)] 0; 0 }, }
32
PRINT-DERIVE INPUT (DEBUG): TokenStream [
43
Ident {
54
ident: "enum",
@@ -122,8 +121,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
122121
span: #3 bytes(308..357),
123122
},
124123
]
125-
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { 0; }; 0 }, }
126-
PRINT-DERIVE DEEP-RE-COLLECTED (DISPLAY): enum E { V = { let _ = { 0 }; 0 }, }
124+
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { 0 }; 0 }, }
127125
PRINT-DERIVE INPUT (DEBUG): TokenStream [
128126
Ident {
129127
ident: "enum",
@@ -280,8 +278,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
280278
span: #11 bytes(432..485),
281279
},
282280
]
283-
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { PATH; }; 0 }, }
284-
PRINT-DERIVE DEEP-RE-COLLECTED (DISPLAY): enum E { V = { let _ = { PATH }; 0 }, }
281+
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { PATH }; 0 }, }
285282
PRINT-DERIVE INPUT (DEBUG): TokenStream [
286283
Ident {
287284
ident: "enum",
@@ -358,8 +355,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
358355
span: #15 bytes(432..485),
359356
},
360357
]
361-
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { 0 + 1; }; 0 }, }
362-
PRINT-DERIVE DEEP-RE-COLLECTED (DISPLAY): enum E { V = { let _ = { 0 + 1 }; 0 }, }
358+
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { 0 + 1 }; 0 }, }
363359
PRINT-DERIVE INPUT (DEBUG): TokenStream [
364360
Ident {
365361
ident: "enum",
@@ -449,8 +445,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
449445
span: #19 bytes(432..485),
450446
},
451447
]
452-
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { PATH + 1; }; 0 }, }
453-
PRINT-DERIVE DEEP-RE-COLLECTED (DISPLAY): enum E { V = { let _ = { PATH + 1 }; 0 }, }
448+
PRINT-DERIVE INPUT (DISPLAY): enum E { V = { let _ = { PATH + 1 }; 0 }, }
454449
PRINT-DERIVE INPUT (DEBUG): TokenStream [
455450
Ident {
456451
ident: "enum",

‎tests/ui/proc-macro/issue-78675-captured-inner-attrs.stdout

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): foo! { #[fake_attr] mod bar {
2-
#![doc = r" Foo"]
3-
} }
1+
PRINT-BANG INPUT (DISPLAY): foo! { #[fake_attr] mod bar { #![doc = r" Foo"] } }
42
PRINT-BANG DEEP-RE-COLLECTED (DISPLAY): foo! { #[fake_attr] mod bar { #! [doc = r" Foo"] } }
53
PRINT-BANG INPUT (DEBUG): TokenStream [
64
Ident {

0 commit comments

Comments
 (0)
Please sign in to comment.