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

Split some functions with many arguments into builder pattern functions #114054

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {

impl<B: WriteBackendMethods> CodegenContext<B> {
pub fn create_diag_handler(&self) -> Handler {
Handler::with_emitter(true, None, Box::new(self.diag_emitter.clone()), None)
Handler::with_emitter(Box::new(self.diag_emitter.clone()))
}

pub fn config(&self, kind: ModuleKind) -> &ModuleConfig {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
false,
TerminalUrl::No,
));
let handler = rustc_errors::Handler::with_emitter(true, None, emitter, None);
let handler = rustc_errors::Handler::with_emitter(emitter);

// a .span_bug or .bug call has already printed what
// it wants to print.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
}
};

if handler.flags.dont_buffer_diagnostics || handler.flags.treat_err_as_bug.is_some() {
if handler.inner.lock().flags.dont_buffer_diagnostics
|| handler.inner.lock().flags.treat_err_as_bug.is_some()
{
self.emit();
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
);

let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
let handler = Handler::with_emitter(true, None, Box::new(je), None);
let handler = Handler::with_emitter(Box::new(je));
handler.span_err(span, "foo");

let bytes = output.lock().unwrap();
Expand Down
75 changes: 26 additions & 49 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ use std::backtrace::{Backtrace, BacktraceStatus};
/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
/// others log errors for later reporting.
pub struct Handler {
flags: HandlerFlags,
inner: Lock<HandlerInner>,
}

Expand Down Expand Up @@ -549,69 +548,47 @@ impl Drop for HandlerInner {

impl Handler {
pub fn with_tty_emitter(
color_config: ColorConfig,
can_emit_warnings: bool,
treat_err_as_bug: Option<NonZeroUsize>,
sm: Option<Lrc<SourceMap>>,
fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
ice_file: Option<PathBuf>,
) -> Self {
Self::with_tty_emitter_and_flags(
color_config,
sm,
fluent_bundle,
fallback_bundle,
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
ice_file,
)
}

pub fn with_tty_emitter_and_flags(
color_config: ColorConfig,
sm: Option<Lrc<SourceMap>>,
fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
flags: HandlerFlags,
ice_file: Option<PathBuf>,
) -> Self {
let emitter = Box::new(EmitterWriter::stderr(
color_config,
ColorConfig::Auto,
sm,
fluent_bundle,
None,
fallback_bundle,
false,
false,
None,
flags.macro_backtrace,
flags.track_diagnostics,
false,
false,
TerminalUrl::No,
));
Self::with_emitter_and_flags(emitter, flags, ice_file)
Self::with_emitter(emitter)
}
pub fn disable_warnings(mut self) -> Self {
self.inner.get_mut().flags.can_emit_warnings = false;
self
}

pub fn with_emitter(
can_emit_warnings: bool,
treat_err_as_bug: Option<NonZeroUsize>,
emitter: Box<dyn Emitter + sync::Send>,
ice_file: Option<PathBuf>,
) -> Self {
Handler::with_emitter_and_flags(
emitter,
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
ice_file,
)
pub fn treat_err_as_bug(mut self, treat_err_as_bug: NonZeroUsize) -> Self {
self.inner.get_mut().flags.treat_err_as_bug = Some(treat_err_as_bug);
self
}

pub fn with_emitter_and_flags(
emitter: Box<dyn Emitter + sync::Send>,
flags: HandlerFlags,
ice_file: Option<PathBuf>,
) -> Self {
pub fn with_flags(mut self, flags: HandlerFlags) -> Self {
self.inner.get_mut().flags = flags;
self
}

pub fn with_ice_file(mut self, ice_file: PathBuf) -> Self {
self.inner.get_mut().ice_file = Some(ice_file);
self
}

pub fn with_emitter(emitter: Box<dyn Emitter + sync::Send>) -> Self {
Self {
flags,
inner: Lock::new(HandlerInner {
flags,
flags: HandlerFlags { can_emit_warnings: true, ..Default::default() },
lint_err_count: 0,
err_count: 0,
warn_count: 0,
Expand All @@ -629,7 +606,7 @@ impl Handler {
check_unstable_expect_diagnostics: false,
unstable_expect_diagnostics: Vec::new(),
fulfilled_expectations: Default::default(),
ice_file,
ice_file: None,
}),
}
}
Expand Down Expand Up @@ -657,7 +634,7 @@ impl Handler {
// This is here to not allow mutation of flags;
// as of this writing it's only used in tests in librustc_middle.
pub fn can_emit_warnings(&self) -> bool {
self.flags.can_emit_warnings
self.inner.lock().flags.can_emit_warnings
}

/// Resets the diagnostic error count as well as the cached emitted diagnostics.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
false,
TerminalUrl::No,
);
let handler = Handler::with_emitter(true, None, Box::new(emitter), None);
let handler = Handler::with_emitter(Box::new(emitter));
#[allow(rustc::untranslatable_diagnostic)]
handler.span_err(msp, "foo");

Expand Down
30 changes: 5 additions & 25 deletions compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::lint::{
use rustc_ast::node_id::NodeId;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc};
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
use rustc_errors::{emitter::SilentEmitter, Handler};
use rustc_errors::{
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
EmissionGuarantee, ErrorGuaranteed, IntoDiagnostic, MultiSpan, Noted, StashKey,
Expand Down Expand Up @@ -224,15 +224,7 @@ impl ParseSess {
pub fn new(locale_resources: Vec<&'static str>, file_path_mapping: FilePathMapping) -> Self {
let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
let sm = Lrc::new(SourceMap::new(file_path_mapping));
let handler = Handler::with_tty_emitter(
ColorConfig::Auto,
true,
None,
Some(sm.clone()),
None,
fallback_bundle,
None,
);
let handler = Handler::with_tty_emitter(Some(sm.clone()), fallback_bundle);
ParseSess::with_span_handler(handler, sm)
}

Expand Down Expand Up @@ -262,21 +254,9 @@ impl ParseSess {
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
let fallback_bundle = fallback_fluent_bundle(Vec::new(), false);
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let fatal_handler = Handler::with_tty_emitter(
ColorConfig::Auto,
false,
None,
None,
None,
fallback_bundle,
None,
);
let handler = Handler::with_emitter(
false,
None,
Box::new(SilentEmitter { fatal_handler, fatal_note }),
None,
);
let fatal_handler = Handler::with_tty_emitter(None, fallback_bundle).disable_warnings();
let handler = Handler::with_emitter(Box::new(SilentEmitter { fatal_handler, fatal_note }))
.disable_warnings();
ParseSess::with_span_handler(handler, sm)
}

Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,11 +1442,11 @@ pub fn build_session(
);
let emitter = default_emitter(&sopts, registry, source_map.clone(), bundle, fallback_bundle);

let span_diagnostic = rustc_errors::Handler::with_emitter_and_flags(
emitter,
sopts.unstable_opts.diagnostic_handler_flags(can_emit_warnings),
ice_file,
);
let mut span_diagnostic = rustc_errors::Handler::with_emitter(emitter)
.with_flags(sopts.unstable_opts.diagnostic_handler_flags(can_emit_warnings));
if let Some(ice_file) = ice_file {
span_diagnostic = span_diagnostic.with_ice_file(ice_file);
}

let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile
{
Expand Down Expand Up @@ -1737,7 +1737,7 @@ pub struct EarlyErrorHandler {
impl EarlyErrorHandler {
pub fn new(output: ErrorOutputType) -> Self {
let emitter = mk_emitter(output);
Self { handler: rustc_errors::Handler::with_emitter(true, None, emitter, None) }
Self { handler: rustc_errors::Handler::with_emitter(emitter) }
}

pub fn abort_if_errors(&self) {
Expand All @@ -1751,7 +1751,7 @@ impl EarlyErrorHandler {
self.handler.abort_if_errors();

let emitter = mk_emitter(output);
self.handler = Handler::with_emitter(true, None, emitter, None);
self.handler = Handler::with_emitter(emitter);
}

#[allow(rustc::untranslatable_diagnostic)]
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ impl Step for Rustfmt {
&[],
);

if builder.config.cmd.bless() {
cargo.env("BLESS", "1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk - This seems reasonable to me, though I'd like to change the var name to be a little more explicit (perhaps something like RUSTC_BLESS?) so that it's somewhat more explicit especially when working with rustfmt out of tree.

Wanted to make sure you didn't have any objections or alternative naming suggestions before I opened a PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently being worked on in #113298

}

let dir = testdir(builder, compiler.host);
t!(fs::create_dir_all(&dir));
cargo.env("RUSTFMT_TEST_DIR", dir);
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ pub(crate) fn new_handler(
}
};

rustc_errors::Handler::with_emitter_and_flags(
emitter,
unstable_opts.diagnostic_handler_flags(true),
None,
)
rustc_errors::Handler::with_emitter(emitter)
.with_flags(unstable_opts.diagnostic_handler_flags(true))
}

/// Parse, resolve, and typecheck the given crate.
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ pub(crate) fn make_test(
);

// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm);

let mut found_main = false;
Expand Down Expand Up @@ -774,7 +774,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
TerminalUrl::No,
);

let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm);
let mut parser =
match maybe_new_parser_from_source_str(&sess, filename, source.to_owned()) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/lint/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn check_rust_syntax(
let emitter = BufferEmitter { buffer: Lrc::clone(&buffer), fallback_bundle };

let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let source = dox[code_block.code].to_owned();
let sess = ParseSess::with_span_handler(handler, sm);

Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
false,
TerminalUrl::No,
);
let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm);

let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code) {
Expand Down
21 changes: 8 additions & 13 deletions src/tools/rustfmt/src/parse/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,13 @@ fn default_handler(
TerminalUrl::No,
))
};
Handler::with_emitter(
true,
None,
Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
source_map,
emitter,
ignore_path_set,
can_reset,
}),
None,
)
Handler::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
source_map,
emitter,
ignore_path_set,
can_reset,
}))
}

impl ParseSess {
Expand Down Expand Up @@ -234,7 +229,7 @@ impl ParseSess {
}

pub(crate) fn set_silent_emitter(&mut self) {
self.parse_sess.span_diagnostic = Handler::with_emitter(true, None, silent_emitter(), None);
self.parse_sess.span_diagnostic = Handler::with_emitter(silent_emitter());
}

pub(crate) fn span_to_filename(&self, span: Span) -> FileName {
Expand Down
6 changes: 6 additions & 0 deletions src/tools/rustfmt/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,12 @@ fn handle_result(

// Ignore LF and CRLF difference for Windows.
if !string_eq_ignore_newline_repr(&fmt_text, &text) {
if let Some(bless) = std::env::var_os("BLESS") {
if bless != "0" {
std::fs::write(file_name, fmt_text).unwrap();
continue;
}
}
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
assert!(
!diff.is_empty(),
Expand Down
Loading