Skip to content

Commit 79c70d2

Browse files
committed
Remove dcx arg from ReportErrorExt::add_args.
Because it also has a `DiagnosticBuilder` arg, which contains a `dcx` reference. Also rename some `builder` variables as `diag`, because that's the usual name.
1 parent e5df8a6 commit 79c70d2

File tree

3 files changed

+61
-75
lines changed

3 files changed

+61
-75
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ where
151151
let mut err = tcx.dcx().create_err(err);
152152

153153
let msg = error.diagnostic_message();
154-
error.add_args(tcx.dcx(), &mut err);
154+
error.add_args(&mut err);
155155

156156
// Use *our* span to label the interp error
157157
err.span_label(our_span, msg);

compiler/rustc_const_eval/src/errors.rs

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,19 @@ pub struct UndefinedBehavior {
438438
pub trait ReportErrorExt {
439439
/// Returns the diagnostic message for this error.
440440
fn diagnostic_message(&self) -> DiagnosticMessage;
441-
fn add_args<G: EmissionGuarantee>(self, dcx: &DiagCtxt, builder: &mut DiagnosticBuilder<'_, G>);
441+
fn add_args<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>);
442442

443443
fn debug(self) -> String
444444
where
445445
Self: Sized,
446446
{
447447
ty::tls::with(move |tcx| {
448448
let dcx = tcx.dcx();
449-
let mut builder = dcx.struct_allow(DiagnosticMessage::Str(String::new().into()));
449+
let mut diag = dcx.struct_allow(DiagnosticMessage::Str(String::new().into()));
450450
let message = self.diagnostic_message();
451-
self.add_args(dcx, &mut builder);
452-
let s = dcx.eagerly_translate_to_string(message, builder.args());
453-
builder.cancel();
451+
self.add_args(&mut diag);
452+
let s = dcx.eagerly_translate_to_string(message, diag.args());
453+
diag.cancel();
454454
s
455455
})
456456
}
@@ -514,20 +514,17 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
514514
}
515515
}
516516

