2019-02-08 10:45:53 +00:00
|
|
|
use crate::snippet::Style;
|
2022-03-24 02:03:04 +00:00
|
|
|
use crate::{
|
2024-02-20 22:37:30 +00:00
|
|
|
CodeSuggestion, DiagCtxt, DiagnosticMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
|
|
|
|
MultiSpan, StashKey, SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
|
2022-03-24 02:03:04 +00:00
|
|
|
};
|
2024-02-11 11:50:50 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
2022-11-06 06:43:25 +00:00
|
|
|
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
|
2022-03-26 07:27:43 +00:00
|
|
|
use rustc_error_messages::FluentValue;
|
2022-03-05 20:54:49 +00:00
|
|
|
use rustc_lint_defs::{Applicability, LintExpectationId};
|
2024-02-20 22:37:30 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2022-10-12 20:55:28 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2022-03-26 07:27:43 +00:00
|
|
|
use std::borrow::Cow;
|
2023-05-17 10:30:14 +00:00
|
|
|
use std::fmt::{self, Debug};
|
2021-09-04 11:26:25 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2024-02-20 22:37:30 +00:00
|
|
|
use std::marker::PhantomData;
|
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 05:44:30 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2024-02-20 22:37:30 +00:00
|
|
|
use std::panic;
|
|
|
|
use std::thread::panicking;
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2024-02-22 07:32:06 +00:00
|
|
|
/// Error type for `DiagInner`'s `suggestions` field, indicating that
|
|
|
|
/// `.disable_suggestions()` was called on the `DiagInner`.
|
2022-01-24 09:19:33 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
|
|
|
|
pub struct SuggestionsDisabled;
|
|
|
|
|
2022-03-26 07:27:43 +00:00
|
|
|
/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
|
|
|
|
/// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of
|
|
|
|
/// diagnostic emission.
|
2024-01-30 05:06:18 +00:00
|
|
|
pub type DiagnosticArg<'iter> = (&'iter DiagnosticArgName, &'iter DiagnosticArgValue);
|
2022-10-03 13:02:49 +00:00
|
|
|
|
|
|
|
/// Name of a diagnostic argument.
|
2024-01-30 05:04:03 +00:00
|
|
|
pub type DiagnosticArgName = Cow<'static, str>;
|
2022-03-26 07:27:43 +00:00
|
|
|
|
|
|
|
/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted
|
|
|
|
/// to a `FluentValue` by the emitter to be used in diagnostic translation.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
|
2024-01-30 04:27:16 +00:00
|
|
|
pub enum DiagnosticArgValue {
|
|
|
|
Str(Cow<'static, str>),
|
2024-01-30 03:46:51 +00:00
|
|
|
// This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
|
|
|
|
// safely fits in an `f64`. Any integers bigger than that will be converted
|
|
|
|
// to strings in `into_diagnostic_arg` and stored using the `Str` variant.
|
|
|
|
Number(i32),
|
2024-01-30 04:27:16 +00:00
|
|
|
StrListSepByAnd(Vec<Cow<'static, str>>),
|
2022-03-26 07:27:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:07:49 +00:00
|
|
|
pub type DiagnosticArgMap = FxIndexMap<DiagnosticArgName, DiagnosticArgValue>;
|
|
|
|
|
2024-02-20 22:37:30 +00:00
|
|
|
/// Trait for types that `DiagnosticBuilder::emit` can return as a "guarantee"
|
|
|
|
/// (or "proof") token that the emission happened.
|
|
|
|
pub trait EmissionGuarantee: Sized {
|
|
|
|
/// This exists so that bugs and fatal errors can both result in `!` (an
|
|
|
|
/// abort) when emitted, but have different aborting behaviour.
|
|
|
|
type EmitResult = Self;
|
|
|
|
|
|
|
|
/// Implementation of `DiagnosticBuilder::emit`, fully controlled by each
|
|
|
|
/// `impl` of `EmissionGuarantee`, to make it impossible to create a value
|
|
|
|
/// of `Self::EmitResult` without actually performing the emission.
|
|
|
|
#[track_caller]
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmissionGuarantee for ErrorGuaranteed {
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_error_guaranteed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmissionGuarantee for () {
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
|
|
|
|
/// bug diagnostics.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct BugAbort;
|
|
|
|
|
|
|
|
impl EmissionGuarantee for BugAbort {
|
|
|
|
type EmitResult = !;
|
|
|
|
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_nothing();
|
|
|
|
panic::panic_any(ExplicitBug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 = !;
|
|
|
|
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_nothing();
|
|
|
|
crate::FatalError.raise()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
|
|
|
|
fn emit_producing_guarantee(db: DiagnosticBuilder<'_, Self>) -> Self::EmitResult {
|
|
|
|
db.emit_producing_nothing();
|
|
|
|
rustc_span::fatal_error::FatalError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait implemented by error types. This is rarely implemented manually. Instead, use
|
|
|
|
/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
|
|
|
|
#[rustc_diagnostic_item = "IntoDiagnostic"]
|
|
|
|
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
|
|
|
|
/// Write out as a diagnostic out of `DiagCtxt`.
|
|
|
|
#[must_use]
|
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, G> IntoDiagnostic<'a, G> for Spanned<T>
|
|
|
|
where
|
|
|
|
T: IntoDiagnostic<'a, G>,
|
|
|
|
G: EmissionGuarantee,
|
|
|
|
{
|
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
|
|
|
|
self.node.into_diagnostic(dcx, level).with_span(self.span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 15:45:41 +00:00
|
|
|
/// Converts a value of a type into a `DiagnosticArg` (typically a field of an `IntoDiagnostic`
|
2022-03-30 08:45:36 +00:00
|
|
|
/// struct). Implemented as a custom trait rather than `From` so that it is implemented on the type
|
|
|
|
/// being converted rather than on `DiagnosticArgValue`, which enables types from other `rustc_*`
|
|
|
|
/// crates to implement this.
|
|
|
|
pub trait IntoDiagnosticArg {
|
2024-01-30 04:27:16 +00:00
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue;
|
2022-03-30 08:45:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 04:27:16 +00:00
|
|
|
impl IntoDiagnosticArg for DiagnosticArgValue {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
|
|
|
|
self
|
2022-11-02 18:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 04:27:16 +00:00
|
|
|
impl Into<FluentValue<'static>> for DiagnosticArgValue {
|
|
|
|
fn into(self) -> FluentValue<'static> {
|
2022-03-26 07:27:43 +00:00
|
|
|
match self {
|
|
|
|
DiagnosticArgValue::Str(s) => From::from(s),
|
|
|
|
DiagnosticArgValue::Number(n) => From::from(n),
|
2022-11-06 06:43:25 +00:00
|
|
|
DiagnosticArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),
|
2022-03-26 07:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:59:45 +00:00
|
|
|
/// Trait implemented by error types. This should not be implemented manually. Instead, use
|
2022-09-18 15:47:31 +00:00
|
|
|
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
|
2022-11-01 12:45:58 +00:00
|
|
|
#[rustc_diagnostic_item = "AddToDiagnostic"]
|
2022-10-03 13:09:05 +00:00
|
|
|
pub trait AddToDiagnostic
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-04-26 10:59:45 +00:00
|
|
|
/// Add a subdiagnostic to an existing diagnostic.
|
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 05:44:30 +00:00
|
|
|
fn add_to_diagnostic<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>) {
|
2022-10-03 13:09:05 +00:00
|
|
|
self.add_to_diagnostic_with(diag, |_, m| m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a subdiagnostic to an existing diagnostic where `f` is invoked on every message used
|
|
|
|
/// (to optionally perform eager translation).
|
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 05:44:30 +00:00
|
|
|
fn add_to_diagnostic_with<G: EmissionGuarantee, F: SubdiagnosticMessageOp<G>>(
|
|
|
|
self,
|
|
|
|
diag: &mut DiagnosticBuilder<'_, G>,
|
|
|
|
f: F,
|
|
|
|
);
|
2022-04-26 10:59:45 +00: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 05:44:30 +00:00
|
|
|
pub trait SubdiagnosticMessageOp<G> =
|
|
|
|
Fn(&mut DiagnosticBuilder<'_, G>, SubdiagnosticMessage) -> SubdiagnosticMessage;
|
2024-02-06 05:35:19 +00:00
|
|
|
|
2022-06-29 15:07:46 +00:00
|
|
|
/// Trait implemented by lint types. This should not be implemented manually. Instead, use
|
|
|
|
/// `#[derive(LintDiagnostic)]` -- see [rustc_macros::LintDiagnostic].
|
|
|
|
#[rustc_diagnostic_item = "DecorateLint"]
|
|
|
|
pub trait DecorateLint<'a, G: EmissionGuarantee> {
|
|
|
|
/// Decorate and emit a lint.
|
2023-12-15 16:17:55 +00:00
|
|
|
fn decorate_lint<'b>(self, diag: &'b mut DiagnosticBuilder<'a, G>);
|
2022-09-16 07:01:02 +00:00
|
|
|
|
|
|
|
fn msg(&self) -> DiagnosticMessage;
|
2022-06-29 15:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:08:20 +00:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable)]
|
|
|
|
pub struct DiagnosticLocation {
|
|
|
|
file: Cow<'static, str>,
|
|
|
|
line: u32,
|
|
|
|
col: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticLocation {
|
|
|
|
#[track_caller]
|
|
|
|
fn caller() -> Self {
|
2024-02-20 22:37:30 +00:00
|
|
|
let loc = panic::Location::caller();
|
2022-10-18 22:08:20 +00:00
|
|
|
DiagnosticLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DiagnosticLocation {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}:{}:{}", self.file, self.line, self.col)
|
|
|
|
}
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
|
2024-01-13 02:11:56 +00:00
|
|
|
pub struct IsLint {
|
|
|
|
/// The lint name.
|
|
|
|
pub(crate) name: String,
|
|
|
|
/// Indicates whether this lint should show up in cargo's future breakage report.
|
|
|
|
has_future_breakage: bool,
|
2017-10-27 06:21:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 19:07:37 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
pub struct DiagnosticStyledString(pub Vec<StringPart>);
|
|
|
|
|
|
|
|
impl DiagnosticStyledString {
|
|
|
|
pub fn new() -> DiagnosticStyledString {
|
|
|
|
DiagnosticStyledString(vec![])
|
|
|
|
}
|
|
|
|
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
|
2024-02-02 22:02:36 +00:00
|
|
|
self.0.push(StringPart::normal(t));
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
|
|
|
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
|
2024-02-02 22:02:36 +00:00
|
|
|
self.0.push(StringPart::highlighted(t));
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
2019-11-24 00:01:20 +00:00
|
|
|
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
|
|
|
|
if highlight {
|
2019-11-24 02:36:33 +00:00
|
|
|
self.push_highlighted(t);
|
2019-11-24 00:01:20 +00:00
|
|
|
} else {
|
2019-11-24 02:36:33 +00:00
|
|
|
self.push_normal(t);
|
2019-11-24 00:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
2024-02-02 22:02:36 +00:00
|
|
|
DiagnosticStyledString(vec![StringPart::normal(t)])
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
2024-02-02 22:02:36 +00:00
|
|
|
DiagnosticStyledString(vec![StringPart::highlighted(t)])
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
2021-06-16 21:35:42 +00:00
|
|
|
|
|
|
|
pub fn content(&self) -> String {
|
2024-01-30 06:10:48 +00:00
|
|
|
self.0.iter().map(|x| x.content.as_str()).collect::<String>()
|
2021-06-16 21:35:42 +00:00
|
|
|
}
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 19:07:37 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2024-01-30 06:10:48 +00:00
|
|
|
pub struct StringPart {
|
|
|
|
content: String,
|
|
|
|
style: Style,
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 21:35:42 +00:00
|
|
|
impl StringPart {
|
2024-02-02 22:02:36 +00:00
|
|
|
pub fn normal<S: Into<String>>(content: S) -> StringPart {
|
|
|
|
StringPart { content: content.into(), style: Style::NoStyle }
|
2024-01-30 06:10:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 22:02:36 +00:00
|
|
|
pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
|
|
|
|
StringPart { content: content.into(), style: Style::Highlight }
|
2021-06-16 21:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 22:37:30 +00:00
|
|
|
/// The main part of a diagnostic. Note that `DiagnosticBuilder`, which wraps
|
|
|
|
/// this type, is used for most operations, and should be used instead whenever
|
|
|
|
/// possible. This type should only be used when `DiagnosticBuilder`'s lifetime
|
|
|
|
/// causes difficulties, e.g. when storing diagnostics within `DiagCtxt`.
|
|
|
|
#[must_use]
|
|
|
|
#[derive(Clone, Debug, Encodable, Decodable)]
|
2024-02-22 07:32:06 +00:00
|
|
|
pub struct DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
// NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
|
|
|
|
// outside of what methods in this crate themselves allow.
|
|
|
|
pub(crate) level: Level,
|
|
|
|
|
|
|
|
pub messages: Vec<(DiagnosticMessage, Style)>,
|
|
|
|
pub code: Option<ErrCode>,
|
|
|
|
pub span: MultiSpan,
|
|
|
|
pub children: Vec<SubDiagnostic>,
|
|
|
|
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
|
2024-02-15 19:07:49 +00:00
|
|
|
pub args: DiagnosticArgMap,
|
2024-02-20 22:37:30 +00:00
|
|
|
|
|
|
|
/// This is not used for highlighting or rendering any error message. Rather, it can be used
|
|
|
|
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
|
|
|
|
/// `span` if there is one. Otherwise, it is `DUMMY_SP`.
|
|
|
|
pub sort_span: Span,
|
|
|
|
|
|
|
|
pub is_lint: Option<IsLint>,
|
|
|
|
|
|
|
|
/// With `-Ztrack_diagnostics` enabled,
|
|
|
|
/// we print where in rustc this error was emitted.
|
|
|
|
pub(crate) emitted_at: DiagnosticLocation,
|
|
|
|
}
|
|
|
|
|
2024-02-22 07:32:06 +00:00
|
|
|
impl DiagInner {
|
2022-10-18 22:08:20 +00:00
|
|
|
#[track_caller]
|
2022-03-26 07:27:43 +00:00
|
|
|
pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
|
2024-02-22 07:32:06 +00:00
|
|
|
DiagInner::new_with_messages(level, vec![(message.into(), Style::NoStyle)])
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 12:47:46 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn new_with_messages(level: Level, messages: Vec<(DiagnosticMessage, Style)>) -> Self {
|
2024-02-22 07:32:06 +00:00
|
|
|
DiagInner {
|
2022-11-09 12:47:46 +00:00
|
|
|
level,
|
2023-12-20 06:12:17 +00:00
|
|
|
messages,
|
2022-11-09 12:47:46 +00:00
|
|
|
code: None,
|
|
|
|
span: MultiSpan::new(),
|
|
|
|
children: vec![],
|
|
|
|
suggestions: Ok(vec![]),
|
|
|
|
args: Default::default(),
|
|
|
|
sort_span: DUMMY_SP,
|
2024-01-13 02:11:56 +00:00
|
|
|
is_lint: None,
|
2022-11-09 14:14:58 +00:00
|
|
|
emitted_at: DiagnosticLocation::caller(),
|
2022-11-09 12:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:11:37 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn level(&self) -> Level {
|
|
|
|
self.level
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:29:29 +00:00
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
match self.level {
|
2024-01-31 00:23:54 +00:00
|
|
|
Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
|
2018-07-20 15:29:29 +00:00
|
|
|
|
2024-02-12 05:48:45 +00:00
|
|
|
Level::ForceWarning(_)
|
2024-01-09 01:28:45 +00:00
|
|
|
| Level::Warning
|
2022-03-20 19:02:18 +00:00
|
|
|
| Level::Note
|
|
|
|
| Level::OnceNote
|
|
|
|
| Level::Help
|
2023-07-25 17:37:45 +00:00
|
|
|
| Level::OnceHelp
|
2024-01-30 22:25:42 +00:00
|
|
|
| Level::FailureNote
|
2022-03-20 19:02:18 +00:00
|
|
|
| Level::Allow
|
|
|
|
| Level::Expect(_) => false,
|
2020-08-13 19:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-03 22:35:50 +00:00
|
|
|
pub(crate) fn update_unstable_expectation_id(
|
2022-03-05 20:54:49 +00:00
|
|
|
&mut self,
|
2023-12-31 12:04:43 +00:00
|
|
|
unstable_to_stable: &FxIndexMap<LintExpectationId, LintExpectationId>,
|
2022-03-05 20:54:49 +00:00
|
|
|
) {
|
2024-01-09 01:28:45 +00:00
|
|
|
if let Level::Expect(expectation_id) | Level::ForceWarning(Some(expectation_id)) =
|
2022-06-05 10:33:45 +00:00
|
|
|
&mut self.level
|
|
|
|
{
|
2022-03-05 20:54:49 +00:00
|
|
|
if expectation_id.is_stable() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The unstable to stable map only maps the unstable `AttrId` to a stable `HirId` with an attribute index.
|
|
|
|
// The lint index inside the attribute is manually transferred here.
|
|
|
|
let lint_index = expectation_id.get_lint_index();
|
|
|
|
expectation_id.set_lint_index(None);
|
2022-07-22 16:48:36 +00:00
|
|
|
let mut stable_id = unstable_to_stable
|
2022-11-29 11:01:17 +00:00
|
|
|
.get(expectation_id)
|
2022-07-22 16:48:36 +00:00
|
|
|
.expect("each unstable `LintExpectationId` must have a matching stable id")
|
|
|
|
.normalize();
|
2022-03-05 20:54:49 +00:00
|
|
|
|
|
|
|
stable_id.set_lint_index(lint_index);
|
|
|
|
*expectation_id = stable_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 06:25:11 +00:00
|
|
|
/// Indicates whether this diagnostic should show up in cargo's future breakage report.
|
2023-12-03 22:35:50 +00:00
|
|
|
pub(crate) fn has_future_breakage(&self) -> bool {
|
2024-01-13 02:11:56 +00:00
|
|
|
matches!(self.is_lint, Some(IsLint { has_future_breakage: true, .. }))
|
2018-07-20 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 22:35:50 +00:00
|
|
|
pub(crate) fn is_force_warn(&self) -> bool {
|
2024-01-09 01:28:45 +00:00
|
|
|
match self.level {
|
|
|
|
Level::ForceWarning(_) => {
|
2024-01-13 02:11:56 +00:00
|
|
|
assert!(self.is_lint.is_some());
|
2024-01-09 01:28:45 +00:00
|
|
|
true
|
|
|
|
}
|
2021-06-04 12:37:20 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 05:44:30 +00:00
|
|
|
// See comment on `DiagnosticBuilder::subdiagnostic_message_to_diagnostic_message`.
|
|
|
|
pub(crate) fn subdiagnostic_message_to_diagnostic_message(
|
|
|
|
&self,
|
|
|
|
attr: impl Into<SubdiagnosticMessage>,
|
|
|
|
) -> DiagnosticMessage {
|
|
|
|
let msg =
|
|
|
|
self.messages.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
|
|
|
|
msg.with_subdiagnostic_message(attr.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sub(
|
|
|
|
&mut self,
|
|
|
|
level: Level,
|
|
|
|
message: impl Into<SubdiagnosticMessage>,
|
|
|
|
span: MultiSpan,
|
|
|
|
) {
|
|
|
|
let sub = SubDiagnostic {
|
|
|
|
level,
|
|
|
|
messages: vec![(
|
|
|
|
self.subdiagnostic_message_to_diagnostic_message(message),
|
|
|
|
Style::NoStyle,
|
|
|
|
)],
|
|
|
|
span,
|
|
|
|
};
|
|
|
|
self.children.push(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn arg(&mut self, name: impl Into<DiagnosticArgName>, arg: impl IntoDiagnosticArg) {
|
|
|
|
self.args.insert(name.into(), arg.into_diagnostic_arg());
|
|
|
|
}
|
|
|
|
|
2024-02-20 22:37:30 +00:00
|
|
|
/// Fields used for Hash, and PartialEq trait.
|
|
|
|
fn keys(
|
|
|
|
&self,
|
|
|
|
) -> (
|
|
|
|
&Level,
|
|
|
|
&[(DiagnosticMessage, Style)],
|
|
|
|
&Option<ErrCode>,
|
|
|
|
&MultiSpan,
|
|
|
|
&[SubDiagnostic],
|
|
|
|
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
|
|
|
|
Vec<(&DiagnosticArgName, &DiagnosticArgValue)>,
|
|
|
|
&Option<IsLint>,
|
|
|
|
) {
|
|
|
|
(
|
|
|
|
&self.level,
|
|
|
|
&self.messages,
|
|
|
|
&self.code,
|
|
|
|
&self.span,
|
|
|
|
&self.children,
|
|
|
|
&self.suggestions,
|
2024-02-15 19:07:49 +00:00
|
|
|
self.args.iter().collect(),
|
2024-02-20 22:37:30 +00:00
|
|
|
// omit self.sort_span
|
|
|
|
&self.is_lint,
|
|
|
|
// omit self.emitted_at
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 07:32:06 +00:00
|
|
|
impl Hash for DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: Hasher,
|
|
|
|
{
|
|
|
|
self.keys().hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 07:32:06 +00:00
|
|
|
impl PartialEq for DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.keys() == other.keys()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A "sub"-diagnostic attached to a parent diagnostic.
|
|
|
|
/// For example, a note attached to an error.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
|
|
|
|
pub struct SubDiagnostic {
|
|
|
|
pub level: Level,
|
|
|
|
pub messages: Vec<(DiagnosticMessage, Style)>,
|
|
|
|
pub span: MultiSpan,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used for emitting structured error messages and other diagnostic information.
|
2024-02-22 07:32:06 +00:00
|
|
|
/// Wraps a `DiagInner`, adding some useful things.
|
2024-02-20 22:37:30 +00:00
|
|
|
/// - 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`.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// If there is some state in a downstream crate you would like to
|
|
|
|
/// access in the methods of `DiagnosticBuilder` here, consider
|
|
|
|
/// extending `DiagCtxtFlags`.
|
|
|
|
#[must_use]
|
|
|
|
pub struct DiagnosticBuilder<'a, G: EmissionGuarantee = ErrorGuaranteed> {
|
|
|
|
pub dcx: &'a DiagCtxt,
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2024-02-22 07:32:06 +00:00
|
|
|
/// Why the Box? `DiagInner` is a large type, and `DiagnosticBuilder` is
|
2024-02-20 22:37:30 +00:00
|
|
|
/// 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).
|
2024-02-22 07:32:06 +00:00
|
|
|
diag: Option<Box<DiagInner>>,
|
2024-02-20 22:37:30 +00:00
|
|
|
|
2024-02-20 22:57:58 +00:00
|
|
|
_marker: PhantomData<G>,
|
2024-02-20 22:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cloning a `DiagnosticBuilder` is a recipe for a diagnostic being emitted
|
|
|
|
// twice, which would be bad.
|
|
|
|
impl<G> !Clone for DiagnosticBuilder<'_, G> {}
|
|
|
|
|
|
|
|
rustc_data_structures::static_assert_size!(
|
|
|
|
DiagnosticBuilder<'_, ()>,
|
|
|
|
2 * std::mem::size_of::<usize>()
|
|
|
|
);
|
|
|
|
|
|
|
|
impl<G: EmissionGuarantee> Deref for DiagnosticBuilder<'_, G> {
|
2024-02-22 07:32:06 +00:00
|
|
|
type Target = DiagInner;
|
2024-02-20 22:37:30 +00:00
|
|
|
|
2024-02-22 07:32:06 +00:00
|
|
|
fn deref(&self) -> &DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
self.diag.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
|
2024-02-22 07:32:06 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
self.diag.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<G: EmissionGuarantee> Debug for DiagnosticBuilder<'_, G> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.diag.fmt(f)
|
|
|
|
}
|
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 05:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `DiagnosticBuilder` impls many `&mut self -> &mut Self` methods. Each one
|
|
|
|
/// modifies an existing diagnostic, either in a standalone fashion, e.g.
|
|
|
|
/// `err.code(code);`, or in a chained fashion to make multiple modifications,
|
|
|
|
/// e.g. `err.code(code).span(span);`.
|
|
|
|
///
|
|
|
|
/// This macro creates an equivalent `self -> Self` method, with a `with_`
|
|
|
|
/// prefix. This can be used in a chained fashion when making a new diagnostic,
|
|
|
|
/// e.g. `let err = struct_err(msg).with_code(code);`, or emitting a new
|
|
|
|
/// diagnostic, e.g. `struct_err(msg).with_code(code).emit();`.
|
|
|
|
///
|
|
|
|
/// Although the latter method can be used to modify an existing diagnostic,
|
|
|
|
/// e.g. `err = err.with_code(code);`, this should be avoided because the former
|
|
|
|
/// method gives shorter code, e.g. `err.code(code);`.
|
|
|
|
///
|
|
|
|
/// Note: the `with_` methods are added only when needed. If you want to use
|
|
|
|
/// one and it's not defined, feel free to add it.
|
|
|
|
///
|
|
|
|
/// Note: any doc comments must be within the `with_fn!` call.
|
|
|
|
macro_rules! with_fn {
|
|
|
|
{
|
|
|
|
$with_f:ident,
|
|
|
|
$(#[$attrs:meta])*
|
|
|
|
pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
|
|
|
|
$($body:tt)*
|
|
|
|
}
|
|
|
|
} => {
|
|
|
|
// The original function.
|
|
|
|
$(#[$attrs])*
|
|
|
|
#[doc = concat!("See [`DiagnosticBuilder::", stringify!($f), "()`].")]
|
|
|
|
pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
|
|
|
|
$($body)*
|
|
|
|
}
|
|
|
|
|
|
|
|
// The `with_*` variant.
|
|
|
|
$(#[$attrs])*
|
|
|
|
#[doc = concat!("See [`DiagnosticBuilder::", stringify!($f), "()`].")]
|
|
|
|
pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
|
|
|
|
$self.$f($($name),*);
|
|
|
|
$self
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
2024-02-20 22:37:30 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
|
|
|
#[track_caller]
|
|
|
|
pub fn new<M: Into<DiagnosticMessage>>(dcx: &'a DiagCtxt, level: Level, message: M) -> Self {
|
2024-02-22 07:32:06 +00:00
|
|
|
Self::new_diagnostic(dcx, DiagInner::new(level, message))
|
2024-02-20 22:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `DiagnosticBuilder` with an already constructed
|
|
|
|
/// diagnostic.
|
|
|
|
#[track_caller]
|
2024-02-22 07:32:06 +00:00
|
|
|
pub(crate) fn new_diagnostic(dcx: &'a DiagCtxt, diag: DiagInner) -> Self {
|
2024-02-20 22:37:30 +00:00
|
|
|
debug!("Created new diagnostic");
|
|
|
|
Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:11:37 +00: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.
|
|
|
|
#[track_caller]
|
2024-01-03 03:27:35 +00:00
|
|
|
pub fn downgrade_to_delayed_bug(&mut self) {
|
2022-01-26 03:39:14 +00:00
|
|
|
assert!(
|
2024-01-31 00:23:54 +00:00
|
|
|
matches!(self.level, Level::Error | Level::DelayedBug),
|
2022-01-26 03:39:14 +00:00
|
|
|
"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
|
|
|
|
self.level
|
|
|
|
);
|
2024-01-31 00:23:54 +00:00
|
|
|
self.level = Level::DelayedBug;
|
2022-01-23 23:11:37 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_span_label,
|
2024-01-03 03:32:17 +00:00
|
|
|
/// Appends a labeled span to the diagnostic.
|
2019-08-12 08:53:09 +00:00
|
|
|
///
|
2024-01-03 03:32:17 +00:00
|
|
|
/// 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.
|
2020-08-11 07:42:25 +00:00
|
|
|
///
|
2024-01-03 03:32:17 +00:00
|
|
|
/// 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.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-05-24 14:09:47 +00:00
|
|
|
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut 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 05:44:30 +00:00
|
|
|
let msg = self.subdiagnostic_message_to_diagnostic_message(label);
|
|
|
|
self.span.push_span_label(span, msg);
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_span_labels,
|
2022-01-23 20:41:46 +00:00
|
|
|
/// Labels all the given spans with the provided label.
|
|
|
|
/// See [`Self::span_label()`] for more information.
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 00:55:21 +00:00
|
|
|
pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
|
2022-01-23 20:41:46 +00:00
|
|
|
for span in spans {
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 00:55:21 +00:00
|
|
|
self.span_label(span, label.to_string());
|
2022-01-23 20:41:46 +00:00
|
|
|
}
|
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2022-01-23 20:41:46 +00:00
|
|
|
|
2022-12-30 06:46:17 +00:00
|
|
|
pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
|
2018-11-06 00:27:28 +00:00
|
|
|
let before = self.span.clone();
|
2023-12-23 22:08:41 +00:00
|
|
|
self.span(after);
|
2018-11-06 00:27:28 +00:00
|
|
|
for span_label in before.span_labels() {
|
|
|
|
if let Some(label) = span_label.label {
|
2022-12-30 06:46:17 +00:00
|
|
|
if span_label.is_primary && keep_label {
|
2022-12-11 22:49:50 +00:00
|
|
|
self.span.push_span_label(after, label);
|
|
|
|
} else {
|
|
|
|
self.span.push_span_label(span_label.span, label);
|
|
|
|
}
|
2018-11-06 00:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
pub fn note_expected_found(
|
2019-11-13 22:16:56 +00:00
|
|
|
&mut self,
|
|
|
|
expected_label: &dyn fmt::Display,
|
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found_label: &dyn fmt::Display,
|
|
|
|
found: DiagnosticStyledString,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn note_expected_found_extra(
|
|
|
|
&mut self,
|
|
|
|
expected_label: &dyn fmt::Display,
|
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found_label: &dyn fmt::Display,
|
|
|
|
found: DiagnosticStyledString,
|
|
|
|
expected_extra: &dyn fmt::Display,
|
|
|
|
found_extra: &dyn fmt::Display,
|
|
|
|
) -> &mut Self {
|
2019-12-20 03:44:06 +00:00
|
|
|
let expected_label = expected_label.to_string();
|
|
|
|
let expected_label = if expected_label.is_empty() {
|
|
|
|
"expected".to_string()
|
|
|
|
} else {
|
2023-07-24 04:08:09 +00:00
|
|
|
format!("expected {expected_label}")
|
2019-12-20 03:44:06 +00:00
|
|
|
};
|
|
|
|
let found_label = found_label.to_string();
|
|
|
|
let found_label = if found_label.is_empty() {
|
|
|
|
"found".to_string()
|
|
|
|
} else {
|
2023-07-24 04:08:09 +00:00
|
|
|
format!("found {found_label}")
|
2019-12-20 03:44:06 +00:00
|
|
|
};
|
2019-11-13 22:16:56 +00:00
|
|
|
let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
|
|
|
|
(expected_label.len() - found_label.len(), 0)
|
|
|
|
} else {
|
|
|
|
(0, found_label.len() - expected_label.len())
|
|
|
|
};
|
2024-02-02 22:02:36 +00:00
|
|
|
let mut msg = vec![StringPart::normal(format!(
|
|
|
|
"{}{} `",
|
|
|
|
" ".repeat(expected_padding),
|
|
|
|
expected_label
|
|
|
|
))];
|
|
|
|
msg.extend(expected.0.into_iter());
|
|
|
|
msg.push(StringPart::normal(format!("`{expected_extra}\n")));
|
|
|
|
msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
|
|
|
|
msg.extend(found.0.into_iter());
|
|
|
|
msg.push(StringPart::normal(format!("`{found_extra}")));
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
// For now, just attach these as notes.
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 22:31:59 +00:00
|
|
|
self.highlighted_note(msg);
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:09:20 +00:00
|
|
|
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
|
Show trait method signature when impl differs
When the trait's span is available, it is already being used, add a
`note` for the cases where the span isn't available:
```
error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> $DIR/trait_type.rs:21:11
|
21 | fn fmt(&self) -> () { }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in the impl
--> $DIR/trait_type.rs:25:4
|
25 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0046]: not all trait items implemented, missing: `fmt`
--> $DIR/trait_type.rs:28:1
|
28 | impl std::fmt::Display for MyType4 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
```
2017-06-01 06:14:43 +00:00
|
|
|
self.highlighted_note(vec![
|
2024-02-02 22:02:36 +00:00
|
|
|
StringPart::normal(format!("`{name}` from trait: `")),
|
|
|
|
StringPart::highlighted(signature),
|
|
|
|
StringPart::normal("`"),
|
Show trait method signature when impl differs
When the trait's span is available, it is already being used, add a
`note` for the cases where the span isn't available:
```
error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> $DIR/trait_type.rs:21:11
|
21 | fn fmt(&self) -> () { }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in the impl
--> $DIR/trait_type.rs:25:4
|
25 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0046]: not all trait items implemented, missing: `fmt`
--> $DIR/trait_type.rs:28:1
|
28 | impl std::fmt::Display for MyType4 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
```
2017-06-01 06:14:43 +00:00
|
|
|
]);
|
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_note,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Add a note attached to this diagnostic.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-05-24 14:09:47 +00:00
|
|
|
pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::Note, msg, MultiSpan::new());
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2024-02-02 22:02:36 +00:00
|
|
|
fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
|
2017-01-11 21:55:41 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::note()`], but it's only printed once.
|
2022-05-24 14:09:47 +00:00
|
|
|
pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::OnceNote, msg, MultiSpan::new());
|
2022-03-20 19:02:18 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_span_note,
|
2019-08-12 08:53:09 +00:00
|
|
|
/// Prints the span with a note above it.
|
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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::note()`], but it gets its own span.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
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 05:44:30 +00:00
|
|
|
pub fn span_note(
|
2022-03-26 07:27:43 +00:00
|
|
|
&mut 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 05:44:30 +00:00
|
|
|
sp: impl Into<MultiSpan>,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-03-26 07:27:43 +00:00
|
|
|
) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::Note, msg, sp.into());
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2022-03-20 19:02:18 +00:00
|
|
|
/// Prints the span with a note above it.
|
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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::note_once()`], but it gets its own span.
|
2022-03-26 07:27:43 +00:00
|
|
|
pub fn span_note_once<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
sp: S,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-03-26 07:27:43 +00:00
|
|
|
) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::OnceNote, msg, sp.into());
|
2022-03-20 19:02:18 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_warn,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Add a warning attached to this diagnostic.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-05-24 14:09:47 +00:00
|
|
|
pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
2024-01-09 01:28:45 +00:00
|
|
|
self.sub(Level::Warning, msg, MultiSpan::new());
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Prints the span with a warning above it.
|
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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::warn()`], but it gets its own span.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-03-26 07:27:43 +00:00
|
|
|
pub fn span_warn<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
sp: S,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-03-26 07:27:43 +00:00
|
|
|
) -> &mut Self {
|
2024-01-09 01:28:45 +00:00
|
|
|
self.sub(Level::Warning, msg, sp.into());
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_help,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Add a help message attached to this diagnostic.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-05-24 14:09:47 +00:00
|
|
|
pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::Help, msg, MultiSpan::new());
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00: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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::help()`], but it's only printed once.
|
2023-07-25 17:37:45 +00:00
|
|
|
pub fn help_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::OnceHelp, msg, MultiSpan::new());
|
2023-07-25 17:37:45 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-12-13 20:56:40 +00:00
|
|
|
/// Add a help message attached to this diagnostic with a customizable highlighted message.
|
2024-02-02 22:02:36 +00:00
|
|
|
pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
|
2021-12-13 20:56:40 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-08-12 08:53:09 +00:00
|
|
|
/// Prints the span with some help above it.
|
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 05:44:30 +00:00
|
|
|
/// This is like [`DiagnosticBuilder::help()`], but it gets its own span.
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-03-26 07:27:43 +00:00
|
|
|
pub fn span_help<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
sp: S,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-03-26 07:27:43 +00:00
|
|
|
) -> &mut Self {
|
2023-12-20 23:24:44 +00:00
|
|
|
self.sub(Level::Help, msg, sp.into());
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
/// Disallow attaching suggestions this diagnostic.
|
|
|
|
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
|
|
|
|
/// (before and after the call to `disable_suggestions`) will be ignored.
|
|
|
|
pub fn disable_suggestions(&mut self) -> &mut Self {
|
|
|
|
self.suggestions = Err(SuggestionsDisabled);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper for pushing to `self.suggestions`, if available (not disable).
|
|
|
|
fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
|
2024-02-10 17:43:55 +00:00
|
|
|
for subst in &suggestion.substitutions {
|
|
|
|
for part in &subst.parts {
|
2024-02-08 16:03:38 +00:00
|
|
|
let span = part.span;
|
|
|
|
let call_site = span.ctxt().outer_expn_data().call_site;
|
2024-02-10 17:43:55 +00:00
|
|
|
if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
|
|
|
|
// Ignore if spans is from derive macro.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 15:12:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
if let Ok(suggestions) = &mut self.suggestions {
|
|
|
|
suggestions.push(suggestion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_multipart_suggestion,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Show a suggestion that has multiple parts to it.
|
|
|
|
/// In other words, multiple changes need to be applied as part of this suggestion.
|
2019-01-25 21:03:27 +00:00
|
|
|
pub fn multipart_suggestion(
|
2018-05-21 16:06:28 +00:00
|
|
|
&mut self,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2018-05-21 16:06:28 +00:00
|
|
|
suggestion: Vec<(Span, String)>,
|
2018-09-15 11:44:28 +00:00
|
|
|
applicability: Applicability,
|
2018-05-21 16:06:28 +00:00
|
|
|
) -> &mut Self {
|
2021-05-10 12:59:54 +00:00
|
|
|
self.multipart_suggestion_with_style(
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2018-09-15 11:44:28 +00:00
|
|
|
applicability,
|
2021-05-10 12:59:54 +00:00
|
|
|
SuggestionStyle::ShowCode,
|
|
|
|
)
|
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 05:44:30 +00:00
|
|
|
} }
|
2017-05-09 08:04:24 +00:00
|
|
|
|
2021-08-10 10:53:43 +00:00
|
|
|
/// Show a suggestion that has multiple parts to it, always as it's own subdiagnostic.
|
|
|
|
/// In other words, multiple changes need to be applied as part of this suggestion.
|
|
|
|
pub fn multipart_suggestion_verbose(
|
|
|
|
&mut self,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2021-08-10 10:53:43 +00:00
|
|
|
suggestion: Vec<(Span, String)>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.multipart_suggestion_with_style(
|
|
|
|
msg,
|
|
|
|
suggestion,
|
|
|
|
applicability,
|
|
|
|
SuggestionStyle::ShowAlways,
|
|
|
|
)
|
|
|
|
}
|
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 05:44:30 +00:00
|
|
|
|
|
|
|
/// [`DiagnosticBuilder::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
|
2021-05-07 17:44:32 +00:00
|
|
|
pub fn multipart_suggestion_with_style(
|
|
|
|
&mut self,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2023-11-20 18:42:09 +00:00
|
|
|
mut suggestion: Vec<(Span, String)>,
|
2021-05-07 17:44:32 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
style: SuggestionStyle,
|
|
|
|
) -> &mut Self {
|
2023-11-20 18:42:09 +00:00
|
|
|
suggestion.sort_unstable();
|
|
|
|
suggestion.dedup();
|
|
|
|
|
|
|
|
let parts = suggestion
|
2023-01-15 13:29:20 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
assert!(!parts.is_empty());
|
|
|
|
debug_assert_eq!(
|
|
|
|
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
|
|
|
|
None,
|
|
|
|
"Span must not be empty and have no suggestion",
|
|
|
|
);
|
|
|
|
debug_assert_eq!(
|
|
|
|
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
|
|
|
|
None,
|
|
|
|
"suggestion must not have overlapping parts",
|
2022-10-11 16:17:59 +00:00
|
|
|
);
|
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
self.push_suggestion(CodeSuggestion {
|
2023-01-15 13:29:20 +00:00
|
|
|
substitutions: vec![Substitution { parts }],
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
2021-05-07 17:44:32 +00:00
|
|
|
style,
|
|
|
|
applicability,
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:16:22 +00:00
|
|
|
/// Prints out a message with for a multipart suggestion without showing the suggested code.
|
|
|
|
///
|
|
|
|
/// This is intended to be used for suggestions that are obvious in what the changes need to
|
|
|
|
/// be from the message, showing the span label inline would be visually unpleasant
|
|
|
|
/// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
|
|
|
|
/// improve understandability.
|
|
|
|
pub fn tool_only_multipart_suggestion(
|
|
|
|
&mut self,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2019-02-11 19:16:22 +00:00
|
|
|
suggestion: Vec<(Span, String)>,
|
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2022-08-22 11:38:08 +00:00
|
|
|
self.multipart_suggestion_with_style(
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2018-09-15 11:44:28 +00:00
|
|
|
applicability,
|
2022-08-22 11:38:08 +00:00
|
|
|
SuggestionStyle::CompletelyHidden,
|
|
|
|
)
|
2017-05-09 08:04:24 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_span_suggestion,
|
2019-01-25 21:03:27 +00:00
|
|
|
/// Prints out a message with a suggested edit of the code.
|
|
|
|
///
|
|
|
|
/// In case of short messages and a simple suggestion, rustc displays it as a label:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// try adding parentheses: `(tup.0).1`
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The message
|
|
|
|
///
|
|
|
|
/// * should not end in any punctuation (a `:` is added automatically)
|
|
|
|
/// * should not be a question (avoid language like "did you mean")
|
|
|
|
/// * should not contain any phrases like "the following", "as shown", etc.
|
|
|
|
/// * may look like "to do xyz, use" or "to do xyz, use abc"
|
|
|
|
/// * may contain a name of a function, variable, or type, but not whole expressions
|
|
|
|
///
|
|
|
|
/// See `CodeSuggestion` for more information.
|
2019-10-03 20:22:18 +00:00
|
|
|
pub fn span_suggestion(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2019-10-03 20:22:18 +00:00
|
|
|
applicability: Applicability,
|
2019-10-04 02:32:56 +00:00
|
|
|
) -> &mut Self {
|
|
|
|
self.span_suggestion_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestion,
|
|
|
|
applicability,
|
|
|
|
SuggestionStyle::ShowCode,
|
|
|
|
);
|
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2019-10-04 02:32:56 +00: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 05:44:30 +00:00
|
|
|
/// [`DiagnosticBuilder::span_suggestion()`] but you can set the [`SuggestionStyle`].
|
2019-10-04 02:32:56 +00:00
|
|
|
pub fn span_suggestion_with_style(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2019-10-04 02:32:56 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
style: SuggestionStyle,
|
2019-10-03 20:22:18 +00:00
|
|
|
) -> &mut Self {
|
2022-10-11 16:17:59 +00:00
|
|
|
debug_assert!(
|
|
|
|
!(sp.is_empty() && suggestion.to_string().is_empty()),
|
|
|
|
"Span must not be empty and have no suggestion"
|
|
|
|
);
|
2022-01-24 09:19:33 +00:00
|
|
|
self.push_suggestion(CodeSuggestion {
|
2018-01-18 11:47:46 +00:00
|
|
|
substitutions: vec![Substitution {
|
2022-04-26 05:17:33 +00:00
|
|
|
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
|
2018-01-18 11:47:46 +00:00
|
|
|
}],
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
2019-10-04 02:32:56 +00:00
|
|
|
style,
|
2018-04-25 21:51:06 +00:00
|
|
|
applicability,
|
2018-01-18 11:47:46 +00:00
|
|
|
});
|
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_span_suggestion_verbose,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Always show the suggested change.
|
2019-10-03 20:22:18 +00:00
|
|
|
pub fn span_suggestion_verbose(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2019-10-03 20:22:18 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-10-04 02:32:56 +00:00
|
|
|
self.span_suggestion_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2019-10-03 20:22:18 +00:00
|
|
|
applicability,
|
2019-10-04 02:32:56 +00:00
|
|
|
SuggestionStyle::ShowAlways,
|
|
|
|
);
|
2019-10-03 20:22:18 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2019-10-03 20:22:18 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_span_suggestions,
|
2019-01-25 21:03:27 +00:00
|
|
|
/// Prints out a message with multiple suggested edits of the code.
|
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 05:44:30 +00:00
|
|
|
/// See also [`DiagnosticBuilder::span_suggestion()`].
|
2019-10-03 20:22:18 +00:00
|
|
|
pub fn span_suggestions(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-10-07 02:05:57 +00:00
|
|
|
suggestions: impl IntoIterator<Item = String>,
|
2019-10-03 20:22:18 +00:00
|
|
|
applicability: Applicability,
|
2022-10-17 17:41:49 +00:00
|
|
|
) -> &mut Self {
|
|
|
|
self.span_suggestions_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestions,
|
|
|
|
applicability,
|
|
|
|
SuggestionStyle::ShowCode,
|
|
|
|
)
|
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 05:44:30 +00:00
|
|
|
} }
|
2022-10-17 17:41:49 +00:00
|
|
|
|
|
|
|
pub fn span_suggestions_with_style(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-11-05 18:29:47 +00:00
|
|
|
suggestions: impl IntoIterator<Item = String>,
|
2022-10-17 17:41:49 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
style: SuggestionStyle,
|
2019-10-03 20:22:18 +00:00
|
|
|
) -> &mut Self {
|
2021-10-01 18:09:31 +00:00
|
|
|
let substitutions = suggestions
|
|
|
|
.into_iter()
|
2023-11-08 23:23:26 +00:00
|
|
|
.map(|snippet| {
|
|
|
|
debug_assert!(
|
|
|
|
!(sp.is_empty() && snippet.is_empty()),
|
|
|
|
"Span must not be empty and have no suggestion"
|
|
|
|
);
|
|
|
|
Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
|
|
|
|
})
|
2021-10-01 18:09:31 +00:00
|
|
|
.collect();
|
2022-01-24 09:19:33 +00:00
|
|
|
self.push_suggestion(CodeSuggestion {
|
2021-10-01 18:09:31 +00:00
|
|
|
substitutions,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
2022-10-17 17:41:49 +00:00
|
|
|
style,
|
2018-04-25 21:51:06 +00:00
|
|
|
applicability,
|
2017-03-24 16:31:41 +00:00
|
|
|
});
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
|
|
|
}
|
2018-05-13 03:44:50 +00:00
|
|
|
|
2022-10-17 17:41:49 +00:00
|
|
|
/// Prints out a message with multiple suggested edits of the code, where each edit consists of
|
|
|
|
/// multiple parts.
|
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 05:44:30 +00:00
|
|
|
/// See also [`DiagnosticBuilder::multipart_suggestion()`].
|
2021-06-28 18:22:47 +00:00
|
|
|
pub fn multipart_suggestions(
|
|
|
|
&mut self,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-10-07 02:05:57 +00:00
|
|
|
suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
|
2021-06-28 18:22:47 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2023-01-15 13:29:20 +00:00
|
|
|
let substitutions = suggestions
|
|
|
|
.into_iter()
|
|
|
|
.map(|sugg| {
|
|
|
|
let mut parts = sugg
|
|
|
|
.into_iter()
|
|
|
|
.map(|(span, snippet)| SubstitutionPart { snippet, span })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
parts.sort_unstable_by_key(|part| part.span);
|
|
|
|
|
|
|
|
assert!(!parts.is_empty());
|
|
|
|
debug_assert_eq!(
|
|
|
|
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
|
|
|
|
None,
|
|
|
|
"Span must not be empty and have no suggestion",
|
|
|
|
);
|
|
|
|
debug_assert_eq!(
|
|
|
|
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
|
|
|
|
None,
|
|
|
|
"suggestion must not have overlapping parts",
|
|
|
|
);
|
|
|
|
|
|
|
|
Substitution { parts }
|
|
|
|
})
|
|
|
|
.collect();
|
2022-10-11 16:17:59 +00:00
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
self.push_suggestion(CodeSuggestion {
|
2023-01-15 13:29:20 +00:00
|
|
|
substitutions,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
|
2021-06-28 18:22:47 +00:00
|
|
|
style: SuggestionStyle::ShowCode,
|
|
|
|
applicability,
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
2022-10-17 17:41:49 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_span_suggestion_short,
|
2019-01-25 21:03:27 +00:00
|
|
|
/// Prints out a message with a suggested edit of the code. If the suggestion is presented
|
|
|
|
/// inline, it will only show the message and not the suggestion.
|
|
|
|
///
|
|
|
|
/// See `CodeSuggestion` for more information.
|
|
|
|
pub fn span_suggestion_short(
|
2018-05-13 03:44:50 +00:00
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2018-05-13 03:44:50 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-10-04 02:32:56 +00:00
|
|
|
self.span_suggestion_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2019-02-11 19:16:22 +00:00
|
|
|
applicability,
|
2019-10-04 02:32:56 +00:00
|
|
|
SuggestionStyle::HideCodeInline,
|
|
|
|
);
|
2018-05-13 03:44:50 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2019-02-08 10:50:53 +00:00
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Prints out a message for a suggestion without showing the suggested code.
|
2019-02-08 10:50:53 +00:00
|
|
|
///
|
|
|
|
/// This is intended to be used for suggestions that are obvious in what the changes need to
|
|
|
|
/// be from the message, showing the span label inline would be visually unpleasant
|
|
|
|
/// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
|
|
|
|
/// improve understandability.
|
|
|
|
pub fn span_suggestion_hidden(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2019-02-08 10:50:53 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-10-04 02:32:56 +00:00
|
|
|
self.span_suggestion_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2019-02-11 19:16:22 +00:00
|
|
|
applicability,
|
2019-10-04 02:32:56 +00:00
|
|
|
SuggestionStyle::HideCodeAlways,
|
|
|
|
);
|
2019-02-08 10:50:53 +00:00
|
|
|
self
|
2019-02-09 11:39:08 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_tool_only_span_suggestion,
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Adds a suggestion to the JSON output that will not be shown in the CLI.
|
2019-02-09 11:39:08 +00:00
|
|
|
///
|
|
|
|
/// This is intended to be used for suggestions that are *very* obvious in what the changes
|
|
|
|
/// need to be from the message, but we still want other tools to be able to apply them.
|
2023-04-08 19:37:41 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2019-02-09 11:39:08 +00:00
|
|
|
pub fn tool_only_span_suggestion(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
2022-05-24 14:09:47 +00:00
|
|
|
msg: impl Into<SubdiagnosticMessage>,
|
2022-04-26 05:17:33 +00:00
|
|
|
suggestion: impl ToString,
|
2019-02-09 11:39:08 +00:00
|
|
|
applicability: Applicability,
|
|
|
|
) -> &mut Self {
|
2019-10-04 02:32:56 +00:00
|
|
|
self.span_suggestion_with_style(
|
|
|
|
sp,
|
|
|
|
msg,
|
|
|
|
suggestion,
|
2019-06-25 21:22:45 +00:00
|
|
|
applicability,
|
2019-10-04 02:32:56 +00:00
|
|
|
SuggestionStyle::CompletelyHidden,
|
|
|
|
);
|
2018-05-13 03:44:50 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2022-10-03 13:14:51 +00:00
|
|
|
/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
|
|
|
|
/// [rustc_macros::Subdiagnostic]). Performs eager translation of any translatable messages
|
|
|
|
/// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of
|
|
|
|
/// interpolated variables).
|
2024-02-14 14:17:27 +00:00
|
|
|
pub fn subdiagnostic(
|
2022-10-03 13:14:51 +00:00
|
|
|
&mut self,
|
2023-12-17 23:15:45 +00:00
|
|
|
dcx: &crate::DiagCtxt,
|
2022-10-03 13:14:51 +00:00
|
|
|
subdiagnostic: impl AddToDiagnostic,
|
|
|
|
) -> &mut Self {
|
|
|
|
subdiagnostic.add_to_diagnostic_with(self, |diag, msg| {
|
2024-02-15 19:07:49 +00:00
|
|
|
let args = diag.args.iter();
|
2022-10-03 13:14:51 +00:00
|
|
|
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
|
2023-12-17 23:15:45 +00:00
|
|
|
dcx.eagerly_translate(msg, args)
|
2022-10-03 13:14:51 +00:00
|
|
|
});
|
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_span,
|
|
|
|
/// Add a span.
|
|
|
|
pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
|
2016-10-11 16:26:32 +00:00
|
|
|
self.span = sp.into();
|
2018-11-28 21:05:36 +00:00
|
|
|
if let Some(span) = self.span.primary_span() {
|
|
|
|
self.sort_span = span;
|
|
|
|
}
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2024-01-13 02:11:56 +00:00
|
|
|
pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
|
|
|
|
self.is_lint = Some(IsLint { name, has_future_breakage });
|
2021-09-04 11:26:25 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
with_fn! { with_code,
|
|
|
|
/// Add an error code.
|
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-13 23:57:07 +00:00
|
|
|
pub fn code(&mut self, code: ErrCode) -> &mut Self {
|
|
|
|
self.code = Some(code);
|
2016-10-11 16:26:32 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2016-10-11 16:26:32 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_primary_message,
|
|
|
|
/// Add a primary message.
|
2023-12-23 22:08:41 +00:00
|
|
|
pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
|
2023-12-20 06:12:17 +00:00
|
|
|
self.messages[0] = (msg.into(), Style::NoStyle);
|
2019-10-06 22:14:34 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2017-01-11 21:55:41 +00: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 05:44:30 +00:00
|
|
|
with_fn! { with_arg,
|
|
|
|
/// Add an argument.
|
2023-12-23 22:08:41 +00:00
|
|
|
pub fn arg(
|
2022-03-30 08:45:36 +00:00
|
|
|
&mut self,
|
2024-02-01 23:14:14 +00:00
|
|
|
name: impl Into<DiagnosticArgName>,
|
2022-05-07 06:26:03 +00:00
|
|
|
arg: impl IntoDiagnosticArg,
|
2022-03-30 08:45:36 +00:00
|
|
|
) -> &mut 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 05:44:30 +00:00
|
|
|
self.deref_mut().arg(name, arg);
|
2022-03-30 08:45:36 +00:00
|
|
|
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 05:44:30 +00:00
|
|
|
} }
|
2022-11-03 12:09:25 +00:00
|
|
|
|
2022-05-24 14:09:47 +00:00
|
|
|
/// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
|
|
|
|
/// combining it with the primary message of the diagnostic (if translatable, otherwise it just
|
|
|
|
/// passes the user's string along).
|
2024-02-14 14:17:27 +00:00
|
|
|
pub(crate) fn subdiagnostic_message_to_diagnostic_message(
|
2022-05-24 14:09:47 +00:00
|
|
|
&self,
|
|
|
|
attr: impl Into<SubdiagnosticMessage>,
|
|
|
|
) -> DiagnosticMessage {
|
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 05:44:30 +00:00
|
|
|
self.deref().subdiagnostic_message_to_diagnostic_message(attr)
|
2022-05-24 14:09:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
/// Convenience function for internal use, clients should use one of the
|
|
|
|
/// public methods above.
|
2021-03-15 19:11:40 +00:00
|
|
|
///
|
|
|
|
/// Used by `proc_macro_server` for implementing `server::Diagnostic`.
|
2023-12-20 23:24:44 +00:00
|
|
|
pub fn sub(&mut self, level: Level, message: impl Into<SubdiagnosticMessage>, span: MultiSpan) {
|
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 05:44:30 +00:00
|
|
|
self.deref_mut().sub(level, message, span);
|
2016-10-11 16:26:32 +00:00
|
|
|
}
|
2017-01-11 21:55:41 +00:00
|
|
|
|
|
|
|
/// Convenience function for internal use, clients should use one of the
|
|
|
|
/// public methods above.
|
2024-02-02 22:02:36 +00:00
|
|
|
fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
|
2023-12-20 06:12:17 +00:00
|
|
|
let messages = messages
|
2022-08-29 18:06:36 +00:00
|
|
|
.into_iter()
|
2024-02-02 22:02:36 +00:00
|
|
|
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.content), m.style))
|
2022-05-24 14:09:47 +00:00
|
|
|
.collect();
|
2023-12-20 23:30:17 +00:00
|
|
|
let sub = SubDiagnostic { level, messages, span };
|
2017-01-11 21:55:41 +00:00
|
|
|
self.children.push(sub);
|
|
|
|
}
|
2021-09-04 11:26:25 +00:00
|
|
|
|
2024-02-20 22:37:30 +00: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`.
|
2024-02-22 07:32:06 +00:00
|
|
|
fn take_diag(&mut self) -> DiagInner {
|
2024-02-20 22:37:30 +00:00
|
|
|
Box::into_inner(self.diag.take().unwrap())
|
2021-09-04 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 22:37:30 +00:00
|
|
|
/// Most `emit_producing_guarantee` functions use this as a starting point.
|
2024-02-20 22:57:58 +00:00
|
|
|
fn emit_producing_nothing(mut self) {
|
2024-02-20 22:37:30 +00:00
|
|
|
let diag = self.take_diag();
|
|
|
|
self.dcx.emit_diagnostic(diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `ErrorGuaranteed::emit_producing_guarantee` uses this.
|
2024-02-20 22:57:58 +00:00
|
|
|
fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
|
2024-02-20 22:37:30 +00:00
|
|
|
let diag = self.take_diag();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
2024-02-22 07:32:06 +00:00
|
|
|
// (Also, even though `level` isn't `pub`, the whole `DiagInner` could
|
2024-02-20 22:37:30 +00:00
|
|
|
// be overwritten with a new one thanks to `DerefMut`. So this assert
|
|
|
|
// protects against that, too.)
|
|
|
|
assert!(
|
|
|
|
matches!(diag.level, Level::Error | Level::DelayedBug),
|
|
|
|
"invalid diagnostic level ({:?})",
|
|
|
|
diag.level,
|
|
|
|
);
|
|
|
|
|
|
|
|
let guar = self.dcx.emit_diagnostic(diag);
|
|
|
|
guar.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit and consume the diagnostic.
|
|
|
|
#[track_caller]
|
|
|
|
pub fn emit(self) -> G::EmitResult {
|
|
|
|
G::emit_producing_guarantee(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
#[track_caller]
|
|
|
|
pub fn emit_unless(mut self, delay: bool) -> G::EmitResult {
|
|
|
|
if delay {
|
|
|
|
self.downgrade_to_delayed_bug();
|
|
|
|
}
|
|
|
|
self.emit()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
|
|
|
|
/// cancelled or it will panic when dropped).
|
|
|
|
pub fn cancel(mut self) {
|
|
|
|
self.diag = None;
|
|
|
|
drop(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stashes diagnostic for possible later improvement in a different,
|
|
|
|
/// later stage of the compiler. The diagnostic can be accessed with
|
|
|
|
/// the provided `span` and `key` through [`DiagCtxt::steal_diagnostic()`].
|
|
|
|
pub fn stash(mut self, span: Span, key: StashKey) {
|
|
|
|
self.dcx.stash_diagnostic(span, key, self.take_diag());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
#[track_caller]
|
|
|
|
pub fn delay_as_bug(mut self) -> G::EmitResult {
|
|
|
|
self.downgrade_to_delayed_bug();
|
|
|
|
self.emit()
|
2021-09-04 11:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 22:37:30 +00:00
|
|
|
/// Destructor bomb: every `DiagnosticBuilder` must be consumed (emitted,
|
|
|
|
/// cancelled, etc.) or we emit a bug.
|
|
|
|
impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self.diag.take() {
|
|
|
|
Some(diag) if !panicking() => {
|
2024-02-22 07:32:06 +00:00
|
|
|
self.dcx.emit_diagnostic(DiagInner::new(
|
2024-02-20 22:37:30 +00:00
|
|
|
Level::Bug,
|
|
|
|
DiagnosticMessage::from("the following error was constructed but not emitted"),
|
|
|
|
));
|
|
|
|
self.dcx.emit_diagnostic(*diag);
|
|
|
|
panic!("error was constructed but not emitted");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-09-04 11:26:25 +00:00
|
|
|
}
|
2017-01-11 21:55:41 +00:00
|
|
|
}
|
2024-02-20 22:37:30 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! struct_span_code_err {
|
|
|
|
($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
|
|
|
|
$dcx.struct_span_err($span, format!($($message)*)).with_code($code)
|
|
|
|
})
|
|
|
|
}
|