Skip to content

Commit f30f6de

Browse files
authored
Rollup merge of #93759 - dtolnay:usetree, r=nagisa
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}, }; ```
2 parents 36461e0 + b64a822 commit f30f6de

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

compiler/rustc_ast_pretty/src/pp/convenience.rs

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ impl Printer {
7575
}
7676

7777
pub fn trailing_comma(&mut self) {
78+
self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() });
79+
}
80+
81+
pub fn trailing_comma_or_space(&mut self) {
7882
self.scan_break(BreakToken {
7983
blank_space: 1,
8084
pre_break: Some(','),

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'a> State<'a> {
142142
if !field.is_last || has_rest {
143143
self.word_space(",");
144144
} else {
145-
self.trailing_comma();
145+
self.trailing_comma_or_space();
146146
}
147147
}
148148
if has_rest {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::pp::Breaks::Inconsistent;
2-
use crate::pprust::state::{AnnNode, PrintState, State};
2+
use crate::pprust::state::delimited::IterDelimited;
3+
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
34

45
use rustc_ast as ast;
56
use rustc_ast::GenericBound;
@@ -138,11 +139,10 @@ impl<'a> State<'a> {
138139
self.end(); // end outer head-block
139140
}
140141
ast::ItemKind::Use(ref tree) => {
141-
self.head(visibility_qualified(&item.vis, "use"));
142+
self.print_visibility(&item.vis);
143+
self.word_nbsp("use");
142144
self.print_use_tree(tree);
143145
self.word(";");
144-
self.end(); // end inner head-block
145-
self.end(); // end outer head-block
146146
}
147147
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
148148
let def = ast::Defaultness::Final;
@@ -615,8 +615,8 @@ impl<'a> State<'a> {
615615
ast::UseTreeKind::Simple(rename, ..) => {
616616
self.print_path(&tree.prefix, false, 0);
617617
if let Some(rename) = rename {
618-
self.space();
619-
self.word_space("as");
618+
self.nbsp();
619+
self.word_nbsp("as");
620620
self.print_ident(rename);
621621
}
622622
}
@@ -628,16 +628,36 @@ impl<'a> State<'a> {
628628
self.word("*");
629629
}
630630
ast::UseTreeKind::Nested(ref items) => {
631-
if tree.prefix.segments.is_empty() {
632-
self.word("{");
633-
} else {
631+
if !tree.prefix.segments.is_empty() {
634632
self.print_path(&tree.prefix, false, 0);
635-
self.word("::{");
633+
self.word("::");
634+
}
635+
if items.is_empty() {
636+
self.word("{}");
637+
} else if items.len() == 1 {
638+
self.print_use_tree(&items[0].0);
639+
} else {
640+
self.cbox(INDENT_UNIT);
641+
self.word("{");
642+
self.zerobreak();
643+
self.ibox(0);
644+
for use_tree in items.iter().delimited() {
645+
self.print_use_tree(&use_tree.0);
646+
if !use_tree.is_last {
647+
self.word(",");
648+
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
649+
self.hardbreak();
650+
} else {
651+
self.space();
652+
}
653+
}
654+
}
655+
self.end();
656+
self.trailing_comma();
657+
self.offset(-INDENT_UNIT);
658+
self.word("}");
659+
self.end();
636660
}
637-
self.commasep(Inconsistent, &items, |this, &(ref tree, _)| {
638-
this.print_use_tree(tree)
639-
});
640-
self.word("}");
641661
}
642662
}
643663
}

src/test/pretty/use-tree.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// pp-exact
2+
// edition:2021
3+
4+
#![allow(unused_imports)]
5+
6+
use ::std::fmt::{self, Debug, Display, Write as _};
7+
8+
use core::option::Option::*;
9+
10+
use core::{
11+
cmp::{Eq, Ord, PartialEq, PartialOrd},
12+
convert::{AsMut, AsRef, From, Into},
13+
iter::{
14+
DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator,
15+
IntoIterator, Iterator,
16+
},
17+
marker::{
18+
Copy as Copy, Send as Send, Sized as Sized, Sync as Sync, Unpin as U,
19+
},
20+
ops::{*, Drop, Fn, FnMut, FnOnce},
21+
};
22+
23+
fn main() {}

0 commit comments

Comments
 (0)