From 56d636a762505bef848152581f9f144e2154a3de Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 3 Mar 2026 22:14:53 +0100 Subject: [PATCH] Implement `Diagnostic` directly on `Box` --- compiler/rustc_errors/src/diagnostic.rs | 9 +++++++++ compiler/rustc_lint/src/early.rs | 17 ++--------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 4dec167b982b..888009bf9d4c 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -7,6 +7,7 @@ use std::path::PathBuf; use std::thread::panicking; +use rustc_data_structures::sync::DynSend; use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg}; use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_macros::{Decodable, Encodable}; @@ -118,6 +119,14 @@ fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> { } } +impl<'a> Diagnostic<'a, ()> + for Box FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSend + 'static> +{ + fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> { + self(dcx, level) + } +} + /// Trait implemented by error types. This should not be implemented manually. Instead, use /// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic]. #[rustc_diagnostic_item = "Subdiagnostic"] diff --git a/compiler/rustc_lint/src/early.rs b/compiler/rustc_lint/src/early.rs index 34381d2c5d48..92232c7d230b 100644 --- a/compiler/rustc_lint/src/early.rs +++ b/compiler/rustc_lint/src/early.rs @@ -7,10 +7,7 @@ use rustc_ast::visit::{self as ast_visit, Visitor, walk_list}; use rustc_ast::{self as ast, AttrVec, HasAttrs}; use rustc_data_structures::stack::ensure_sufficient_stack; -use rustc_data_structures::sync::DynSend; -use rustc_errors::{ - BufferedEarlyLint, DecorateDiagCompat, Diag, DiagCtxtHandle, Diagnostic, Level, LintBuffer, -}; +use rustc_errors::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer}; use rustc_feature::Features; use rustc_middle::ty::{RegisteredTools, TyCtxt}; use rustc_session::Session; @@ -38,16 +35,6 @@ pub struct EarlyContextAndPass<'ecx, 'tcx, T: EarlyLintPass> { impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> { fn check_id(&mut self, id: ast::NodeId) { - struct BoxDiag( - Box FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>, - ); - - impl<'a> Diagnostic<'a, ()> for BoxDiag { - fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> { - (self.0)(dcx, level) - } - } - for early_lint in self.context.buffered.take(id) { let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint; match diagnostic { @@ -63,7 +50,7 @@ fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> { ); } DecorateDiagCompat::Dynamic(d) => { - self.context.opt_span_diag_lint(lint_id.lint, span, BoxDiag(d)); + self.context.opt_span_diag_lint(lint_id.lint, span, d); } } }