2019-12-22 22:42:04 +00:00
|
|
|
use crate::snippet::Style;
|
2022-03-24 02:03:04 +00:00
|
|
|
use crate::{
|
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
|
|
|
CodeSuggestion, DelayedBugKind, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee,
|
|
|
|
ErrCode, Level, MultiSpan, SubdiagnosticMessage, Substitution, SubstitutionPart,
|
|
|
|
SuggestionStyle,
|
2022-03-24 02:03:04 +00:00
|
|
|
};
|
2023-12-31 12:04:43 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, 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};
|
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};
|
2022-10-18 22:08:20 +00:00
|
|
|
use std::panic::Location;
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
/// Error type for `Diagnostic`'s `suggestions` field, indicating that
|
|
|
|
/// `.disable_suggestions()` was called on the `Diagnostic`.
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
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.
|
2022-10-03 13:09:05 +00:00
|
|
|
fn add_to_diagnostic(self, diag: &mut Diagnostic) {
|
|
|
|
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).
|
|
|
|
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
|
|
|
|
where
|
|
|
|
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage;
|
2022-04-26 10:59:45 +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
|
|
|
}
|
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
#[must_use]
|
2021-09-04 11:26:25 +00:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable)]
|
2016-10-11 16:26:32 +00:00
|
|
|
pub struct Diagnostic {
|
2022-01-23 23:11:37 +00:00
|
|
|
// NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
|
|
|
|
// outside of what methods in this crate themselves allow.
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) level: Level,
|
2022-01-23 23:11:37 +00:00
|
|
|
|
2023-12-20 06:12:17 +00:00
|
|
|
pub messages: Vec<(DiagnosticMessage, Style)>,
|
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 code: Option<ErrCode>,
|
2016-10-11 16:26:32 +00:00
|
|
|
pub span: MultiSpan,
|
|
|
|
pub children: Vec<SubDiagnostic>,
|
2022-01-24 09:19:33 +00:00
|
|
|
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
|
2024-01-30 05:04:03 +00:00
|
|
|
args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>,
|
2018-11-28 21:05:36 +00:00
|
|
|
|
2022-11-16 20:34:16 +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`.
|
2018-11-28 21:05:36 +00:00
|
|
|
pub sort_span: Span,
|
2021-09-04 11:26:25 +00:00
|
|
|
|
2024-01-13 02:11:56 +00:00
|
|
|
pub is_lint: Option<IsLint>,
|
2022-10-18 22:08:20 +00:00
|
|
|
|
|
|
|
/// With `-Ztrack_diagnostics` enabled,
|
|
|
|
/// we print where in rustc this error was emitted.
|
2024-02-01 08:17:39 +00:00
|
|
|
pub(crate) emitted_at: DiagnosticLocation,
|
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 {
|
|
|
|
let loc = Location::caller();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// A "sub"-diagnostic attached to a parent diagnostic.
|
|
|
|
/// For example, a note attached to an error.
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
|
2016-10-11 16:26:32 +00:00
|
|
|
pub struct SubDiagnostic {
|
|
|
|
pub level: Level,
|
2023-12-20 06:12:17 +00:00
|
|
|
pub messages: Vec<(DiagnosticMessage, Style)>,
|
2016-10-11 16:26:32 +00:00
|
|
|
pub span: MultiSpan,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 16:26:32 +00:00
|
|
|
impl Diagnostic {
|
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 {
|
2023-12-20 05:53:42 +00:00
|
|
|
Diagnostic::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 {
|
|
|
|
Diagnostic {
|
|
|
|
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-12 03:15:14 +00:00
|
|
|
Level::Bug
|
|
|
|
| Level::DelayedBug(DelayedBugKind::Normal)
|
|
|
|
| Level::Fatal
|
|
|
|
| Level::Error
|
|
|
|
| Level::FailureNote => true,
|
2018-07-20 15:29:29 +00:00
|
|
|
|
2024-01-09 01:28:45 +00:00
|
|
|
Level::ForceWarning(_)
|
|
|
|
| Level::Warning
|
2024-01-12 03:15:14 +00:00
|
|
|
| Level::DelayedBug(DelayedBugKind::GoodPath)
|
2022-03-20 19:02:18 +00:00
|
|
|
| Level::Note
|
|
|
|
| Level::OnceNote
|
|
|
|
| Level::Help
|
2023-07-25 17:37:45 +00:00
|
|
|
| Level::OnceHelp
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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!(
|
|
|
|
self.is_error(),
|
|
|
|
"downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
|
|
|
|
self.level
|
|
|
|
);
|
2024-01-12 03:15:14 +00:00
|
|
|
self.level = Level::DelayedBug(DelayedBugKind::Normal);
|
2022-01-23 23:11:37 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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("`"),
|
2019-12-22 22:42:04 +00:00
|
|
|
]);
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:02:18 +00:00
|
|
|
/// Prints the span with a note above it.
|
|
|
|
/// This is like [`Diagnostic::note()`], but it gets its own span.
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-12 08:53:09 +00:00
|
|
|
/// Prints the span with a note above it.
|
2020-12-15 04:03:19 +00:00
|
|
|
/// This is like [`Diagnostic::note()`], 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_note<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::Note, msg, sp.into());
|
2016-10-11 16:26:32 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:02:18 +00:00
|
|
|
/// Prints the span with a note above it.
|
|
|
|
/// This is like [`Diagnostic::note()`], 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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// Prints the span with a warning above it.
|
|
|
|
/// This is like [`Diagnostic::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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:37:45 +00:00
|
|
|
/// Prints the span with a help above it.
|
|
|
|
/// This is like [`Diagnostic::help()`], but it gets its own span.
|
|
|
|
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.
|
2020-12-15 04:03:19 +00:00
|
|
|
/// This is like [`Diagnostic::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-08 16:03:38 +00:00
|
|
|
let in_derive = suggestion.substitutions.iter().any(|subst| {
|
|
|
|
subst.parts.iter().any(|part| {
|
|
|
|
let span = part.span;
|
|
|
|
let call_site = span.ctxt().outer_expn_data().call_site;
|
|
|
|
span.in_derive_expansion() && span.overlaps_or_adjacent(call_site)
|
|
|
|
})
|
|
|
|
});
|
2024-01-24 15:12:50 +00:00
|
|
|
if in_derive {
|
|
|
|
// Ignore if spans is from derive macro.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:19:33 +00:00
|
|
|
if let Ok(suggestions) = &mut self.suggestions {
|
|
|
|
suggestions.push(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,
|
|
|
|
)
|
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,
|
|
|
|
)
|
|
|
|
}
|
2021-05-07 17:44:32 +00:00
|
|
|
/// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:03:19 +00:00
|
|
|
/// [`Diagnostic::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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:03:27 +00:00
|
|
|
/// Prints out a message with multiple suggested edits of the code.
|
2020-12-15 04:03:19 +00:00
|
|
|
/// See also [`Diagnostic::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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:24:49 +00:00
|
|
|
/// [`Diagnostic::span_suggestions()`] but you can set the [`SuggestionStyle`].
|
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.
|
|
|
|
/// See also [`Diagnostic::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
|
|
|
|
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(
|
2019-12-22 22:42:04 +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,
|
2019-12-22 22:42:04 +00:00
|
|
|
applicability: Applicability,
|
2018-05-13 03:44:50 +00:00
|
|
|
) -> &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
|
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(
|
2019-12-22 22:42:04 +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,
|
2019-12-22 22:42:04 +00:00
|
|
|
applicability: Applicability,
|
2019-02-08 10:50:53 +00:00
|
|
|
) -> &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
|
|
|
}
|
|
|
|
|
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(
|
2019-12-22 22:42:04 +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,
|
2019-12-22 22:42:04 +00:00
|
|
|
applicability: Applicability,
|
2019-02-09 11:39:08 +00:00
|
|
|
) -> &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
|
|
|
|
}
|
2016-10-11 16:26:32 +00:00
|
|
|
|
2022-10-03 13:09:05 +00:00
|
|
|
/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
|
|
|
|
/// [rustc_macros::Subdiagnostic]).
|
2022-09-18 15:46:16 +00:00
|
|
|
pub fn subdiagnostic(&mut self, subdiagnostic: impl AddToDiagnostic) -> &mut Self {
|
2022-04-26 10:59:45 +00:00
|
|
|
subdiagnostic.add_to_diagnostic(self);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
pub fn eager_subdiagnostic(
|
|
|
|
&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| {
|
|
|
|
let args = diag.args();
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-23 22:08:41 +00:00
|
|
|
pub fn span<S: Into<MultiSpan>>(&mut self, sp: S) -> &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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:02:49 +00:00
|
|
|
// Exact iteration order of diagnostic arguments shouldn't make a difference to output because
|
|
|
|
// they're only used in interpolation.
|
|
|
|
#[allow(rustc::potential_query_instability)]
|
2024-01-30 05:06:18 +00:00
|
|
|
pub fn args(&self) -> impl Iterator<Item = DiagnosticArg<'_>> {
|
2022-10-03 13:02:49 +00:00
|
|
|
self.args.iter()
|
2017-01-11 21:55:41 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-10-03 13:02:49 +00:00
|
|
|
self.args.insert(name.into(), arg.into_diagnostic_arg());
|
2022-03-30 08:45:36 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-30 05:04:03 +00:00
|
|
|
pub fn replace_args(&mut self, args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>) {
|
2022-11-03 12:09:25 +00:00
|
|
|
self.args = args;
|
|
|
|
}
|
|
|
|
|
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).
|
2023-12-03 22:35:50 +00:00
|
|
|
fn subdiagnostic_message_to_diagnostic_message(
|
2022-05-24 14:09:47 +00:00
|
|
|
&self,
|
|
|
|
attr: impl Into<SubdiagnosticMessage>,
|
|
|
|
) -> DiagnosticMessage {
|
|
|
|
let msg =
|
2023-12-20 06:12:17 +00:00
|
|
|
self.messages.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages");
|
2022-05-24 14:09:47 +00:00
|
|
|
msg.with_subdiagnostic_message(attr.into())
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-10-11 16:26:32 +00:00
|
|
|
let sub = SubDiagnostic {
|
2017-08-07 05:54:09 +00:00
|
|
|
level,
|
2023-12-20 06:12:17 +00:00
|
|
|
messages: vec![(
|
2022-05-24 14:09:47 +00:00
|
|
|
self.subdiagnostic_message_to_diagnostic_message(message),
|
|
|
|
Style::NoStyle,
|
|
|
|
)],
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2016-10-11 16:26:32 +00:00
|
|
|
};
|
|
|
|
self.children.push(sub);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/// Fields used for Hash, and PartialEq trait
|
|
|
|
fn keys(
|
|
|
|
&self,
|
|
|
|
) -> (
|
|
|
|
&Level,
|
2022-06-03 16:42:42 +00:00
|
|
|
&[(DiagnosticMessage, Style)],
|
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
|
|
|
&Option<ErrCode>,
|
2021-09-04 11:26:25 +00:00
|
|
|
&MultiSpan,
|
2024-01-30 05:57:29 +00:00
|
|
|
&[SubDiagnostic],
|
2022-01-24 09:19:33 +00:00
|
|
|
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
|
2024-01-30 05:57:29 +00:00
|
|
|
Vec<(&DiagnosticArgName, &DiagnosticArgValue)>,
|
|
|
|
&Option<IsLint>,
|
2021-09-04 11:26:25 +00:00
|
|
|
) {
|
|
|
|
(
|
|
|
|
&self.level,
|
2023-12-20 06:12:17 +00:00
|
|
|
&self.messages,
|
2021-09-04 11:26:25 +00:00
|
|
|
&self.code,
|
|
|
|
&self.span,
|
2024-01-30 05:57:29 +00:00
|
|
|
&self.children,
|
2021-09-04 11:26:25 +00:00
|
|
|
&self.suggestions,
|
2024-01-30 05:57:29 +00:00
|
|
|
self.args().collect(),
|
|
|
|
// omit self.sort_span
|
|
|
|
&self.is_lint,
|
|
|
|
// omit self.emitted_at
|
2021-09-04 11:26:25 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for Diagnostic {
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: Hasher,
|
|
|
|
{
|
|
|
|
self.keys().hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Diagnostic {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.keys() == other.keys()
|
|
|
|
}
|
2017-01-11 21:55:41 +00:00
|
|
|
}
|