]> git.lizzy.rs Git - rust.git/blob - tests/ui-fulldeps/internal-lints/diagnostics.rs
Auto merge of #107000 - GuillaumeGomez:fix-items-in-doc-hidden-block, r=notriddle...
[rust.git] / tests / ui-fulldeps / internal-lints / diagnostics.rs
1 // compile-flags: -Z unstable-options
2
3 #![crate_type = "lib"]
4 #![feature(rustc_attrs)]
5 #![feature(rustc_private)]
6 #![deny(rustc::untranslatable_diagnostic)]
7 #![deny(rustc::diagnostic_outside_of_impl)]
8
9 extern crate rustc_errors;
10 extern crate rustc_macros;
11 extern crate rustc_session;
12 extern crate rustc_span;
13
14 use rustc_errors::{
15     AddToDiagnostic, IntoDiagnostic, Diagnostic, DiagnosticBuilder,
16     ErrorGuaranteed, Handler, fluent, SubdiagnosticMessage,
17 };
18 use rustc_macros::{Diagnostic, Subdiagnostic};
19 use rustc_span::Span;
20
21 #[derive(Diagnostic)]
22 #[diag(compiletest_example)]
23 struct DeriveDiagnostic {
24     #[primary_span]
25     span: Span,
26 }
27
28 #[derive(Subdiagnostic)]
29 #[note(compiletest_example)]
30 struct Note {
31     #[primary_span]
32     span: Span,
33 }
34
35 pub struct UntranslatableInIntoDiagnostic;
36
37 impl<'a> IntoDiagnostic<'a, ErrorGuaranteed> for UntranslatableInIntoDiagnostic {
38     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
39         handler.struct_err("untranslatable diagnostic")
40         //~^ ERROR diagnostics should be created using translatable messages
41     }
42 }
43
44 pub struct TranslatableInIntoDiagnostic;
45
46 impl<'a> IntoDiagnostic<'a, ErrorGuaranteed> for TranslatableInIntoDiagnostic {
47     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
48         handler.struct_err(fluent::compiletest_example)
49     }
50 }
51
52 pub struct UntranslatableInAddToDiagnostic;
53
54 impl AddToDiagnostic for UntranslatableInAddToDiagnostic {
55     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
56     where
57         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
58     {
59         diag.note("untranslatable diagnostic");
60         //~^ ERROR diagnostics should be created using translatable messages
61     }
62 }
63
64 pub struct TranslatableInAddToDiagnostic;
65
66 impl AddToDiagnostic for TranslatableInAddToDiagnostic {
67     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
68     where
69         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
70     {
71         diag.note(fluent::note);
72     }
73 }
74
75 pub fn make_diagnostics<'a>(handler: &'a Handler) {
76     let _diag = handler.struct_err(fluent::compiletest_example);
77     //~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
78
79     let _diag = handler.struct_err("untranslatable diagnostic");
80     //~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
81     //~^^ ERROR diagnostics should be created using translatable messages
82 }
83
84 // Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted.
85
86 #[rustc_lint_diagnostics]
87 pub fn skipped_because_of_annotation<'a>(handler: &'a Handler) {
88     let _diag = handler.struct_err("untranslatable diagnostic"); // okay!
89 }