Skip to content

Commit da700b3

Browse files
Rollup merge of rust-lang#119601 - nnethercote:Emitter-cleanups, r=oli-obk
`Emitter` cleanups Some improvements I found while looking at this code. r? `@oli-obk`
2 parents 3087b36 + 8843223 commit da700b3

File tree

13 files changed

+465
-52
lines changed

13 files changed

+465
-52
lines changed

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ fn report_ice(
13931393
) {
13941394
let fallback_bundle =
13951395
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
1396-
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
1396+
let emitter = Box::new(rustc_errors::emitter::HumanEmitter::stderr(
13971397
rustc_errors::ColorConfig::Auto,
13981398
fallback_bundle,
13991399
));

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::source_map::SourceMap;
2020
use rustc_span::SourceFile;
2121

2222
/// Generates diagnostics using annotate-snippet
23-
pub struct AnnotateSnippetEmitterWriter {
23+
pub struct AnnotateSnippetEmitter {
2424
source_map: Option<Lrc<SourceMap>>,
2525
fluent_bundle: Option<Lrc<FluentBundle>>,
2626
fallback_bundle: LazyFallbackBundle,
@@ -33,7 +33,7 @@ pub struct AnnotateSnippetEmitterWriter {
3333
macro_backtrace: bool,
3434
}
3535

36-
impl Translate for AnnotateSnippetEmitterWriter {
36+
impl Translate for AnnotateSnippetEmitter {
3737
fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
3838
self.fluent_bundle.as_ref()
3939
}
@@ -43,7 +43,7 @@ impl Translate for AnnotateSnippetEmitterWriter {
4343
}
4444
}
4545

46-
impl Emitter for AnnotateSnippetEmitterWriter {
46+
impl Emitter for AnnotateSnippetEmitter {
4747
/// The entry point for the diagnostics generation
4848
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
4949
let fluent_args = to_fluent_args(diag.args());
@@ -97,7 +97,7 @@ fn annotation_type_for_level(level: Level) -> AnnotationType {
9797
}
9898
}
9999

100-
impl AnnotateSnippetEmitterWriter {
100+
impl AnnotateSnippetEmitter {
101101
pub fn new(
102102
source_map: Option<Lrc<SourceMap>>,
103103
fluent_bundle: Option<Lrc<FluentBundle>>,

compiler/rustc_errors/src/emitter.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ impl HumanReadableErrorType {
6161
self,
6262
mut dst: Box<dyn WriteColor + Send>,
6363
fallback_bundle: LazyFallbackBundle,
64-
) -> EmitterWriter {
64+
) -> HumanEmitter {
6565
let (short, color_config) = self.unzip();
6666
let color = color_config.suggests_using_colors();
6767
if !dst.supports_color() && color {
6868
dst = Box::new(Ansi::new(dst));
6969
}
70-
EmitterWriter::new(dst, fallback_bundle).short_message(short)
70+
HumanEmitter::new(dst, fallback_bundle).short_message(short)
7171
}
7272
}
7373

@@ -196,13 +196,15 @@ pub trait Emitter: Translate {
196196
fn emit_diagnostic(&mut self, diag: &Diagnostic);
197197

198198
/// Emit a notification that an artifact has been output.
199-
/// This is currently only supported for the JSON format,
200-
/// other formats can, and will, simply ignore it.
199+
/// Currently only supported for the JSON format.
201200
fn emit_artifact_notification(&mut self, _path: &Path, _artifact_type: &str) {}
202201

202+
/// Emit a report about future breakage.
203+
/// Currently only supported for the JSON format.
203204
fn emit_future_breakage_report(&mut self, _diags: Vec<Diagnostic>) {}
204205

205-
/// Emit list of unused externs
206+
/// Emit list of unused externs.
207+
/// Currently only supported for the JSON format.
206208
fn emit_unused_externs(
207209
&mut self,
208210
_lint_level: rustc_lint_defs::Level,
@@ -501,7 +503,7 @@ pub trait Emitter: Translate {
501503
}
502504
}
503505

504-
impl Translate for EmitterWriter {
506+
impl Translate for HumanEmitter {
505507
fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
506508
self.fluent_bundle.as_ref()
507509
}
@@ -511,7 +513,7 @@ impl Translate for EmitterWriter {
511513
}
512514
}
513515

514-
impl Emitter for EmitterWriter {
516+
impl Emitter for HumanEmitter {
515517
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
516518
self.sm.as_ref()
517519
}
@@ -622,7 +624,7 @@ impl ColorConfig {
622624

623625
/// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short`
624626
#[derive(Setters)]
625-
pub struct EmitterWriter {
627+
pub struct HumanEmitter {
626628
#[setters(skip)]
627629
dst: IntoDynSyncSend<Destination>,
628630
sm: Option<Lrc<SourceMap>>,
@@ -647,14 +649,14 @@ pub struct FileWithAnnotatedLines {
647649
multiline_depth: usize,
648650
}
649651

650-
impl EmitterWriter {
651-
pub fn stderr(color_config: ColorConfig, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
652+
impl HumanEmitter {
653+
pub fn stderr(color_config: ColorConfig, fallback_bundle: LazyFallbackBundle) -> HumanEmitter {
652654
let dst = from_stderr(color_config);
653655
Self::create(dst, fallback_bundle)
654656
}
655657

656-
fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
657-
EmitterWriter {
658+
fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> HumanEmitter {
659+
HumanEmitter {
658660
dst: IntoDynSyncSend(dst),
659661
sm: None,
660662
fluent_bundle: None,
@@ -673,7 +675,7 @@ impl EmitterWriter {
673675
pub fn new(
674676
dst: Box<dyn WriteColor + Send>,
675677
fallback_bundle: LazyFallbackBundle,
676-
) -> EmitterWriter {
678+
) -> HumanEmitter {
677679
Self::create(dst, fallback_bundle)
678680
}
679681

compiler/rustc_errors/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use snippet::Style;
5353
pub use termcolor::{Color, ColorSpec, WriteColor};
5454

5555
use crate::diagnostic_impls::{DelayedAtWithNewline, DelayedAtWithoutNewline};
56-
use emitter::{is_case_difference, DynEmitter, Emitter, EmitterWriter};
56+
use emitter::{is_case_difference, DynEmitter, Emitter, HumanEmitter};
5757
use registry::Registry;
5858
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
5959
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
@@ -571,7 +571,7 @@ impl DiagCtxt {
571571
sm: Option<Lrc<SourceMap>>,
572572
fallback_bundle: LazyFallbackBundle,
573573
) -> Self {
574-
let emitter = Box::new(EmitterWriter::stderr(ColorConfig::Auto, fallback_bundle).sm(sm));
574+
let emitter = Box::new(HumanEmitter::stderr(ColorConfig::Auto, fallback_bundle).sm(sm));
575575
Self::with_emitter(emitter)
576576
}
577577
pub fn disable_warnings(mut self) -> Self {

compiler/rustc_expand/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
77
use rustc_span::{BytePos, Span};
88

99
use rustc_data_structures::sync::Lrc;
10-
use rustc_errors::emitter::EmitterWriter;
10+
use rustc_errors::emitter::HumanEmitter;
1111
use rustc_errors::{DiagCtxt, MultiSpan, PResult};
1212
use termcolor::WriteColor;
1313

@@ -30,7 +30,7 @@ fn create_test_handler() -> (DiagCtxt, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
3030
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
3131
false,
3232
);
33-
let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle)
33+
let emitter = HumanEmitter::new(Box::new(Shared { data: output.clone() }), fallback_bundle)
3434
.sm(Some(source_map.clone()))
3535
.diagnostic_width(Some(140));
3636
let dcx = DiagCtxt::with_emitter(Box::new(emitter));

compiler/rustc_session/src/config.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -2035,23 +2035,14 @@ fn check_error_format_stability(
20352035
early_dcx: &mut EarlyDiagCtxt,
20362036
unstable_opts: &UnstableOptions,
20372037
error_format: ErrorOutputType,
2038-
json_rendered: HumanReadableErrorType,
20392038
) {
20402039
if !unstable_opts.unstable_options {
2041-
if let ErrorOutputType::Json { pretty: true, json_rendered } = error_format {
2042-
early_dcx.abort_if_error_and_set_error_format(ErrorOutputType::Json {
2043-
pretty: false,
2044-
json_rendered,
2045-
});
2040+
if let ErrorOutputType::Json { pretty: true, .. } = error_format {
20462041
early_dcx.early_fatal("`--error-format=pretty-json` is unstable");
20472042
}
20482043
if let ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet(_)) =
20492044
error_format
20502045
{
2051-
early_dcx.abort_if_error_and_set_error_format(ErrorOutputType::Json {
2052-
pretty: false,
2053-
json_rendered,
2054-
});
20552046
early_dcx.early_fatal("`--error-format=human-annotate-rs` is unstable");
20562047
}
20572048
}
@@ -2649,7 +2640,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26492640
let mut unstable_opts = UnstableOptions::build(early_dcx, matches);
26502641
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches);
26512642

2652-
check_error_format_stability(early_dcx, &unstable_opts, error_format, json_rendered);
2643+
check_error_format_stability(early_dcx, &unstable_opts, error_format);
26532644

26542645
if !unstable_opts.unstable_options && json_unused_externs.is_enabled() {
26552646
early_dcx.early_fatal(

compiler/rustc_session/src/session.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
1717
use rustc_data_structures::sync::{
1818
AtomicU64, DynSend, DynSync, Lock, Lrc, OneThread, Ordering::SeqCst,
1919
};
20-
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitterWriter;
21-
use rustc_errors::emitter::{DynEmitter, EmitterWriter, HumanReadableErrorType};
20+
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
21+
use rustc_errors::emitter::{DynEmitter, HumanEmitter, HumanReadableErrorType};
2222
use rustc_errors::json::JsonEmitter;
2323
use rustc_errors::registry::Registry;
2424
use rustc_errors::{
@@ -1000,7 +1000,7 @@ fn default_emitter(
10001000
let (short, color_config) = kind.unzip();
10011001

10021002
if let HumanReadableErrorType::AnnotateSnippet(_) = kind {
1003-
let emitter = AnnotateSnippetEmitterWriter::new(
1003+
let emitter = AnnotateSnippetEmitter::new(
10041004
Some(source_map),
10051005
bundle,
10061006
fallback_bundle,
@@ -1009,7 +1009,7 @@ fn default_emitter(
10091009
);
10101010
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
10111011
} else {
1012-
let emitter = EmitterWriter::stderr(color_config, fallback_bundle)
1012+
let emitter = HumanEmitter::stderr(color_config, fallback_bundle)
10131013
.fluent_bundle(bundle)
10141014
.sm(Some(source_map))
10151015
.short_message(short)
@@ -1504,7 +1504,7 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
15041504
let emitter: Box<DynEmitter> = match output {
15051505
config::ErrorOutputType::HumanReadable(kind) => {
15061506
let (short, color_config) = kind.unzip();
1507-
Box::new(EmitterWriter::stderr(color_config, fallback_bundle).short_message(short))
1507+
Box::new(HumanEmitter::stderr(color_config, fallback_bundle).short_message(short))
15081508
}
15091509
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::basic(
15101510
pretty,

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22
use rustc_data_structures::sync::Lrc;
33
use rustc_data_structures::unord::UnordSet;
4-
use rustc_errors::emitter::{DynEmitter, EmitterWriter};
4+
use rustc_errors::emitter::{DynEmitter, HumanEmitter};
55
use rustc_errors::json::JsonEmitter;
66
use rustc_errors::TerminalUrl;
77
use rustc_feature::UnstableFeatures;
@@ -138,7 +138,7 @@ pub(crate) fn new_dcx(
138138
ErrorOutputType::HumanReadable(kind) => {
139139
let (short, color_config) = kind.unzip();
140140
Box::new(
141-
EmitterWriter::stderr(color_config, fallback_bundle)
141+
HumanEmitter::stderr(color_config, fallback_bundle)
142142
.sm(source_map.map(|sm| sm as _))
143143
.short_message(short)
144144
.teach(unstable_opts.teach)

src/librustdoc/doctest.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ pub(crate) fn make_test(
557557
// crate already is included.
558558
let result = rustc_driver::catch_fatal_errors(|| {
559559
rustc_span::create_session_if_not_set_then(edition, |_| {
560-
use rustc_errors::emitter::{Emitter, EmitterWriter};
560+
use rustc_errors::emitter::{Emitter, HumanEmitter};
561561
use rustc_errors::DiagCtxt;
562562
use rustc_parse::parser::ForceCollect;
563563
use rustc_span::source_map::FilePathMapping;
@@ -572,11 +572,11 @@ pub(crate) fn make_test(
572572
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
573573
false,
574574
);
575-
supports_color = EmitterWriter::stderr(ColorConfig::Auto, fallback_bundle.clone())
575+
supports_color = HumanEmitter::stderr(ColorConfig::Auto, fallback_bundle.clone())
576576
.diagnostic_width(Some(80))
577577
.supports_color();
578578

579-
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
579+
let emitter = HumanEmitter::new(Box::new(io::sink()), fallback_bundle);
580580

581581
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
582582
let dcx = DiagCtxt::with_emitter(Box::new(emitter)).disable_warnings();
@@ -739,7 +739,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
739739
}
740740
rustc_driver::catch_fatal_errors(|| {
741741
rustc_span::create_session_if_not_set_then(edition, |_| {
742-
use rustc_errors::emitter::EmitterWriter;
742+
use rustc_errors::emitter::HumanEmitter;
743743
use rustc_errors::DiagCtxt;
744744
use rustc_span::source_map::FilePathMapping;
745745

@@ -752,7 +752,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
752752
false,
753753
);
754754

755-
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
755+
let emitter = HumanEmitter::new(Box::new(io::sink()), fallback_bundle);
756756

757757
let dcx = DiagCtxt::with_emitter(Box::new(emitter)).disable_warnings();
758758
let sess = ParseSess::with_dcx(dcx, sm);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::doc::{NEEDLESS_DOCTEST_MAIN, TEST_ATTR_IN_DOCTEST};
55
use clippy_utils::diagnostics::span_lint;
66
use rustc_ast::{CoroutineKind, Fn, FnRetTy, Item, ItemKind};
77
use rustc_data_structures::sync::Lrc;
8-
use rustc_errors::emitter::EmitterWriter;
8+
use rustc_errors::emitter::HumanEmitter;
99
use rustc_errors::DiagCtxt;
1010
use rustc_lint::LateContext;
1111
use rustc_parse::maybe_new_parser_from_source_str;
@@ -44,7 +44,7 @@ pub fn check(
4444

4545
let fallback_bundle =
4646
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
47-
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle);
47+
let emitter = HumanEmitter::new(Box::new(io::sink()), fallback_bundle);
4848
let dcx = DiagCtxt::with_emitter(Box::new(emitter)).disable_warnings();
4949
#[expect(clippy::arc_with_non_send_sync)] // `Lrc` is expected by with_dcx
5050
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22
use std::sync::atomic::{AtomicBool, Ordering};
33

44
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
5-
use rustc_errors::emitter::{DynEmitter, Emitter, EmitterWriter};
5+
use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter};
66
use rustc_errors::translation::Translate;
77
use rustc_errors::{ColorConfig, DiagCtxt, Diagnostic, Level as DiagnosticLevel};
88
use rustc_session::parse::ParseSess as RawParseSess;
@@ -139,7 +139,7 @@ fn default_dcx(
139139
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
140140
false,
141141
);
142-
Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
142+
Box::new(HumanEmitter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
143143
};
144144
DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
145145
has_non_ignorable_parser_errors: false,

tests/ui/lint/use_suggestion_json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ignore-windows
22
// ignore-sgx std::os::fortanix_sgx::usercalls::alloc::Iter changes compiler suggestions
3-
// compile-flags: --error-format pretty-json --json=diagnostic-rendered-ansi
3+
// compile-flags: --error-format pretty-json --json=diagnostic-rendered-ansi -Z unstable-options
44

55
// The output for humans should just highlight the whole span without showing
66
// the suggested replacement, but we also want to test that suggested

0 commit comments

Comments
 (0)