Skip to content

Commit daeb4f0

Browse files
committed
Improve token stream pretty-printing some more.
Especially the handling of `::`, `:`, `#![attr]`.
1 parent cf14962 commit daeb4f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+260
-203
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+70-7
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
159159
use TokenTree::Delimited as Del;
160160
use TokenTree::Token as Tok;
161161

162-
// Each match arm has one or more examples in comments. The default is to
163-
// insert space between adjacent tokens, except for the cases listed in
164-
// this match.
162+
// Each match arm has one or more examples in comments.
165163
match (tt1, tt2) {
166164
// No space after line doc comments.
167165
(Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,
@@ -173,10 +171,24 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
173171
// ANYTHING + `,`: `foo,`
174172
// ANYTHING + `;`: `x = 3;`, `[T; 3]`
175173
// ANYTHING + `.`: `x.y`, `tup.0`
176-
(_, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) => false,
174+
// ANYTHING + `:`: `'a: loop { ... }`, `x: u8`, `where T: U`,
175+
// `<Self as T>::x`, `Trait<'a>: Sized`, `X<Y<Z>>: Send`,
176+
// `let (a, b): (u32, u32);`
177+
(_, Tok(Token { kind: Comma | Semi | Dot | Colon, .. }, _)) => false,
178+
179+
// ANYTHING-BUT-`,`|`:`|`mut`|`<` + `[`: `<expr>[1]`, `vec![]`, `#[attr]`,
180+
// `#![attr]`, but not `data: [T; 0]`, `f(a, [])`, `&mut [T]`,
181+
// `NonNull< [T] >`
182+
(Tok(Token { kind: Comma | Colon | Lt, .. }, _), Del(_, Bracket, _)) => true,
183+
(Tok(Token { kind: Ident(sym, is_raw), .. }, _), Del(_, Bracket, _))
184+
if *sym == kw::Mut && !is_raw =>
185+
{
186+
true
187+
}
188+
(Tok(_, _), Del(_, Bracket, _)) => false,
177189

178190
// IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
179-
// but `let (a, b) = (1, 2)` needs a space after the `let`
191+
// but `let (a, b) = (1, 2)` needs a space after the `let`
180192
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, Parenthesis, _))
181193
if !Ident::new(*sym, *span).is_reserved()
182194
|| *sym == kw::Fn
@@ -187,8 +199,45 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
187199
false
188200
}
189201

190-
// `#` + `[`: `#[attr]`
191-
(Tok(Token { kind: Pound, .. }, _), Del(_, Bracket, _)) => false,
202+
// IDENT|`self`|`Self`|`$crate`|`crate`|`super` + `::`: `x::y`,
203+
// `Self::a`, `$crate::x`, `crate::x`, `super::x`, but
204+
// `if ::a::b() { ... }` needs a space after the `if`.
205+
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: ModSep, .. }, _))
206+
if !Ident::new(*sym, *span).is_reserved()
207+
|| *sym == kw::SelfLower
208+
|| *sym == kw::SelfUpper
209+
|| *sym == kw::DollarCrate
210+
|| *sym == kw::Crate
211+
|| *sym == kw::Super
212+
|| *is_raw =>
213+
{
214+
false
215+
}
216+
217+
// `>` + `::`: `<S as T>::x`
218+
// `>>` + `::`: `<S as T<'a>>::x`
219+
(Tok(Token { kind: Gt | BinOp(Shr), .. }, _), Tok(Token { kind: ModSep, .. }, _)) => false,
220+
221+
// `::` + IDENT: `foo::bar`
222+
// `::` + `<`: `T::<u8>`
223+
// `::` + `*`: `use a::*`
224+
// `::` + `{`: `use a::{b, c}`
225+
(
226+
Tok(Token { kind: ModSep, .. }, _),
227+
Tok(Token { kind: Ident(..) | Lt | BinOp(Star), .. }, _) | Del(_, Brace, _),
228+
) => false,
229+
230+
// `impl` + `<`: `impl<T> Foo<T> { ... }`
231+
// `for` + `<`: `for<'a> fn()`
232+
(Tok(Token { kind: Ident(sym, is_raw), .. }, _), Tok(Token { kind: Lt, .. }, _))
233+
if (*sym == kw::Impl || *sym == kw::For) && !is_raw =>
234+
{
235+
false
236+
}
237+
238+
// `>` + `(`: `f::<u8>()`
239+
// `>>` + `(`: `collect::<Vec<_>>()`
240+
(Tok(Token { kind: Gt | BinOp(Shr), .. }, _), Del(_, Parenthesis, _)) => false,
192241

193242
// `#` + `!`: `#![attr]`
194243
(Tok(Token { kind: Pound, .. }, _), Tok(Token { kind: Not, .. }, _)) => false,
@@ -200,6 +249,20 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
200249
false
201250
}
202251

