-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
/// 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`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,6 +302,7 @@ symbols! { | |
Saturating, | ||
Send, | ||
SeqCst, | ||
Sized, | ||
SliceIndex, | ||
SliceIter, | ||
Some, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 beif is_punct(tt1) && is_punct(tt2) { ... }
at the start instead of the assert.There was a problem hiding this comment.
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 howspace_between
is called. I plan to add theSpacing::Unknown
in that PR, for tokens coming from proc macros. Those will be the cases wherespace_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.
There was a problem hiding this comment.
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.