1
Fork 0

Rework how diagnostic lints are stored.

`Diagnostic::code` has the type `DiagnosticId`, which has `Error` and
`Lint` variants. Plus `Diagnostic::is_lint` is a bool, which should be
redundant w.r.t. `Diagnostic::code`.

Seems simple. Except it's possible for a lint to have an error code, in
which case its `code` field is recorded as `Error`, and `is_lint` is
required to indicate that it's a lint. This is what happens with
`derive(LintDiagnostic)` lints. Which means those lints don't have a
lint name or a `has_future_breakage` field because those are stored in
the `DiagnosticId::Lint`.

It's all a bit messy and confused and seems unintentional.

This commit:
- removes `DiagnosticId`;
- changes `Diagnostic::code` to `Option<String>`, which means both
  errors and lints can straightforwardly have an error code;
- changes `Diagnostic::is_lint` to `Option<IsLint>`, where `IsLint` is a
  new type containing a lint name and a `has_future_breakage` bool, so
  all lints can have those, error code or not.
This commit is contained in:
Nicholas Nethercote 2024-01-13 13:11:56 +11:00
parent 2de99ec787
commit d71f535a6f
24 changed files with 110 additions and 130 deletions

View file

@ -15,10 +15,9 @@ use termcolor::{ColorSpec, WriteColor};
use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
use crate::registry::Registry;
use crate::translation::{to_fluent_args, Translate};
use crate::DiagnosticId;
use crate::{
CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
TerminalUrl,
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
SubDiagnostic, TerminalUrl,
};
use rustc_lint_defs::Applicability;
@ -301,7 +300,8 @@ struct DiagnosticSpanMacroExpansion {
#[derive(Serialize)]
struct DiagnosticCode {
/// The code itself.
/// The error code (e.g. "E1234"), if the diagnostic has one. Or the lint
/// name, if it's a lint without an error code.
code: String,
/// An explanation for the code.
explanation: Option<&'static str>,
@ -399,9 +399,21 @@ impl Diagnostic {
let output = String::from_utf8(output).unwrap();
let translated_message = je.translate_messages(&diag.messages, &args);
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(),
})
} else if let Some(IsLint { name, .. }) = &diag.is_lint {
Some(DiagnosticCode { code: name.to_string(), explanation: None })
} else {
None
};
Diagnostic {
message: translated_message.to_string(),
code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
code,
level: diag.level.to_str(),
spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
children: diag
@ -592,18 +604,3 @@ impl DiagnosticSpanLine {
.unwrap_or_else(|_| vec![])
}
}
impl DiagnosticCode {
fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
s.map(|s| {
let s = match s {
DiagnosticId::Error(s) => s,
DiagnosticId::Lint { name, .. } => name,
};
let je_result =
je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
DiagnosticCode { code: s, explanation: je_result.ok() }
})
}
}