1
Fork 0

Rollup merge of #119538 - nnethercote:cleanup-errors-5, r=compiler-errors

Cleanup error handlers: round 5

More rustc_errors cleanups. A sequel to https://github.com/rust-lang/rust/pull/119171.

r? ````@compiler-errors````
This commit is contained in:
Michael Goulet 2024-01-05 10:57:21 -05:00 committed by GitHub
commit f361b591ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
57 changed files with 369 additions and 464 deletions

View file

@ -86,9 +86,7 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
/// Maps `Diagnostic::Level` to `snippet::AnnotationType`
fn annotation_type_for_level(level: Level) -> AnnotationType {
match level {
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error { .. } => {
AnnotationType::Error
}
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error => AnnotationType::Error,
Level::Warning(_) => AnnotationType::Warning,
Level::Note | Level::OnceNote => AnnotationType::Note,
Level::Help | Level::OnceHelp => AnnotationType::Help,

View file

@ -212,6 +212,9 @@ impl StringPart {
}
}
// Note: most of these methods are setters that return `&mut Self`. The small
// number of simple getter functions all have `get_` prefixes to distinguish
// them from the setters.
impl Diagnostic {
#[track_caller]
pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
@ -241,11 +244,9 @@ impl Diagnostic {
pub fn is_error(&self) -> bool {
match self.level {
Level::Bug
| Level::DelayedBug
| Level::Fatal
| Level::Error { .. }
| Level::FailureNote => true,
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error | Level::FailureNote => {
true
}
Level::Warning(_)
| Level::Note
@ -308,25 +309,27 @@ impl Diagnostic {
/// In the meantime, though, callsites are required to deal with the "bug"
/// locally in whichever way makes the most sense.
#[track_caller]
pub fn downgrade_to_delayed_bug(&mut self) -> &mut Self {
pub fn downgrade_to_delayed_bug(&mut self) {
assert!(
self.is_error(),
"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
self.level
);
self.level = Level::DelayedBug;
self
}
/// Adds a span/label to be included in the resulting snippet.
/// Appends a labeled span to the diagnostic.
///
/// This is pushed onto the [`MultiSpan`] that was created when the diagnostic
/// was first built. That means it will be shown together with the original
/// span/label, *not* a span added by one of the `span_{note,warn,help,suggestions}` methods.
/// Labels are used to convey additional context for the diagnostic's primary span. They will
/// be shown together with the original diagnostic's span, *not* with spans added by
/// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
/// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
/// either.
///
/// This span is *not* considered a ["primary span"][`MultiSpan`]; only
/// the `Span` supplied when creating the diagnostic is primary.
/// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
/// the diagnostic was constructed. However, the label span is *not* considered a
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
/// primary.
#[rustc_lint_diagnostics]
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self {
self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
@ -344,7 +347,7 @@ impl Diagnostic {
pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
let before = self.span.clone();
self.set_span(after);
self.span(after);
for span_label in before.span_labels() {
if let Some(label) = span_label.label {
if span_label.is_primary && keep_label {
@ -876,7 +879,7 @@ impl Diagnostic {
self
}
pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
pub fn span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
self.span = sp.into();
if let Some(span) = self.span.primary_span() {
self.sort_span = span;
@ -884,7 +887,7 @@ impl Diagnostic {
self
}
pub fn set_is_lint(&mut self) -> &mut Self {
pub fn is_lint(&mut self) -> &mut Self {
self.is_lint = true;
self
}
@ -903,7 +906,7 @@ impl Diagnostic {
self.code.clone()
}
pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
self.messages[0] = (msg.into(), Style::NoStyle);
self
}
@ -915,7 +918,7 @@ impl Diagnostic {
self.args.iter()
}
pub fn set_arg(
pub fn arg(
&mut self,
name: impl Into<Cow<'static, str>>,
arg: impl IntoDiagnosticArg,

View file

@ -31,7 +31,7 @@ where
{
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
let mut diag = self.node.into_diagnostic(dcx, level);
diag.set_span(self.span);
diag.span(self.span);
diag
}
}
@ -207,11 +207,11 @@ macro_rules! forward {
// Forward pattern for &mut self -> &mut Self
(
$(#[$attrs:meta])*
pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)?) -> &mut Self
pub fn $n:ident(&mut self $(, $name:ident: $ty:ty)* $(,)?) -> &mut Self
) => {
$(#[$attrs])*
#[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")]
pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
pub fn $n(&mut self $(, $name: $ty)*) -> &mut Self {
self.diagnostic.$n($($name),*);
self
}
@ -356,35 +356,16 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
self.emit()
}
forward!(
#[track_caller]
pub fn downgrade_to_delayed_bug(&mut self,) -> &mut Self
);
forward!(
/// Appends a labeled span to the diagnostic.
///
/// Labels are used to convey additional context for the diagnostic's primary span. They will
/// be shown together with the original diagnostic's span, *not* with spans added by
/// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
/// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
/// either.
///
/// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
/// the diagnostic was constructed. However, the label span is *not* considered a
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
/// primary.
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self);
forward!(
/// Labels all the given spans with the provided label.
/// See [`Diagnostic::span_label()`] for more information.
pub fn span_labels(
forward!(pub fn span_label(
&mut self,
span: Span,
label: impl Into<SubdiagnosticMessage>
) -> &mut Self);
forward!(pub fn span_labels(
&mut self,
spans: impl IntoIterator<Item = Span>,
label: &str,
) -> &mut Self);
forward!(pub fn note_expected_found(
&mut self,
expected_label: &dyn fmt::Display,
@ -392,7 +373,6 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
found_label: &dyn fmt::Display,
found: DiagnosticStyledString,
) -> &mut Self);
forward!(pub fn note_expected_found_extra(
&mut self,
expected_label: &dyn fmt::Display,
@ -402,7 +382,6 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
expected_extra: &dyn fmt::Display,
found_extra: &dyn fmt::Display,
) -> &mut Self);
forward!(pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
forward!(pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
forward!(pub fn span_note(
@ -428,10 +407,8 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
sp: impl Into<MultiSpan>,
msg: impl Into<SubdiagnosticMessage>,
) -> &mut Self);
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
forward!(pub fn is_lint(&mut self) -> &mut Self);
forward!(pub fn disable_suggestions(&mut self) -> &mut Self);
forward!(pub fn multipart_suggestion(
&mut self,
msg: impl Into<SubdiagnosticMessage>,
@ -498,16 +475,14 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);
forward!(pub fn set_primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
forward!(pub fn set_span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self);
forward!(pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
forward!(pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self);
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
forward!(pub fn set_arg(
forward!(pub fn arg(
&mut self,
name: impl Into<Cow<'static, str>>,
arg: impl IntoDiagnosticArg,
) -> &mut Self);
forward!(pub fn subdiagnostic(
&mut self,
subdiagnostic: impl crate::AddToDiagnostic

View file

@ -254,29 +254,29 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetDataLayoutErrors<'_>
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
diag =
DiagnosticBuilder::new(dcx, level, fluent::errors_target_invalid_address_space);
diag.set_arg("addr_space", addr_space);
diag.set_arg("cause", cause);
diag.set_arg("err", err);
diag.arg("addr_space", addr_space);
diag.arg("cause", cause);
diag.arg("err", err);
diag
}
TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => {
diag = DiagnosticBuilder::new(dcx, level, fluent::errors_target_invalid_bits);
diag.set_arg("kind", kind);
diag.set_arg("bit", bit);
diag.set_arg("cause", cause);
diag.set_arg("err", err);
diag.arg("kind", kind);
diag.arg("bit", bit);
diag.arg("cause", cause);
diag.arg("err", err);
diag
}
TargetDataLayoutErrors::MissingAlignment { cause } => {
diag = DiagnosticBuilder::new(dcx, level, fluent::errors_target_missing_alignment);
diag.set_arg("cause", cause);
diag.arg("cause", cause);
diag
}
TargetDataLayoutErrors::InvalidAlignment { cause, err } => {
diag = DiagnosticBuilder::new(dcx, level, fluent::errors_target_invalid_alignment);
diag.set_arg("cause", cause);
diag.set_arg("err_kind", err.diag_ident());
diag.set_arg("align", err.align());
diag.arg("cause", cause);
diag.arg("err_kind", err.diag_ident());
diag.arg("align", err.align());
diag
}
TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => {
@ -285,8 +285,8 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetDataLayoutErrors<'_>
level,
fluent::errors_target_inconsistent_architecture,
);
diag.set_arg("dl", dl);
diag.set_arg("target", target);
diag.arg("dl", dl);
diag.arg("target", target);
diag
}
TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => {
@ -295,13 +295,13 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for TargetDataLayoutErrors<'_>
level,
fluent::errors_target_inconsistent_pointer_width,
);
diag.set_arg("pointer_size", pointer_size);
diag.set_arg("target", target);
diag.arg("pointer_size", pointer_size);
diag.arg("target", target);
diag
}
TargetDataLayoutErrors::InvalidBitsSize { err } => {
diag = DiagnosticBuilder::new(dcx, level, fluent::errors_target_invalid_bits_size);
diag.set_arg("err", err);
diag.arg("err", err);
diag
}
}

View file

@ -673,7 +673,7 @@ impl DiagCtxt {
let key = (span.with_parent(None), key);
if diag.is_error() {
if matches!(diag.level, Error { lint: true }) {
if diag.level == Error && diag.is_lint {
inner.lint_err_count += 1;
} else {
inner.err_count += 1;
@ -697,7 +697,7 @@ impl DiagCtxt {
let key = (span.with_parent(None), key);
let diag = inner.stashed_diagnostics.remove(&key)?;
if diag.is_error() {
if matches!(diag.level, Error { lint: true }) {
if diag.level == Error && diag.is_lint {
inner.lint_err_count -= 1;
} else {
inner.err_count -= 1;
@ -732,7 +732,7 @@ impl DiagCtxt {
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ()> {
let mut result = self.struct_warn(msg);
result.set_span(span);
result.span(span);
result
}
@ -789,7 +789,7 @@ impl DiagCtxt {
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_> {
let mut result = self.struct_err(msg);
result.set_span(span);
result.span(span);
result
}
@ -812,7 +812,7 @@ impl DiagCtxt {
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_err(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_> {
DiagnosticBuilder::new(self, Error { lint: false }, msg)
DiagnosticBuilder::new(self, Error, msg)
}
/// Construct a builder at the `Error` level with the `msg` and the `code`.
@ -850,7 +850,7 @@ impl DiagCtxt {
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, FatalAbort> {
let mut result = self.struct_fatal(msg);
result.set_span(span);
result.span(span);
result
}
@ -878,16 +878,6 @@ impl DiagCtxt {
DiagnosticBuilder::new(self, Fatal, msg)
}
/// Construct a builder at the `Fatal` level with the `msg`, that doesn't abort.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_almost_fatal(
&self,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, FatalError> {
DiagnosticBuilder::new(self, Fatal, msg)
}
/// Construct a builder at the `Help` level with the `msg`.
#[rustc_lint_diagnostics]
pub fn struct_help(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
@ -917,7 +907,7 @@ impl DiagCtxt {
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, BugAbort> {
let mut result = self.struct_bug(msg);
result.set_span(span);
result.span(span);
result
}
@ -1008,7 +998,7 @@ impl DiagCtxt {
self.span_bug(sp, msg);
}
let mut diagnostic = Diagnostic::new(DelayedBug, msg);
diagnostic.set_span(sp);
diagnostic.span(sp);
self.emit_diagnostic(diagnostic).unwrap()
}
@ -1039,7 +1029,7 @@ impl DiagCtxt {
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ()> {
let mut db = DiagnosticBuilder::new(self, Note, msg);
db.set_span(span);
db.span(span);
db
}
@ -1222,7 +1212,7 @@ impl DiagCtxt {
#[track_caller]
pub fn create_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> DiagnosticBuilder<'a> {
err.into_diagnostic(self, Error { lint: false })
err.into_diagnostic(self, Error)
}
#[track_caller]
@ -1377,7 +1367,7 @@ impl DiagCtxtInner {
for diag in diags {
// Decrement the count tracking the stash; emitting will increment it.
if diag.is_error() {
if matches!(diag.level, Error { lint: true }) {
if diag.level == Error && diag.is_lint {
self.lint_err_count -= 1;
} else {
self.err_count -= 1;
@ -1408,7 +1398,7 @@ impl DiagCtxtInner {
&mut self,
diagnostic: &mut Diagnostic,
) -> Option<ErrorGuaranteed> {
if matches!(diagnostic.level, Error { .. } | Fatal) && self.treat_err_as_bug() {
if matches!(diagnostic.level, Error | Fatal) && self.treat_err_as_bug() {
diagnostic.level = Bug;
}
@ -1509,7 +1499,7 @@ impl DiagCtxtInner {
}
}
if diagnostic.is_error() {
if matches!(diagnostic.level, Error { lint: true }) {
if diagnostic.level == Error && diagnostic.is_lint {
self.bump_lint_err_count();
} else {
self.bump_err_count();
@ -1705,11 +1695,7 @@ pub enum Level {
/// most common case.
///
/// Its `EmissionGuarantee` is `ErrorGuaranteed`.
Error {
/// If this error comes from a lint, don't abort compilation even when abort_if_errors() is
/// called.
lint: bool,
},
Error,
/// A warning about the code being compiled. Does not prevent compilation from finishing.
///
@ -1768,7 +1754,7 @@ impl Level {
fn color(self) -> ColorSpec {
let mut spec = ColorSpec::new();
match self {
Bug | DelayedBug | Fatal | Error { .. } => {
Bug | DelayedBug | Fatal | Error => {
spec.set_fg(Some(Color::Red)).set_intense(true);
}
Warning(_) => {
@ -1789,7 +1775,7 @@ impl Level {
pub fn to_str(self) -> &'static str {
match self {
Bug | DelayedBug => "error: internal compiler error",
Fatal | Error { .. } => "error",
Fatal | Error => "error",
Warning(_) => "warning",
Note | OnceNote => "note",
Help | OnceHelp => "help",