-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
Change struct expr pretty printing to match rustfmt style This PR backports trailing comma support from https://github.com/dtolnay/prettyplease into rustc_ast_pretty and uses it to improve the formatting of struct expressions. Example: ```rust macro_rules! stringify_expr { ($expr:expr) => { stringify!($expr) }; } fn main() { println!("{}", stringify_expr!(Struct { a: Struct { b, c }, })); println!("{}", stringify_expr!(Struct { aaaaaaaaaa: AAAAAAAAAA, bbbbbbbbbb: Struct { cccccccccc: CCCCCCCCCC, dddddddddd: DDDDDDDDDD, eeeeeeeeee: EEEEEEEEEE, }, })); } ``` 🤮 Before: ```console Struct{a: Struct{b, c,},} Struct{aaaaaaaaaa: AAAAAAAAAA, bbbbbbbbbb: Struct{cccccccccc: CCCCCCCCCC, dddddddddd: DDDDDDDDDD, eeeeeeeeee: EEEEEEEEEE,},} ``` After: ```console Struct { a: Struct { b, c } } Struct { aaaaaaaaaa: AAAAAAAAAA, bbbbbbbbbb: Struct { cccccccccc: CCCCCCCCCC, dddddddddd: DDDDDDDDDD, eeeeeeeeee: EEEEEEEEEE, }, } ```
- Loading branch information
Showing
11 changed files
with
148 additions
and
70 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::iter::Peekable; | ||
use std::mem; | ||
use std::ops::Deref; | ||
|
||
pub struct Delimited<I: Iterator> { | ||
is_first: bool, | ||
iter: Peekable<I>, | ||
} | ||
|
||
pub trait IterDelimited: Iterator + Sized { | ||
fn delimited(self) -> Delimited<Self> { | ||
Delimited { is_first: true, iter: self.peekable() } | ||
} | ||
} | ||
|
||
impl<I: Iterator> IterDelimited for I {} | ||
|
||
pub struct IteratorItem<T> { | ||
value: T, | ||
pub is_first: bool, | ||
pub is_last: bool, | ||
} | ||
|
||
impl<I: Iterator> Iterator for Delimited<I> { | ||
type Item = IteratorItem<I::Item>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let value = self.iter.next()?; | ||
let is_first = mem::replace(&mut self.is_first, false); | ||
let is_last = self.iter.peek().is_none(); | ||
Some(IteratorItem { value, is_first, is_last }) | ||
} | ||
} | ||
|
||
impl<T> Deref for IteratorItem<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.value | ||
} | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr
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