Skip to content

Commit

Permalink
Store a single copy of the error registry in DiagCtxt
Browse files Browse the repository at this point in the history
And pass this to the individual emitters when necessary.
  • Loading branch information
bjorn3 committed Dec 6, 2024
1 parent ea6f5cb commit 030545d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 46 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,11 @@ impl Translate for SharedEmitter {
}

impl Emitter for SharedEmitter {
fn emit_diagnostic(&mut self, mut diag: rustc_errors::DiagInner) {
fn emit_diagnostic(
&mut self,
mut diag: rustc_errors::DiagInner,
_registry: &rustc_errors::registry::Registry,
) {
// Check that we aren't missing anything interesting when converting to
// the cut-down local `DiagInner`.
assert_eq!(diag.span, MultiSpan::new());
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_span::SourceFile;
use rustc_span::source_map::SourceMap;

use crate::emitter::FileWithAnnotatedLines;
use crate::registry::Registry;
use crate::snippet::Line;
use crate::translation::{Translate, to_fluent_args};
use crate::{
Expand Down Expand Up @@ -45,7 +46,7 @@ impl Translate for AnnotateSnippetEmitter {

impl Emitter for AnnotateSnippetEmitter {
/// The entry point for the diagnostics generation
fn emit_diagnostic(&mut self, mut diag: DiagInner) {
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
let fluent_args = to_fluent_args(diag.args.iter());

let mut suggestions = diag.suggestions.unwrap_tag();
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStr
use tracing::{debug, instrument, trace, warn};

use crate::diagnostic::DiagLocation;
use crate::registry::Registry;
use crate::snippet::{
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
};
Expand Down Expand Up @@ -181,15 +182,15 @@ pub type DynEmitter = dyn Emitter + DynSend;
/// Emitter trait for emitting errors.
pub trait Emitter: Translate {
/// Emit a structured diagnostic.
fn emit_diagnostic(&mut self, diag: DiagInner);
fn emit_diagnostic(&mut self, diag: DiagInner, registry: &Registry);

/// Emit a notification that an artifact has been output.
/// Currently only supported for the JSON format.
fn emit_artifact_notification(&mut self, _path: &Path, _artifact_type: &str) {}

/// Emit a report about future breakage.
/// Currently only supported for the JSON format.
fn emit_future_breakage_report(&mut self, _diags: Vec<DiagInner>) {}
fn emit_future_breakage_report(&mut self, _diags: Vec<DiagInner>, _registry: &Registry) {}

/// Emit list of unused externs.
/// Currently only supported for the JSON format.
Expand Down Expand Up @@ -500,7 +501,7 @@ impl Emitter for HumanEmitter {
self.sm.as_deref()
}

fn emit_diagnostic(&mut self, mut diag: DiagInner) {
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
let fluent_args = to_fluent_args(diag.args.iter());

let mut suggestions = diag.suggestions.unwrap_tag();
Expand Down Expand Up @@ -561,7 +562,7 @@ impl Emitter for SilentEmitter {
None
}

fn emit_diagnostic(&mut self, mut diag: DiagInner) {
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
if let Some(fatal_note) = &self.fatal_note {
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
Expand Down
20 changes: 11 additions & 9 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ mod tests;
pub struct JsonEmitter {
#[setters(skip)]
dst: IntoDynSyncSend<Box<dyn Write + Send>>,
registry: Option<Registry>,
#[setters(skip)]
sm: Lrc<SourceMap>,
fluent_bundle: Option<Lrc<FluentBundle>>,
Expand Down Expand Up @@ -74,7 +73,6 @@ impl JsonEmitter {
) -> JsonEmitter {
JsonEmitter {
dst: IntoDynSyncSend(dst),
registry: None,
sm,
fluent_bundle: None,
fallback_bundle,
Expand Down Expand Up @@ -121,8 +119,8 @@ impl Translate for JsonEmitter {
}

impl Emitter for JsonEmitter {
fn emit_diagnostic(&mut self, diag: crate::DiagInner) {
let data = Diagnostic::from_errors_diagnostic(diag, self);
fn emit_diagnostic(&mut self, diag: crate::DiagInner, registry: &Registry) {
let data = Diagnostic::from_errors_diagnostic(diag, self, registry);
let result = self.emit(EmitTyped::Diagnostic(data));
if let Err(e) = result {
panic!("failed to print diagnostics: {e:?}");
Expand All @@ -137,7 +135,7 @@ impl Emitter for JsonEmitter {
}
}

fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>) {
fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>, registry: &Registry) {
let data: Vec<FutureBreakageItem<'_>> = diags
.into_iter()
.map(|mut diag| {
Expand All @@ -151,7 +149,7 @@ impl Emitter for JsonEmitter {
}
FutureBreakageItem {
diagnostic: EmitTyped::Diagnostic(Diagnostic::from_errors_diagnostic(
diag, self,
diag, self, registry,
)),
}
})
Expand Down Expand Up @@ -291,7 +289,11 @@ struct UnusedExterns<'a> {

impl Diagnostic {
/// Converts from `rustc_errors::DiagInner` to `Diagnostic`.
fn from_errors_diagnostic(diag: crate::DiagInner, je: &JsonEmitter) -> Diagnostic {
fn from_errors_diagnostic(
diag: crate::DiagInner,
je: &JsonEmitter,
registry: &Registry,
) -> Diagnostic {
let args = to_fluent_args(diag.args.iter());
let sugg_to_diag = |sugg: &CodeSuggestion| {
let translated_message =
Expand Down Expand Up @@ -344,7 +346,7 @@ impl Diagnostic {
let code = if let Some(code) = diag.code {
Some(DiagnosticCode {
code: code.to_string(),
explanation: je.registry.as_ref().unwrap().try_find_description(code).ok(),
explanation: registry.try_find_description(code).ok(),
})
} else if let Some(IsLint { name, .. }) = &diag.is_lint {
Some(DiagnosticCode { code: name.to_string(), explanation: None })
Expand Down Expand Up @@ -382,7 +384,7 @@ impl Diagnostic {
} else {
OutputTheme::Ascii
})
.emit_diagnostic(diag);
.emit_diagnostic(diag, registry);
let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();
let buf = String::from_utf8(buf).unwrap();

Expand Down
24 changes: 17 additions & 7 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub use diagnostic_impls::{
};
pub use emitter::ColorConfig;
use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
use registry::Registry;
use rustc_data_structures::AtomicRef;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
Expand All @@ -77,6 +76,8 @@ pub use snippet::Style;
pub use termcolor::{Color, ColorSpec, WriteColor};
use tracing::debug;

use crate::registry::Registry;

pub mod annotate_snippet_emitter_writer;
pub mod codes;
mod diagnostic;
Expand Down Expand Up @@ -483,6 +484,8 @@ impl<'a> std::ops::Deref for DiagCtxtHandle<'a> {
struct DiagCtxtInner {
flags: DiagCtxtFlags,

registry: Registry,

/// The error guarantees from all emitted errors. The length gives the error count.
err_guars: Vec<ErrorGuaranteed>,
/// The error guarantee from all emitted lint errors. The length gives the
Expand Down Expand Up @@ -664,6 +667,11 @@ impl DiagCtxt {
self
}

pub fn with_registry(mut self, registry: Registry) -> Self {
self.inner.get_mut().registry = registry;
self
}

pub fn new(emitter: Box<DynEmitter>) -> Self {
Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
}
Expand Down Expand Up @@ -694,7 +702,7 @@ impl DiagCtxt {
struct FalseEmitter;

impl Emitter for FalseEmitter {
fn emit_diagnostic(&mut self, _: DiagInner) {
fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) {
unimplemented!("false emitter must only used during `wrap_emitter`")
}

Expand Down Expand Up @@ -759,6 +767,7 @@ impl DiagCtxt {
let mut inner = self.inner.borrow_mut();
let DiagCtxtInner {
flags: _,
registry: _,
err_guars,
lint_err_guars,
delayed_bugs,
Expand Down Expand Up @@ -964,7 +973,7 @@ impl<'a> DiagCtxtHandle<'a> {
self.inner.borrow().has_errors_or_delayed_bugs()
}

pub fn print_error_count(&self, registry: &Registry) {
pub fn print_error_count(&self) {
let mut inner = self.inner.borrow_mut();

// Any stashed diagnostics should have been handled by
Expand Down Expand Up @@ -1014,7 +1023,7 @@ impl<'a> DiagCtxtHandle<'a> {
.emitted_diagnostic_codes
.iter()
.filter_map(|&code| {
if registry.try_find_description(code).is_ok() {
if inner.registry.try_find_description(code).is_ok() {
Some(code.to_string())
} else {
None
Expand Down Expand Up @@ -1075,10 +1084,10 @@ impl<'a> DiagCtxtHandle<'a> {
}

pub fn emit_future_breakage_report(&self) {
let mut inner = self.inner.borrow_mut();
let inner = &mut *self.inner.borrow_mut();
let diags = std::mem::take(&mut inner.future_breakage_diagnostics);
if !diags.is_empty() {
inner.emitter.emit_future_breakage_report(diags);
inner.emitter.emit_future_breakage_report(diags, &inner.registry);
}
}

Expand Down Expand Up @@ -1409,6 +1418,7 @@ impl DiagCtxtInner {
fn new(emitter: Box<DynEmitter>) -> Self {
Self {
flags: DiagCtxtFlags { can_emit_warnings: true, ..Default::default() },
registry: Registry::new(&[]),
err_guars: Vec::new(),
lint_err_guars: Vec::new(),
delayed_bugs: Vec::new(),
Expand Down Expand Up @@ -1582,7 +1592,7 @@ impl DiagCtxtInner {
}
self.has_printed = true;

self.emitter.emit_diagnostic(diagnostic);
self.emitter.emit_diagnostic(diagnostic, &self.registry);
}

if is_error {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
temps_dir,
},
bundle,
config.registry.clone(),
config.registry,
locale_resources,
config.lint_caps,
target,
Expand Down Expand Up @@ -499,7 +499,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
// If `f` panics, `finish_diagnostics` will run during
// unwinding because of the `defer`.
let sess_abort_guard = defer(|| {
compiler.sess.finish_diagnostics(&config.registry);
compiler.sess.finish_diagnostics();
});

let res = f(&compiler);
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use rustc_errors::emitter::{
DynEmitter, HumanEmitter, HumanReadableErrorType, OutputTheme, stderr_destination,
};
use rustc_errors::json::JsonEmitter;
use rustc_errors::registry::Registry;
use rustc_errors::{
Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort,
FluentBundle, LazyFallbackBundle, TerminalUrl, fallback_fluent_bundle,
Expand Down Expand Up @@ -276,11 +275,11 @@ impl Session {
}

/// Invoked all the way at the end to finish off diagnostics printing.
pub fn finish_diagnostics(&self, registry: &Registry) -> Option<ErrorGuaranteed> {
pub fn finish_diagnostics(&self) -> Option<ErrorGuaranteed> {
let mut guar = None;
guar = guar.or(self.check_miri_unleashed_features());
guar = guar.or(self.dcx().emit_stashed_diagnostics());
self.dcx().print_error_count(registry);
self.dcx().print_error_count();
if self.opts.json_future_incompat {
self.dcx().emit_future_breakage_report();
}
Expand Down Expand Up @@ -880,7 +879,6 @@ impl Session {
#[allow(rustc::bad_opt_access)]
fn default_emitter(
sopts: &config::Options,
registry: rustc_errors::registry::Registry,
source_map: Lrc<SourceMap>,
bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
Expand Down Expand Up @@ -943,7 +941,6 @@ fn default_emitter(
json_rendered,
color_config,
)
.registry(Some(registry))
.fluent_bundle(bundle)
.ui_testing(sopts.unstable_opts.ui_testing)
.ignored_directories_in_source_blocks(
Expand Down Expand Up @@ -999,11 +996,11 @@ pub fn build_session(
sopts.unstable_opts.translate_directionality_markers,
);
let source_map = rustc_span::source_map::get_source_map().unwrap();
let emitter =
default_emitter(&sopts, registry, Lrc::clone(&source_map), bundle, fallback_bundle);
let emitter = default_emitter(&sopts, Lrc::clone(&source_map), bundle, fallback_bundle);

let mut dcx =
DiagCtxt::new(emitter).with_flags(sopts.unstable_opts.dcx_flags(can_emit_warnings));
let mut dcx = DiagCtxt::new(emitter)
.with_flags(sopts.unstable_opts.dcx_flags(can_emit_warnings))
.with_registry(registry);
if let Some(ice_file) = ice_file {
dcx = dcx.with_ice_file(ice_file);
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/passes/lint/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_errors::emitter::Emitter;
use rustc_errors::registry::Registry;
use rustc_errors::translation::{Translate, to_fluent_args};
use rustc_errors::{Applicability, DiagCtxt, DiagInner, LazyFallbackBundle};
use rustc_parse::{source_str_to_stream, unwrap_or_emit_fatal};
Expand Down Expand Up @@ -155,7 +156,7 @@ impl Translate for BufferEmitter {
}

impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: DiagInner) {
fn emit_diagnostic(&mut self, diag: DiagInner, _registry: &Registry) {
let mut buffer = self.buffer.borrow_mut();

let fluent_args = to_fluent_args(diag.args.iter());
Expand Down
Loading

0 comments on commit 030545d

Please sign in to comment.