2022-05-24 15:09:47 +01:00
|
|
|
use crate::{
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
DiagCtxt, Diagnostic, DiagnosticMessage, ErrorGuaranteed, ExplicitBug, Level, StashKey,
|
2022-05-24 15:09:47 +01:00
|
|
|
};
|
2022-09-24 11:05:37 -07:00
|
|
|
use rustc_span::source_map::Spanned;
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::Span;
|
2016-10-11 12:26:32 -04:00
|
|
|
use std::fmt::{self, Debug};
|
2022-01-27 09:44:25 +00:00
|
|
|
use std::marker::PhantomData;
|
2016-10-11 12:26:32 -04:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2022-11-23 01:07:36 -05:00
|
|
|
use std::panic;
|
2016-10-11 12:26:32 -04:00
|
|
|
use std::thread::panicking;
|
|
|
|
|
2023-12-22 10:32:00 +11:00
|
|
|
/// Trait implemented by error types. This is rarely implemented manually. Instead, use
|
2022-09-18 11:46:56 -04:00
|
|
|
/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
|
2022-11-01 08:45:58 -04:00
|
|
|
#[rustc_diagnostic_item = "IntoDiagnostic"]
|
2023-12-04 08:46:50 +11:00
|
|
|
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
|
2023-12-17 21:48:57 +11:00
|
|
|
/// Write out as a diagnostic out of `DiagCtxt`.
|
2022-09-07 09:36:08 -04:00
|
|
|
#[must_use]
|
2023-12-18 14:12:39 +11:00
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G>;
|
2022-09-07 09:36:08 -04:00
|
|
|
}
|
|
|
|
|
2023-12-04 08:46:50 +11:00
|
|
|
impl<'a, T, G> IntoDiagnostic<'a, G> for Spanned<T>
|
2022-09-24 11:05:37 -07:00
|
|
|
where
|
2023-12-04 08:46:50 +11:00
|
|
|
T: IntoDiagnostic<'a, G>,
|
|
|
|
G: EmissionGuarantee,
|
2022-09-24 11:05:37 -07:00
|
|
|
{
|
2023-12-18 14:12:39 +11:00
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
|
2024-01-09 09:08:49 +11:00
|
|
|
self.node.into_diagnostic(dcx, level).with_span(self.span)
|
2022-09-24 11:05:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
/// Used for emitting structured error messages and other diagnostic information.
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
/// Wraps a `Diagnostic`, adding some useful things.
|
|
|
|
/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
|
|
|
|
/// that it has been emitted or cancelled.
|
|
|
|
/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
|
|
|
|
///
|
2024-01-08 07:47:02 +11:00
|
|
|
/// Each constructed `DiagnosticBuilder` must be consumed by a function such as
|
|
|
|
/// `emit`, `cancel`, `delay_as_bug`, or `into_diagnostic`. A panic occurrs if a
|
|
|
|
/// `DiagnosticBuilder` is dropped without being consumed by one of these
|
|
|
|
/// functions.
|
2018-09-15 06:27:55 +02:00
|
|
|
///
|
|
|
|
/// If there is some state in a downstream crate you would like to
|
|
|
|
/// access in the methods of `DiagnosticBuilder` here, consider
|
2023-12-18 08:47:03 +11:00
|
|
|
/// extending `DiagCtxtFlags`.
|
2016-10-11 12:26:32 -04:00
|
|
|
#[must_use]
|
2023-12-19 15:26:24 +11:00
|
|
|
pub struct DiagnosticBuilder<'a, G: EmissionGuarantee = ErrorGuaranteed> {
|
2024-01-08 07:47:02 +11:00
|
|
|
pub dcx: &'a DiagCtxt,
|
2022-01-24 11:23:14 +00:00
|
|
|
|
2024-01-08 07:47:02 +11:00
|
|
|
/// Why the `Option`? It is always `Some` until the `DiagnosticBuilder` is
|
|
|
|
/// consumed via `emit`, `cancel`, etc. At that point it is consumed and
|
|
|
|
/// replaced with `None`. Then `drop` checks that it is `None`; if not, it
|
|
|
|
/// panics because a diagnostic was built but not used.
|
|
|
|
///
|
|
|
|
/// Why the Box? `Diagnostic` is a large type, and `DiagnosticBuilder` is
|
|
|
|
/// often used as a return value, especially within the frequently-used
|
|
|
|
/// `PResult` type. In theory, return value optimization (RVO) should avoid
|
|
|
|
/// unnecessary copying. In practice, it does not (at the time of writing).
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
// FIXME(nnethercote) Make private once this moves to diagnostic.rs.
|
|
|
|
pub(crate) diag: Option<Box<Diagnostic>>,
|
2023-12-22 10:55:54 +11:00
|
|
|
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
// FIXME(nnethercote) Make private once this moves to diagnostic.rs.
|
|
|
|
pub(crate) _marker: PhantomData<G>,
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:35:54 +11:00
|
|
|
// Cloning a `DiagnosticBuilder` is a recipe for a diagnostic being emitted
|
|
|
|
// twice, which would be bad.
|
|
|
|
impl<G> !Clone for DiagnosticBuilder<'_, G> {}
|
|
|
|
|
2022-01-26 03:39:14 +00:00
|
|
|
rustc_data_structures::static_assert_size!(
|
2024-01-08 07:47:02 +11:00
|
|
|
DiagnosticBuilder<'_, ()>,
|
|
|
|
2 * std::mem::size_of::<usize>()
|
2022-01-26 03:39:14 +00:00
|
|
|
);
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
/// Trait for types that `DiagnosticBuilder::emit` can return as a "guarantee"
|
|
|
|
/// (or "proof") token that the emission happened.
|
|
|
|
pub trait EmissionGuarantee: Sized {
|
2023-12-18 16:31:15 +11:00
|
|
|
/// This exists so that bugs and fatal errors can both result in `!` (an
|
|
|
|
/// abort) when emitted, but have different aborting behaviour.
|
|
|
|
type EmitResult = Self;
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
/// Implementation of `DiagnosticBuilder::emit`, fully controlled by each
|
|
|
|
/// `impl` of `EmissionGuarantee`, to make it impossible to create a value
|
2023-12-18 16:31:15 +11:00
|
|
|
/// of `Self::EmitResult` without actually performing the emission.
|
2022-01-27 09:44:25 +00:00
|
|
|
#[track_caller]
|
2024-01-05 17:33:12 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult;
|
2022-01-27 09:44:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 17:02:37 +11:00
|
|
|
impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
2024-01-08 07:47:02 +11:00
|
|
|
/// Takes the diagnostic. For use by methods that consume the
|
|
|
|
/// DiagnosticBuilder: `emit`, `cancel`, etc. Afterwards, `drop` is the
|
|
|
|
/// only code that will be run on `self`.
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
// FIXME(nnethercote) Make private once this moves to diagnostic.rs.
|
|
|
|
pub(crate) fn take_diag(&mut self) -> Diagnostic {
|
2024-01-08 07:47:02 +11:00
|
|
|
Box::into_inner(self.diag.take().unwrap())
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:02:37 +11:00
|
|
|
/// Most `emit_producing_guarantee` functions use this as a starting point.
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
// FIXME(nnethercote) Make private once this moves to diagnostic.rs.
|
|
|
|
pub(crate) fn emit_producing_nothing(mut self) {
|
2024-01-08 07:47:02 +11:00
|
|
|
let diag = self.take_diag();
|
|
|
|
self.dcx.emit_diagnostic(diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `ErrorGuaranteed::emit_producing_guarantee` uses this.
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
// FIXME(nnethercote) Make private once this moves to diagnostic.rs.
|
|
|
|
pub(crate) fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
|
2024-01-08 07:47:02 +11:00
|
|
|
let diag = self.take_diag();
|
|
|
|
|
2024-02-07 10:26:50 +11:00
|
|
|
// The only error levels that produce `ErrorGuaranteed` are
|
|
|
|
// `Error` and `DelayedBug`. But `DelayedBug` should never occur here
|
|
|
|
// because delayed bugs have their level changed to `Bug` when they are
|
|
|
|
// actually printed, so they produce an ICE.
|
|
|
|
//
|
|
|
|
// (Also, even though `level` isn't `pub`, the whole `Diagnostic` could
|
|
|
|
// be overwritten with a new one thanks to `DerefMut`. So this assert
|
|
|
|
// protects against that, too.)
|
2024-01-08 07:47:02 +11:00
|
|
|
assert!(
|
2024-02-07 10:26:50 +11:00
|
|
|
matches!(diag.level, Level::Error | Level::DelayedBug),
|
|
|
|
"invalid diagnostic level ({:?})",
|
2024-01-08 07:47:02 +11:00
|
|
|
diag.level,
|
|
|
|
);
|
|
|
|
|
|
|
|
let guar = self.dcx.emit_diagnostic(diag);
|
|
|
|
guar.unwrap()
|
2023-12-18 17:02:37 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 12:34:26 -06:00
|
|
|
impl EmissionGuarantee for ErrorGuaranteed {
|
2024-01-08 07:47:02 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_error_guaranteed()
|
2022-01-27 09:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmissionGuarantee for () {
|
2024-01-05 17:33:12 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
2023-12-18 17:02:37 +11:00
|
|
|
db.emit_producing_nothing();
|
2022-01-27 09:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-03 22:26:34 +11:00
|
|
|
/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
|
2023-12-18 16:31:15 +11:00
|
|
|
/// bug diagnostics.
|
2023-12-03 22:26:34 +11:00
|
|
|
#[derive(Copy, Clone)]
|
2023-12-18 16:31:15 +11:00
|
|
|
pub struct BugAbort;
|
|
|
|
|
|
|
|
impl EmissionGuarantee for BugAbort {
|
|
|
|
type EmitResult = !;
|
2023-12-03 22:26:34 +11:00
|
|
|
|
2024-01-05 17:33:12 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
2023-12-18 17:02:37 +11:00
|
|
|
db.emit_producing_nothing();
|
2022-11-23 01:07:36 -05:00
|
|
|
panic::panic_any(ExplicitBug);
|
|
|
|
}
|
2022-03-09 16:11:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:31:15 +11:00
|
|
|
/// Marker type which enables implementation of `create_fatal` and `emit_fatal` functions for
|
|
|
|
/// fatal diagnostics.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct FatalAbort;
|
|
|
|
|
|
|
|
impl EmissionGuarantee for FatalAbort {
|
|
|
|
type EmitResult = !;
|
|
|
|
|
2024-01-05 17:33:12 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
2023-12-18 17:02:37 +11:00
|
|
|
db.emit_producing_nothing();
|
2022-03-09 16:11:28 -08:00
|
|
|
crate::FatalError.raise()
|
|
|
|
}
|
2022-08-19 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
|
2024-01-05 17:33:12 +11:00
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
2023-12-18 17:02:37 +11:00
|
|
|
db.emit_producing_nothing();
|
2022-08-19 14:48:15 +01:00
|
|
|
rustc_span::fatal_error::FatalError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
impl<G: EmissionGuarantee> Deref for DiagnosticBuilder<'_, G> {
|
2016-10-11 12:26:32 -04:00
|
|
|
type Target = Diagnostic;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Diagnostic {
|
2024-01-08 07:47:02 +11:00
|
|
|
self.diag.as_ref().unwrap()
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
|
2016-10-11 12:26:32 -04:00
|
|
|
fn deref_mut(&mut self) -> &mut Diagnostic {
|
2024-01-08 07:47:02 +11:00
|
|
|
self.diag.as_mut().unwrap()
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
2023-12-18 14:12:39 +11:00
|
|
|
#[rustc_lint_diagnostics]
|
2023-12-04 10:44:57 +11:00
|
|
|
#[track_caller]
|
2023-12-18 14:12:39 +11:00
|
|
|
pub fn new<M: Into<DiagnosticMessage>>(dcx: &'a DiagCtxt, level: Level, message: M) -> Self {
|
|
|
|
Self::new_diagnostic(dcx, Diagnostic::new(level, message))
|
2023-12-04 10:44:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `DiagnosticBuilder` with an already constructed
|
|
|
|
/// diagnostic.
|
|
|
|
#[track_caller]
|
2024-01-08 07:47:02 +11:00
|
|
|
pub(crate) fn new_diagnostic(dcx: &'a DiagCtxt, diag: Diagnostic) -> Self {
|
2023-12-04 10:44:57 +11:00
|
|
|
debug!("Created new diagnostic");
|
2024-01-08 07:47:02 +11:00
|
|
|
Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
|
2023-12-04 10:44:57 +11:00
|
|
|
}
|
|
|
|
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 12:17:35 +11:00
|
|
|
/// Emit and consume the diagnostic.
|
2022-01-27 09:44:25 +00:00
|
|
|
#[track_caller]
|
2024-01-05 17:33:12 +11:00
|
|
|
pub fn emit(self) -> G::EmitResult {
|
|
|
|
G::emit_producing_guarantee(self)
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 12:17:35 +11:00
|
|
|
}
|
|
|
|
|
2019-03-11 16:43:27 +01:00
|
|
|
/// Emit the diagnostic unless `delay` is true,
|
|
|
|
/// in which case the emission will be delayed as a bug.
|
|
|
|
///
|
|
|
|
/// See `emit` and `delay_as_bug` for details.
|
2022-01-27 09:44:25 +00:00
|
|
|
#[track_caller]
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 12:17:35 +11:00
|
|
|
pub fn emit_unless(mut self, delay: bool) -> G::EmitResult {
|
2020-02-02 09:47:58 +10:00
|
|
|
if delay {
|
2022-01-23 23:11:37 +00:00
|
|
|
self.downgrade_to_delayed_bug();
|
2020-02-02 09:47:58 +10:00
|
|
|
}
|
2022-01-27 09:44:25 +00:00
|
|
|
self.emit()
|
2019-03-11 16:43:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-08 07:47:02 +11:00
|
|
|
/// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
|
2022-01-26 03:39:14 +00:00
|
|
|
/// cancelled or it will panic when dropped).
|
|
|
|
pub fn cancel(mut self) {
|
2024-01-08 07:47:02 +11:00
|
|
|
self.diag = None;
|
2022-01-26 03:39:14 +00:00
|
|
|
drop(self);
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:45:21 +02:00
|
|
|
/// Stashes diagnostic for possible later improvement in a different,
|
|
|
|
/// later stage of the compiler. The diagnostic can be accessed with
|
2023-12-17 21:48:57 +11:00
|
|
|
/// the provided `span` and `key` through [`DiagCtxt::steal_diagnostic()`].
|
2024-02-02 13:05:00 +11:00
|
|
|
pub fn stash(mut self, span: Span, key: StashKey) {
|
|
|
|
self.dcx.stash_diagnostic(span, key, self.take_diag());
|
2023-04-30 02:20:53 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 12:52:22 -07:00
|
|
|
/// Delay emission of this diagnostic as a bug.
|
|
|
|
///
|
|
|
|
/// This can be useful in contexts where an error indicates a bug but
|
|
|
|
/// typically this only happens when other compilation errors have already
|
|
|
|
/// happened. In those cases this can be used to defer emission of this
|
|
|
|
/// diagnostic as a bug in the compiler only if no other errors have been
|
|
|
|
/// emitted.
|
|
|
|
///
|
|
|
|
/// In the meantime, though, callsites are required to deal with the "bug"
|
|
|
|
/// locally in whichever way makes the most sense.
|
2022-01-23 23:11:37 +00:00
|
|
|
#[track_caller]
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 12:17:35 +11:00
|
|
|
pub fn delay_as_bug(mut self) -> G::EmitResult {
|
2022-01-23 23:11:37 +00:00
|
|
|
self.downgrade_to_delayed_bug();
|
2022-11-03 14:15:17 +08:00
|
|
|
self.emit()
|
2017-08-23 12:52:22 -07:00
|
|
|
}
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
impl<G: EmissionGuarantee> Debug for DiagnosticBuilder<'_, G> {
|
2019-02-07 03:53:01 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2024-01-08 07:47:02 +11:00
|
|
|
self.diag.fmt(f)
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 07:47:02 +11:00
|
|
|
/// Destructor bomb: every `DiagnosticBuilder` must be consumed (emitted,
|
|
|
|
/// cancelled, etc.) or we emit a bug.
|
2023-12-22 10:55:54 +11:00
|
|
|
impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
|
2016-10-11 12:26:32 -04:00
|
|
|
fn drop(&mut self) {
|
2024-01-08 07:47:02 +11:00
|
|
|
match self.diag.take() {
|
|
|
|
Some(diag) if !panicking() => {
|
|
|
|
self.dcx.emit_diagnostic(Diagnostic::new(
|
|
|
|
Level::Bug,
|
|
|
|
DiagnosticMessage::from("the following error was constructed but not emitted"),
|
|
|
|
));
|
|
|
|
self.dcx.emit_diagnostic(*diag);
|
|
|
|
panic!("error was constructed but not emitted");
|
2022-01-26 03:39:14 +00:00
|
|
|
}
|
2024-01-08 07:47:02 +11:00
|
|
|
_ => {}
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 21:25:16 +01:00
|
|
|
|
|
|
|
#[macro_export]
|
2024-01-04 09:08:36 +11:00
|
|
|
macro_rules! struct_span_code_err {
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
|
|
|
|
$dcx.struct_span_err($span, format!($($message)*)).with_code($code)
|
2019-12-31 21:25:16 +01:00
|
|
|
})
|
|
|
|
}
|