Skip to content

Commit 6b175a8

Browse files
committed
Add SubdiagnosticMessageOp as a trait alias.
It avoids a lot of repetition.
1 parent 0d53135 commit 6b175a8

File tree

19 files changed

+68
-181
lines changed

19 files changed

+68
-181
lines changed

compiler/rustc_ast_lowering/src/errors.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use rustc_errors::{codes::*, DiagnosticArgFromDisplay};
1+
use rustc_errors::{
2+
codes::*, AddToDiagnostic, Diagnostic, DiagnosticArgFromDisplay, SubdiagnosticMessageOp,
3+
};
24
use rustc_macros::{Diagnostic, Subdiagnostic};
35
use rustc_span::{symbol::Ident, Span, Symbol};
46

@@ -38,14 +40,8 @@ pub struct InvalidAbi {
3840

3941
pub struct InvalidAbiReason(pub &'static str);
4042

41-
impl rustc_errors::AddToDiagnostic for InvalidAbiReason {
42-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
43-
where
44-
F: Fn(
45-
&mut rustc_errors::Diagnostic,
46-
rustc_errors::SubdiagnosticMessage,
47-
) -> rustc_errors::SubdiagnosticMessage,
48-
{
43+
impl AddToDiagnostic for InvalidAbiReason {
44+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
4945
#[allow(rustc::untranslatable_diagnostic)]
5046
diag.note(self.0);
5147
}

compiler/rustc_ast_passes/src/errors.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Errors emitted by ast_passes.
22
33
use rustc_ast::ParamKindOrd;
4-
use rustc_errors::{codes::*, AddToDiagnostic, Applicability};
4+
use rustc_errors::{codes::*, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessageOp};
55
use rustc_macros::{Diagnostic, Subdiagnostic};
66
use rustc_span::{symbol::Ident, Span, Symbol};
77

@@ -372,13 +372,7 @@ pub struct EmptyLabelManySpans(pub Vec<Span>);
372372

373373
// The derive for `Vec<Span>` does multiple calls to `span_label`, adding commas between each
374374
impl AddToDiagnostic for EmptyLabelManySpans {
375-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
376-
where
377-
F: Fn(
378-
&mut rustc_errors::Diagnostic,
379-
rustc_errors::SubdiagnosticMessage,
380-
) -> rustc_errors::SubdiagnosticMessage,
381-
{
375+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
382376
diag.span_labels(self.0, "");
383377
}
384378
}
@@ -735,13 +729,7 @@ pub struct StableFeature {
735729
}
736730

737731
impl AddToDiagnostic for StableFeature {
738-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
739-
where
740-
F: Fn(
741-
&mut rustc_errors::Diagnostic,
742-
rustc_errors::SubdiagnosticMessage,
743-
) -> rustc_errors::SubdiagnosticMessage,
744-
{
732+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
745733
diag.arg("name", self.name);
746734
diag.arg("since", self.since);
747735
diag.help(fluent::ast_passes_stable_since);

compiler/rustc_builtin_macros/src/errors.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::{
2-
codes::*, AddToDiagnostic, DiagCtxt, DiagnosticBuilder, EmissionGuarantee, IntoDiagnostic,
3-
Level, MultiSpan, SingleLabelManySpans,
2+
codes::*, AddToDiagnostic, DiagCtxt, Diagnostic, DiagnosticBuilder, EmissionGuarantee,
3+
IntoDiagnostic, Level, MultiSpan, SingleLabelManySpans, SubdiagnosticMessageOp,
44
};
55
use rustc_macros::{Diagnostic, Subdiagnostic};
66
use rustc_span::{symbol::Ident, Span, Symbol};
@@ -611,13 +611,7 @@ pub(crate) struct FormatUnusedArg {
611611
// Allow the singular form to be a subdiagnostic of the multiple-unused
612612
// form of diagnostic.
613613
impl AddToDiagnostic for FormatUnusedArg {
614-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
615-
where
616-
F: Fn(
617-
&mut rustc_errors::Diagnostic,
618-
rustc_errors::SubdiagnosticMessage,
619-
) -> rustc_errors::SubdiagnosticMessage,
620-
{
614+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
621615
diag.arg("named", self.named);
622616
let msg = f(diag, crate::fluent_generated::builtin_macros_format_unused_arg.into());
623617
diag.span_label(self.span, msg);

compiler/rustc_errors/src/diagnostic.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ where
7777

7878
/// Add a subdiagnostic to an existing diagnostic where `f` is invoked on every message used
7979
/// (to optionally perform eager translation).
80-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
81-
where
82-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage;
80+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F);
8381
}
8482

83+
pub trait SubdiagnosticMessageOp =
84+
Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage;
85+
8586
/// Trait implemented by lint types. This should not be implemented manually. Instead, use
8687
/// `#[derive(LintDiagnostic)]` -- see [rustc_macros::LintDiagnostic].
8788
#[rustc_diagnostic_item = "DecorateLint"]

compiler/rustc_errors/src/diagnostic_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::diagnostic::DiagnosticLocation;
22
use crate::{fluent_generated as fluent, AddToDiagnostic};
33
use crate::{
44
DiagCtxt, DiagnosticArgValue, DiagnosticBuilder, EmissionGuarantee, ErrCode, IntoDiagnostic,
5-
IntoDiagnosticArg, Level,
5+
IntoDiagnosticArg, Level, SubdiagnosticMessageOp,
66
};
77
use rustc_ast as ast;
88
use rustc_ast_pretty::pprust;
@@ -299,7 +299,7 @@ pub struct SingleLabelManySpans {
299299
pub label: &'static str,
300300
}
301301
impl AddToDiagnostic for SingleLabelManySpans {
302-
fn add_to_diagnostic_with<F>(self, diag: &mut crate::Diagnostic, _: F) {
302+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut crate::Diagnostic, _: F) {
303303
diag.span_labels(self.spans, self.label);
304304
}
305305
}

compiler/rustc_errors/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(never_type)]
2020
#![feature(rustc_attrs)]
2121
#![feature(rustdoc_internals)]
22+
#![feature(trait_alias)]
2223
#![feature(try_blocks)]
2324
#![feature(yeet_expr)]
2425
// tidy-alphabetical-end
@@ -35,6 +36,7 @@ pub use codes::*;
3536
pub use diagnostic::{
3637
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName,
3738
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, StringPart, SubDiagnostic,
39+
SubdiagnosticMessageOp,
3840
};
3941
pub use diagnostic_builder::{
4042
BugAbort, DiagnosticBuilder, EmissionGuarantee, FatalAbort, IntoDiagnostic,

compiler/rustc_hir_typeck/src/errors.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::borrow::Cow;
44
use crate::fluent_generated as fluent;
55
use rustc_errors::{
66
codes::*, AddToDiagnostic, Applicability, Diagnostic, DiagnosticArgValue, IntoDiagnosticArg,
7-
MultiSpan, SubdiagnosticMessage,
7+
MultiSpan, SubdiagnosticMessageOp,
88
};
99
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1010
use rustc_middle::ty::Ty;
@@ -195,10 +195,7 @@ pub struct TypeMismatchFruTypo {
195195
}
196196

197197
impl AddToDiagnostic for TypeMismatchFruTypo {
198-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
199-
where
200-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
201-
{
198+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
202199
diag.arg("expr", self.expr.as_deref().unwrap_or("NONE"));
203200

204201
// Only explain that `a ..b` is a range if it's split up
@@ -373,10 +370,7 @@ pub struct RemoveSemiForCoerce {
373370
}
374371

375372
impl AddToDiagnostic for RemoveSemiForCoerce {
376-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
377-
where
378-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
379-
{
373+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
380374
let mut multispan: MultiSpan = self.semi.into();
381375
multispan.push_span_label(self.expr, fluent::hir_typeck_remove_semi_for_coerce_expr);
382376
multispan.push_span_label(self.ret, fluent::hir_typeck_remove_semi_for_coerce_ret);
@@ -547,14 +541,8 @@ pub enum CastUnknownPointerSub {
547541
From(Span),
548542
}
549543

550-
impl rustc_errors::AddToDiagnostic for CastUnknownPointerSub {
551-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
552-
where
553-
F: Fn(
554-
&mut Diagnostic,
555-
rustc_errors::SubdiagnosticMessage,
556-
) -> rustc_errors::SubdiagnosticMessage,
557-
{
544+
impl AddToDiagnostic for CastUnknownPointerSub {
545+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
558546
match self {
559547
CastUnknownPointerSub::To(span) => {
560548
let msg = f(diag, crate::fluent_generated::hir_typeck_label_to);

compiler/rustc_infer/src/errors/mod.rs

+10-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use hir::GenericParamKind;
22
use rustc_errors::{
33
codes::*, AddToDiagnostic, Applicability, Diagnostic, DiagnosticMessage,
4-
DiagnosticStyledString, IntoDiagnosticArg, MultiSpan, SubdiagnosticMessage,
4+
DiagnosticStyledString, IntoDiagnosticArg, MultiSpan, SubdiagnosticMessageOp,
55
};
66
use rustc_hir as hir;
77
use rustc_hir::FnRetTy;
@@ -225,10 +225,7 @@ pub enum RegionOriginNote<'a> {
225225
}
226226

227227
impl AddToDiagnostic for RegionOriginNote<'_> {
228-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
229-
where
230-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
231-
{
228+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
232229
let mut label_or_note = |span, msg: DiagnosticMessage| {
233230
let sub_count = diag.children.iter().filter(|d| d.span.is_dummy()).count();
234231
let expanded_sub_count = diag.children.iter().filter(|d| !d.span.is_dummy()).count();
@@ -289,10 +286,7 @@ pub enum LifetimeMismatchLabels {
289286
}
290287

291288
impl AddToDiagnostic for LifetimeMismatchLabels {
292-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
293-
where
294-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
295-
{
289+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
296290
match self {
297291
LifetimeMismatchLabels::InRet { param_span, ret_span, span, label_var1 } => {
298292
diag.span_label(param_span, fluent::infer_declared_different);
@@ -336,10 +330,7 @@ pub struct AddLifetimeParamsSuggestion<'a> {
336330
}
337331

338332
impl AddToDiagnostic for AddLifetimeParamsSuggestion<'_> {
339-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
340-
where
341-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
342-
{
333+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
343334
let mut mk_suggestion = || {
344335
let (
345336
hir::Ty { kind: hir::TyKind::Ref(lifetime_sub, _), .. },
@@ -437,10 +428,7 @@ pub struct IntroducesStaticBecauseUnmetLifetimeReq {
437428
}
438429

439430
impl AddToDiagnostic for IntroducesStaticBecauseUnmetLifetimeReq {
440-
fn add_to_diagnostic_with<F>(mut self, diag: &mut Diagnostic, _: F)
441-
where
442-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
443-
{
431+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(mut self, diag: &mut Diagnostic, _: F) {
444432
self.unmet_requirements
445433
.push_span_label(self.binding_span, fluent::infer_msl_introduces_static);
446434
diag.span_note(self.unmet_requirements, fluent::infer_msl_unmet_req);
@@ -755,10 +743,7 @@ pub struct ConsiderBorrowingParamHelp {
755743
}
756744

757745
impl AddToDiagnostic for ConsiderBorrowingParamHelp {
758-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
759-
where
760-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
761-
{
746+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
762747
let mut type_param_span: MultiSpan = self.spans.clone().into();
763748
for &span in &self.spans {
764749
// Seems like we can't call f() here as Into<DiagnosticMessage> is required
@@ -799,10 +784,7 @@ pub struct DynTraitConstraintSuggestion {
799784
}
800785

801786
impl AddToDiagnostic for DynTraitConstraintSuggestion {
802-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
803-
where
804-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
805-
{
787+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
806788
let mut multi_span: MultiSpan = vec![self.span].into();
807789
multi_span.push_span_label(self.span, fluent::infer_dtcs_has_lifetime_req_label);
808790
multi_span.push_span_label(self.ident.span, fluent::infer_dtcs_introduces_requirement);
@@ -845,10 +827,7 @@ pub struct ReqIntroducedLocations {
845827
}
846828

847829
impl AddToDiagnostic for ReqIntroducedLocations {
848-
fn add_to_diagnostic_with<F>(mut self, diag: &mut Diagnostic, f: F)
849-
where
850-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
851-
{
830+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(mut self, diag: &mut Diagnostic, f: F) {
852831
for sp in self.spans {
853832
self.span.push_span_label(sp, fluent::infer_ril_introduced_here);
854833
}
@@ -867,10 +846,7 @@ pub struct MoreTargeted {
867846
}
868847

869848
impl AddToDiagnostic for MoreTargeted {
870-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _f: F)
871-
where
872-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
873-
{
849+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _f: F) {
874850
diag.code(E0772);
875851
diag.primary_message(fluent::infer_more_targeted);
876852
diag.arg("ident", self.ident);
@@ -1289,10 +1265,7 @@ pub struct SuggestTuplePatternMany {
12891265
}
12901266

12911267
impl AddToDiagnostic for SuggestTuplePatternMany {
1292-
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
1293-
where
1294-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
1295-
{
1268+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
12961269
diag.arg("path", self.path);
12971270
let message = f(diag, crate::fluent_generated::infer_stp_wrap_many.into());
12981271
diag.multipart_suggestions(

compiler/rustc_infer/src/errors/note_and_explain.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::fluent_generated as fluent;
22
use crate::infer::error_reporting::nice_region_error::find_anon_type;
3-
use rustc_errors::{AddToDiagnostic, Diagnostic, IntoDiagnosticArg, SubdiagnosticMessage};
3+
use rustc_errors::{AddToDiagnostic, Diagnostic, IntoDiagnosticArg, SubdiagnosticMessageOp};
44
use rustc_middle::ty::{self, TyCtxt};
55
use rustc_span::{symbol::kw, Span};
66

@@ -160,10 +160,7 @@ impl RegionExplanation<'_> {
160160
}
161161

162162
impl AddToDiagnostic for RegionExplanation<'_> {
163-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
164-
where
165-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
166-
{
163+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, f: F) {
167164
diag.arg("pref_kind", self.prefix);
168165
diag.arg("suff_kind", self.suffix);
169166
diag.arg("desc_kind", self.desc.kind);

compiler/rustc_lint/src/errors.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fluent_generated as fluent;
2-
use rustc_errors::{codes::*, AddToDiagnostic, Diagnostic, SubdiagnosticMessage};
2+
use rustc_errors::{codes::*, AddToDiagnostic, Diagnostic, SubdiagnosticMessageOp};
33
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_session::lint::Level;
55
use rustc_span::{Span, Symbol};
@@ -24,10 +24,7 @@ pub enum OverruledAttributeSub {
2424
}
2525

2626
impl AddToDiagnostic for OverruledAttributeSub {
27-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
28-
where
29-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
30-
{
27+
fn add_to_diagnostic_with<F: SubdiagnosticMessageOp>(self, diag: &mut Diagnostic, _: F) {
3128
match self {
3229
OverruledAttributeSub::DefaultSource { id } => {
3330
diag.note(fluent::lint_default_source);

0 commit comments

Comments
 (0)