Skip to content

Commit a09f775

Browse files
committed
Auto merge of #92426 - calebcartwright:sync-rustfmt-subtree, r=calebcartwright
Sync rustfmt subtree r? `@ghost` Mostly refactoring and a few minor lint fixes, along with a couple small bug fixes
2 parents b60e32c + 521fdcb commit a09f775

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+710
-410
lines changed

Diff for: src/tools/rustfmt/rust-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2021-11-08"
2+
channel = "nightly-2021-12-29"
33
components = ["rustc-dev"]

Diff for: src/tools/rustfmt/src/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let exit_code = match execute(&opts) {
2727
Ok(code) => code,
2828
Err(e) => {
29-
eprintln!("{}", e.to_string());
29+
eprintln!("{}", e);
3030
1
3131
}
3232
};

Diff for: src/tools/rustfmt/src/closures.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,21 @@ fn rewrite_closure_fn_decl(
236236
context: &RewriteContext<'_>,
237237
shape: Shape,
238238
) -> Option<(String, usize)> {
239-
let is_async = if asyncness.is_async() { "async " } else { "" };
240-
let mover = if capture == ast::CaptureBy::Value {
241-
"move "
239+
let immovable = if movability == ast::Movability::Static {
240+
"static "
242241
} else {
243242
""
244243
};
245-
let immovable = if movability == ast::Movability::Static {
246-
"static "
244+
let is_async = if asyncness.is_async() { "async " } else { "" };
245+
let mover = if capture == ast::CaptureBy::Value {
246+
"move "
247247
} else {
248248
""
249249
};
250250
// 4 = "|| {".len(), which is overconservative when the closure consists of
251251
// a single expression.
252252
let nested_shape = shape
253-
.shrink_left(is_async.len() + mover.len() + immovable.len())?
253+
.shrink_left(immovable.len() + is_async.len() + mover.len())?
254254
.sub_width(4)?;
255255

256256
// 1 = |
@@ -288,7 +288,7 @@ fn rewrite_closure_fn_decl(
288288
.tactic(tactic)
289289
.preserve_newline(true);
290290
let list_str = write_list(&item_vec, &fmt)?;
291-
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str);
291+
let mut prefix = format!("{}{}{}|{}|", immovable, is_async, mover, list_str);
292292

293293
if !ret_str.is_empty() {
294294
if prefix.contains('\n') {

Diff for: src/tools/rustfmt/src/config/file_lines.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use thiserror::Error;
1313

1414
/// A range of lines in a file, inclusive of both ends.
1515
pub struct LineRange {
16-
pub file: Lrc<SourceFile>,
17-
pub lo: usize,
18-
pub hi: usize,
16+
pub(crate) file: Lrc<SourceFile>,
17+
pub(crate) lo: usize,
18+
pub(crate) hi: usize,
1919
}
2020

2121
/// Defines the name of an input - either a file or stdin.
@@ -75,7 +75,7 @@ impl Serialize for FileName {
7575
}
7676

7777
impl LineRange {
78-
pub fn file_name(&self) -> FileName {
78+
pub(crate) fn file_name(&self) -> FileName {
7979
self.file.name.clone().into()
8080
}
8181
}

Diff for: src/tools/rustfmt/src/config/options.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,24 @@ pub enum Verbosity {
218218
pub struct WidthHeuristics {
219219
// Maximum width of the args of a function call before falling back
220220
// to vertical formatting.
221-
pub fn_call_width: usize,
221+
pub(crate) fn_call_width: usize,
222222
// Maximum width of the args of a function-like attributes before falling
223223
// back to vertical formatting.
224-
pub attr_fn_like_width: usize,
224+
pub(crate) attr_fn_like_width: usize,
225225
// Maximum width in the body of a struct lit before falling back to
226226
// vertical formatting.
227-
pub struct_lit_width: usize,
227+
pub(crate) struct_lit_width: usize,
228228
// Maximum width in the body of a struct variant before falling back
229229
// to vertical formatting.
230-
pub struct_variant_width: usize,
230+
pub(crate) struct_variant_width: usize,
231231
// Maximum width of an array literal before falling back to vertical
232232
// formatting.
233-
pub array_width: usize,
233+
pub(crate) array_width: usize,
234234
// Maximum length of a chain to fit on a single line.
235-
pub chain_width: usize,
235+
pub(crate) chain_width: usize,
236236
// Maximum line length for single line if-else expressions. A value
237237
// of zero means always break if-else expressions.
238-
pub single_line_if_else_max_width: usize,
238+
pub(crate) single_line_if_else_max_width: usize,
239239
}
240240

241241
impl fmt::Display for WidthHeuristics {

Diff for: src/tools/rustfmt/src/expr.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,21 @@ pub(crate) fn format_expr(
108108
ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
109109
ast::ExprKind::Struct(ref struct_expr) => {
110110
let ast::StructExpr {
111-
fields, path, rest, ..
111+
qself,
112+
fields,
113+
path,
114+
rest,
112115
} = &**struct_expr;
113-
rewrite_struct_lit(context, path, fields, rest, &expr.attrs, expr.span, shape)
116+
rewrite_struct_lit(
117+
context,
118+
path,
119+
qself.as_ref(),
120+
fields,
121+
rest,
122+
&expr.attrs,
123+
expr.span,
124+
shape,
125+
)
114126
}
115127
ast::ExprKind::Tup(ref items) => {
116128
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
@@ -1511,6 +1523,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool
15111523
fn rewrite_struct_lit<'a>(
15121524
context: &RewriteContext<'_>,
15131525
path: &ast::Path,
1526+
qself: Option<&ast::QSelf>,
15141527
fields: &'a [ast::ExprField],
15151528
struct_rest: &ast::StructRest,
15161529
attrs: &[ast::Attribute],
@@ -1527,7 +1540,7 @@ fn rewrite_struct_lit<'a>(
15271540

15281541
// 2 = " {".len()
15291542
let path_shape = shape.sub_width(2)?;
1530-
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
1543+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
15311544

15321545
let has_base_or_rest = match struct_rest {
15331546
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),
@@ -2003,9 +2016,7 @@ fn choose_rhs<R: Rewrite>(
20032016
has_rhs_comment: bool,
20042017
) -> Option<String> {
20052018
match orig_rhs {
2006-
Some(ref new_str) if new_str.is_empty() => {
2007-
return Some(String::new());
2008-
}
2019+
Some(ref new_str) if new_str.is_empty() => Some(String::new()),
20092020
Some(ref new_str)
20102021
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
20112022
{

Diff for: src/tools/rustfmt/src/formatting.rs

+43-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::{self, Write};
55
use std::time::{Duration, Instant};
66

77
use rustc_ast::ast;
8+
use rustc_ast::AstLike;
89
use rustc_span::Span;
910

1011
use self::newline_style::apply_newline_style;
@@ -13,9 +14,9 @@ use crate::config::{Config, FileName, Verbosity};
1314
use crate::formatting::generated::is_generated_file;
1415
use crate::issues::BadIssueSeeker;
1516
use crate::modules::Module;
16-
use crate::syntux::parser::{DirectoryOwnership, Parser, ParserError};
17-
use crate::syntux::session::ParseSess;
18-
use crate::utils::count_newlines;
17+
use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
18+
use crate::parse::session::ParseSess;
19+
use crate::utils::{contains_skip, count_newlines};
1920
use crate::visitor::FmtVisitor;
2021
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
2122

@@ -58,6 +59,39 @@ impl<'b, T: Write + 'b> Session<'b, T> {
5859
}
5960
}
6061

62+
/// Determine if a module should be skipped. True if the module should be skipped, false otherwise.
63+
fn should_skip_module<T: FormatHandler>(
64+
config: &Config,
65+
context: &FormatContext<'_, T>,
66+
input_is_stdin: bool,
67+
main_file: &FileName,
68+
path: &FileName,
69+
module: &Module<'_>,
70+
) -> bool {
71+
if contains_skip(module.attrs()) {
72+
return true;
73+
}
74+
75+
if config.skip_children() && path != main_file {
76+
return true;
77+
}
78+
79+
if !input_is_stdin && context.ignore_file(path) {
80+
return true;
81+
}
82+
83+
if !config.format_generated_files() {
84+
let source_file = context.parse_session.span_to_file_contents(module.span);
85+
let src = source_file.src.as_ref().expect("SourceFile without src");
86+
87+
if is_generated_file(src) {
88+
return true;
89+
}
90+
}
91+
92+
false
93+
}
94+
6195
// Format an entire crate (or subset of the module tree).
6296
fn format_project<T: FormatHandler>(
6397
input: Input,
@@ -97,23 +131,19 @@ fn format_project<T: FormatHandler>(
97131
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
98132
!input_is_stdin && !config.skip_children(),
99133
)
100-
.visit_crate(&krate)?;
134+
.visit_crate(&krate)?
135+
.into_iter()
136+
.filter(|(path, module)| {
137+
!should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
138+
})
139+
.collect::<Vec<_>>();
101140

102141
timer = timer.done_parsing();
103142

104143
// Suppress error output if we have to do any further parsing.
105144
context.parse_session.set_silent_emitter();
106145

107146
for (path, module) in files {
108-
let source_file = context.parse_session.span_to_file_contents(module.span);
109-
let src = source_file.src.as_ref().expect("SourceFile without src");
110-
111-
let should_ignore = (!input_is_stdin && context.ignore_file(&path))
112-
|| (!config.format_generated_files() && is_generated_file(src));
113-
114-
if (config.skip_children() && path != main_file) || should_ignore {
115-
continue;
116-
}
117147
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
118148
context.format_file(path, &module, is_macro_def)?;
119149
}

Diff for: src/tools/rustfmt/src/items.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1535,15 +1535,15 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
15351535
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
15361536
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
15371537
match (visitor_kind, &op_ty) {
1538-
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(ref op_bounds)) => {
1538+
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(op_bounds)) => {
15391539
let op = OpaqueType { bounds: op_bounds };
15401540
rewrite_ty(rw_info, Some(bounds), Some(&op), vis)
15411541
}
15421542
(Item(_) | AssocTraitItem(_) | ForeignItem(_), None) => {
15431543
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
15441544
}
15451545
(AssocImplItem(_), _) => {
1546-
let result = if let Some(ref op_bounds) = op_ty {
1546+
let result = if let Some(op_bounds) = op_ty {
15471547
let op = OpaqueType { bounds: op_bounds };
15481548
rewrite_ty(rw_info, Some(bounds), Some(&op), &DEFAULT_VISIBILITY)
15491549
} else {
@@ -3124,7 +3124,7 @@ impl Rewrite for ast::ForeignItem {
31243124
let inner_attrs = inner_attributes(&self.attrs);
31253125
let fn_ctxt = visit::FnCtxt::Foreign;
31263126
visitor.visit_fn(
3127-
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
3127+
visit::FnKind::Fn(fn_ctxt, self.ident, sig, &self.vis, Some(body)),
31283128
generics,
31293129
&sig.decl,
31303130
self.span,
@@ -3137,7 +3137,7 @@ impl Rewrite for ast::ForeignItem {
31373137
context,
31383138
shape.indent,
31393139
self.ident,
3140-
&FnSig::from_method_sig(&sig, generics, &self.vis),
3140+
&FnSig::from_method_sig(sig, generics, &self.vis),
31413141
span,
31423142
FnBraceStyle::None,
31433143
)
@@ -3166,7 +3166,7 @@ impl Rewrite for ast::ForeignItem {
31663166
.map(|s| s + ";")
31673167
}
31683168
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
3169-
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);
3169+
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);
31703170
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
31713171
}
31723172
ast::ForeignItemKind::MacCall(ref mac) => {

Diff for: src/tools/rustfmt/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate log;
1515
// N.B. these crates are loaded from the sysroot, so they need extern crate.
1616
extern crate rustc_ast;
1717
extern crate rustc_ast_pretty;
18+
extern crate rustc_builtin_macros;
1819
extern crate rustc_data_structures;
1920
extern crate rustc_errors;
2021
extern crate rustc_expand;
@@ -40,8 +41,8 @@ use crate::emitter::Emitter;
4041
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
4142
use crate::issues::Issue;
4243
use crate::modules::ModuleResolutionError;
44+
use crate::parse::parser::DirectoryOwnership;
4345
use crate::shape::Indent;
44-
use crate::syntux::parser::DirectoryOwnership;
4546
use crate::utils::indent_next_line;
4647

4748
pub use crate::config::{
@@ -77,6 +78,7 @@ mod missed_spans;
7778
pub(crate) mod modules;
7879
mod overflow;
7980
mod pairs;
81+
mod parse;
8082
mod patterns;
8183
mod release_channel;
8284
mod reorder;
@@ -89,7 +91,6 @@ pub(crate) mod source_map;
8991
mod spanned;
9092
mod stmt;
9193
mod string;
92-
mod syntux;
9394
#[cfg(test)]
9495
mod test;
9596
mod types;

Diff for: src/tools/rustfmt/src/lists.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,8 @@ where
448448
true
449449
} else if starts_with_newline(comment) {
450450
false
451-
} else if comment.trim().contains('\n') || comment.trim().len() > width {
452-
true
453451
} else {
454-
false
452+
comment.trim().contains('\n') || comment.trim().len() > width
455453
};
456454

457455
rewrite_comment(

0 commit comments

Comments
 (0)