517-
fn add_args<G: EmissionGuarantee>(
518-
self,
519-
dcx: &DiagCtxt,
520-
builder: &mut DiagnosticBuilder<'_, G>,
521-
) {
517+
fn add_args<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>) {
522518
use UndefinedBehaviorInfo::*;
519+
let dcx = diag.dcx;
523520
match self {
524521
Ub(_) => {}
525522
Custom(custom) => {
526523
(custom.add_args)(&mut |name, value| {
527-
builder.arg(name, value);
524+
diag.arg(name, value);
528525
});
529526
}
530-
ValidationError(e) => e.add_args(dcx, builder),
527+
ValidationError(e) => e.add_args(diag),
531528

532529
Unreachable
533530
| DivisionByZero
@@ -542,65 +539,63 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
542539
| UninhabitedEnumVariantWritten(_)
543540
| UninhabitedEnumVariantRead(_) => {}
544541
BoundsCheckFailed { len, index } => {
545-
builder.arg("len", len);
546-
builder.arg("index", index);
542+
diag.arg("len", len);
543+
diag.arg("index", index);
547544
}
548545
UnterminatedCString(ptr) | InvalidFunctionPointer(ptr) | InvalidVTablePointer(ptr) => {
549-
builder.arg("pointer", ptr);
546+
diag.arg("pointer", ptr);
550547
}
551548
PointerUseAfterFree(alloc_id, msg) => {
552-
builder
553-
.arg("alloc_id", alloc_id)
549+
diag.arg("alloc_id", alloc_id)
554550
.arg("bad_pointer_message", bad_pointer_message(msg, dcx));
555551
}
556552
PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, ptr_size, msg } => {
557-
builder
558-
.arg("alloc_id", alloc_id)
553+
diag.arg("alloc_id", alloc_id)
559554
.arg("alloc_size", alloc_size.bytes())
560555
.arg("ptr_offset", ptr_offset)
561556
.arg("ptr_size", ptr_size.bytes())
562557
.arg("bad_pointer_message", bad_pointer_message(msg, dcx));
563558
}
564559
DanglingIntPointer(ptr, msg) => {
565560
if ptr != 0 {
566-
builder.arg("pointer", format!("{ptr:#x}[noalloc]"));
561+
diag.arg("pointer", format!("{ptr:#x}[noalloc]"));
567562
}
568563

569-
builder.arg("bad_pointer_message", bad_pointer_message(msg, dcx));
564+
diag.arg("bad_pointer_message", bad_pointer_message(msg, dcx));
570565
}
571566
AlignmentCheckFailed(Misalignment { required, has }, msg) => {
572-
builder.arg("required", required.bytes());
573-
builder.arg("has", has.bytes());
574-
builder.arg("msg", format!("{msg:?}"));
567+
diag.arg("required", required.bytes());
568+
diag.arg("has", has.bytes());
569+
diag.arg("msg", format!("{msg:?}"));
575570
}
576571
WriteToReadOnly(alloc) | DerefFunctionPointer(alloc) | DerefVTablePointer(alloc) => {
577-
builder.arg("allocation", alloc);
572+
diag.arg("allocation", alloc);
578573
}
579574
InvalidBool(b) => {
580-
builder.arg("value", format!("{b:02x}"));
575+
diag.arg("value", format!("{b:02x}"));
581576
}
582577
InvalidChar(c) => {
583-
builder.arg("value", format!("{c:08x}"));
578+
diag.arg("value", format!("{c:08x}"));
584579
}
585580
InvalidTag(tag) => {
586-
builder.arg("tag", format!("{tag:x}"));
581+
diag.arg("tag", format!("{tag:x}"));
587582
}
588583
InvalidStr(err) => {
589-
builder.arg("err", format!("{err}"));
584+
diag.arg("err", format!("{err}"));
590585
}
591586
InvalidUninitBytes(Some((alloc, info))) => {
592-
builder.arg("alloc", alloc);
593-
builder.arg("access", info.access);
594-
builder.arg("uninit", info.bad);
587+
diag.arg("alloc", alloc);
588+
diag.arg("access", info.access);
589+
diag.arg("uninit", info.bad);
595590
}
596591
ScalarSizeMismatch(info) => {
597-
builder.arg("target_size", info.target_size);
598-
builder.arg("data_size", info.data_size);
592+
diag.arg("target_size", info.target_size);
593+
diag.arg("data_size", info.data_size);
599594
}
600595
AbiMismatchArgument { caller_ty, callee_ty }
601596
| AbiMismatchReturn { caller_ty, callee_ty } => {
602-
builder.arg("caller_ty", caller_ty.to_string());
603-
builder.arg("callee_ty", callee_ty.to_string());
597+
diag.arg("caller_ty", caller_ty.to_string());
598+
diag.arg("callee_ty", callee_ty.to_string());
604599
}
605600
}
606601
}
@@ -678,7 +673,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
678673
}
679674
}
680675

