Skip to content

Sync rustfmt subtree #92426

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

Merged
merged 23 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0167c53
Merge commit '8da837185714cefbb261e93e9846afb11c1dc60e' into sync-rus…
calebcartwright Dec 3, 2021
f40b1d9
Backport: Do not touch module with #![rustfmt::skip] (4297)
ytmimi Nov 24, 2021
740fb57
clippy fixes
matthiaskrgr Dec 12, 2021
57ac92b
Prevent duplicate comma when formatting struct pattern with ".."
ytmimi Nov 18, 2021
b4afb38
Remove `SymbolStr`.
nnethercote Dec 14, 2021
3bb5f81
Remove unnecessary sigils around `Symbol::as_str()` calls.
nnethercote Dec 15, 2021
122e1c3
Remove unnecessary sigils around `Ident::as_str()` calls.
nnethercote Dec 15, 2021
8bf82ae
Merge remote-tracking branch 'upstream/master' into subtree-sync-2021…
calebcartwright Dec 19, 2021
b214938
chore: bump toolchain
calebcartwright Dec 20, 2021
0346bc7
Merge pull request #5140 from calebcartwright/subtree-sync-2021-12-19
calebcartwright Dec 20, 2021
40b73d8
refactor: rename syntux mod to parse
calebcartwright Dec 20, 2021
9ce5470
refactor: move macro arg parsing to parse mod
calebcartwright Dec 21, 2021
c8cf454
refactor: move lazy_static parsing to parse mod
calebcartwright Dec 21, 2021
6298756
refactor: extract final rustc_parse touchpoint from macros.rs
calebcartwright Dec 21, 2021
97c3e48
refactor: encapsulate cfg_if parsing within parse mod
calebcartwright Dec 21, 2021
7b8303d
chore: cleanup unused imports
calebcartwright Dec 21, 2021
0b2fd9b
Fix static async closure qualifier order
dtolnay Dec 23, 2021
76eb077
Retain qualified path when rewriting struct literals
ytmimi Dec 24, 2021
e8afb62
chore: reduce some vis. for updated unreachable_pub lint
calebcartwright Dec 27, 2021
50bbb43
chore: bump toolchain
calebcartwright Dec 29, 2021
f935f0c
feat: support parsing asm! args
calebcartwright Dec 29, 2021
4a053f2
Do not flatten match arm block with leading attributes
davidlattimore Dec 29, 2021
521fdcb
Merge commit '4a053f206fd6799a25823c307f7d7f9d897be118' into sync-rus…
calebcartwright Dec 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tools/rustfmt/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2021-11-08"
channel = "nightly-2021-12-29"
components = ["rustc-dev"]
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
let exit_code = match execute(&opts) {
Ok(code) => code,
Err(e) => {
eprintln!("{}", e.to_string());
eprintln!("{}", e);
1
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/tools/rustfmt/src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,21 @@ fn rewrite_closure_fn_decl(
context: &RewriteContext<'_>,
shape: Shape,
) -> Option<(String, usize)> {
let is_async = if asyncness.is_async() { "async " } else { "" };
let mover = if capture == ast::CaptureBy::Value {
"move "
let immovable = if movability == ast::Movability::Static {
"static "
} else {
""
};
let immovable = if movability == ast::Movability::Static {
"static "
let is_async = if asyncness.is_async() { "async " } else { "" };
let mover = if capture == ast::CaptureBy::Value {
"move "
} else {
""
};
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let nested_shape = shape
.shrink_left(is_async.len() + mover.len() + immovable.len())?
.shrink_left(immovable.len() + is_async.len() + mover.len())?
.sub_width(4)?;

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

if !ret_str.is_empty() {
if prefix.contains('\n') {
Expand Down
8 changes: 4 additions & 4 deletions src/tools/rustfmt/src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use thiserror::Error;

/// A range of lines in a file, inclusive of both ends.
pub struct LineRange {
pub file: Lrc<SourceFile>,
pub lo: usize,
pub hi: usize,
pub(crate) file: Lrc<SourceFile>,
pub(crate) lo: usize,
pub(crate) hi: usize,
}

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

impl LineRange {
pub fn file_name(&self) -> FileName {
pub(crate) fn file_name(&self) -> FileName {
self.file.name.clone().into()
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/tools/rustfmt/src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,24 @@ pub enum Verbosity {
pub struct WidthHeuristics {
// Maximum width of the args of a function call before falling back
// to vertical formatting.
pub fn_call_width: usize,
pub(crate) fn_call_width: usize,
// Maximum width of the args of a function-like attributes before falling
// back to vertical formatting.
pub attr_fn_like_width: usize,
pub(crate) attr_fn_like_width: usize,
// Maximum width in the body of a struct lit before falling back to
// vertical formatting.
pub struct_lit_width: usize,
pub(crate) struct_lit_width: usize,
// Maximum width in the body of a struct variant before falling back
// to vertical formatting.
pub struct_variant_width: usize,
pub(crate) struct_variant_width: usize,
// Maximum width of an array literal before falling back to vertical
// formatting.
pub array_width: usize,
pub(crate) array_width: usize,
// Maximum length of a chain to fit on a single line.
pub chain_width: usize,
pub(crate) chain_width: usize,
// Maximum line length for single line if-else expressions. A value
// of zero means always break if-else expressions.
pub single_line_if_else_max_width: usize,
pub(crate) single_line_if_else_max_width: usize,
}

impl fmt::Display for WidthHeuristics {
Expand Down
23 changes: 17 additions & 6 deletions src/tools/rustfmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,21 @@ pub(crate) fn format_expr(
ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
ast::ExprKind::Struct(ref struct_expr) => {
let ast::StructExpr {
fields, path, rest, ..
qself,
fields,
path,
rest,
} = &**struct_expr;
rewrite_struct_lit(context, path, fields, rest, &expr.attrs, expr.span, shape)
rewrite_struct_lit(
context,
path,
qself.as_ref(),
fields,
rest,
&expr.attrs,
expr.span,
shape,
)
}
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
Expand Down Expand Up @@ -1511,6 +1523,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool
fn rewrite_struct_lit<'a>(
context: &RewriteContext<'_>,
path: &ast::Path,
qself: Option<&ast::QSelf>,
fields: &'a [ast::ExprField],
struct_rest: &ast::StructRest,
attrs: &[ast::Attribute],
Expand All @@ -1527,7 +1540,7 @@ fn rewrite_struct_lit<'a>(

// 2 = " {".len()
let path_shape = shape.sub_width(2)?;
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;

let has_base_or_rest = match struct_rest {
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),
Expand Down Expand Up @@ -2003,9 +2016,7 @@ fn choose_rhs<R: Rewrite>(
has_rhs_comment: bool,
) -> Option<String> {
match orig_rhs {
Some(ref new_str) if new_str.is_empty() => {
return Some(String::new());
}
Some(ref new_str) if new_str.is_empty() => Some(String::new()),
Some(ref new_str)
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
{
Expand Down
56 changes: 43 additions & 13 deletions src/tools/rustfmt/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::{self, Write};
use std::time::{Duration, Instant};

use rustc_ast::ast;
use rustc_ast::AstLike;
use rustc_span::Span;

use self::newline_style::apply_newline_style;
Expand All @@ -13,9 +14,9 @@ use crate::config::{Config, FileName, Verbosity};
use crate::formatting::generated::is_generated_file;
use crate::issues::BadIssueSeeker;
use crate::modules::Module;
use crate::syntux::parser::{DirectoryOwnership, Parser, ParserError};
use crate::syntux::session::ParseSess;
use crate::utils::count_newlines;
use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
use crate::parse::session::ParseSess;
use crate::utils::{contains_skip, count_newlines};
use crate::visitor::FmtVisitor;
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};

Expand Down Expand Up @@ -58,6 +59,39 @@ impl<'b, T: Write + 'b> Session<'b, T> {
}
}

/// Determine if a module should be skipped. True if the module should be skipped, false otherwise.
fn should_skip_module<T: FormatHandler>(
config: &Config,
context: &FormatContext<'_, T>,
input_is_stdin: bool,
main_file: &FileName,
path: &FileName,
module: &Module<'_>,
) -> bool {
if contains_skip(module.attrs()) {
return true;
}

if config.skip_children() && path != main_file {
return true;
}

if !input_is_stdin && context.ignore_file(path) {
return true;
}

if !config.format_generated_files() {
let source_file = context.parse_session.span_to_file_contents(module.span);
let src = source_file.src.as_ref().expect("SourceFile without src");

if is_generated_file(src) {
return true;
}
}

false
}

// Format an entire crate (or subset of the module tree).
fn format_project<T: FormatHandler>(
input: Input,
Expand Down Expand Up @@ -97,23 +131,19 @@ fn format_project<T: FormatHandler>(
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
!input_is_stdin && !config.skip_children(),
)
.visit_crate(&krate)?;
.visit_crate(&krate)?
.into_iter()
.filter(|(path, module)| {
!should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
})
.collect::<Vec<_>>();

timer = timer.done_parsing();

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

for (path, module) in files {
let source_file = context.parse_session.span_to_file_contents(module.span);
let src = source_file.src.as_ref().expect("SourceFile without src");

let should_ignore = (!input_is_stdin && context.ignore_file(&path))
|| (!config.format_generated_files() && is_generated_file(src));

if (config.skip_children() && path != main_file) || should_ignore {
continue;
}
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
context.format_file(path, &module, is_macro_def)?;
}
Expand Down
10 changes: 5 additions & 5 deletions src/tools/rustfmt/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,15 +1535,15 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
match (visitor_kind, &op_ty) {
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(ref op_bounds)) => {
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(op_bounds)) => {
let op = OpaqueType { bounds: op_bounds };
rewrite_ty(rw_info, Some(bounds), Some(&op), vis)
}
(Item(_) | AssocTraitItem(_) | ForeignItem(_), None) => {
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
}
(AssocImplItem(_), _) => {
let result = if let Some(ref op_bounds) = op_ty {
let result = if let Some(op_bounds) = op_ty {
let op = OpaqueType { bounds: op_bounds };
rewrite_ty(rw_info, Some(bounds), Some(&op), &DEFAULT_VISIBILITY)
} else {
Expand Down Expand Up @@ -3124,7 +3124,7 @@ impl Rewrite for ast::ForeignItem {
let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn(
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
visit::FnKind::Fn(fn_ctxt, self.ident, sig, &self.vis, Some(body)),
generics,
&sig.decl,
self.span,
Expand All @@ -3137,7 +3137,7 @@ impl Rewrite for ast::ForeignItem {
context,
shape.indent,
self.ident,
&FnSig::from_method_sig(&sig, generics, &self.vis),
&FnSig::from_method_sig(sig, generics, &self.vis),
span,
FnBraceStyle::None,
)
Expand Down Expand Up @@ -3166,7 +3166,7 @@ impl Rewrite for ast::ForeignItem {
.map(|s| s + ";")
}
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
}
ast::ForeignItemKind::MacCall(ref mac) => {
Expand Down
5 changes: 3 additions & 2 deletions src/tools/rustfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate log;
// N.B. these crates are loaded from the sysroot, so they need extern crate.
extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_builtin_macros;
extern crate rustc_data_structures;
extern crate rustc_errors;
extern crate rustc_expand;
Expand All @@ -40,8 +41,8 @@ use crate::emitter::Emitter;
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
use crate::issues::Issue;
use crate::modules::ModuleResolutionError;
use crate::parse::parser::DirectoryOwnership;
use crate::shape::Indent;
use crate::syntux::parser::DirectoryOwnership;
use crate::utils::indent_next_line;

pub use crate::config::{
Expand Down Expand Up @@ -77,6 +78,7 @@ mod missed_spans;
pub(crate) mod modules;
mod overflow;
mod pairs;
mod parse;
mod patterns;
mod release_channel;
mod reorder;
Expand All @@ -89,7 +91,6 @@ pub(crate) mod source_map;
mod spanned;
mod stmt;
mod string;
mod syntux;
#[cfg(test)]
mod test;
mod types;
Expand Down
4 changes: 1 addition & 3 deletions src/tools/rustfmt/src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,8 @@ where
true
} else if starts_with_newline(comment) {
false
} else if comment.trim().contains('\n') || comment.trim().len() > width {
true
} else {
false
comment.trim().contains('\n') || comment.trim().len() > width
};

rewrite_comment(
Expand Down
Loading