From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- tests/ui-fulldeps/internal-lints/diagnostics.rs | 89 +++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/ui-fulldeps/internal-lints/diagnostics.rs (limited to 'tests/ui-fulldeps/internal-lints/diagnostics.rs') diff --git a/tests/ui-fulldeps/internal-lints/diagnostics.rs b/tests/ui-fulldeps/internal-lints/diagnostics.rs new file mode 100644 index 000000000..643e81d99 --- /dev/null +++ b/tests/ui-fulldeps/internal-lints/diagnostics.rs @@ -0,0 +1,89 @@ +// compile-flags: -Z unstable-options + +#![crate_type = "lib"] +#![feature(rustc_attrs)] +#![feature(rustc_private)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] + +extern crate rustc_errors; +extern crate rustc_macros; +extern crate rustc_session; +extern crate rustc_span; + +use rustc_errors::{ + AddToDiagnostic, IntoDiagnostic, Diagnostic, DiagnosticBuilder, + ErrorGuaranteed, Handler, fluent, SubdiagnosticMessage, +}; +use rustc_macros::{Diagnostic, Subdiagnostic}; +use rustc_span::Span; + +#[derive(Diagnostic)] +#[diag(compiletest_example)] +struct DeriveDiagnostic { + #[primary_span] + span: Span, +} + +#[derive(Subdiagnostic)] +#[note(compiletest_example)] +struct Note { + #[primary_span] + span: Span, +} + +pub struct UntranslatableInIntoDiagnostic; + +impl<'a> IntoDiagnostic<'a, ErrorGuaranteed> for UntranslatableInIntoDiagnostic { + fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> { + handler.struct_err("untranslatable diagnostic") + //~^ ERROR diagnostics should be created using translatable messages + } +} + +pub struct TranslatableInIntoDiagnostic; + +impl<'a> IntoDiagnostic<'a, ErrorGuaranteed> for TranslatableInIntoDiagnostic { + fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> { + handler.struct_err(fluent::compiletest_example) + } +} + +pub struct UntranslatableInAddToDiagnostic; + +impl AddToDiagnostic for UntranslatableInAddToDiagnostic { + fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) + where + F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, + { + diag.note("untranslatable diagnostic"); + //~^ ERROR diagnostics should be created using translatable messages + } +} + +pub struct TranslatableInAddToDiagnostic; + +impl AddToDiagnostic for TranslatableInAddToDiagnostic { + fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) + where + F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, + { + diag.note(fluent::note); + } +} + +pub fn make_diagnostics<'a>(handler: &'a Handler) { + let _diag = handler.struct_err(fluent::compiletest_example); + //~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls + + let _diag = handler.struct_err("untranslatable diagnostic"); + //~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls + //~^^ ERROR diagnostics should be created using translatable messages +} + +// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted. + +#[rustc_lint_diagnostics] +pub fn skipped_because_of_annotation<'a>(handler: &'a Handler) { + let _diag = handler.struct_err("untranslatable diagnostic"); // okay! +} -- cgit v1.2.3