Store a single copy of the error registry in DiagCtxt
And pass this to the individual emitters when necessary.
This commit is contained in:
parent
ea6f5cbd2f
commit
030545d8c3
9 changed files with 67 additions and 46 deletions
|
@ -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::{
|
||||
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
@ -181,7 +182,7 @@ 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.
|
||||
|
@ -189,7 +190,7 @@ pub trait Emitter: Translate {
|
|||
|
||||
/// 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.
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
|
|
|
@ -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>>,
|
||||
|
@ -74,7 +73,6 @@ impl JsonEmitter {
|
|||
) -> JsonEmitter {
|
||||
JsonEmitter {
|
||||
dst: IntoDynSyncSend(dst),
|
||||
registry: None,
|
||||
sm,
|
||||
fluent_bundle: None,
|
||||
fallback_bundle,
|
||||
|
@ -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:?}");
|
||||
|
@ -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| {
|
||||
|
@ -151,7 +149,7 @@ impl Emitter for JsonEmitter {
|
|||
}
|
||||
FutureBreakageItem {
|
||||
diagnostic: EmitTyped::Diagnostic(Diagnostic::from_errors_diagnostic(
|
||||
diag, self,
|
||||
diag, self, registry,
|
||||
)),
|
||||
}
|
||||
})
|
||||
|
@ -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 =
|
||||
|
@ -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 })
|
||||
|
@ -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();
|
||||
|
||||
|
|
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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)) }
|
||||
}
|
||||
|
@ -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`")
|
||||
}
|
||||
|
||||
|
@ -759,6 +767,7 @@ impl DiagCtxt {
|
|||
let mut inner = self.inner.borrow_mut();
|
||||
let DiagCtxtInner {
|
||||
flags: _,
|
||||
registry: _,
|
||||
err_guars,
|
||||
lint_err_guars,
|
||||
delayed_bugs,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue