Skip to content
Merged
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
Use builder pattern instead of lots of arguments for `EmitterWriter::…
…new`
  • Loading branch information
oli-obk committed Jul 31, 2023
commit 0e7ec9683dae0a0bc66797b1351059ed642f4e2d
47 changes: 6 additions & 41 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,11 @@ impl HumanReadableErrorType {
pub fn new_emitter(
self,
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMap>>,
bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
teach: bool,
diagnostic_width: Option<usize>,
macro_backtrace: bool,
track_diagnostics: bool,
terminal_url: TerminalUrl,
) -> EmitterWriter {
let (short, color_config) = self.unzip();
let color = color_config.suggests_using_colors();
EmitterWriter::new(
dst,
source_map,
bundle,
fallback_bundle,
short,
teach,
color,
diagnostic_width,
macro_backtrace,
track_diagnostics,
terminal_url,
)
EmitterWriter::new(dst, fallback_bundle, color).short_message(short)
}
}

Expand Down Expand Up @@ -668,6 +649,10 @@ pub struct FileWithAnnotatedLines {
impl EmitterWriter {
pub fn stderr(color_config: ColorConfig, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
let dst = Destination::from_stderr(color_config);
Self::create(dst, fallback_bundle)
}

fn create(dst: Destination, fallback_bundle: LazyFallbackBundle) -> EmitterWriter {
EmitterWriter {
dst,
sm: None,
Expand All @@ -685,30 +670,10 @@ impl EmitterWriter {

pub fn new(
dst: Box<dyn Write + Send>,
source_map: Option<Lrc<SourceMap>>,
fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
short_message: bool,
teach: bool,
colored: bool,
diagnostic_width: Option<usize>,
macro_backtrace: bool,
track_diagnostics: bool,
terminal_url: TerminalUrl,
) -> EmitterWriter {
EmitterWriter {
dst: Raw(dst, colored),
sm: source_map,
fluent_bundle,
fallback_bundle,
short_message,
teach,
ui_testing: false,
diagnostic_width,
macro_backtrace,
track_diagnostics,
terminal_url,
}
Self::create(Raw(dst, colored), fallback_bundle)
}

fn maybe_anonymized(&self, line_num: usize) -> Cow<'static, str> {
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,13 @@ impl Diagnostic {
let buf = BufWriter::default();
let output = buf.clone();
je.json_rendered
.new_emitter(
Box::new(buf),
Some(je.sm.clone()),
je.fluent_bundle.clone(),
je.fallback_bundle.clone(),
false,
je.diagnostic_width,
je.macro_backtrace,
je.track_diagnostics,
je.terminal_url,
)
.new_emitter(Box::new(buf), je.fallback_bundle.clone())
.sm(Some(je.sm.clone()))
.fluent_bundle(je.fluent_bundle.clone())
.diagnostic_width(je.diagnostic_width)
.macro_backtrace(je.macro_backtrace)
.track_diagnostics(je.track_diagnostics)
.terminal_url(je.terminal_url)
.ui_testing(je.ui_testing)
.emit_diagnostic(diag);
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
Expand Down
19 changes: 5 additions & 14 deletions compiler/rustc_expand/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_span::{BytePos, Span};

use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter;
use rustc_errors::{Handler, MultiSpan, PResult, TerminalUrl};
use rustc_errors::{Handler, MultiSpan, PResult};

use std::io;
use std::io::prelude::*;
Expand All @@ -29,19 +29,10 @@ fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
false,
);
let emitter = EmitterWriter::new(
Box::new(Shared { data: output.clone() }),
Some(source_map.clone()),
None,
fallback_bundle,
false,
false,
false,
Some(140),
false,
false,
TerminalUrl::No,
);
let emitter =
EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle, false)
.sm(Some(source_map.clone()))
.diagnostic_width(Some(140));
let handler = Handler::with_emitter(Box::new(emitter));
(handler, source_map, output)
}
Expand Down
30 changes: 3 additions & 27 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{ColorConfig, ErrorGuaranteed, FatalError, TerminalUrl};
use rustc_errors::{ColorConfig, ErrorGuaranteed, FatalError};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{self as hir, intravisit, CRATE_HIR_ID};
use rustc_interface::interface;
Expand Down Expand Up @@ -562,19 +562,7 @@ pub(crate) fn make_test(
.diagnostic_width(Some(80))
.supports_color();

let emitter = EmitterWriter::new(
Box::new(io::sink()),
None,
None,
fallback_bundle,
false,
false,
false,
None,
false,
false,
TerminalUrl::No,
);
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false);

// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
Expand Down Expand Up @@ -750,19 +738,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
false,
);

let emitter = EmitterWriter::new(
Box::new(io::sink()),
None,
None,
fallback_bundle,
false,
false,
false,
None,
false,
false,
TerminalUrl::No,
);
let emitter = EmitterWriter::new(Box::new(io::sink()), fallback_bundle, false);

let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm);
Expand Down
10 changes: 1 addition & 9 deletions src/tools/clippy/clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_ast::token::CommentKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter;
use rustc_errors::{Applicability, Handler, SuggestionStyle, TerminalUrl};
use rustc_errors::{Applicability, Handler, SuggestionStyle};
use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{AnonConst, Expr};
Expand Down Expand Up @@ -718,16 +718,8 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
rustc_errors::fallback_fluent_bundle(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
let emitter = EmitterWriter::new(
Box::new(io::sink()),
None,
None,
fallback_bundle,
false,
false,
false,
None,
false,
false,
TerminalUrl::No,
);
let handler = Handler::with_emitter(Box::new(emitter)).disable_warnings();
let sess = ParseSess::with_span_handler(handler, sm);
Expand Down
15 changes: 2 additions & 13 deletions src/tools/rustfmt/src/parse/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use rustc_data_structures::sync::{Lrc, Send};
use rustc_errors::emitter::{Emitter, EmitterWriter};
use rustc_errors::translation::Translate;
use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel, TerminalUrl};
use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel};
use rustc_session::parse::ParseSess as RawParseSess;
use rustc_span::{
source_map::{FilePathMapping, SourceMap},
Expand Down Expand Up @@ -139,18 +139,7 @@ fn default_handler(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::stderr(
emit_color,
Some(source_map.clone()),
None,
fallback_bundle,
false,
false,
None,
false,
false,
TerminalUrl::No,
))
Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
};
Handler::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
Expand Down