252+
// `~` + `const`: `impl ~const Clone`
253+
(Tok(Token { kind: Tilde, .. }, _), Tok(Token { kind: Ident(sym, is_raw), .. }, _))
254+
if *sym == kw::Const && !is_raw =>
255+
{
256+
false
257+
}
258+
259+
// `?` + `Sized`: `dyn ?Sized`
260+
(Tok(Token { kind: Question, .. }, _), Tok(Token { kind: Ident(sym, is_raw), .. }, _))
261+
if *sym == sym::Sized && !is_raw =>
262+
{
263+
false
264+
}
265+
203266
_ => true,
204267
}
205268
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ symbols! {
302302
Saturating,
303303
Send,
304304
SeqCst,
305+
Sized,
305306
SliceIndex,
306307
SliceIter,
307308
Some,

tests/pretty/ast-stmt-expr-attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ fn syntax() {
113113
let _ = #[attr] continue;
114114
let _ = #[attr] return;
115115
let _ = #[attr] foo!();
116-
let _ = #[attr] foo!(#! [attr]);
116+
let _ = #[attr] foo!(#![attr]);
117117
let _ = #[attr] foo![];
118-
let _ = #[attr] foo![#! [attr]];
118+
let _ = #[attr] foo![#![attr]];
119119
let _ = #[attr] foo! {};
120-
let _ = #[attr] foo! { #! [attr] };
120+
let _ = #[attr] foo! { #![attr] };
121121
let _ = #[attr] Foo { bar: baz };
122122
let _ = #[attr] Foo { ..foo };
123123
let _ = #[attr] Foo { bar: baz, ..foo };

tests/pretty/cast-lt.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
// pretty-mode:expanded
99
// pp-exact:cast-lt.pp
1010

11-
macro_rules! negative { ($e : expr) => { $e < 0 } }
11+
macro_rules! negative { ($e: expr) => { $e < 0 } }
1212

1313
fn main() { (1 as i32) < 0; }

tests/pretty/delimited-token-groups.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#![feature(rustc_attrs)]
44

5-
macro_rules! mac { ($($tt : tt) *) => () }
5+
macro_rules! mac { ($($tt: tt) *) => () }
66

77
mac! {
8-
struct S { field1 : u8, field2 : u16, } impl Clone for S
8+
struct S { field1: u8, field2: u16, } impl Clone for S
99
{
1010
fn clone() -> S
1111
{
@@ -17,8 +17,8 @@ mac! {
1717

1818
mac! {
1919
a(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
20-
aaaaaaaa aaaaaaaa) a
21-
[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
20+
aaaaaaaa aaaaaaaa)
21+
a[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
2222
aaaaaaaa aaaaaaaa] a
2323
{
2424
aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa

tests/pretty/macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#![feature(decl_macro)]
44

5-
pub(crate) macro mac { ($arg : expr) => { $arg + $arg } }
5+
pub(crate) macro mac { ($arg: expr) => { $arg + $arg } }
66

77
fn main() {}

tests/pretty/macro_rules.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ macro_rules! matcher_brackets {
1111
}
1212

1313
macro_rules! all_fragments {
14-
($b : block, $e : expr, $i : ident, $it : item, $l : lifetime, $lit :
15-
literal, $m : meta, $p : pat, $pth : path, $s : stmt, $tt : tt, $ty : ty,
16-
$vis : vis) => {};
14+
($b: block, $e: expr, $i: ident, $it: item, $l: lifetime, $lit: literal,
15+
$m: meta, $p: pat, $pth: path, $s: stmt, $tt: tt, $ty: ty, $vis: vis) =>
16+
{};
1717
}
1818

1919
fn main() {}

tests/pretty/offset_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// pp-exact
22
#![feature(offset_of)]
33

4-
fn main() { std::mem::offset_of!(std :: ops :: Range < usize >, end); }
4+
fn main() { std::mem::offset_of!(std::ops::Range < usize >, end); }
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
async fn f(mut x : u8) {}
2-
async fn g((mut x, y, mut z) : (u8, u8, u8)) {}
3-
async fn g(mut x : u8, (a, mut b, c) : (u8, u8, u8), y : u8) {}
1+
async fn f(mut x: u8) {}
2+
async fn g((mut x, y, mut z): (u8, u8, u8)) {}
3+
async fn g(mut x: u8, (a, mut b, c): (u8, u8, u8), y: u8) {}

tests/ui/hygiene/unpretty-debug.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![feature /* 0#0 */(no_core)]
99
#![no_core /* 0#0 */]
1010

11-
macro_rules! foo /* 0#0 */ { ($x : ident) => { y + $x } }
11+
macro_rules! foo /* 0#0 */ { ($x: ident) => { y + $x } }
1212

1313
fn bar /* 0#0 */() {
1414
let x /* 0#0 */ = 1;

tests/ui/infinite/issue-41731-infinite-macro-print.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ LL | stack!("overflow");
1616
= note: expanding `stack! { "overflow" }`
1717
= note: to `print! (stack! ("overflow"));`
1818
= note: expanding `print! { stack! ("overflow") }`
19-
= note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))); }`
19+
= note: to `{ $crate::io::_print($crate::format_args! (stack! ("overflow"))); }`
2020
= note: expanding `stack! { "overflow" }`
2121
= note: to `print! (stack! ("overflow"));`
2222
= note: expanding `print! { stack! ("overflow") }`
23-
= note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))); }`
23+
= note: to `{ $crate::io::_print($crate::format_args! (stack! ("overflow"))); }`
2424

2525
error: format argument must be a string literal
2626
--> $DIR/issue-41731-infinite-macro-print.rs:14:5

tests/ui/infinite/issue-41731-infinite-macro-println.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ LL | stack!("overflow");
1616
= note: expanding `stack! { "overflow" }`
1717
= note: to `println! (stack! ("overflow"));`
1818
= note: expanding `println! { stack! ("overflow") }`
19-
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))); }`
19+
= note: to `{ $crate::io::_print($crate::format_args_nl! (stack! ("overflow"))); }`
2020
= note: expanding `stack! { "overflow" }`
2121
= note: to `println! (stack! ("overflow"));`
2222
= note: expanding `println! { stack! ("overflow") }`
23-
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))); }`
23+
= note: to `{ $crate::io::_print($crate::format_args_nl! (stack! ("overflow"))); }`
2424

2525
error: format argument must be a string literal
2626
--> $DIR/issue-41731-infinite-macro-println.rs:14:5

0 commit comments

Comments
 (0)