Skip to content

Commit 4b695f7

Browse files
committed
Auto merge of #100765 - Xiretza:session-diagnostic-unification, r=compiler-errors
Kind-less SessionDiagnostic derive From #100730 (comment): > Hm, maybe we should expose some `sess.struct_$SOMETHING` (like `struct_diagnostic`?) that is generic over `EmissionGuarantee`, then make the `SessionDiagnostic` derive generic, i.e. > > ```rust > impl<'tcx> SessionDiagnostic for UnusedGenericParams { > fn into_diagnostic<T: EmissionGuarantee>( .. ) -> DiagnosticBuilder<'tcx, T> { > let mut diag = sess.struct_diagnostic(rustc_errors:..); > .. > } > } > ``` Discussed [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/147480-t-compiler.2Fwg-diagnostics/topic/.23100717.20translation.20-.20kind-less.20derive).
2 parents 39a9b88 + 7f3a6fd commit 4b695f7

File tree

20 files changed

+567
-517
lines changed

20 files changed

+567
-517
lines changed

compiler/rustc_borrowck/src/session_diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::ty::Ty;
33
use rustc_span::Span;
44

55
#[derive(SessionDiagnostic)]
6-
#[error(borrowck::move_unsized, code = "E0161")]
6+
#[diag(borrowck::move_unsized, code = "E0161")]
77
pub(crate) struct MoveUnsized<'tcx> {
88
pub ty: Ty<'tcx>,
99
#[primary_span]
@@ -12,7 +12,7 @@ pub(crate) struct MoveUnsized<'tcx> {
1212
}
1313

