2022-06-10 15:50:06 +01:00
|
|
|
//@ compile-flags: -Z unstable-options
|
|
|
|
|
|
|
|
#![crate_type = "lib"]
|
2022-08-31 12:06:22 +01:00
|
|
|
#![feature(rustc_attrs)]
|
2022-06-10 15:50:06 +01:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
|
|
|
|
|
|
|
extern crate rustc_errors;
|
2023-04-16 14:33:00 +02:00
|
|
|
extern crate rustc_fluent_macro;
|
2022-06-10 15:50:06 +01:00
|
|
|
extern crate rustc_macros;
|
|
|
|
extern crate rustc_session;
|
|
|
|
extern crate rustc_span;
|
|
|
|
|
2022-09-05 12:05:14 -04:00
|
|
|
use rustc_errors::{
|
2024-02-23 10:20:45 +11:00
|
|
|
AddToDiagnostic, Diag, EmissionGuarantee, DiagCtxt, IntoDiagnostic, Level,
|
2024-02-22 18:32:06 +11:00
|
|
|
SubdiagnosticMessageOp,
|
2022-09-05 12:05:14 -04:00
|
|
|
};
|
2023-04-16 14:33:00 +02:00
|
|
|
use rustc_macros::{Diagnostic, Subdiagnostic};
|
2022-06-10 15:50:06 +01:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
2023-11-22 09:53:07 +11:00
|
|
|
rustc_fluent_macro::fluent_messages! { "./diagnostics.ftl" }
|
2022-10-13 10:13:02 +01:00
|
|
|
|
2022-09-18 11:46:56 -04:00
|
|
|
#[derive(Diagnostic)]
|
2022-10-13 10:13:02 +01:00
|
|
|
#[diag(no_crate_example)]
|
2022-09-15 00:01:44 -04:00
|
|
|
struct DeriveDiagnostic {
|
2022-06-10 15:50:06 +01:00
|
|
|
#[primary_span]
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:47:31 -04:00
|
|
|
#[derive(Subdiagnostic)]
|
2022-10-13 10:13:02 +01:00
|
|
|
#[note(no_crate_example)]
|
2022-06-10 15:50:06 +01:00
|
|
|
struct Note {
|
|
|
|
#[primary_span]
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:01:44 -04:00
|
|
|
pub struct UntranslatableInIntoDiagnostic;
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2023-12-18 14:12:39 +11:00
|
|
|
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UntranslatableInIntoDiagnostic {
|
2024-02-23 10:20:45 +11:00
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
|
|
|
|
Diag::new(dcx, level, "untranslatable diagnostic")
|
2022-06-10 15:50:06 +01:00
|
|
|
//~^ ERROR diagnostics should be created using translatable messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:01:44 -04:00
|
|
|
pub struct TranslatableInIntoDiagnostic;
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2023-12-18 14:12:39 +11:00
|
|
|
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for TranslatableInIntoDiagnostic {
|
2024-02-23 10:20:45 +11:00
|
|
|
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
|
|
|
|
Diag::new(dcx, level, crate::fluent_generated::no_crate_example)
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:46:16 -04:00
|
|
|
pub struct UntranslatableInAddToDiagnostic;
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2022-09-18 11:46:16 -04:00
|
|
|
impl AddToDiagnostic for UntranslatableInAddToDiagnostic {
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
fn add_to_diagnostic_with<G: EmissionGuarantee, F: SubdiagnosticMessageOp<G>>(
|
|
|
|
self,
|
2024-02-23 10:20:45 +11:00
|
|
|
diag: &mut Diag<'_, 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 16:44:30 +11:00
|
|
|
f: F,
|
|
|
|
) {
|
2022-06-10 15:50:06 +01:00
|
|
|
diag.note("untranslatable diagnostic");
|
|
|
|
//~^ ERROR diagnostics should be created using translatable messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:46:16 -04:00
|
|
|
pub struct TranslatableInAddToDiagnostic;
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2022-09-18 11:46:16 -04:00
|
|
|
impl AddToDiagnostic for TranslatableInAddToDiagnostic {
|
Reduce capabilities of `Diagnostic`.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)
`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.
The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)
All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.
There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.
There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-06 16:44:30 +11:00
|
|
|
fn add_to_diagnostic_with<G: EmissionGuarantee, F: SubdiagnosticMessageOp<G>>(
|
|
|
|
self,
|
2024-02-23 10:20:45 +11:00
|
|
|
diag: &mut Diag<'_, 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 16:44:30 +11:00
|
|
|
f: F,
|
|
|
|
) {
|
2022-10-13 10:13:02 +01:00
|
|
|
diag.note(crate::fluent_generated::no_crate_note);
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 14:13:53 +11:00
|
|
|
pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
|
|
|
|
let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example);
|
2022-09-18 11:46:16 -04:00
|
|
|
//~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2023-12-18 14:13:53 +11:00
|
|
|
let _diag = dcx.struct_err("untranslatable diagnostic");
|
2022-09-18 11:46:16 -04:00
|
|
|
//~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
|
2022-06-10 15:50:06 +01:00
|
|
|
//~^^ ERROR diagnostics should be created using translatable messages
|
|
|
|
}
|
2022-08-31 12:06:22 +01:00
|
|
|
|
|
|
|
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted.
|
|
|
|
|
|
|
|
#[rustc_lint_diagnostics]
|
2023-12-18 14:13:53 +11:00
|
|
|
pub fn skipped_because_of_annotation<'a>(dcx: &'a DiagCtxt) {
|
|
|
|
let _diag = dcx.struct_err("untranslatable diagnostic"); // okay!
|
2022-08-31 12:06:22 +01:00
|
|
|
}
|