Skip to content

Commit 5ba81fa

Browse files
committed
lint: add diagnostic translation migration lints
Introduce allow-by-default lints for checking whether diagnostics are written in `SessionDiagnostic`/`AddSubdiagnostic` impls and whether diagnostics are translatable. These lints can be denied for modules once they are fully migrated to impls and translation. Signed-off-by: David Wood <david.wood@huawei.com>
1 parent 52ee2a2 commit 5ba81fa

File tree

15 files changed

+327
-37
lines changed

15 files changed

+327
-37
lines changed

compiler/rustc_error_messages/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(let_chains)]
22
#![feature(once_cell)]
33
#![feature(path_try_exists)]
4+
#![feature(rustc_attrs)]
45
#![feature(type_alias_impl_trait)]
56

67
use fluent_bundle::FluentResource;
@@ -241,6 +242,7 @@ type FluentId = Cow<'static, str>;
241242
/// message so messages of this type must be combined with a `DiagnosticMessage` (using
242243
/// `DiagnosticMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
243244
/// the `SessionSubdiagnostic` derive refer to Fluent identifiers directly.
245+
#[rustc_diagnostic_item = "SubdiagnosticMessage"]
244246
pub enum SubdiagnosticMessage {
245247
/// Non-translatable diagnostic message.
246248
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
@@ -281,6 +283,7 @@ impl<S: Into<String>> From<S> for SubdiagnosticMessage {
281283
///
282284
/// Intended to be removed once diagnostics are entirely translatable.
283285
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
286+
#[rustc_diagnostic_item = "DiagnosticMessage"]
284287
pub enum DiagnosticMessage {
285288
/// Non-translatable diagnostic message.
286289
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?

compiler/rustc_errors/src/diagnostic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
8080

8181
/// Trait implemented by error types. This should not be implemented manually. Instead, use
8282
/// `#[derive(SessionSubdiagnostic)]` -- see [rustc_macros::SessionSubdiagnostic].
83+
#[rustc_diagnostic_item = "AddSubdiagnostic"]
8384
pub trait AddSubdiagnostic {
8485
/// Add a subdiagnostic to an existing diagnostic.
8586
fn add_to_diagnostic(self, diag: &mut Diagnostic);
@@ -283,6 +284,7 @@ impl Diagnostic {
283284
///
284285
/// This span is *not* considered a ["primary span"][`MultiSpan`]; only
285286
/// the `Span` supplied when creating the diagnostic is primary.
287+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
286288
pub fn span_label(&mut self, span: Span, label: impl Into<SubdiagnosticMessage>) -> &mut Self {
287289
self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label));
288290
self
@@ -401,6 +403,7 @@ impl Diagnostic {
401403
}
402404

403405
/// Add a note attached to this diagnostic.
406+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
404407
pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
405408
self.sub(Level::Note, msg, MultiSpan::new(), None);
406409
self
@@ -423,6 +426,7 @@ impl Diagnostic {
423426

424427
/// Prints the span with a note above it.
425428
/// This is like [`Diagnostic::note()`], but it gets its own span.
429+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
426430
pub fn span_note<S: Into<MultiSpan>>(
427431
&mut self,
428432
sp: S,
@@ -444,13 +448,15 @@ impl Diagnostic {
444448
}
445449

446450
/// Add a warning attached to this diagnostic.
451+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
447452
pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
448453
self.sub(Level::Warning, msg, MultiSpan::new(), None);
449454
self
450455
}
451456

452457
/// Prints the span with a warning above it.
453458
/// This is like [`Diagnostic::warn()`], but it gets its own span.
459+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
454460
pub fn span_warn<S: Into<MultiSpan>>(
455461
&mut self,
456462
sp: S,
@@ -461,6 +467,7 @@ impl Diagnostic {
461467
}
462468

463469
/// Add a help message attached to this diagnostic.
470+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
464471
pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
465472
self.sub(Level::Help, msg, MultiSpan::new(), None);
466473
self
@@ -474,6 +481,7 @@ impl Diagnostic {
474481

475482
/// Prints the span with some help above it.
476483
/// This is like [`Diagnostic::help()`], but it gets its own span.
484+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
477485
pub fn span_help<S: Into<MultiSpan>>(
478486
&mut self,
479487
sp: S,

compiler/rustc_errors/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(let_else)]
1010
#![feature(never_type)]
1111
#![feature(adt_const_params)]
12+
#![feature(rustc_attrs)]
1213
#![allow(incomplete_features)]
1314
#![allow(rustc::potential_query_instability)]
1415

@@ -644,6 +645,7 @@ impl Handler {
644645
/// Attempting to `.emit()` the builder will only emit if either:
645646
/// * `can_emit_warnings` is `true`
646647
/// * `is_force_warn` was set in `DiagnosticId::Lint`
648+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
647649
pub fn struct_span_warn(
648650
&self,
649651
span: impl Into<MultiSpan>,
@@ -655,6 +657,7 @@ impl Handler {
655657
}
656658

657659
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
660+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
658661
pub fn struct_span_allow(
659662
&self,
660663
span: impl Into<MultiSpan>,
@@ -667,6 +670,7 @@ impl Handler {
667670

668671
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
669672
/// Also include a code.
673+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
670674
pub fn struct_span_warn_with_code(
671675
&self,
672676
span: impl Into<MultiSpan>,
@@ -683,16 +687,19 @@ impl Handler {
683687
/// Attempting to `.emit()` the builder will only emit if either:
684688
/// * `can_emit_warnings` is `true`
685689
/// * `is_force_warn` was set in `DiagnosticId::Lint`
690+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
686691
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
687692
DiagnosticBuilder::new(self, Level::Warning, msg)
688693
}
689694

690695
/// Construct a builder at the `Allow` level with the `msg`.
696+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
691697
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
692698
DiagnosticBuilder::new(self, Level::Allow, msg)
693699
}
694700

695701
/// Construct a builder at the `Expect` level with the `msg`.
702+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
696703
pub fn struct_expect(
697704
&self,
698705
msg: impl Into<DiagnosticMessage>,
@@ -702,6 +709,7 @@ impl Handler {
702709
}
703710

704711
/// Construct a builder at the `Error` level at the given `span` and with the `msg`.
712+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
705713
pub fn struct_span_err(
706714
&self,
707715
span: impl Into<MultiSpan>,
@@ -713,6 +721,7 @@ impl Handler {
713721
}
714722

715723
/// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
724+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
716725
pub fn struct_span_err_with_code(
717726
&self,
718727
span: impl Into<MultiSpan>,
@@ -726,6 +735,7 @@ impl Handler {
726735

727736
/// Construct a builder at the `Error` level with the `msg`.
728737
// FIXME: This method should be removed (every error should have an associated error code).
738+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
729739
pub fn struct_err(
730740
&self,
731741
msg: impl Into<DiagnosticMessage>,
@@ -740,6 +750,7 @@ impl Handler {
740750
}
741751

742752
/// Construct a builder at the `Error` level with the `msg` and the `code`.
753+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
743754
pub fn struct_err_with_code(
744755
&self,
745756
msg: impl Into<DiagnosticMessage>,
@@ -751,6 +762,7 @@ impl Handler {
751762
}
752763

753764
/// Construct a builder at the `Warn` level with the `msg` and the `code`.
765+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
754766
pub fn struct_warn_with_code(
755767
&self,
756768
msg: impl Into<DiagnosticMessage>,
@@ -762,6 +774,7 @@ impl Handler {
762774
}
763775

764776
/// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
777+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
765778
pub fn struct_span_fatal(
766779
&self,
767780
span: impl Into<MultiSpan>,
@@ -773,6 +786,7 @@ impl Handler {
773786
}
774787

775788
/// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
789+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
776790
pub fn struct_span_fatal_with_code(
777791
&self,
778792
span: impl Into<MultiSpan>,
@@ -785,28 +799,33 @@ impl Handler {
785799
}
786800

787801
/// Construct a builder at the `Error` level with the `msg`.
802+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
788803
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
789804
DiagnosticBuilder::new_fatal(self, msg)
790805
}
791806

792807
/// Construct a builder at the `Help` level with the `msg`.
808+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
793809
pub fn struct_help(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
794810
DiagnosticBuilder::new(self, Level::Help, msg)
795811
}
796812

797813
/// Construct a builder at the `Note` level with the `msg`.
814+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
798815
pub fn struct_note_without_error(
799816
&self,
800817
msg: impl Into<DiagnosticMessage>,
801818
) -> DiagnosticBuilder<'_, ()> {
802819
DiagnosticBuilder::new(self, Level::Note, msg)
803820
}
804821

822+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
805823
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
806824
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
807825
FatalError.raise()
808826
}
809827

828+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
810829
pub fn span_fatal_with_code(
811830
&self,
812831
span: impl Into<MultiSpan>,
@@ -817,6 +836,7 @@ impl Handler {
817836
FatalError.raise()
818837
}
819838

839+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
820840
pub fn span_err(
821841
&self,
822842
span: impl Into<MultiSpan>,
@@ -825,6 +845,7 @@ impl Handler {
825845
self.emit_diag_at_span(Diagnostic::new(Error { lint: false }, msg), span).unwrap()
826846
}
827847

848+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
828849
pub fn span_err_with_code(
829850
&self,
830851
span: impl Into<MultiSpan>,
@@ -837,10 +858,12 @@ impl Handler {
837858
);
838859
}
839860

861+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
840862
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
841863
self.emit_diag_at_span(Diagnostic::new(Warning, msg), span);
842864
}
843865

866+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
844867
pub fn span_warn_with_code(
845868
&self,
846869
span: impl Into<MultiSpan>,

compiler/rustc_feature/src/builtin_attrs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,9 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
615615
// Used by the `rustc::potential_query_instability` lint to warn methods which
616616
// might not be stable during incremental compilation.
617617
rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
618+
// Used by the `rustc::untranslatable_diagnostic` and `rustc::diagnostic_outside_of_impl` lints
619+
// to assist in changes to diagnostic APIs.
620+
rustc_attr!(rustc_lint_diagnostics, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
618621

619622
// ==========================================================================
620623
// Internal attributes, Const related:

0 commit comments

Comments
 (0)