Skip to content

Commit 10eb844

Browse files
Remove eagerly_format_to_string from DiagCtxt
1 parent 8c87d07 commit 10eb844

6 files changed

Lines changed: 38 additions & 60 deletions

File tree

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -949,17 +949,14 @@ pub(crate) struct AsmClobberNoReg {
949949

950950
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AsmClobberNoReg {
951951
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
952-
// eager translation as `span_labels` takes `AsRef<str>`
953-
let lbl1 = dcx.eagerly_format_to_string(msg!("clobber_abi"), [].into_iter());
954-
let lbl2 = dcx.eagerly_format_to_string(msg!("generic outputs"), [].into_iter());
955952
Diag::new(
956953
dcx,
957954
level,
958955
msg!("asm with `clobber_abi` must specify explicit registers for outputs"),
959956
)
960957
.with_span(self.spans.clone())
961-
.with_span_labels(self.clobbers, &lbl1)
962-
.with_span_labels(self.spans, &lbl2)
958+
.with_span_labels(self.clobbers, "clobber_abi")
959+
.with_span_labels(self.spans, "generic outputs")
963960
}
964961
}
965962

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::ffi::CString;
22
use std::path::Path;
33

44
use rustc_data_structures::small_c_str::SmallCStr;
5-
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, msg};
5+
use rustc_errors::{
6+
Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, format_diag_message, msg,
7+
};
68
use rustc_macros::Diagnostic;
79
use rustc_span::Span;
810

@@ -24,7 +26,7 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
2426
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
2527
let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
2628
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
27-
let message = dcx.eagerly_format_to_string(message.clone(), diag.args.iter());
29+
let message = format_diag_message(message, &diag.args);
2830
Diag::new(
2931
dcx,
3032
level,

compiler/rustc_const_eval/src/errors.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_abi::WrappingRange;
66
use rustc_errors::codes::*;
77
use rustc_errors::formatting::DiagMessageAddArg;
88
use rustc_errors::{
9-
Diag, DiagArgValue, DiagMessage, Diagnostic, EmissionGuarantee, Level, MultiSpan,
10-
Subdiagnostic, msg,
9+
Diag, DiagArgMap, DiagArgValue, DiagMessage, Diagnostic, EmissionGuarantee, Level, MultiSpan,
10+
Subdiagnostic, format_diag_message, msg,
1111
};
1212
use rustc_hir::ConstContext;
1313
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -623,7 +623,7 @@ pub trait ReportErrorExt {
623623
let mut diag = dcx.struct_allow(DiagMessage::Str(String::new().into()));
624624
let message = self.diagnostic_message();
625625
self.add_args(&mut diag);
626-
let s = dcx.eagerly_format_to_string(message, diag.args.iter());
626+
let s = format_diag_message(&message, &diag.args).into_owned();
627627
diag.cancel();
628628
s
629629
})
@@ -1085,12 +1085,12 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
10851085
}
10861086

10871087
let message = if let Some(path) = self.path {
1088-
err.dcx.eagerly_format_to_string(
1089-
msg!("constructing invalid value at {$path}"),
1090-
[("path".into(), DiagArgValue::Str(path.into()))].iter().map(|(a, b)| (a, b)),
1088+
format_diag_message(
1089+
&msg!("constructing invalid value at {$path}"),
1090+
&DiagArgMap::from_iter([("path".into(), DiagArgValue::Str(path.into()))]),
10911091
)
10921092
} else {
1093-
err.dcx.eagerly_format_to_string(msg!("constructing invalid value"), [].into_iter())
1093+
Cow::Borrowed("constructing invalid value")
10941094
};
10951095

10961096
err.arg("front_matter", message);
@@ -1116,12 +1116,13 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
11161116
msg!("in the range {$lo}..={$hi}")
11171117
};
11181118

1119-
let args = [
1120-
("lo".into(), DiagArgValue::Str(lo.to_string().into())),
1121-
("hi".into(), DiagArgValue::Str(hi.to_string().into())),
1122-
];
1123-
let args = args.iter().map(|(a, b)| (a, b));
1124-
let message = err.dcx.eagerly_format_to_string(msg, args);
1119+
let message = format_diag_message(
1120+
&msg,
1121+
&DiagArgMap::from_iter([
1122+
("lo".into(), DiagArgValue::Str(lo.to_string().into())),
1123+
("hi".into(), DiagArgValue::Str(hi.to_string().into())),
1124+
]),
1125+
);
11251126
err.arg("in_range", message);
11261127
}
11271128

