Skip to content

Commit 059b7d0

Browse files
authored
Rollup merge of rust-lang#139944 - shepmaster:eager-diagnostics, r=nnethercote
Move eager translation to a method on Diag This will allow us to eagerly translate messages on a top-level diagnostic, such as a `LintDiagnostic`. As a bonus, we can remove the awkward closure passed into Subdiagnostic and make better use of `Into`. r? `@nnethercote`
2 parents a7af610 + 0117884 commit 059b7d0

File tree

20 files changed

+98
-271
lines changed

20 files changed

+98
-271
lines changed

compiler/rustc_ast_passes/src/errors.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_ast::ParamKindOrd;
44
use rustc_errors::codes::*;
5-
use rustc_errors::{Applicability, Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
5+
use rustc_errors::{Applicability, Diag, EmissionGuarantee, Subdiagnostic};
66
use rustc_macros::{Diagnostic, Subdiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
88

@@ -394,11 +394,7 @@ pub(crate) struct EmptyLabelManySpans(pub Vec<Span>);
394394

395395
// The derive for `Vec<Span>` does multiple calls to `span_label`, adding commas between each
396396
impl Subdiagnostic for EmptyLabelManySpans {
397-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
398-
self,
399-
diag: &mut Diag<'_, G>,
400-
_: &F,
401-
) {
397+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
402398
diag.span_labels(self.0, "");
403399
}
404400
}
@@ -749,11 +745,7 @@ pub(crate) struct StableFeature {
749745
}
750746

751747
impl Subdiagnostic for StableFeature {
752-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
753-
self,
754-
diag: &mut Diag<'_, G>,
755-
_: &F,
756-
) {
748+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
757749
diag.arg("name", self.name);
758750
diag.arg("since", self.since);
759751
diag.help(fluent::ast_passes_stable_since);

compiler/rustc_builtin_macros/src/errors.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_errors::codes::*;
22
use rustc_errors::{
33
Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan, SingleLabelManySpans,
4-
SubdiagMessageOp, Subdiagnostic,
4+
Subdiagnostic,
55
};
66
use rustc_macros::{Diagnostic, Subdiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
@@ -684,13 +684,9 @@ pub(crate) struct FormatUnusedArg {
684684
// Allow the singular form to be a subdiagnostic of the multiple-unused
685685
// form of diagnostic.
686686
impl Subdiagnostic for FormatUnusedArg {
687-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
688-
self,
689-
diag: &mut Diag<'_, G>,
690-
f: &F,
691-
) {
687+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
692688
diag.arg("named", self.named);
693-
let msg = f(diag, crate::fluent_generated::builtin_macros_format_unused_arg.into());
689+
let msg = diag.eagerly_translate(crate::fluent_generated::builtin_macros_format_unused_arg);
694690
diag.span_label(self.span, msg);
695691
}
696692
}

compiler/rustc_const_eval/src/errors.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_abi::WrappingRange;
66
use rustc_errors::codes::*;
77
use rustc_errors::{
88
Diag, DiagArgValue, DiagCtxtHandle, DiagMessage, Diagnostic, EmissionGuarantee, Level,
9-
MultiSpan, SubdiagMessageOp, Subdiagnostic,
9+
MultiSpan, Subdiagnostic,
1010
};
1111
use rustc_hir::ConstContext;
1212
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
@@ -290,19 +290,15 @@ pub struct FrameNote {
290290
}
291291

292292
impl Subdiagnostic for FrameNote {
293-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
294-
self,
295-
diag: &mut Diag<'_, G>,
296-
f: &F,
297-
) {
293+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
298294
diag.arg("times", self.times);
299295
diag.arg("where_", self.where_);
300296
diag.arg("instance", self.instance);
301297
let mut span: MultiSpan = self.span.into();
302298
if self.has_label && !self.span.is_dummy() {
303299
span.push_span_label(self.span, fluent::const_eval_frame_note_last);
304300
}
305-
let msg = f(diag, fluent::const_eval_frame_note.into());
301+
let msg = diag.eagerly_translate(fluent::const_eval_frame_note);
306302
diag.span_note(span, msg);
307303
}
308304
}

compiler/rustc_errors/src/diagnostic.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,9 @@ where
181181
Self: Sized,
182182
{
183183
/// Add a subdiagnostic to an existing diagnostic.
184-
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
185-
self.add_to_diag_with(diag, &|_, m| m);
186-
}
187-
188-
/// Add a subdiagnostic to an existing diagnostic where `f` is invoked on every message used
189-
/// (to optionally perform eager translation).
190-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
191-
self,
192-
diag: &mut Diag<'_, G>,
193-
f: &F,
194-
);
184+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>);
195185
}
196186