1414
#[derive(SessionDiagnostic)]
15-
#[error(borrowck::higher_ranked_lifetime_error)]
15+
#[diag(borrowck::higher_ranked_lifetime_error)]
1616
pub(crate) struct HigherRankedLifetimeError {
1717
#[subdiagnostic]
1818
pub cause: Option<HigherRankedErrorCause>,
@@ -29,14 +29,14 @@ pub(crate) enum HigherRankedErrorCause {
2929
}
3030

3131
#[derive(SessionDiagnostic)]
32-
#[error(borrowck::higher_ranked_subtype_error)]
32+
#[diag(borrowck::higher_ranked_subtype_error)]
3333
pub(crate) struct HigherRankedSubtypeError {
3434
#[primary_span]
3535
pub span: Span,
3636
}
3737

3838
#[derive(SessionDiagnostic)]
39-
#[error(borrowck::generic_does_not_live_long_enough)]
39+
#[diag(borrowck::generic_does_not_live_long_enough)]
4040
pub(crate) struct GenericDoesNotLiveLongEnough {
4141
pub kind: String,
4242
#[primary_span]

compiler/rustc_builtin_macros/src/cfg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ pub fn expand_cfg(
3636
}
3737

3838
#[derive(SessionDiagnostic)]
39-
#[error(builtin_macros::requires_cfg_pattern)]
39+
#[diag(builtin_macros::requires_cfg_pattern)]
4040
struct RequiresCfgPattern {
4141
#[primary_span]
4242
#[label]
4343
span: Span,
4444
}
4545

4646
#[derive(SessionDiagnostic)]
47-
#[error(builtin_macros::expected_one_cfg_pattern)]
47+
#[diag(builtin_macros::expected_one_cfg_pattern)]
4848
struct OneCfgPattern {
4949
#[primary_span]
5050
span: Span,

compiler/rustc_const_eval/src/errors.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_macros::SessionDiagnostic;
33
use rustc_span::Span;
44

55
#[derive(SessionDiagnostic)]
6-
#[error(const_eval::unstable_in_stable)]
6+
#[diag(const_eval::unstable_in_stable)]
77
pub(crate) struct UnstableInStable {
88
pub gate: String,
99
#[primary_span]
@@ -22,14 +22,14 @@ pub(crate) struct UnstableInStable {
2222
}
2323

2424
#[derive(SessionDiagnostic)]
25-
#[error(const_eval::thread_local_access, code = "E0625")]
25+
#[diag(const_eval::thread_local_access, code = "E0625")]
2626
pub(crate) struct NonConstOpErr {
2727
#[primary_span]
2828
pub span: Span,
2929
}
3030

3131
#[derive(SessionDiagnostic)]
32-
#[error(const_eval::static_access, code = "E0013")]
32+
#[diag(const_eval::static_access, code = "E0013")]
3333
#[help]
3434
pub(crate) struct StaticAccessErr {
3535
#[primary_span]
@@ -41,7 +41,7 @@ pub(crate) struct StaticAccessErr {
4141
}
4242

4343
#[derive(SessionDiagnostic)]
44-
#[error(const_eval::raw_ptr_to_int)]
44+
#[diag(const_eval::raw_ptr_to_int)]
4545
#[note]
4646
#[note(const_eval::note2)]
4747
pub(crate) struct RawPtrToIntErr {
@@ -50,38 +50,38 @@ pub(crate) struct RawPtrToIntErr {
5050
}
5151

5252
#[derive(SessionDiagnostic)]
53-
#[error(const_eval::raw_ptr_comparison)]
53+
#[diag(const_eval::raw_ptr_comparison)]
5454
#[note]
5555
pub(crate) struct RawPtrComparisonErr {
5656
#[primary_span]
5757
pub span: Span,
5858
}
5959

6060
#[derive(SessionDiagnostic)]
61-
#[error(const_eval::panic_non_str)]
61+
#[diag(const_eval::panic_non_str)]
6262
pub(crate) struct PanicNonStrErr {
6363
#[primary_span]
6464
pub span: Span,
6565
}
6666

6767
#[derive(SessionDiagnostic)]
68-
#[error(const_eval::mut_deref, code = "E0658")]
68+
#[diag(const_eval::mut_deref, code = "E0658")]
6969
pub(crate) struct MutDerefErr {
7070
#[primary_span]
7171
pub span: Span,
7272
pub kind: ConstContext,
7373
}
7474

7575
#[derive(SessionDiagnostic)]
76-
#[error(const_eval::transient_mut_borrow, code = "E0658")]
76+
#[diag(const_eval::transient_mut_borrow, code = "E0658")]
7777
pub(crate) struct TransientMutBorrowErr {
7878
#[primary_span]
7979
pub span: Span,
8080
pub kind: ConstContext,
8181
}
8282

8383
#[derive(SessionDiagnostic)]
84-
#[error(const_eval::transient_mut_borrow_raw, code = "E0658")]
84+
#[diag(const_eval::transient_mut_borrow_raw, code = "E0658")]
8585
pub(crate) struct TransientMutBorrowErrRaw {
8686
#[primary_span]
8787
pub span: Span,

compiler/rustc_errors/src/diagnostic_builder.rs

+30
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ pub trait EmissionGuarantee: Sized {
8484
/// of `Self` without actually performing the emission.
8585
#[track_caller]
8686
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self;
87+
88+
/// Creates a new `DiagnosticBuilder` that will return this type of guarantee.
89+
#[track_caller]
90+
fn make_diagnostic_builder(
91+
handler: &Handler,
92+
msg: impl Into<DiagnosticMessage>,
93+
) -> DiagnosticBuilder<'_, Self>;
8794
}
8895

8996
/// Private module for sealing the `IsError` helper trait.
@@ -166,6 +173,15 @@ impl EmissionGuarantee for ErrorGuaranteed {
166173
}
167174
}
168175
}
176+
177+
fn make_diagnostic_builder(
178+
handler: &Handler,
179+
msg: impl Into<DiagnosticMessage>,
180+
) -> DiagnosticBuilder<'_, Self> {
181+
DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>(
182+
handler, msg,
183+
)
184+
}
169185
}
170186

171187
impl<'a> DiagnosticBuilder<'a, ()> {
@@ -208,6 +224,13 @@ impl EmissionGuarantee for () {
208224
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
209225
}
210226
}
227+
228+
fn make_diagnostic_builder(
229+
handler: &Handler,
230+
msg: impl Into<DiagnosticMessage>,
231+
) -> DiagnosticBuilder<'_, Self> {
232+
DiagnosticBuilder::new(handler, Level::Warning(None), msg)
233+
}
211234
}
212235

213236
impl<'a> DiagnosticBuilder<'a, !> {
@@ -247,6 +270,13 @@ impl EmissionGuarantee for ! {
247270
// Then fatally error, returning `!`
248271
crate::FatalError.raise()
249272
}
273+
274+
fn make_diagnostic_builder(
275+
handler: &Handler,
276+
msg: impl Into<DiagnosticMessage>,
277+
) -> DiagnosticBuilder<'_, Self> {
278+
DiagnosticBuilder::new_fatal(handler, msg)
279+
}
250280
}
251281

