Skip to content

Commit

Permalink
Auto merge of rust-lang#100765 - Xiretza:session-diagnostic-unificati…
Browse files Browse the repository at this point in the history
…on, r=compiler-errors

Kind-less SessionDiagnostic derive

From rust-lang#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).
  • Loading branch information
bors committed Aug 21, 2022
2 parents 39a9b88 + 7f3a6fd commit 4b695f7
Show file tree
Hide file tree
Showing 20 changed files with 567 additions and 517 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_middle::ty::Ty;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error(borrowck::move_unsized, code = "E0161")]
#[diag(borrowck::move_unsized, code = "E0161")]
pub(crate) struct MoveUnsized<'tcx> {
pub ty: Ty<'tcx>,
#[primary_span]
Expand All @@ -12,7 +12,7 @@ pub(crate) struct MoveUnsized<'tcx> {
}

#[derive(SessionDiagnostic)]
#[error(borrowck::higher_ranked_lifetime_error)]
#[diag(borrowck::higher_ranked_lifetime_error)]
pub(crate) struct HigherRankedLifetimeError {
#[subdiagnostic]
pub cause: Option<HigherRankedErrorCause>,
Expand All @@ -29,14 +29,14 @@ pub(crate) enum HigherRankedErrorCause {
}

#[derive(SessionDiagnostic)]
#[error(borrowck::higher_ranked_subtype_error)]
#[diag(borrowck::higher_ranked_subtype_error)]
pub(crate) struct HigherRankedSubtypeError {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(borrowck::generic_does_not_live_long_enough)]
#[diag(borrowck::generic_does_not_live_long_enough)]
pub(crate) struct GenericDoesNotLiveLongEnough {
pub kind: String,
#[primary_span]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub fn expand_cfg(
}

#[derive(SessionDiagnostic)]
#[error(builtin_macros::requires_cfg_pattern)]
#[diag(builtin_macros::requires_cfg_pattern)]
struct RequiresCfgPattern {
#[primary_span]
#[label]
span: Span,
}

#[derive(SessionDiagnostic)]
#[error(builtin_macros::expected_one_cfg_pattern)]
#[diag(builtin_macros::expected_one_cfg_pattern)]
struct OneCfgPattern {
#[primary_span]
span: Span,
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_macros::SessionDiagnostic;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error(const_eval::unstable_in_stable)]
#[diag(const_eval::unstable_in_stable)]
pub(crate) struct UnstableInStable {
pub gate: String,
#[primary_span]
Expand All @@ -22,14 +22,14 @@ pub(crate) struct UnstableInStable {
}

#[derive(SessionDiagnostic)]
#[error(const_eval::thread_local_access, code = "E0625")]
#[diag(const_eval::thread_local_access, code = "E0625")]
pub(crate) struct NonConstOpErr {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(const_eval::static_access, code = "E0013")]
#[diag(const_eval::static_access, code = "E0013")]
#[help]
pub(crate) struct StaticAccessErr {
#[primary_span]
Expand All @@ -41,7 +41,7 @@ pub(crate) struct StaticAccessErr {
}

#[derive(SessionDiagnostic)]
#[error(const_eval::raw_ptr_to_int)]
#[diag(const_eval::raw_ptr_to_int)]
#[note]
#[note(const_eval::note2)]
pub(crate) struct RawPtrToIntErr {
Expand All @@ -50,38 +50,38 @@ pub(crate) struct RawPtrToIntErr {
}

#[derive(SessionDiagnostic)]
#[error(const_eval::raw_ptr_comparison)]
#[diag(const_eval::raw_ptr_comparison)]
#[note]
pub(crate) struct RawPtrComparisonErr {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(const_eval::panic_non_str)]
#[diag(const_eval::panic_non_str)]
pub(crate) struct PanicNonStrErr {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(const_eval::mut_deref, code = "E0658")]
#[diag(const_eval::mut_deref, code = "E0658")]
pub(crate) struct MutDerefErr {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
}

#[derive(SessionDiagnostic)]
#[error(const_eval::transient_mut_borrow, code = "E0658")]
#[diag(const_eval::transient_mut_borrow, code = "E0658")]
pub(crate) struct TransientMutBorrowErr {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
}

#[derive(SessionDiagnostic)]
#[error(const_eval::transient_mut_borrow_raw, code = "E0658")]
#[diag(const_eval::transient_mut_borrow_raw, code = "E0658")]
pub(crate) struct TransientMutBorrowErrRaw {
#[primary_span]
pub span: Span,
Expand Down
30 changes: 30 additions & 0 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ pub trait EmissionGuarantee: Sized {
/// of `Self` without actually performing the emission.
#[track_caller]
fn diagnostic_builder_emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self;

/// Creates a new `DiagnosticBuilder` that will return this type of guarantee.
#[track_caller]
fn make_diagnostic_builder(
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self>;
}

/// Private module for sealing the `IsError` helper trait.
Expand Down Expand Up @@ -166,6 +173,15 @@ impl EmissionGuarantee for ErrorGuaranteed {
}
}
}

fn make_diagnostic_builder(
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>(
handler, msg,
)
}
}

impl<'a> DiagnosticBuilder<'a, ()> {
Expand Down Expand Up @@ -208,6 +224,13 @@ impl EmissionGuarantee for () {
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
}
}

fn make_diagnostic_builder(
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder::new(handler, Level::Warning(None), msg)
}
}

impl<'a> DiagnosticBuilder<'a, !> {
Expand Down Expand Up @@ -247,6 +270,13 @@ impl EmissionGuarantee for ! {
// Then fatally error, returning `!`
crate::FatalError.raise()
}

fn make_diagnostic_builder(
handler: &Handler,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, Self> {
DiagnosticBuilder::new_fatal(handler, msg)
}
}

/// In general, the `DiagnosticBuilder` uses deref to allow access to
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,15 @@ impl Handler {
self.inner.borrow_mut().emit_stashed_diagnostics()
}

/// Construct a builder with the `msg` at the level appropriate for the specific `EmissionGuarantee`.
#[rustc_lint_diagnostics]
pub fn struct_diagnostic<G: EmissionGuarantee>(
&self,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, G> {
G::make_diagnostic_builder(self, msg)
}

/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
///
/// Attempting to `.emit()` the builder will only emit if either:
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ use rustc_span::symbol::MacroRulesNormalizedIdent;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error(expand::expr_repeat_no_syntax_vars)]
#[diag(expand::expr_repeat_no_syntax_vars)]
pub(crate) struct NoSyntaxVarsExprRepeat {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::must_repeat_once)]
#[diag(expand::must_repeat_once)]
pub(crate) struct MustRepeatOnce {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::count_repetition_misplaced)]
#[diag(expand::count_repetition_misplaced)]
pub(crate) struct CountRepetitionMisplaced {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::meta_var_expr_unrecognized_var)]
#[diag(expand::meta_var_expr_unrecognized_var)]
pub(crate) struct MetaVarExprUnrecognizedVar {
#[primary_span]
pub span: Span,
pub key: MacroRulesNormalizedIdent,
}

