Skip to content

Commit d8333a7

Browse files
authored
Rollup merge of rust-lang#97948 - davidtwco:diagnostic-translation-lints, r=oli-obk
lint: add diagnostic translation migration lints Introduce allow-by-default lints for checking whether diagnostics are written in `SessionDiagnostic` or `AddSubdiagnostic` impls and whether diagnostics are translatable. These lints can be denied for modules once they are fully migrated to impls and translation. These lints are intended to be temporary - once all diagnostics have been changed then we can just change the APIs we have and that will enforce these constraints thereafter. r? `````@oli-obk`````
2 parents 9e5c5c5 + 5ba81fa commit d8333a7

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

@@ -648,6 +649,7 @@ impl Handler {
648649
/// Attempting to `.emit()` the builder will only emit if either:
649650
/// * `can_emit_warnings` is `true`
650651
/// * `is_force_warn` was set in `DiagnosticId::Lint`
652+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
651653
pub fn struct_span_warn(
652654
&self,
653655
span: impl Into<MultiSpan>,
@@ -659,6 +661,7 @@ impl Handler {
659661
}
660662

661663
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
664+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
662665
pub fn struct_span_allow(
663666
&self,
664667
span: impl Into<MultiSpan>,
@@ -671,6 +674,7 @@ impl Handler {
671674

672675
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
673676
/// Also include a code.
677+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
674678
pub fn struct_span_warn_with_code(
675679
&self,
676680
span: impl Into<MultiSpan>,
@@ -687,16 +691,19 @@ impl Handler {
687691
/// Attempting to `.emit()` the builder will only emit if either:
688692
/// * `can_emit_warnings` is `true`
689693
/// * `is_force_warn` was set in `DiagnosticId::Lint`
694+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
690695
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
691696
DiagnosticBuilder::new(self, Level::Warning, msg)
692697
}
693698

694699
/// Construct a builder at the `Allow` level with the `msg`.
700+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
695701
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
696702
DiagnosticBuilder::new(self, Level::Allow, msg)
697703
}
698704

699705
/// Construct a builder at the `Expect` level with the `msg`.
706+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
700707
pub fn struct_expect(
701708
&self,
702709
msg: impl Into<DiagnosticMessage>,
@@ -706,6 +713,7 @@ impl Handler {
706713
}
707714

708715
/// Construct a builder at the `Error` level at the given `span` and with the `msg`.
716+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
709717
pub fn struct_span_err(
710718
&self,
711719
span: impl Into<MultiSpan>,
@@ -717,6 +725,7 @@ impl Handler {
717725
}
718726

719727
/// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
728+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
720729
pub fn struct_span_err_with_code(
721730
&self,
722731
span: impl Into<MultiSpan>,
@@ -730,6 +739,7 @@ impl Handler {
730739

731740
/// Construct a builder at the `Error` level with the `msg`.
732741
// FIXME: This method should be removed (every error should have an associated error code).
742+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
733743
pub fn struct_err(
734744
&self,
735745
msg: impl Into<DiagnosticMessage>,
@@ -744,6 +754,7 @@ impl Handler {
744754
}
745755

746756
/// Construct a builder at the `Error` level with the `msg` and the `code`.
757+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
747758
pub fn struct_err_with_code(
748759
&self,
749760
msg: impl Into<DiagnosticMessage>,
@@ -755,6 +766,7 @@ impl Handler {
755766
}
756767

757768
/// Construct a builder at the `Warn` level with the `msg` and the `code`.
769+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
758770
pub fn struct_warn_with_code(
759771
&self,
760772
msg: impl Into<DiagnosticMessage>,
@@ -766,6 +778,7 @@ impl Handler {
766778
}
767779

768780
/// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
781+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
769782
pub fn struct_span_fatal(
770783
&self,
771784
span: impl Into<MultiSpan>,
@@ -777,6 +790,7 @@ impl Handler {
777790
}
778791

779792
/// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
793+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
780794
pub fn struct_span_fatal_with_code(
781795
&self,
782796
span: impl Into<MultiSpan>,
@@ -789,28 +803,33 @@ impl Handler {
789803
}
790804

791805
/// Construct a builder at the `Error` level with the `msg`.
806+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
792807
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
793808
DiagnosticBuilder::new_fatal(self, msg)
794809
}
795810

796811
/// Construct a builder at the `Help` level with the `msg`.
812+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
797813
pub fn struct_help(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
798814
DiagnosticBuilder::new(self, Level::Help, msg)
799815
}
800816

801817
/// Construct a builder at the `Note` level with the `msg`.
818+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
802819
pub fn struct_note_without_error(
803820
&self,
804821
msg: impl Into<DiagnosticMessage>,
805822
) -> DiagnosticBuilder<'_, ()> {
806823
DiagnosticBuilder::new(self, Level::Note, msg)
807824
}
808825

826+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
809827
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
810828
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
811829
FatalError.raise()
812830
}
813831

832+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
814833
pub fn span_fatal_with_code(
815834
&self,
816835
span: impl Into<MultiSpan>,
@@ -821,6 +840,7 @@ impl Handler {
821840
FatalError.raise()
822841
}
823842

843+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
824844
pub fn span_err(
825845
&self,
826846
span: impl Into<MultiSpan>,
@@ -829,6 +849,7 @@ impl Handler {
829849
self.emit_diag_at_span(Diagnostic::new(Error { lint: false }, msg), span).unwrap()
830850
}
831851

852+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
832853
pub fn span_err_with_code(
833854
&self,
834855
span: impl Into<MultiSpan>,
@@ -841,10 +862,12 @@ impl Handler {
841862
);
842863
}
843864

865+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
844866
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
845867
self.emit_diag_at_span(Diagnostic::new(Warning, msg), span);
846868
}
847869

870+
#[cfg_attr(not(bootstrap), rustc_lint_diagnostics)]
848871
pub fn span_warn_with_code(
849872
&self,
850873
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)