252282
/// In general, the `DiagnosticBuilder` uses deref to allow access to

compiler/rustc_errors/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,15 @@ impl Handler {
641641
self.inner.borrow_mut().emit_stashed_diagnostics()
642642
}
643643

644+
/// Construct a builder with the `msg` at the level appropriate for the specific `EmissionGuarantee`.
645+
#[rustc_lint_diagnostics]
646+
pub fn struct_diagnostic<G: EmissionGuarantee>(
647+
&self,
648+
msg: impl Into<DiagnosticMessage>,
649+
) -> DiagnosticBuilder<'_, G> {
650+
G::make_diagnostic_builder(self, msg)
651+
}
652+
644653
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
645654
///
646655
/// Attempting to `.emit()` the builder will only emit if either:

compiler/rustc_expand/src/errors.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,44 @@ use rustc_span::symbol::MacroRulesNormalizedIdent;
33
use rustc_span::Span;
44

55
#[derive(SessionDiagnostic)]
6-
#[error(expand::expr_repeat_no_syntax_vars)]
6+
#[diag(expand::expr_repeat_no_syntax_vars)]
77
pub(crate) struct NoSyntaxVarsExprRepeat {
88
#[primary_span]
99
pub span: Span,
1010
}
1111

1212
#[derive(SessionDiagnostic)]
13-
#[error(expand::must_repeat_once)]
13+
#[diag(expand::must_repeat_once)]
1414
pub(crate) struct MustRepeatOnce {
1515
#[primary_span]
1616
pub span: Span,
1717
}
1818

1919
#[derive(SessionDiagnostic)]
20-
#[error(expand::count_repetition_misplaced)]
20+
#[diag(expand::count_repetition_misplaced)]
2121
pub(crate) struct CountRepetitionMisplaced {
2222
#[primary_span]
2323
pub span: Span,
2424
}
2525

2626
#[derive(SessionDiagnostic)]
27-
#[error(expand::meta_var_expr_unrecognized_var)]
27+
#[diag(expand::meta_var_expr_unrecognized_var)]
2828
pub(crate) struct MetaVarExprUnrecognizedVar {
2929
#[primary_span]
3030
pub span: Span,
3131
pub key: MacroRulesNormalizedIdent,
3232
}
3333

3434
#[derive(SessionDiagnostic)]
35-
#[error(expand::var_still_repeating)]
35+
#[diag(expand::var_still_repeating)]
3636
pub(crate) struct VarStillRepeating {
3737
#[primary_span]
3838
pub span: Span,
3939
pub ident: MacroRulesNormalizedIdent,
4040
}
4141

4242
#[derive(SessionDiagnostic)]
43-
#[error(expand::meta_var_dif_seq_matchers)]
43+
#[diag(expand::meta_var_dif_seq_matchers)]
4444
pub(crate) struct MetaVarsDifSeqMatchers {
4545
#[primary_span]
4646
pub span: Span,

compiler/rustc_interface/src/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
265265
}
266266

267267
#[derive(SessionDiagnostic)]
268-
#[error(interface::ferris_identifier)]
268+
#[diag(interface::ferris_identifier)]
269269
struct FerrisIdentifier {
270270
#[primary_span]
271271
spans: Vec<Span>,
@@ -274,7 +274,7 @@ struct FerrisIdentifier {
274274
}
275275

276276
#[derive(SessionDiagnostic)]
277-
#[error(interface::emoji_identifier)]
277+
#[diag(interface::emoji_identifier)]
278278
struct EmojiIdentifier {
279279
#[primary_span]
280280
spans: Vec<Span>,

compiler/rustc_lint/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ impl InvalidAtomicOrdering {
15461546

15471547
if matches!(fail_ordering, sym::Release | sym::AcqRel) {
15481548
#[derive(LintDiagnostic)]
1549-
#[lint(lint::atomic_ordering_invalid)]
1549+
#[diag(lint::atomic_ordering_invalid)]
15501550
#[help]
15511551
struct InvalidAtomicOrderingDiag {
15521552
method: Symbol,

0 commit comments

Comments
 (0)