Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve print_tts by making space_between smarter #117433

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![deny(rustc::diagnostic_outside_of_impl)]
#![feature(associated_type_bounds)]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(let_chains)]
#![feature(with_negative_coherence)]
#![recursion_limit = "256"]

Expand Down
176 changes: 144 additions & 32 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,37 +146,144 @@ pub fn print_crate<'a>(
s.s.eof()
}

/// This makes printed token streams look slightly nicer,
/// and also addresses some specific regressions described in #63896 and #73345.
fn space_between(prev: &TokenTree, curr: &TokenTree) -> bool {
if let TokenTree::Token(token, _) = prev {
// No space after these tokens, e.g. `x.y`, `$e`
// (The carets point to `prev`.) ^ ^
if matches!(token.kind, token::Dot | token::Dollar) {
return false;
}
if let token::DocComment(comment_kind, ..) = token.kind {
return comment_kind != CommentKind::Line;
}
}
match curr {
// No space before these tokens, e.g. `foo,`, `println!`, `x.y`
// (The carets point to `curr`.) ^ ^ ^
//
// FIXME: having `Not` here works well for macro invocations like
// `println!()`, but is bad when `!` means "logical not" or "the never
// type", where the lack of space causes ugliness like this:
// `Fn() ->!`, `x =! y`, `if! x { f(); }`.
TokenTree::Token(token, _) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
// No space before parentheses if preceded by these tokens, e.g. `foo(...)`
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }, _))
}
// No space before brackets if preceded by these tokens, e.g. `#[...]`
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }, _))
}
TokenTree::Delimited(..) => true,
fn is_punct(tt: &TokenTree) -> bool {
matches!(tt, TokenTree::Token(tok, _) if tok.is_punct())
}

/// Should two consecutive token trees be printed with a space between them?
///
/// NOTE: should always be false if both token trees are punctuation, so that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function should return anything in this case, it should rather have assert!(!is_punct(tt1) || !is_punct(tt2)) at the start instead.

The decision should be made based on Spacing in that case, and we should never reach this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or the Spacing-based decision can be made inside this function, then it will be if is_punct(tt1) && is_punct(tt2) { ... } at the start instead of the assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid making any pretty-printing decisions based on Spacing in this PR. We can leave those to #114571, which will change how space_between is called. I plan to add the Spacing::Unknown in that PR, for tokens coming from proc macros. Those will be the cases where space_between is used.

With that decided, the current position of the assertion has the advantage that it's only checked in the case where space_between returns false.

So I think this is good enough to merge, or do a crater run if you think that is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.
Crater run is needed in any case.

/// any old proc macro that parses pretty-printed code won't glue together
/// tokens that shouldn't be glued.
///
/// Note: some old proc macros parse pretty-printed output, so changes here can
/// break old code. For example:
/// - #63896: `#[allow(unused,` must be printed rather than `#[allow(unused ,`
/// - #73345: `#[allow(unused)] must be printed rather than `# [allow(unused)]
///
fn space_between(prev: Option<&TokenTree>, tt1: &TokenTree, tt2: &TokenTree) -> bool {
use token::*;
use Delimiter::*;
use TokenTree::Delimited as Del;
use TokenTree::Token as Tok;

// Each match arm has one or more examples in comments.
match (tt1, tt2) {
// No space after line doc comments.
(Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,

// `.` + NON-PUNCT: `x.y`, `tup.0`
// `$` + NON-PUNCT: `$e`
(Tok(Token { kind: Dot | Dollar, .. }, _), tt2) if !is_punct(tt2) => false,

// NON-PUNCT + `,`: `foo,`
// NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
// NON-PUNCT + `.`: `x.y`, `tup.0`
// NON-PUNCT + `:`: `'a: loop { ... }`, `x: u8`, `where T: U`,
// `<Self as T>::x`, `Trait<'a>: Sized`, `X<Y<Z>>: Send`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples still involve punctuation and need an update.

// `let (a, b): (u32, u32);`
(tt1, Tok(Token { kind: Comma | Semi | Dot | Colon, .. }, _)) if !is_punct(tt1) => false,

// ANYTHING-BUT-`,`|`:`|`mut`|`<` + `[`: `<expr>[1]`, `vec![]`, `#[attr]`,
// `#![attr]`, but not `data: [T; 0]`, `f(a, [])`, `&mut [T]`,
// `NonNull< [T] >`
(Tok(Token { kind: Comma | Colon | Lt, .. }, _), Del(_, Bracket, _)) => true,
(Tok(Token { kind: Ident(sym, is_raw), .. }, _), Del(_, Bracket, _))
if *sym == kw::Mut && !is_raw =>
{
true
}
(Tok(_, _), Del(_, Bracket, _)) => false,

// IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
// but `let (a, b) = (1, 2)` needs a space after the `let`
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, Parenthesis, _))
if !Ident::new(*sym, *span).is_reserved()
|| *sym == kw::Fn
|| *sym == kw::SelfUpper
|| *sym == kw::Pub
|| *is_raw =>
{
false
}

// IDENT|`self`|`Self`|`$crate`|`crate`|`super` + `::`: `x::y`,
// `Self::a`, `$crate::x`, `crate::x`, `super::x`, but
// `if ::a::b() { ... }` needs a space after the `if`.
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: ModSep, .. }, _))
if !Ident::new(*sym, *span).is_reserved()
|| sym.is_path_segment_keyword()
|| *is_raw =>
{
false
}