@@ -1131,19 +1132,18 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
11311132
}
11321133
PointerAsInt { expected } | Uninit { expected } => {
11331134
let msg = match expected {
1134-
ExpectedKind::Reference => msg!("expected a reference"),
1135-
ExpectedKind::Box => msg!("expected a box"),
1136-
ExpectedKind::RawPtr => msg!("expected a raw pointer"),
1137-
ExpectedKind::InitScalar => msg!("expected initialized scalar value"),
1138-
ExpectedKind::Bool => msg!("expected a boolean"),
1139-
ExpectedKind::Char => msg!("expected a unicode scalar value"),
1140-
ExpectedKind::Float => msg!("expected a floating point number"),
1141-
ExpectedKind::Int => msg!("expected an integer"),
1142-
ExpectedKind::FnPtr => msg!("expected a function pointer"),
1143-
ExpectedKind::EnumTag => msg!("expected a valid enum tag"),
1144-
ExpectedKind::Str => msg!("expected a string"),
1135+
ExpectedKind::Reference => "expected a reference",
1136+
ExpectedKind::Box => "expected a box",
1137+
ExpectedKind::RawPtr => "expected a raw pointer",
1138+
ExpectedKind::InitScalar => "expected initialized scalar value",
1139+
ExpectedKind::Bool => "expected a boolean",
1140+
ExpectedKind::Char => "expected a unicode scalar value",
1141+
ExpectedKind::Float => "expected a floating point number",
1142+
ExpectedKind::Int => "expected an integer",
1143+
ExpectedKind::FnPtr => "expected a function pointer",
1144+
ExpectedKind::EnumTag => "expected a valid enum tag",
1145+
ExpectedKind::Str => "expected a string",
11451146
};
1146-
let msg = err.dcx.eagerly_format_to_string(msg, [].into_iter());
11471147
err.arg("expected", msg);
11481148
}
11491149
InvalidEnumTag { value }

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::{Left, Right};
22
use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
33
use rustc_data_structures::debug_assert_matches;
4-
use rustc_errors::{DiagCtxtHandle, msg};
4+
use rustc_errors::{DiagCtxtHandle, format_diag_message, msg};
55
use rustc_hir::def_id::DefId;
66
use rustc_hir::limit::Limit;
77
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
@@ -235,9 +235,9 @@ pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tc
235235
let mut diag = dcx.struct_allow("");
236236
let msg = e.diagnostic_message();
237237
e.add_args(&mut diag);
238-
let s = dcx.eagerly_format_to_string(msg, diag.args.iter());
238+
let msg = format_diag_message(&msg, &diag.args).into_owned();
239239
diag.cancel();
240-
s
240+
msg
241241
}
242242

243243
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,6 @@ impl DiagCtxt {
483483
self.inner.borrow_mut().emitter = emitter;
484484
}
485485

486-
/// Format `message` eagerly with `args` to `String`.
487-
pub fn eagerly_format_to_string<'a>(
488-
&self,
489-
message: DiagMessage,
490-
args: impl Iterator<Item = DiagArg<'a>>,
491-
) -> String {
492-
let inner = self.inner.borrow();
493-
inner.eagerly_format_to_string(message, args)
494-
}
495-
496486
// This is here to not allow mutation of flags;
497487
// as of this writing it's used in Session::consider_optimizing and
498488
// in tests in rustc_interface.
@@ -1408,16 +1398,6 @@ impl DiagCtxtInner {
14081398
self.has_errors().or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14091399
}
14101400

1411-
/// Format `message` eagerly with `args` to `String`.
1412-
fn eagerly_format_to_string<'a>(
1413-
&self,
1414-
message: DiagMessage,
1415-
args: impl Iterator<Item = DiagArg<'a>>,
1416-
) -> String {
1417-
let args = args.map(|(name, val)| (name.clone(), val.clone())).collect();
1418-
format_diag_message(&message, &args).to_string()
1419-
}
1420-
14211401
fn flush_delayed(&mut self) {
14221402
// Stashed diagnostics must be emitted before delayed bugs are flushed.
14231403
// Otherwise, we might ICE prematurely when errors would have

compiler/rustc_resolve/src/errors.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_errors::codes::*;
2+
use rustc_errors::formatting::DiagMessageAddArg;
23
use rustc_errors::{
34
Applicability, Diag, DiagCtxtHandle, DiagMessage, Diagnostic, ElidedLifetimeInPathSubdiag,
45
EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic, msg,
@@ -1364,12 +1365,10 @@ impl Subdiagnostic for FoundItemConfigureOut {
13641365
let mut multispan: MultiSpan = self.span.into();
13651366
match self.item_was {
13661367
ItemWas::BehindFeature { feature, span } => {
1367-
let key = "feature".into();
13681368
let value = feature.into_diag_arg(&mut None);
1369-
let msg = diag.dcx.eagerly_format_to_string(
1370-
msg!("the item is gated behind the `{$feature}` feature"),
1371-
[(&key, &value)].into_iter(),
1372-
);
1369+
let msg = msg!("the item is gated behind the `{$feature}` feature")
1370+
.arg("feature", value)
1371+
.format();
13731372
multispan.push_span_label(span, msg);
13741373
}
13751374
ItemWas::CfgOut { span } => {

0 commit comments

Comments
 (0)