681-
fn add_args<G: EmissionGuarantee>(self, dcx: &DiagCtxt, err: &mut DiagnosticBuilder<'_, G>) {
676+
fn add_args<G: EmissionGuarantee>(self, err: &mut DiagnosticBuilder<'_, G>) {
682677
use crate::fluent_generated as fluent;
683678
use rustc_middle::mir::interpret::ValidationErrorKind::*;
684679

@@ -688,12 +683,12 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
688683
}
689684

690685
let message = if let Some(path) = self.path {
691-
dcx.eagerly_translate_to_string(
686+
err.dcx.eagerly_translate_to_string(
692687
fluent::const_eval_validation_front_matter_invalid_value_with_path,
693688
[("path".into(), DiagnosticArgValue::Str(path.into()))].iter().map(|(a, b)| (a, b)),
694689
)
695690
} else {
696-
dcx.eagerly_translate_to_string(
691+
err.dcx.eagerly_translate_to_string(
697692
fluent::const_eval_validation_front_matter_invalid_value,
698693
[].into_iter(),
699694
)
@@ -704,7 +699,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
704699
fn add_range_arg<G: EmissionGuarantee>(
705700
r: WrappingRange,
706701
max_hi: u128,
707-
dcx: &DiagCtxt,
708702
err: &mut DiagnosticBuilder<'_, G>,
709703
) {
710704
let WrappingRange { start: lo, end: hi } = r;
@@ -728,7 +722,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
728722
("hi".into(), DiagnosticArgValue::Str(hi.to_string().into())),
729723
];
730724
let args = args.iter().map(|(a, b)| (a, b));
731-
let message = dcx.eagerly_translate_to_string(msg, args);
725+
let message = err.dcx.eagerly_translate_to_string(msg, args);
732726
err.arg("in_range", message);
733727
}
734728

@@ -750,7 +744,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
750744
ExpectedKind::EnumTag => fluent::const_eval_validation_expected_enum_tag,
751745
ExpectedKind::Str => fluent::const_eval_validation_expected_str,
752746
};
753-
let msg = dcx.eagerly_translate_to_string(msg, [].into_iter());
747+
let msg = err.dcx.eagerly_translate_to_string(msg, [].into_iter());
754748
err.arg("expected", msg);
755749
}
756750
InvalidEnumTag { value }
@@ -761,11 +755,11 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
761755
err.arg("value", value);
762756
}
763757
NullablePtrOutOfRange { range, max_value } | PtrOutOfRange { range, max_value } => {
764-
add_range_arg(range, max_value, dcx, err)
758+
add_range_arg(range, max_value, err)
765759
}
766760
OutOfRange { range, max_value, value } => {
767761
err.arg("value", value);
768-
add_range_arg(range, max_value, dcx, err);
762+
add_range_arg(range, max_value, err);
769763
}
770764
UnalignedPtr { required_bytes, found_bytes, .. } => {
771765
err.arg("required_bytes", required_bytes);
@@ -804,24 +798,24 @@ impl ReportErrorExt for UnsupportedOpInfo {
804798
UnsupportedOpInfo::ReadExternStatic(_) => const_eval_read_extern_static,
805799
}
806800
}
807-
fn add_args<G: EmissionGuarantee>(self, _: &DiagCtxt, builder: &mut DiagnosticBuilder<'_, G>) {
801+
fn add_args<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>) {
808802
use crate::fluent_generated::*;
809803

810804
use UnsupportedOpInfo::*;
811805
if let ReadPointerAsInt(_) | OverwritePartialPointer(_) | ReadPartialPointer(_) = self {
812-
builder.help(const_eval_ptr_as_bytes_1);
813-
builder.help(const_eval_ptr_as_bytes_2);
806+
diag.help(const_eval_ptr_as_bytes_1);
807+
diag.help(const_eval_ptr_as_bytes_2);
814808
}
815809
match self {
816810
// `ReadPointerAsInt(Some(info))` is never printed anyway, it only serves as an error to
817811
// be further processed by validity checking which then turns it into something nice to
818812
// print. So it's not worth the effort of having diagnostics that can print the `info`.
819813
UnsizedLocal | Unsupported(_) | ReadPointerAsInt(_) => {}
820814
OverwritePartialPointer(ptr) | ReadPartialPointer(ptr) => {
821-
builder.arg("ptr", ptr);
815+
diag.arg("ptr", ptr);
822816
}
823817
ThreadLocalStatic(did) | ReadExternStatic(did) => {
824-
builder.arg("did", format!("{did:?}"));
818+
diag.arg("did", format!("{did:?}"));
825819
}
826820
}
827821
}
@@ -837,18 +831,14 @@ impl<'tcx> ReportErrorExt for InterpError<'tcx> {
837831
InterpError::MachineStop(e) => e.diagnostic_message(),
838832
}
839833
}
840-
fn add_args<G: EmissionGuarantee>(
841-
self,
842-
dcx: &DiagCtxt,
843-
builder: &mut DiagnosticBuilder<'_, G>,
844-
) {
834+
fn add_args<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>) {
845835
match self {
846-
InterpError::UndefinedBehavior(ub) => ub.add_args(dcx, builder),
847-
InterpError::Unsupported(e) => e.add_args(dcx, builder),
848-
InterpError::InvalidProgram(e) => e.add_args(dcx, builder),
849-
InterpError::ResourceExhaustion(e) => e.add_args(dcx, builder),
836+
InterpError::UndefinedBehavior(ub) => ub.add_args(diag),
837+
InterpError::Unsupported(e) => e.add_args(diag),
838+
InterpError::InvalidProgram(e) => e.add_args(diag),
839+
InterpError::ResourceExhaustion(e) => e.add_args(diag),
850840
InterpError::MachineStop(e) => e.add_args(&mut |name, value| {
851-
builder.arg(name, value);
841+
diag.arg(name, value);
852842
}),
853843
}
854844
}
@@ -866,28 +856,24 @@ impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> {
866856
}
867857
}
868858
}
869-
fn add_args<G: EmissionGuarantee>(
870-
self,
871-
dcx: &DiagCtxt,
872-
builder: &mut DiagnosticBuilder<'_, G>,
873-
) {
859+
fn add_args<G: EmissionGuarantee>(self, diag: &mut DiagnosticBuilder<'_, G>) {
874860
match self {
875861
InvalidProgramInfo::TooGeneric | InvalidProgramInfo::AlreadyReported(_) => {}
876862
InvalidProgramInfo::Layout(e) => {
877-
// The level doesn't matter, `diag` is consumed without it being used.
863+
// The level doesn't matter, `dummy_diag` is consumed without it being used.
878864
let dummy_level = Level::Bug;
879-
let diag: DiagnosticBuilder<'_, ()> =
880-
e.into_diagnostic().into_diagnostic(dcx, dummy_level);
881-
for (name, val) in diag.args() {
882-
builder.arg(name.clone(), val.clone());
865+
let dummy_diag: DiagnosticBuilder<'_, ()> =
866+
e.into_diagnostic().into_diagnostic(diag.dcx, dummy_level);
867+
for (name, val) in dummy_diag.args() {
868+
diag.arg(name.clone(), val.clone());
883869
}
884-
diag.cancel();
870+
dummy_diag.cancel();
885871
}
886872
InvalidProgramInfo::FnAbiAdjustForForeignAbi(
887873
AdjustForForeignAbiError::Unsupported { arch, abi },
888874
) => {
889-
builder.arg("arch", arch);
890-
builder.arg("abi", abi.name());
875+
diag.arg("arch", arch);
876+
diag.arg("abi", abi.name());
891877
}
892878
}
893879
}
@@ -902,7 +888,7 @@ impl ReportErrorExt for ResourceExhaustionInfo {
902888
ResourceExhaustionInfo::AddressSpaceFull => const_eval_address_space_full,
903889
}
904890
}
905-
fn add_args<G: EmissionGuarantee>(self, _: &DiagCtxt, _: &mut DiagnosticBuilder<'_, G>) {}
891+
fn add_args<G: EmissionGuarantee>(self, _: &mut DiagnosticBuilder<'_, G>) {}
906892
}
907893

908894
impl rustc_errors::IntoDiagnosticArg for InternKind {

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
477477
#[allow(rustc::untranslatable_diagnostic)]
478478
let mut diag = dcx.struct_allow("");
479479
let msg = e.diagnostic_message();
480-
e.add_args(dcx, &mut diag);
480+
e.add_args(&mut diag);
481481
let s = dcx.eagerly_translate_to_string(msg, diag.args());
482482
diag.cancel();
483483
s

0 commit comments

Comments
 (0)