#[derive(SessionDiagnostic)]
#[error(expand::var_still_repeating)]
#[diag(expand::var_still_repeating)]
pub(crate) struct VarStillRepeating {
#[primary_span]
pub span: Span,
pub ident: MacroRulesNormalizedIdent,
}

#[derive(SessionDiagnostic)]
#[error(expand::meta_var_dif_seq_matchers)]
#[diag(expand::meta_var_dif_seq_matchers)]
pub(crate) struct MetaVarsDifSeqMatchers {
#[primary_span]
pub span: Span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
}

#[derive(SessionDiagnostic)]
#[error(interface::ferris_identifier)]
#[diag(interface::ferris_identifier)]
struct FerrisIdentifier {
#[primary_span]
spans: Vec<Span>,
Expand All @@ -274,7 +274,7 @@ struct FerrisIdentifier {
}

#[derive(SessionDiagnostic)]
#[error(interface::emoji_identifier)]
#[diag(interface::emoji_identifier)]
struct EmojiIdentifier {
#[primary_span]
spans: Vec<Span>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ impl InvalidAtomicOrdering {

if matches!(fail_ordering, sym::Release | sym::AcqRel) {
#[derive(LintDiagnostic)]
#[lint(lint::atomic_ordering_invalid)]
#[diag(lint::atomic_ordering_invalid)]
#[help]
struct InvalidAtomicOrderingDiag {
method: Symbol,
Expand Down
Loading

0 comments on commit 4b695f7

Please sign in to comment.