Skip to content

Commit

Permalink
Rollup merge of rust-lang#93759 - dtolnay:usetree, r=nagisa
Browse files Browse the repository at this point in the history
Pretty print ItemKind::Use in rustfmt style

This PR backports the formatting for `use` items from https://github.com/dtolnay/prettyplease into rustc_ast_pretty.

Before:

```rust
use core::{cmp::{Eq, Ord, PartialEq, PartialOrd},
    convert::{AsMut, AsRef, From, Into},
    iter::{DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator,
    IntoIterator, Iterator},
    marker::{Copy as Copy, Send as Send, Sized as Sized, Sync as Sync, Unpin
    as U}, ops::{*, Drop, Fn, FnMut, FnOnce}};
```

After:

```rust
use core::{
    cmp::{Eq, Ord, PartialEq, PartialOrd},
    convert::{AsMut, AsRef, From, Into},
    iter::{
        DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator,
        IntoIterator, Iterator,
    },
    marker::{
        Copy as Copy, Send as Send, Sized as Sized, Sync as Sync, Unpin as U,
    },
    ops::{*, Drop, Fn, FnMut, FnOnce},
};
```
  • Loading branch information
matthiaskrgr authored Feb 12, 2022
2 parents 36461e0 + b64a822 commit f30f6de
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_ast_pretty/src/pp/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl Printer {
}

pub fn trailing_comma(&mut self) {
self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() });
}

pub fn trailing_comma_or_space(&mut self) {
self.scan_break(BreakToken {
blank_space: 1,
pre_break: Some(','),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'a> State<'a> {
if !field.is_last || has_rest {
self.word_space(",");
} else {
self.trailing_comma();
self.trailing_comma_or_space();
}
}
if has_rest {
Expand Down
48 changes: 34 additions & 14 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, PrintState, State};
use crate::pprust::state::delimited::IterDelimited;
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};

use rustc_ast as ast;
use rustc_ast::GenericBound;
Expand Down Expand Up @@ -138,11 +139,10 @@ impl<'a> State<'a> {
self.end(); // end outer head-block
}
ast::ItemKind::Use(ref tree) => {
self.head(visibility_qualified(&item.vis, "use"));
self.print_visibility(&item.vis);
self.word_nbsp("use");
self.print_use_tree(tree);
self.word(";");
self.end(); // end inner head-block
self.end(); // end outer head-block
}
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
let def = ast::Defaultness::Final;
Expand Down Expand Up @@ -615,8 +615,8 @@ impl<'a> State<'a> {
ast::UseTreeKind::Simple(rename, ..) => {
self.print_path(&tree.prefix, false, 0);
if let Some(rename) = rename {
self.space();
self.word_space("as");
self.nbsp();
self.word_nbsp("as");
self.print_ident(rename);
}
}
Expand All @@ -628,16 +628,36 @@ impl<'a> State<'a> {
self.word("*");
}
ast::UseTreeKind::Nested(ref items) => {
if tree.prefix.segments.is_empty() {
self.word("{");
} else {
if !tree.prefix.segments.is_empty() {
self.print_path(&tree.prefix, false, 0);
self.word("::{");
self.word("::");
}
if items.is_empty() {
self.word("{}");
} else if items.len() == 1 {
self.print_use_tree(&items[0].0);
} else {
self.cbox(INDENT_UNIT);
self.word("{");
self.zerobreak();
self.ibox(0);
for use_tree in items.iter().delimited() {
self.print_use_tree(&use_tree.0);
if !use_tree.is_last {
self.word(",");
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
self.hardbreak();
} else {
self.space();
}
}
}
self.end();
self.trailing_comma();
self.offset(-INDENT_UNIT);
self.word("}");
self.end();
}
self.commasep(Inconsistent, &items, |this, &(ref tree, _)| {
this.print_use_tree(tree)
});
self.word("}");
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/pretty/use-tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// pp-exact
// edition:2021

#![allow(unused_imports)]

use ::std::fmt::{self, Debug, Display, Write as _};

use core::option::Option::*;

use core::{
cmp::{Eq, Ord, PartialEq, PartialOrd},
convert::{AsMut, AsRef, From, Into},
iter::{
DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator,
IntoIterator, Iterator,
},
marker::{
Copy as Copy, Send as Send, Sized as Sized, Sync as Sync, Unpin as U,
},
ops::{*, Drop, Fn, FnMut, FnOnce},
};

fn main() {}

0 comments on commit f30f6de

Please sign in to comment.