Skip to content

Commit 56d636a

Browse files
Implement Diagnostic directly on Box<FnOnce...>
1 parent b5ff567 commit 56d636a

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::panic;
77
use std::path::PathBuf;
88
use std::thread::panicking;
99

10+
use rustc_data_structures::sync::DynSend;
1011
use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg};
1112
use rustc_lint_defs::{Applicability, LintExpectationId};
1213
use rustc_macros::{Decodable, Encodable};
@@ -118,6 +119,14 @@ where
118119
}
119120
}
120121

122+
impl<'a> Diagnostic<'a, ()>
123+
for Box<dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSend + 'static>
124+
{
125+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
126+
self(dcx, level)
127+
}
128+
}
129+
121130
/// Trait implemented by error types. This should not be implemented manually. Instead, use
122131
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
123132
#[rustc_diagnostic_item = "Subdiagnostic"]

compiler/rustc_lint/src/early.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use rustc_ast::visit::{self as ast_visit, Visitor, walk_list};
88
use rustc_ast::{self as ast, AttrVec, HasAttrs};
99
use rustc_data_structures::stack::ensure_sufficient_stack;
10-
use rustc_data_structures::sync::DynSend;
11-
use rustc_errors::{
12-
BufferedEarlyLint, DecorateDiagCompat, Diag, DiagCtxtHandle, Diagnostic, Level, LintBuffer,
13-
};
10+
use rustc_errors::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer};
1411
use rustc_feature::Features;
1512
use rustc_middle::ty::{RegisteredTools, TyCtxt};
1613
use rustc_session::Session;
@@ -38,16 +35,6 @@ pub struct EarlyContextAndPass<'ecx, 'tcx, T: EarlyLintPass> {
3835

3936
impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
4037
fn check_id(&mut self, id: ast::NodeId) {
41-
struct BoxDiag(
42-
Box<dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>,
43-
);
44-
45-
impl<'a> Diagnostic<'a, ()> for BoxDiag {
46-
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
47-
(self.0)(dcx, level)
48-
}
49-
}
50-
5138
for early_lint in self.context.buffered.take(id) {
5239
let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint;
5340
match diagnostic {
@@ -63,7 +50,7 @@ impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
6350
);
6451
}
6552
DecorateDiagCompat::Dynamic(d) => {
66-
self.context.opt_span_diag_lint(lint_id.lint, span, BoxDiag(d));
53+
self.context.opt_span_diag_lint(lint_id.lint, span, d);
6754
}
6855
}
6956
}

0 commit comments

Comments
 (0)