Skip to content

Commit 52bdc37

Browse files
committed
Auto merge of #114054 - oli-obk:cleanups, r=estebank
Split some functions with many arguments into builder pattern functions r? `@estebank` This doesn't resolve all of the ones in rustc, mostly because I need to do other cleanups in order to be able to use some builder derives from crates.io Works around #90672 by making `x test rustfmt --bless` format itself instead of testing that it is formatted
2 parents a6236fa + 841f8dc commit 52bdc37

File tree

15 files changed

+69
-108
lines changed

15 files changed

+69
-108
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
362362

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

368368
pub fn config(&self, kind: ModuleKind) -> &ModuleConfig {

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14151415
false,
14161416
TerminalUrl::No,
14171417
));
1418-
let handler = rustc_errors::Handler::with_emitter(true, None, emitter, None);
1418+
let handler = rustc_errors::Handler::with_emitter(emitter);
14191419

14201420
// a .span_bug or .bug call has already printed what
14211421
// it wants to print.

compiler/rustc_errors/src/diagnostic_builder.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
536536
}
537537
};
538538

539-
if handler.flags.dont_buffer_diagnostics || handler.flags.treat_err_as_bug.is_some() {
539+
if handler.inner.lock().flags.dont_buffer_diagnostics
540+
|| handler.inner.lock().flags.treat_err_as_bug.is_some()
541+
{
540542
self.emit();
541543
return None;
542544
}

compiler/rustc_errors/src/json/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
6464
);
6565

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

7070
let bytes = output.lock().unwrap();

compiler/rustc_errors/src/lib.rs

+26-49
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ use std::backtrace::{Backtrace, BacktraceStatus};
391391
/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
392392
/// others log errors for later reporting.
393393
pub struct Handler {
394-
flags: HandlerFlags,
395394
inner: Lock<HandlerInner>,
396395
}
397396