197-
pub trait SubdiagMessageOp<G: EmissionGuarantee> =
198-
Fn(&mut Diag<'_, G>, SubdiagMessage) -> SubdiagMessage;
199-
200187
/// Trait implemented by lint types. This should not be implemented manually. Instead, use
201188
/// `#[derive(LintDiagnostic)]` -- see [rustc_macros::LintDiagnostic].
202189
#[rustc_diagnostic_item = "LintDiagnostic"]
@@ -1227,15 +1214,21 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
12271214
/// interpolated variables).
12281215
#[rustc_lint_diagnostics]
12291216
pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1230-
let dcx = self.dcx;
1231-
subdiagnostic.add_to_diag_with(self, &|diag, msg| {
1232-
let args = diag.args.iter();
1233-
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
1234-
dcx.eagerly_translate(msg, args)
1235-
});
1217+
subdiagnostic.add_to_diag(self);
12361218
self
12371219
}
12381220

1221+
/// Fluent variables are not namespaced from each other, so when
1222+
/// `Diagnostic`s and `Subdiagnostic`s use the same variable name,
1223+
/// one value will clobber the other. Eagerly translating the
1224+
/// diagnostic uses the variables defined right then, before the
1225+
/// clobbering occurs.
1226+
pub fn eagerly_translate(&self, msg: impl Into<SubdiagMessage>) -> SubdiagMessage {
1227+
let args = self.args.iter();
1228+
let msg = self.subdiagnostic_message_to_diagnostic_message(msg.into());
1229+
self.dcx.eagerly_translate(msg, args)
1230+
}
1231+
12391232
with_fn! { with_span,
12401233
/// Add a span.
12411234
#[rustc_lint_diagnostics]

compiler/rustc_errors/src/diagnostic_impls.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use {rustc_ast as ast, rustc_hir as hir};
1919
use crate::diagnostic::DiagLocation;
2020
use crate::{
2121
Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, ErrCode, IntoDiagArg, Level,
22-
SubdiagMessageOp, Subdiagnostic, fluent_generated as fluent,
22+
Subdiagnostic, fluent_generated as fluent,
2323
};
2424

2525
pub struct DiagArgFromDisplay<'a>(pub &'a dyn fmt::Display);
@@ -384,11 +384,7 @@ pub struct SingleLabelManySpans {
384384
pub label: &'static str,
385385
}
386386
impl Subdiagnostic for SingleLabelManySpans {
387-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
388-
self,
389-
diag: &mut Diag<'_, G>,
390-
_: &F,
391-
) {
387+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
392388
diag.span_labels(self.spans, self.label);
393389
}
394390
}

compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub use codes::*;
4747
pub use diagnostic::{
4848
BugAbort, Diag, DiagArg, DiagArgMap, DiagArgName, DiagArgValue, DiagInner, DiagStyledString,
4949
Diagnostic, EmissionGuarantee, FatalAbort, IntoDiagArg, LintDiagnostic, StringPart, Subdiag,
50-
SubdiagMessageOp, Subdiagnostic,
50+
Subdiagnostic,
5151
};
5252
pub use diagnostic_impls::{
5353
DiagArgFromDisplay, DiagSymbolList, ElidedLifetimeInPathSubdiag, ExpectedLifetimeParameter,

compiler/rustc_hir_typeck/src/errors.rs

+8-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::borrow::Cow;
55
use rustc_errors::codes::*;
66
use rustc_errors::{
77
Applicability, Diag, DiagArgValue, DiagSymbolList, EmissionGuarantee, IntoDiagArg, MultiSpan,
8-
SubdiagMessageOp, Subdiagnostic,
8+
Subdiagnostic,
99
};
1010
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1111
use rustc_middle::ty::{self, Ty};
@@ -270,11 +270,7 @@ pub(crate) struct SuggestAnnotations {
270270
pub suggestions: Vec<SuggestAnnotation>,
271271
}
272272
impl Subdiagnostic for SuggestAnnotations {
273-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
274-
self,
275-
diag: &mut Diag<'_, G>,
276-
_: &F,
277-
) {
273+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
278274
if self.suggestions.is_empty() {
279275
return;
280276
}
@@ -337,11 +333,7 @@ pub(crate) struct TypeMismatchFruTypo {
337333
}
338334

339335
impl Subdiagnostic for TypeMismatchFruTypo {
340-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
341-
self,
342-
diag: &mut Diag<'_, G>,
343-
_f: &F,
344-
) {
336+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
345337
diag.arg("expr", self.expr.as_deref().unwrap_or("NONE"));
346338

347339
// Only explain that `a ..b` is a range if it's split up
@@ -599,11 +591,7 @@ pub(crate) struct RemoveSemiForCoerce {
599591
}
600592

601593
impl Subdiagnostic for RemoveSemiForCoerce {
602-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
603-
self,
604-
diag: &mut Diag<'_, G>,
605-
_f: &F,
606-
) {
594+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
607595
let mut multispan: MultiSpan = self.semi.into();
608596
multispan.push_span_label(self.expr, fluent::hir_typeck_remove_semi_for_coerce_expr);
609597
multispan.push_span_label(self.ret, fluent::hir_typeck_remove_semi_for_coerce_ret);
@@ -778,20 +766,16 @@ pub(crate) enum CastUnknownPointerSub {
778766
}
779767

780768
impl rustc_errors::Subdiagnostic for CastUnknownPointerSub {
781-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
782-
self,
783-
diag: &mut Diag<'_, G>,
784-
f: &F,
785-
) {
769+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
786770
match self {
787771
CastUnknownPointerSub::To(span) => {
788-
let msg = f(diag, crate::fluent_generated::hir_typeck_label_to);
772+
let msg = diag.eagerly_translate(fluent::hir_typeck_label_to);
789773
diag.span_label(span, msg);
790-
let msg = f(diag, crate::fluent_generated::hir_typeck_note);
774+
let msg = diag.eagerly_translate(fluent::hir_typeck_note);
791775
diag.note(msg);
792776
}
793777
CastUnknownPointerSub::From(span) => {
794-
let msg = f(diag, crate::fluent_generated::hir_typeck_label_from);
778+
let msg = diag.eagerly_translate(fluent::hir_typeck_label_from);
795779
diag.span_label(span, msg);
796780
}
797781
}

compiler/rustc_lint/src/errors.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_errors::codes::*;
2-
use rustc_errors::{Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
2+
use rustc_errors::{Diag, EmissionGuarantee, Subdiagnostic};
33
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_session::lint::Level;
55
use rustc_span::{Span, Symbol};
@@ -26,11 +26,7 @@ pub(crate) enum OverruledAttributeSub {
2626
}
2727

2828
impl Subdiagnostic for OverruledAttributeSub {
29-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
30-
self,
31-
diag: &mut Diag<'_, G>,
32-
_f: &F,
33-
) {
29+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
3430
match self {
3531
OverruledAttributeSub::DefaultSource { id } => {
3632
diag.note(fluent::lint_default_source);

compiler/rustc_lint/src/if_let_rescope.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use std::ops::ControlFlow;
33

44
use hir::intravisit::{self, Visitor};
55
use rustc_ast::Recovered;
6-
use rustc_errors::{
7-
Applicability, Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic, SuggestionStyle,
8-
};
6+
use rustc_errors::{Applicability, Diag, EmissionGuarantee, Subdiagnostic, SuggestionStyle};
97
use rustc_hir::{self as hir, HirIdSet};
108
use rustc_macros::{LintDiagnostic, Subdiagnostic};
119
use rustc_middle::ty::adjustment::Adjust;
@@ -327,11 +325,7 @@ struct IfLetRescopeRewrite {
327325
}
328326

329327
impl Subdiagnostic for IfLetRescopeRewrite {
330-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
331-
self,
332-
diag: &mut Diag<'_, G>,
333-
f: &F,
334-
) {
328+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
335329
let mut suggestions = vec![];
336330
for match_head in self.match_heads {
337331
match match_head {
@@ -360,7 +354,7 @@ impl Subdiagnostic for IfLetRescopeRewrite {
360354
.chain(repeat('}').take(closing_brackets.count))
361355
.collect(),
362356
));
363-
let msg = f(diag, crate::fluent_generated::lint_suggestion);
357+
let msg = diag.eagerly_translate(crate::fluent_generated::lint_suggestion);
364358
diag.multipart_suggestion_with_style(
365359
msg,
366360
suggestions,

compiler/rustc_lint/src/lints.rs

+8-36
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_abi::ExternAbi;
66
use rustc_errors::codes::*;
77
use rustc_errors::{
88
Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString, ElidedLifetimeInPathSubdiag,
9-
EmissionGuarantee, LintDiagnostic, MultiSpan, SubdiagMessageOp, Subdiagnostic, SuggestionStyle,
9+
EmissionGuarantee, LintDiagnostic, MultiSpan, Subdiagnostic, SuggestionStyle,
1010
};
1111
use rustc_hir::def::Namespace;
1212
use rustc_hir::def_id::DefId;
@@ -449,11 +449,7 @@ pub(crate) struct BuiltinUnpermittedTypeInitSub {
449449
}
450450

451451
impl Subdiagnostic for BuiltinUnpermittedTypeInitSub {
452-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
453-
self,
454-
diag: &mut Diag<'_, G>,
455-
_f: &F,
456-
) {
452+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
457453
let mut err = self.err;
458454
loop {
459455
if let Some(span) = err.span {
@@ -504,11 +500,7 @@ pub(crate) struct BuiltinClashingExternSub<'a> {
504500
}
505501

506502
impl Subdiagnostic for BuiltinClashingExternSub<'_> {
507-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
508-
self,
509-
diag: &mut Diag<'_, G>,
510-
_f: &F,
511-
) {
503+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
512504
let mut expected_str = DiagStyledString::new();
513505
expected_str.push(self.expected.fn_sig(self.tcx).to_string(), false);
514506
let mut found_str = DiagStyledString::new();
@@ -824,11 +816,7 @@ pub(crate) struct HiddenUnicodeCodepointsDiagLabels {
824816
}
825817

826818
impl Subdiagnostic for HiddenUnicodeCodepointsDiagLabels {
827-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
828-
self,
829-
diag: &mut Diag<'_, G>,
830-
_f: &F,
831-
) {
819+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
832820
for (c, span) in self.spans {
833821
diag.span_label(span, format!("{c:?}"));
834822
}
@@ -842,11 +830,7 @@ pub(crate) enum HiddenUnicodeCodepointsDiagSub {
842830

843831
// Used because of multiple multipart_suggestion and note
844832
impl Subdiagnostic for HiddenUnicodeCodepointsDiagSub {
845-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
846-
self,
847-
diag: &mut Diag<'_, G>,
848-
_f: &F,
849-
) {
833+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
850834
match self {
851835
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
852836
diag.multipart_suggestion_with_style(
@@ -1015,11 +999,7 @@ pub(crate) struct NonBindingLetSub {
1015999
}
10161000

10171001
impl Subdiagnostic for NonBindingLetSub {
1018-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
1019-
self,
1020-
diag: &mut Diag<'_, G>,
1021-
_f: &F,
1022-
) {
1002+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
10231003
let can_suggest_binding = self.drop_fn_start_end.is_some() || !self.is_assign_desugar;
10241004

10251005
if can_suggest_binding {
@@ -1303,11 +1283,7 @@ pub(crate) enum NonSnakeCaseDiagSub {
13031283
}
13041284

13051285
impl Subdiagnostic for NonSnakeCaseDiagSub {
1306-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
1307-
self,
1308-
diag: &mut Diag<'_, G>,
1309-
_f: &F,
1310-
) {
1286+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
13111287
match self {
13121288
NonSnakeCaseDiagSub::Label { span } => {
13131289
diag.span_label(span, fluent::lint_label);
@@ -1629,11 +1605,7 @@ pub(crate) enum OverflowingBinHexSign {
16291605
}
16301606

16311607
impl Subdiagnostic for OverflowingBinHexSign {
1632-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
1633-
self,
1634-
diag: &mut Diag<'_, G>,
1635-
_f: &F,
1636-
) {
1608+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
16371609
match self {
16381610
OverflowingBinHexSign::Positive => {
16391611
diag.note(fluent::lint_positive_note);

0 commit comments

Comments
 (0)