Skip to content
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

Parallel formatting to increase speed #6095

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Formatted passes tests
  • Loading branch information
MarcusGrass committed Feb 26, 2024
commit 606d89e6b55feee3dc057c9ca754fe4bc4f1379a
6 changes: 5 additions & 1 deletion config_proc_macro/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ fn get_name_value_str_lit(attr: &syn::Attribute, name: &str) -> Option<String> {
match &attr.meta {
syn::Meta::NameValue(syn::MetaNameValue {
path,
value: syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit_str), .. }),
value:
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}),
..
}) if path.is_ident(name) => Some(lit_str.value()),
_ => None,
Expand Down
7 changes: 6 additions & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,12 @@ impl Rewrite for ast::Attribute {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let snippet = context.snippet(self.span);
if self.is_doc_comment() {
rewrite_doc_comment(snippet, shape.comment(context.config), context.config, context.printer)
rewrite_doc_comment(
snippet,
shape.comment(context.config),
context.config,
context.printer,
)
} else {
let should_skip = self
.ident()
Expand Down
35 changes: 27 additions & 8 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::str::FromStr;
use std::thread::JoinHandle;

use getopts::{Matches, Options};
use rustfmt_nightly::{buf_eprintln, buf_println};
use rustfmt_nightly::print::Printer;
use rustfmt_nightly::{buf_eprintln, buf_println};

use crate::rustfmt::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
Expand Down Expand Up @@ -347,10 +347,18 @@ fn format(
let printer = Printer::new(color);
let mut session = Session::new(cfg, Some(&mut session_out), &printer);
if !file.exists() {
buf_eprintln!(printer, "Error: file `{}` does not exist", file.to_str().unwrap());
buf_eprintln!(
printer,
"Error: file `{}` does not exist",
file.to_str().unwrap()
);
session.add_operational_error();
} else if file.is_dir() {
buf_eprintln!(printer, "Error: `{}` is a directory", file.to_str().unwrap());
buf_eprintln!(
printer,
"Error: `{}` is a directory",
file.to_str().unwrap()
);
session.add_operational_error();
} else {
// Check the file directory if the config-path could not be read or not provided
Expand All @@ -371,9 +379,12 @@ fn format(
};
if local_config.verbose() == Verbosity::Verbose {
if let Some(path) = config_path {
buf_println!(printer, "Using rustfmt config file {} for {}",
buf_println!(
printer,
"Using rustfmt config file {} for {}",
path.display(),
file.display());
file.display()
);
}
}

Expand Down Expand Up @@ -406,7 +417,12 @@ fn format(
outstanding += 1;
if outstanding >= num_cpus {
if let Ok(thread_out) = recv.recv() {
handles.remove(&thread_out.id).unwrap().join().unwrap().unwrap();
handles
.remove(&thread_out.id)
.unwrap()
.join()
.unwrap()
.unwrap();
let output = thread_out.session_result?;
stdout().write_all(&output).unwrap();
thread_out.printer.dump()?;
Expand Down Expand Up @@ -450,10 +466,13 @@ fn format_and_emit_report<T: Write>(session: &mut Session<'_, T>, input: Input)
match session.format(input) {
Ok(report) => {
if report.has_warnings() {
buf_eprintln!(session.printer, "{}",
buf_eprintln!(
session.printer,
"{}",
FormatReportFormatterBuilder::new(&report)
.enable_colors(should_print_with_colors(session))
.build());
.build()
);
}
}
Err(msg) => {
Expand Down
35 changes: 28 additions & 7 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use regex::Regex;
use rustc_span::Span;

use crate::config::Config;
use crate::print::Printer;
use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape};
use crate::string::{rewrite_string, StringFormat};
Expand All @@ -16,7 +17,6 @@ use crate::utils::{
trimmed_last_line_width, unicode_str_width,
};
use crate::{ErrorKind, FormattingError};
use crate::print::Printer;

lazy_static! {
/// A regex matching reference doc links.
Expand Down Expand Up @@ -249,7 +249,12 @@ pub(crate) fn combine_strs_with_missing_comments(
Some(result)
}

pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config, printer: &Printer) -> Option<String> {
pub(crate) fn rewrite_doc_comment(
orig: &str,
shape: Shape,
config: &Config,
printer: &Printer,
) -> Option<String> {
identify_comment(orig, false, shape, config, true, printer)
}

Expand Down Expand Up @@ -781,9 +786,12 @@ impl<'a> CommentRewrite<'a> {
.doc_comment_code_block_width()
.min(config.max_width());
config.set().max_width(comment_max_width);
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config, false, printer)
{
if let Some(s) = crate::format_code_block(
&self.code_block_buffer,
&config,
false,
printer,
) {
trim_custom_comment_prefix(&s.snippet)
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
Expand Down Expand Up @@ -948,7 +956,14 @@ fn rewrite_comment_inner(
});

for (i, (line, has_leading_whitespace)) in lines.enumerate() {
if rewriter.handle_line(orig, i, line, has_leading_whitespace, is_doc_comment, printer) {
if rewriter.handle_line(
orig,
i,
line,
has_leading_whitespace,
is_doc_comment,
printer,
) {
break;
}
}
Expand Down Expand Up @@ -1015,7 +1030,13 @@ pub(crate) fn rewrite_missing_comment(
// check the span starts with a comment
let pos = trimmed_snippet.find('/');
if !trimmed_snippet.is_empty() && pos.is_some() {
rewrite_comment(trimmed_snippet, false, shape, context.config, context.printer)
rewrite_comment(
trimmed_snippet,
false,
shape,
context.config,
context.printer,
)
} else {
Some(String::new())
}
Expand Down
2 changes: 1 addition & 1 deletion src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ pub(crate) use self::files_with_backup::*;
pub(crate) use self::json::*;
pub(crate) use self::modified_lines::*;
pub(crate) use self::stdout::*;
use crate::print::Printer;
use crate::FileName;
use std::io::{self, Write};
use std::path::Path;
use crate::print::Printer;

mod checkstyle;
mod diff;
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use std::fs;
use crate::buf_println;
use std::fs;

#[derive(Debug, Default)]
pub(crate) struct FilesEmitter {
Expand Down
4 changes: 2 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use crate::formatting::generated::is_generated_file;
use crate::modules::Module;
use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
use crate::parse::session::ParseSess;
use crate::print::Printer;
use crate::utils::{contains_skip, count_newlines};
use crate::visitor::FmtVisitor;
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session, buf_eprintln};
use crate::print::Printer;
use crate::{buf_eprintln, modules, source_file, ErrorKind, FormatReport, Input, Session};

mod generated;
mod newline_style;
Expand Down
2 changes: 1 addition & 1 deletion src/git-rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::str::FromStr;

use getopts::{Matches, Options};
use rustfmt_nightly as rustfmt;
use tracing_subscriber::EnvFilter;
use rustfmt_nightly::print::Printer;
use tracing_subscriber::EnvFilter;

use crate::rustfmt::{load_config, CliOptions, FormatReportFormatterBuilder, Input, Session};

Expand Down
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ mod overflow;
mod pairs;
mod parse;
mod patterns;
pub mod print;
mod release_channel;
mod reorder;
mod rewrite;
Expand All @@ -100,7 +101,6 @@ mod test;
mod types;
mod vertical;
pub(crate) mod visitor;
pub mod print;

/// The various errors that can occur during formatting. Note that not all of
/// these can currently be propagated to clients.
Expand Down Expand Up @@ -300,7 +300,12 @@ impl fmt::Display for FormatReport {

/// Format the given snippet. The snippet is expected to be *complete* code.
/// When we cannot parse the given snippet, this function returns `None`.
fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool, printer: &Printer) -> Option<FormattedSnippet> {
fn format_snippet(
snippet: &str,
config: &Config,
is_macro_def: bool,
printer: &Printer,
) -> Option<FormattedSnippet> {
let mut config = config.clone();
panic::catch_unwind(|| {
let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
Expand Down Expand Up @@ -532,9 +537,7 @@ pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
EmitMode::Json => Box::new(emitter::JsonEmitter::default()),
EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter::default()),
EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter::default()),
EmitMode::Diff => {
Box::new(emitter::DiffEmitter::new(config.clone()))
},
EmitMode::Diff => Box::new(emitter::DiffEmitter::new(config.clone())),
}
}

Expand Down Expand Up @@ -589,7 +592,9 @@ mod unit_tests {
// even when we cannot parse the given snippet.
let snippet = "let";
assert!(format_snippet(snippet, &Config::default(), false, &Printer::no_color()).is_none());
assert!(format_code_block(snippet, &Config::default(), false, &Printer::no_color()).is_none());
assert!(
format_code_block(snippet, &Config::default(), false, &Printer::no_color()).is_none()
);
}

fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
Expand Down Expand Up @@ -618,7 +623,10 @@ mod unit_tests {
fn test_format_code_block_fail() {
#[rustfmt::skip]
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
assert!(format_code_block(code_block, &Config::default(), false, &Printer::no_color()).is_none());
assert!(
format_code_block(code_block, &Config::default(), false, &Printer::no_color())
.is_none()
);
}

#[test]
Expand Down
15 changes: 12 additions & 3 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ where
}

// Format a list of commented items into a string.
pub(crate) fn write_list<I, T>(items: I, formatting: &ListFormatting<'_>, printer: &Printer) -> Option<String>
pub(crate) fn write_list<I, T>(
items: I,
formatting: &ListFormatting<'_>,
printer: &Printer,
) -> Option<String>
where
I: IntoIterator<Item = T> + Clone,
T: AsRef<ListItem>,
Expand Down Expand Up @@ -363,8 +367,13 @@ where
// Block style in non-vertical mode.
let block_mode = tactic == DefinitiveListTactic::Horizontal;
// Width restriction is only relevant in vertical mode.
let comment =
rewrite_comment(comment, block_mode, formatting.shape, formatting.config, printer)?;
let comment = rewrite_comment(
comment,
block_mode,
formatting.shape,
formatting.config,
printer,
)?;
result.push_str(&comment);

if !inner_item.is_empty() {
Expand Down
21 changes: 11 additions & 10 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,17 +1289,18 @@ impl MacroBranch {
config.set().max_width(new_width);

// First try to format as items, then as statements.
let new_body_snippet = match crate::format_snippet(&body_str, &config, true, context.printer) {
Some(new_body) => new_body,
None => {
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
match crate::format_code_block(&body_str, &config, true, context.printer) {
Some(new_body) => new_body,
None => return None,
let new_body_snippet =
match crate::format_snippet(&body_str, &config, true, context.printer) {
Some(new_body) => new_body,
None => {
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
match crate::format_code_block(&body_str, &config, true, context.printer) {
Some(new_body) => new_body,
None => return None,
}
}
}
};
};

if !filtered_str_fits(&new_body_snippet.snippet, config.max_width(), shape) {
return None;
Expand Down
16 changes: 11 additions & 5 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,21 @@ impl<'a> FmtVisitor<'a> {
self.push_str(&comment_indent.to_string_with_newline(self.config));

let other_lines = &subslice[offset + 1..];
let comment_str =
rewrite_comment(other_lines, false, comment_shape, self.config, self.printer)
.unwrap_or_else(|| String::from(other_lines));
let comment_str = rewrite_comment(
other_lines,
false,
comment_shape,
self.config,
self.printer,
)
.unwrap_or_else(|| String::from(other_lines));
self.push_str(&comment_str);
}
}
} else {
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config, self.printer)
.unwrap_or_else(|| String::from(subslice));
let comment_str =
rewrite_comment(subslice, false, comment_shape, self.config, self.printer)
.unwrap_or_else(|| String::from(subslice));
self.push_str(&comment_str);
}

Expand Down
8 changes: 5 additions & 3 deletions src/parse/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::config::file_lines::LineRange;
use crate::config::options::Color;
use crate::ignore_path::IgnorePathSet;
use crate::parse::parser::{ModError, ModulePathSuccess};
use crate::print::Printer;
use crate::source_map::LineRangeUtils;
use crate::utils::starts_with_newline;
use crate::visitor::SnippetProvider;
use crate::{Config, ErrorKind, FileName};
use crate::print::Printer;

/// ParseSess holds structs necessary for constructing a parser.
pub(crate) struct ParseSess {
Expand Down Expand Up @@ -134,8 +134,10 @@ fn default_dcx(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::new(Box::new(printer.clone()), fallback_bundle))
//Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
Box::new(EmitterWriter::new(
Box::new(printer.clone()),
fallback_bundle,
))
};
DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
Expand Down
Loading