@@ -549,69 +548,47 @@ impl Drop for HandlerInner {
549548

550549
impl Handler {
551550
pub fn with_tty_emitter(
552-
color_config: ColorConfig,
553-
can_emit_warnings: bool,
554-
treat_err_as_bug: Option<NonZeroUsize>,
555551
sm: Option<Lrc<SourceMap>>,
556-
fluent_bundle: Option<Lrc<FluentBundle>>,
557552
fallback_bundle: LazyFallbackBundle,
558-
ice_file: Option<PathBuf>,
559-
) -> Self {
560-
Self::with_tty_emitter_and_flags(
561-
color_config,
562-
sm,
563-
fluent_bundle,
564-
fallback_bundle,
565-
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
566-
ice_file,
567-
)
568-
}
569-
570-
pub fn with_tty_emitter_and_flags(
571-
color_config: ColorConfig,
572-
sm: Option<Lrc<SourceMap>>,
573-
fluent_bundle: Option<Lrc<FluentBundle>>,
574-
fallback_bundle: LazyFallbackBundle,
575-
flags: HandlerFlags,
576-
ice_file: Option<PathBuf>,
577553
) -> Self {
578554
let emitter = Box::new(EmitterWriter::stderr(
579-
color_config,
555+
ColorConfig::Auto,
580556
sm,
581-
fluent_bundle,
557+
None,
582558
fallback_bundle,
583559
false,
584560
false,
585561
None,
586-
flags.macro_backtrace,
587-
flags.track_diagnostics,
562+
false,
563+
false,
588564
TerminalUrl::No,
589565
));
590-
Self::with_emitter_and_flags(emitter, flags, ice_file)
566+
Self::with_emitter(emitter)
567+
}
568+
pub fn disable_warnings(mut self) -> Self {
569+
self.inner.get_mut().flags.can_emit_warnings = false;
570+
self
591571
}
592572

593-
pub fn with_emitter(
594-
can_emit_warnings: bool,
595-
treat_err_as_bug: Option<NonZeroUsize>,
596-
emitter: Box<dyn Emitter + sync::Send>,
597-
ice_file: Option<PathBuf>,
598-
) -> Self {
599-
Handler::with_emitter_and_flags(
600-
emitter,
601-
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
602-
ice_file,
603-
)
573+
pub fn treat_err_as_bug(mut self, treat_err_as_bug: NonZeroUsize) -> Self {
574+
self.inner.get_mut().flags.treat_err_as_bug = Some(treat_err_as_bug);
575+
self
604576
}
605577

606-
pub fn with_emitter_and_flags(
607-
emitter: Box<dyn Emitter + sync::Send>,
608-
flags: HandlerFlags,
609-
ice_file: Option<PathBuf>,
610-
) -> Self {
578+
pub fn with_flags(mut self, flags: HandlerFlags) -> Self {
579+
self.inner.get_mut().flags = flags;
580+
self
581+
}
582+
583+
pub fn with_ice_file(mut self, ice_file: PathBuf) -> Self {
584+
self.inner.get_mut().ice_file = Some(ice_file);
585+
self
586+
}
587+
588+
pub fn with_emitter(emitter: Box<dyn Emitter + sync::Send>) -> Self {
611589
Self {
612-
flags,
613590
inner: Lock::new(HandlerInner {
614-
flags,
591+
flags: HandlerFlags { can_emit_warnings: true, ..Default::default() },
615592
lint_err_count: 0,
616593
err_count: 0,
617594
warn_count: 0,
@@ -629,7 +606,7 @@ impl Handler {
629606
check_unstable_expect_diagnostics: false,
630607
unstable_expect_diagnostics: Vec::new(),
631608
fulfilled_expectations: Default::default(),
632-
ice_file,
609+
ice_file: None,
633610
}),
634611
}
635612
}
@@ -657,7 +634,7 @@ impl Handler {
657634
// This is here to not allow mutation of flags;
658635
// as of this writing it's only used in tests in librustc_middle.
659636
pub fn can_emit_warnings(&self) -> bool {
660-
self.flags.can_emit_warnings
637+
self.inner.lock().flags.can_emit_warnings
661638
}
662639

663640
/// Resets the diagnostic error count as well as the cached emitted diagnostics.

compiler/rustc_expand/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
161161
false,
162162
TerminalUrl::No,
163163
);
164-
let handler = Handler::with_emitter(true, None, Box::new(emitter), None);
164+
let handler = Handler::with_emitter(Box::new(emitter));
165165
#[allow(rustc::untranslatable_diagnostic)]
166166
handler.span_err(msp, "foo");
167167

compiler/rustc_session/src/parse.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::lint::{
99
use rustc_ast::node_id::NodeId;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
1111
use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc};
12-
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
12+
use rustc_errors::{emitter::SilentEmitter, Handler};
1313
use rustc_errors::{
1414
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
1515
EmissionGuarantee, ErrorGuaranteed, IntoDiagnostic, MultiSpan, Noted, StashKey,
@@ -224,15 +224,7 @@ impl ParseSess {
224224
pub fn new(locale_resources: Vec<&'static str>, file_path_mapping: FilePathMapping) -> Self {
225225
let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
226226
let sm = Lrc::new(SourceMap::new(file_path_mapping));
227-
let handler = Handler::with_tty_emitter(
228-
ColorConfig::Auto,
229-
true,
230-
None,
231-
Some(sm.clone()),
232-
None,
233-
fallback_bundle,
234-
None,
235-
);
227+
let handler = Handler::with_tty_emitter(Some(sm.clone()), fallback_bundle);
236228
ParseSess::with_span_handler(handler, sm)
237229
}
238230

@@ -262,21 +254,9 @@ impl ParseSess {
262254
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
263255
let fallback_bundle = fallback_fluent_bundle(Vec::new(), false);
264256
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
265-
let fatal_handler = Handler::with_tty_emitter(
266-
ColorConfig::Auto,
267-
false,
268-
None,
269-
None,
270-
None,
271-
fallback_bundle,
272-
None,
273-
);
274-
let handler = Handler::with_emitter(
275-
false,
276-
None,
277-
Box::new(SilentEmitter { fatal_handler, fatal_note }),
278-
None,
279-
);
257+
let fatal_handler = Handler::with_tty_emitter(None, fallback_bundle).disable_warnings();
258+
let handler = Handler::with_emitter(Box::new(SilentEmitter { fatal_handler, fatal_note }))
259+
.disable_warnings();
280260
ParseSess::with_span_handler(handler, sm)
281261
}
282262

compiler/rustc_session/src/session.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1442,11 +1442,11 @@ pub fn build_session(
14421442
);
14431443
let emitter = default_emitter(&sopts, registry, source_map.clone(), bundle, fallback_bundle);
14441444

1445-
let span_diagnostic = rustc_errors::Handler::with_emitter_and_flags(
1446-
emitter,
1447-
sopts.unstable_opts.diagnostic_handler_flags(can_emit_warnings),
1448-
ice_file,
1449-
);
1445+
let mut span_diagnostic = rustc_errors::Handler::with_emitter(emitter)
1446+
.with_flags(sopts.unstable_opts.diagnostic_handler_flags(can_emit_warnings));
1447+
if let Some(ice_file) = ice_file {
1448+
span_diagnostic = span_diagnostic.with_ice_file(ice_file);
1449+
}
14501450

14511451
let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile
14521452
{
@@ -1737,7 +1737,7 @@ pub struct EarlyErrorHandler {
17371737
impl EarlyErrorHandler {
17381738
pub fn new(output: ErrorOutputType) -> Self {
17391739
let emitter = mk_emitter(output);
1740-
Self { handler: rustc_errors::Handler::with_emitter(true, None, emitter, None) }
1740+
Self { handler: rustc_errors::Handler::with_emitter(emitter) }
17411741
}
17421742

17431743
pub fn abort_if_errors(&self) {
@@ -1751,7 +1751,7 @@ impl EarlyErrorHandler {
17511751
self.handler.abort_if_errors();
17521752

17531753
let emitter = mk_emitter(output);
1754-
self.handler = Handler::with_emitter(true, None, emitter, None);
1754+
self.handler = Handler::with_emitter(emitter);
17551755
}
17561756

17571757
#[allow(rustc::untranslatable_diagnostic)]

src/bootstrap/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ impl Step for Rustfmt {
430430
&[],
431431
);
432432

433+
if builder.config.cmd.bless() {
434+
cargo.env("BLESS", "1");
435+
}
436+
433437
let dir = testdir(builder, compiler.host);
434438
t!(fs::create_dir_all(&dir));
435439
cargo.env("RUSTFMT_TEST_DIR", dir);

src/librustdoc/core.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,8 @@ pub(crate) fn new_handler(
173173
}
174174
};
175175

176-
rustc_errors::Handler::with_emitter_and_flags(
177-
emitter,
178-
unstable_opts.diagnostic_handler_flags(true),
179-
None,
180-
)
176+
rustc_errors::Handler::with_emitter(emitter)
177+
.with_flags(unstable_opts.diagnostic_handler_flags(true))
181178
}
182179

183180
/// Parse, resolve, and typecheck the given crate.

src/librustdoc/doctest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub(crate) fn make_test(
587587
);
588588

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

593593
let mut found_main = false;
@@ -774,7 +774,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
774774
TerminalUrl::No,
775775
);
776776

777-
let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
777+
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
778778
let sess = ParseSess::with_span_handler(handler, sm);
779779
let mut parser =
780780
match maybe_new_parser_from_source_str(&sess, filename, source.to_owned()) {

src/librustdoc/passes/lint/check_code_block_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn check_rust_syntax(
4040
let emitter = BufferEmitter { buffer: Lrc::clone(&buffer), fallback_bundle };
4141

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

src/tools/clippy/clippy_lints/src/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
729729
false,
730730
TerminalUrl::No,
731731
);
732-
let handler = Handler::with_emitter(false, None, Box::new(emitter), None);
732+
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
733733
let sess = ParseSess::with_span_handler(handler, sm);
734734

735735
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code) {

src/tools/rustfmt/src/parse/session.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,13 @@ fn default_handler(
152152
TerminalUrl::No,
153153
))
154154
};
155-
Handler::with_emitter(
156-
true,
157-
None,
158-
Box::new(SilentOnIgnoredFilesEmitter {
159-
has_non_ignorable_parser_errors: false,
160-
source_map,
161-
emitter,
162-
ignore_path_set,
163-
can_reset,
164-
}),
165-
None,
166-
)
155+
Handler::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
156+
has_non_ignorable_parser_errors: false,
157+
source_map,
158+
emitter,
159+
ignore_path_set,
160+
can_reset,
161+
}))
167162
}
168163

169164
impl ParseSess {
@@ -234,7 +229,7 @@ impl ParseSess {
234229
}
235230

236231
pub(crate) fn set_silent_emitter(&mut self) {
237-
self.parse_sess.span_diagnostic = Handler::with_emitter(true, None, silent_emitter(), None);
232+
self.parse_sess.span_diagnostic = Handler::with_emitter(silent_emitter());
238233
}
239234

240235
pub(crate) fn span_to_filename(&self, span: Span) -> FileName {

src/tools/rustfmt/src/test/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,12 @@ fn handle_result(
838838

839839
// Ignore LF and CRLF difference for Windows.
840840
if !string_eq_ignore_newline_repr(&fmt_text, &text) {
841+
if let Some(bless) = std::env::var_os("BLESS") {
842+
if bless != "0" {
843+
std::fs::write(file_name, fmt_text).unwrap();
844+
continue;
845+
}
846+
}
841847
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
842848
assert!(
843849
!diff.is_empty(),

0 commit comments

Comments
 (0)