rust/compiler/rustc_lint/src/errors.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.1 KiB
Rust
Raw Normal View History

use rustc_errors::codes::*;
use rustc_errors::{Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::lint::Level;
2022-08-20 00:04:21 +00:00
use rustc_span::{Span, Symbol};
use crate::fluent_generated as fluent;
#[derive(Diagnostic)]
#[diag(lint_overruled_attribute, code = E0453)]
2022-10-07 00:28:51 +00:00
pub struct OverruledAttribute<'a> {
2022-08-20 00:47:05 +00:00
#[primary_span]
pub span: Span,
#[label]
pub overruled: Span,
2022-10-07 00:28:51 +00:00
pub lint_level: &'a str,
2022-08-20 00:47:05 +00:00
pub lint_source: Symbol,
#[subdiagnostic]
pub sub: OverruledAttributeSub,
}
2022-08-20 00:47:05 +00:00
pub enum OverruledAttributeSub {
DefaultSource { id: String },
NodeSource { span: Span, reason: Option<Symbol> },
CommandLineSource,
}
impl Subdiagnostic for OverruledAttributeSub {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
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,
diag: &mut Diag<'_, G>,
_f: &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
) {
2022-08-20 00:47:05 +00:00
match self {
OverruledAttributeSub::DefaultSource { id } => {
2022-10-22 09:07:54 +00:00
diag.note(fluent::lint_default_source);
diag.arg("id", id);
2022-08-20 00:47:05 +00:00
}
OverruledAttributeSub::NodeSource { span, reason } => {
2022-10-22 09:07:54 +00:00
diag.span_label(span, fluent::lint_node_source);
2022-08-20 00:47:05 +00:00
if let Some(rationale) = reason {
2022-09-22 00:42:52 +00:00
#[allow(rustc::untranslatable_diagnostic)]
diag.note(rationale.to_string());
2022-08-20 00:47:05 +00:00
}
}
OverruledAttributeSub::CommandLineSource => {
2022-10-22 09:07:54 +00:00
diag.note(fluent::lint_command_line_source);
2022-08-20 00:47:05 +00:00
}
}
}
}
#[derive(Diagnostic)]
#[diag(lint_malformed_attribute, code = E0452)]
pub struct MalformedAttribute {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sub: MalformedAttributeSub,
}
#[derive(Subdiagnostic)]
pub enum MalformedAttributeSub {
2022-10-22 09:07:54 +00:00
#[label(lint_bad_attribute_argument)]
BadAttributeArgument(#[primary_span] Span),
2022-10-22 09:07:54 +00:00
#[label(lint_reason_must_be_string_literal)]
ReasonMustBeStringLiteral(#[primary_span] Span),
2022-10-22 09:07:54 +00:00
#[label(lint_reason_must_come_last)]
ReasonMustComeLast(#[primary_span] Span),
}
#[derive(Diagnostic)]
#[diag(lint_unknown_tool_in_scoped_lint, code = E0710)]
pub struct UnknownToolInScopedLint {
#[primary_span]
pub span: Option<Span>,
pub tool_name: Symbol,
pub lint_name: String,
#[help]
pub is_nightly_build: bool,
}
#[derive(Diagnostic)]
#[diag(lint_builtin_ellipsis_inclusive_range_patterns, code = E0783)]
pub struct BuiltinEllipsisInclusiveRangePatterns {
#[primary_span]
pub span: Span,
#[suggestion(style = "short", code = "{replace}", applicability = "machine-applicable")]
pub suggestion: Span,
pub replace: String,
}
2022-08-20 19:48:03 +00:00
#[derive(Subdiagnostic)]
2022-10-22 09:07:54 +00:00
#[note(lint_requested_level)]
pub struct RequestedLevel<'a> {
2022-08-20 19:48:03 +00:00
pub level: Level,
pub lint_name: &'a str,
2022-08-20 19:48:03 +00:00
}
#[derive(Diagnostic)]
#[diag(lint_unsupported_group, code = E0602)]
2022-08-20 19:48:03 +00:00
pub struct UnsupportedGroup {
pub lint_group: String,
}
#[derive(Diagnostic)]
#[diag(lint_check_name_unknown_tool, code = E0602)]
pub struct CheckNameUnknownTool<'a> {
2022-08-20 19:48:03 +00:00
pub tool_name: Symbol,
#[subdiagnostic]
pub sub: RequestedLevel<'a>,
2022-08-20 19:48:03 +00:00
}