Start migrating DecorateDiagCompat::Builtin items to DecorateDiagCompat::Dynamic

This commit is contained in:
Guillaume Gomez
2026-03-20 00:40:47 +01:00
parent 461e9738a4
commit 9c591c6508
7 changed files with 51 additions and 30 deletions
@@ -14,6 +14,18 @@ pub enum DecorateDiagCompat {
Builtin(BuiltinLintDiag),
}
pub struct DynamicDiag<
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static,
>(Box<F>);
impl<F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>
DynamicDiag<F>
{
pub fn new(f: F) -> Self {
Self(Box::new(f))
}
}
impl std::fmt::Debug for DecorateDiagCompat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DecorateDiagCompat").finish()
@@ -34,6 +46,15 @@ fn from(b: BuiltinLintDiag) -> Self {
}
}
impl<F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>
From<DynamicDiag<F>> for DecorateDiagCompat
{
#[inline]
fn from(d: DynamicDiag<F>) -> Self {
Self::Dynamic(d.0)
}
}
/// Lints that are buffered up early on in the `Session` before the
/// `LintLevels` is calculated.
#[derive(Debug)]
+1 -1
View File
@@ -34,7 +34,7 @@
Ansi256Color, AnsiColor, Color, EffectIter, Effects, Reset, RgbColor, Style as Anstyle,
};
pub use codes::*;
pub use decorate_diag::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer};
pub use decorate_diag::{BufferedEarlyLint, DecorateDiagCompat, DynamicDiag, LintBuffer};
pub use diagnostic::{
BugAbort, Diag, DiagDecorator, DiagInner, DiagLocation, DiagStyledString, Diagnostic,
EmissionGuarantee, FatalAbort, StringPart, Subdiag, Subdiagnostic,
@@ -170,13 +170,6 @@ fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
lints::ReservedMultihash { suggestion }.into_diag(dcx, level)
}
}
BuiltinLintDiag::BreakWithLabelAndLoop(sugg_span) => lints::BreakWithLabelAndLoop {
sub: lints::BreakWithLabelAndLoopSub {
left: sugg_span.shrink_to_lo(),
right: sugg_span.shrink_to_hi(),
},
}
.into_diag(dcx, level),
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
let suggestion = match sugg {
Some((right_sp, sugg)) => lints::DeprecatedWhereClauseLocationSugg::MoveToEnd {
-18
View File
@@ -3220,24 +3220,6 @@ pub(crate) struct RawPrefix {
pub suggestion: Span,
}
#[derive(Diagnostic)]
#[diag(
"this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression"
)]
pub(crate) struct BreakWithLabelAndLoop {
#[subdiagnostic]
pub sub: BreakWithLabelAndLoopSub,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion("wrap this expression in parentheses", applicability = "machine-applicable")]
pub(crate) struct BreakWithLabelAndLoopSub {
#[suggestion_part(code = "(")]
pub left: Span,
#[suggestion_part(code = ")")]
pub right: Span,
}
#[derive(Diagnostic)]
#[diag("where clause not allowed here")]
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
-1
View File
@@ -686,7 +686,6 @@ pub enum BuiltinLintDiag {
is_string: bool,
suggestion: Span,
},
BreakWithLabelAndLoop(Span),
UnicodeTextFlow(Span, String),
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
SingleUseLifetime {
+18
View File
@@ -4499,3 +4499,21 @@ pub(super) fn from_token(token: &Token) -> Option<Self> {
}
}
}
#[derive(Diagnostic)]
#[diag(
"this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression"
)]
pub(crate) struct BreakWithLabelAndLoop {
#[subdiagnostic]
pub sub: BreakWithLabelAndLoopSub,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion("wrap this expression in parentheses", applicability = "machine-applicable")]
pub(crate) struct BreakWithLabelAndLoopSub {
#[suggestion_part(code = "(")]
pub left: Span,
#[suggestion_part(code = ")")]
pub right: Span,
}
+11 -3
View File
@@ -19,10 +19,9 @@
StmtKind, Ty, TyKind, UnOp, UnsafeBinderCastKind, YieldKind,
};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{Applicability, Diag, PResult, StashKey, Subdiagnostic};
use rustc_errors::{Applicability, Diag, Diagnostic, PResult, StashKey, Subdiagnostic};
use rustc_literal_escaper::unescape_char;
use rustc_session::errors::{ExprParenthesesNeeded, report_lit_error};
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
use rustc_span::edition::Edition;
use rustc_span::{BytePos, ErrorGuaranteed, Ident, Pos, Span, Spanned, Symbol, kw, respan, sym};
@@ -1921,11 +1920,20 @@ fn parse_expr_break(&mut self) -> PResult<'a, Box<Expr>> {
_ => false,
}
{
let span = expr.span;
self.psess.buffer_lint(
BREAK_WITH_LABEL_AND_LOOP,
lo.to(expr.span),
ast::CRATE_NODE_ID,
BuiltinLintDiag::BreakWithLabelAndLoop(expr.span),
rustc_errors::DynamicDiag::new(move |dcx, level| {
errors::BreakWithLabelAndLoop {
sub: errors::BreakWithLabelAndLoopSub {
left: span.shrink_to_lo(),
right: span.shrink_to_hi(),
},
}
.into_diag(dcx, level)
}),
);
}