// `::` + IDENT: `foo::bar`
// `::` + `{`: `use a::{b, c}`
(
Tok(Token { kind: ModSep, .. }, _),
Tok(Token { kind: Ident(..), .. }, _) | Del(_, Brace, _),
) => false,

// `impl` + `<`: `impl<T> Foo<T> { ... }`
// `for` + `<`: `for<'a> fn()`
(Tok(Token { kind: Ident(sym, is_raw), .. }, _), Tok(Token { kind: Lt, .. }, _))
if (*sym == kw::Impl || *sym == kw::For) && !is_raw =>
{
false
}

// `fn` + IDENT + `<`: `fn f<T>(t: T) { ... }`
(Tok(Token { kind: Ident(..), .. }, _), Tok(Token { kind: Lt, .. }, _))
if let Some(prev) = prev
&& let Tok(Token { kind: Ident(sym, is_raw), .. }, _) = prev
&& *sym == kw::Fn
&& !is_raw =>
{
false
}

// `>` + `(`: `f::<u8>()`
// `>>` + `(`: `collect::<Vec<_>>()`
(Tok(Token { kind: Gt | BinOp(Shr), .. }, _), Del(_, Parenthesis, _)) => false,

// IDENT + `!`: `println!()`, but `if !x { ... }` needs a space after the `if`
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Not, .. }, _))
if !Ident::new(*sym, *span).is_reserved() || *is_raw =>
{
false
}

// ANYTHING-BUT-`macro_rules` + `!` + NON-PUNCT-OR-BRACE: `foo!()`, `vec![]`,
// `if !cond { ... }`, but not `macro_rules! m { ... }`
(Tok(Token { kind: Not, .. }, _), tt2) if is_punct(tt2) => true,
(Tok(Token { kind: Not, .. }, _), Del(_, Brace, _)) => true,
(Tok(Token { kind: Not, .. }, _), _) =>
if let Some(prev) = prev
&& let Tok(Token { kind: Ident(sym, is_raw), .. }, _) = prev
&& *sym == sym::macro_rules
&& !is_raw
{
true
} else {
false
}

// `~` + `const`: `impl ~const Clone`
(Tok(Token { kind: Tilde, .. }, _), Tok(Token { kind: Ident(sym, is_raw), .. }, _))
if *sym == kw::Const && !is_raw =>
{
false
}

// `?` + `Sized`: `dyn ?Sized`
(Tok(Token { kind: Question, .. }, _), Tok(Token { kind: Ident(sym, is_raw), .. }, _))
if *sym == sym::Sized && !is_raw =>
{
false
}

_ => true,
}
}

Expand Down Expand Up @@ -571,14 +678,19 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}

fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
let mut prev = None;
let mut iter = tts.trees().peekable();
while let Some(tt) = iter.next() {
self.print_tt(tt, convert_dollar_crate);
if let Some(next) = iter.peek() {
if space_between(tt, next) {
if space_between(prev, tt, next) {
self.space();
} else {
// There must be a space between two punctuation tokens.
assert!(!is_punct(tt) || !is_punct(next));
}
}
prev = Some(tt);
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ symbols! {
Saturating,
Send,
SeqCst,
Sized,
SliceIndex,
SliceIter,
Some,
Expand Down
6 changes: 3 additions & 3 deletions tests/pretty/ast-stmt-expr-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ fn syntax() {
let _ = #[attr] continue;
let _ = #[attr] return;
let _ = #[attr] foo!();
let _ = #[attr] foo!(#! [attr]);
let _ = #[attr] foo!(# ![attr]);
let _ = #[attr] foo![];
let _ = #[attr] foo![#! [attr]];
let _ = #[attr] foo![# ![attr]];
let _ = #[attr] foo! {};
let _ = #[attr] foo! { #! [attr] };
let _ = #[attr] foo! { # ![attr] };
let _ = #[attr] Foo { bar: baz };
let _ = #[attr] Foo { ..foo };
let _ = #[attr] Foo { bar: baz, ..foo };
Expand Down
2 changes: 1 addition & 1 deletion tests/pretty/cast-lt.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// pretty-mode:expanded
// pp-exact:cast-lt.pp

macro_rules! negative { ($e : expr) => { $e < 0 } }
macro_rules! negative { ($e: expr) => { $e < 0 } }

fn main() { (1 as i32) < 0; }
10 changes: 5 additions & 5 deletions tests/pretty/delimited-token-groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

#![feature(rustc_attrs)]

macro_rules! mac { ($($tt : tt) *) => () }
macro_rules! mac { ($($tt: tt) *) => () }

mac! {
struct S { field1 : u8, field2 : u16, } impl Clone for S
struct S { field1: u8, field2: u16, } impl Clone for S
{
fn clone() -> S
{
panic! () ;
panic!();

}
}
}

mac! {
a(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa) a
[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa)
a[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa] a
{
aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
Expand Down
2 changes: 1 addition & 1 deletion tests/pretty/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

#![feature(decl_macro)]

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

fn main() {}
14 changes: 7 additions & 7 deletions tests/pretty/macro_rules.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// pp-exact

macro_rules! brace { () => {} ; }
macro_rules! brace { () => {}; }

macro_rules! bracket[() => {} ;];
macro_rules! bracket[() => {};];

macro_rules! paren(() => {} ;);
macro_rules! paren(() => {};);

macro_rules! matcher_brackets {
(paren) => {} ; (bracket) => {} ; (brace) => {} ;
(paren) => {}; (bracket) => {}; (brace) => {};
}

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

fn main() {}
2 changes: 1 addition & 1 deletion tests/pretty/offset_of.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// pp-exact
#![feature(offset_of)]

fn main() { std::mem::offset_of!(std :: ops :: Range < usize >, end); }
fn main() { std::mem::offset_of!(std::ops::Range < usize > , end); }
2 changes: 1 addition & 1 deletion tests/pretty/stmt_expr_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn _8() {
}

fn _9() {
macro_rules! stmt_mac { () => { let _ = () ; } }
macro_rules! stmt_mac { () => { let _ = (); } }

#[rustc_dummy]
stmt_mac!();
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/rustc-macro-dep-files/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use proc_macro::TokenStream;
#[proc_macro_derive(A)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert!(input.contains("struct A ;"));
assert!(input.contains("struct A;"));
"struct B;".parse().unwrap()
}
6 changes: 3 additions & 3 deletions tests/ui/async-await/issues/issue-60674.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
async fn f(mut x : u8) {}
async fn g((mut x, y, mut z) : (u8, u8, u8)) {}
async fn g(mut x : u8, (a, mut b, c) : (u8, u8, u8), y : u8) {}
async fn f(mut x: u8) {}
async fn g((mut x, y, mut z): (u8, u8, u8)) {}
async fn g(mut x: u8, (a, mut b, c): (u8, u8, u8), y: u8) {}
2 changes: 1 addition & 1 deletion tests/ui/hygiene/unpretty-debug.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![feature /* 0#0 */(no_core)]
#![no_core /* 0#0 */]

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

fn bar /* 0#0 */() {
let x /* 0#0 */ = 1;
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/infinite/issue-41731-infinite-macro-print.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ LL | stack!("overflow");
| ^^^^^^^^^^^^^^^^^^
|
= note: expanding `stack! { "overflow" }`
= note: to `print! (stack! ("overflow")) ;`
= note: expanding `print! { stack! ("overflow") }`
= note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))) ; }`
= note: to `print!(stack!("overflow"));`
= note: expanding `print! { stack!("overflow") }`
= note: to `{ $crate::io::_print($crate::format_args!(stack!("overflow"))); }`
= note: expanding `stack! { "overflow" }`
= note: to `print! (stack! ("overflow")) ;`
= note: expanding `print! { stack! ("overflow") }`
= note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))) ; }`
= note: to `print!(stack!("overflow"));`
= note: expanding `print! { stack!("overflow") }`
= note: to `{ $crate::io::_print($crate::format_args!(stack!("overflow"))); }`

error: format argument must be a string literal
--> $DIR/issue-41731-infinite-macro-print.rs:14:5
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/infinite/issue-41731-infinite-macro-println.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ LL | stack!("overflow");
| ^^^^^^^^^^^^^^^^^^
|
= note: expanding `stack! { "overflow" }`
= note: to `println! (stack! ("overflow")) ;`
= note: expanding `println! { stack! ("overflow") }`
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))) ; }`
= note: to `println!(stack!("overflow"));`
= note: expanding `println! { stack!("overflow") }`
= note: to `{ $crate::io::_print($crate::format_args_nl!(stack!("overflow"))); }`
= note: expanding `stack! { "overflow" }`
= note: to `println! (stack! ("overflow")) ;`
= note: expanding `println! { stack! ("overflow") }`
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))) ; }`
= note: to `println!(stack!("overflow"));`
= note: expanding `println! { stack!("overflow") }`
= note: to `{ $crate::io::_print($crate::format_args_nl!(stack!("overflow"))); }`

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