Skip to content

Commit 47bb142

Browse files
Initial diagnostics reafactoring
1 parent 3ae0ef7 commit 47bb142

File tree

2 files changed

+212
-1
lines changed

2 files changed

+212
-1
lines changed

compiler/rustc_const_eval/src/errors.rs

Lines changed: 211 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir::interpret::{
99
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
1010
};
1111
use rustc_middle::ty::{self, Ty};
12-
use rustc_span::Span;
12+
use rustc_span::{ErrorGuaranteed, Span};
1313
use rustc_target::abi::call::AdjustForForeignAbiError;
1414
use rustc_target::abi::{Size, WrappingRange};
1515

@@ -476,6 +476,150 @@ fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String {
476476
handler.eagerly_translate_to_string(msg, [].into_iter())
477477
}
478478

479+
pub struct UndefinedBehaviorInfoExt<'a>(UndefinedBehaviorInfo<'a>);
480+
481+
impl IntoDiagnostic<'_> for UndefinedBehaviorInfoExt<'_> {
482+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
483+
use crate::fluent_generated::*;
484+
use UndefinedBehaviorInfo::*;
485+
match self.0 {
486+
#[allow(rustc::untranslatable_diagnostic)]
487+
Ub(str) => handler.struct_diagnostic(str.clone()),
488+
Unreachable => handler.struct_diagnostic(const_eval_unreachable),
489+
BoundsCheckFailed { .. } => handler.struct_diagnostic(const_eval_bounds_check_failed),
490+
DivisionByZero => handler.struct_diagnostic(const_eval_division_by_zero),
491+
RemainderByZero => handler.struct_diagnostic(const_eval_remainder_by_zero),
492+
DivisionOverflow => handler.struct_diagnostic(const_eval_division_overflow),
493+
RemainderOverflow => handler.struct_diagnostic(const_eval_remainder_overflow),
494+
PointerArithOverflow => {
495+
handler.struct_diagnostic(const_eval_pointer_arithmetic_overflow)
496+
}
497+
InvalidMeta(InvalidMetaKind::SliceTooBig) => {
498+
handler.struct_diagnostic(const_eval_invalid_meta_slice)
499+
}
500+
InvalidMeta(InvalidMetaKind::TooBig) => {
501+
handler.struct_diagnostic(const_eval_invalid_meta)
502+
}
503+
UnterminatedCString(_) => handler.struct_diagnostic(const_eval_unterminated_c_string),
504+
PointerUseAfterFree(_) => handler.struct_diagnostic(const_eval_pointer_use_after_free),
505+
PointerOutOfBounds { ptr_size: Size::ZERO, .. } => {
506+
handler.struct_diagnostic(const_eval_zst_pointer_out_of_bounds)
507+
}
508+
PointerOutOfBounds { .. } => {
509+
handler.struct_diagnostic(const_eval_pointer_out_of_bounds)
510+
}
511+
DanglingIntPointer(0, _) => handler.struct_diagnostic(const_eval_dangling_null_pointer),
512+
DanglingIntPointer(..) => handler.struct_diagnostic(const_eval_dangling_int_pointer),
513+
AlignmentCheckFailed { .. } => {
514+
handler.struct_diagnostic(const_eval_alignment_check_failed)
515+
}
516+
WriteToReadOnly(_) => handler.struct_diagnostic(const_eval_write_to_read_only),
517+
DerefFunctionPointer(_) => handler.struct_diagnostic(const_eval_deref_function_pointer),
518+
DerefVTablePointer(_) => handler.struct_diagnostic(const_eval_deref_vtable_pointer),
519+
InvalidBool(_) => handler.struct_diagnostic(const_eval_invalid_bool),
520+
InvalidChar(_) => handler.struct_diagnostic(const_eval_invalid_char),
521+
InvalidTag(_) => handler.struct_diagnostic(const_eval_invalid_tag),
522+
InvalidFunctionPointer(_) => {
523+
handler.struct_diagnostic(const_eval_invalid_function_pointer)
524+
}
525+
InvalidVTablePointer(_) => handler.struct_diagnostic(const_eval_invalid_vtable_pointer),
526+
InvalidStr(_) => handler.struct_diagnostic(const_eval_invalid_str),
527+
InvalidUninitBytes(None) => {
528+
handler.struct_diagnostic(const_eval_invalid_uninit_bytes_unknown)
529+
}
530+
InvalidUninitBytes(Some(_)) => {
531+
handler.struct_diagnostic(const_eval_invalid_uninit_bytes)
532+
}
533+
DeadLocal => handler.struct_diagnostic(const_eval_dead_local),
534+
ScalarSizeMismatch(_) => handler.struct_diagnostic(const_eval_scalar_size_mismatch),
535+
UninhabitedEnumVariantWritten => {
536+
handler.struct_diagnostic(const_eval_uninhabited_enum_variant_written)
537+
}
538+
Validation(e) => ValidationErrorInfoExt(e).into_diagnostic(handler),
539+
Custom(x) => handler.struct_diagnostic((x.msg)()),
540+
}
541+
}
542+
}
543+
544+
pub struct ValidationErrorInfoExt<'tcx>(ValidationErrorInfo<'tcx>);
545+
546+
impl IntoDiagnostic<'_> for ValidationErrorInfoExt<'_> {
547+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
548+
use crate::fluent_generated::*;
549+
use crate::interpret::ValidationErrorKind::*;
550+
let msg = match self.0.kind {
551+
PtrToUninhabited { ptr_kind: PointerKind::Box, .. } => const_eval_box_to_uninhabited,
552+
PtrToUninhabited { ptr_kind: PointerKind::Ref, .. } => const_eval_ref_to_uninhabited,
553+
554+
PtrToStatic { ptr_kind: PointerKind::Box, .. } => const_eval_box_to_static,
555+
PtrToStatic { ptr_kind: PointerKind::Ref, .. } => const_eval_ref_to_static,
556+
557+
PtrToMut { ptr_kind: PointerKind::Box, .. } => const_eval_box_to_mut,
558+
PtrToMut { ptr_kind: PointerKind::Ref, .. } => const_eval_ref_to_mut,
559+
560+
ExpectedNonPtr { .. } => const_eval_expected_non_ptr,
561+
MutableRefInConst => const_eval_mutable_ref_in_const,
562+
NullFnPtr => const_eval_null_fn_ptr,
563+
NeverVal => const_eval_never_val,
564+
NullablePtrOutOfRange { .. } => const_eval_nullable_ptr_out_of_range,
565+
PtrOutOfRange { .. } => const_eval_ptr_out_of_range,
566+
OutOfRange { .. } => const_eval_out_of_range,
567+
UnsafeCell => const_eval_unsafe_cell,
568+
UninhabitedVal { .. } => const_eval_uninhabited_val,
569+
InvalidEnumTag { .. } => const_eval_invalid_enum_tag,
570+
UninitEnumTag => const_eval_uninit_enum_tag,
571+
UninitStr => const_eval_uninit_str,
572+
Uninit { expected: ExpectedKind::Bool } => const_eval_uninit_bool,
573+
Uninit { expected: ExpectedKind::Reference } => const_eval_uninit_ref,
574+
Uninit { expected: ExpectedKind::Box } => const_eval_uninit_box,
575+
Uninit { expected: ExpectedKind::RawPtr } => const_eval_uninit_raw_ptr,
576+
Uninit { expected: ExpectedKind::InitScalar } => const_eval_uninit_init_scalar,
577+
Uninit { expected: ExpectedKind::Char } => const_eval_uninit_char,
578+
Uninit { expected: ExpectedKind::Float } => const_eval_uninit_float,
579+
Uninit { expected: ExpectedKind::Int } => const_eval_uninit_int,
580+
Uninit { expected: ExpectedKind::FnPtr } => const_eval_uninit_fn_ptr,
581+
UninitVal => const_eval_uninit,
582+
InvalidVTablePtr { .. } => const_eval_invalid_vtable_ptr,
583+
InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Box } => {
584+
const_eval_invalid_box_slice_meta
585+
}
586+
InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Ref } => {
587+
const_eval_invalid_ref_slice_meta
588+
}
589+
590+
InvalidMetaTooLarge { ptr_kind: PointerKind::Box } => const_eval_invalid_box_meta,
591+
InvalidMetaTooLarge { ptr_kind: PointerKind::Ref } => const_eval_invalid_ref_meta,
592+
UnalignedPtr { ptr_kind: PointerKind::Ref, .. } => const_eval_unaligned_ref,
593+
UnalignedPtr { ptr_kind: PointerKind::Box, .. } => const_eval_unaligned_box,
594+
595+
NullPtr { ptr_kind: PointerKind::Box } => const_eval_null_box,
596+
NullPtr { ptr_kind: PointerKind::Ref } => const_eval_null_ref,
597+
DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => {
598+
const_eval_dangling_box_no_provenance
599+
}
600+
DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => {
601+
const_eval_dangling_ref_no_provenance
602+
}
603+
DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => {
604+
const_eval_dangling_box_out_of_bounds
605+
}
606+
DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => {
607+
const_eval_dangling_ref_out_of_bounds
608+
}
609+
DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => {
610+
const_eval_dangling_box_use_after_free
611+
}
612+
DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => {
613+
const_eval_dangling_ref_use_after_free
614+
}
615+
InvalidBool { .. } => const_eval_validation_invalid_bool,
616+
InvalidChar { .. } => const_eval_validation_invalid_char,
617+
InvalidFnPtr { .. } => const_eval_invalid_fn_ptr,
618+
};
619+
handler.struct_diagnostic(msg)
620+
}
621+
}
622+
479623
impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
480624
fn diagnostic_message(&self) -> DiagnosticMessage {
481625
use crate::fluent_generated::*;
@@ -797,6 +941,39 @@ impl ReportErrorExt for UnsupportedOpInfo {
797941
}
798942
}
799943

944+
pub struct UnsupportedExt(UnsupportedOpInfo);
945+
946+
impl IntoDiagnostic<'_> for UnsupportedExt {
947+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
948+
use crate::fluent_generated::*;
949+
let msg = match self.0 {
950+
UnsupportedOpInfo::Unsupported(s) => s.clone().into(),
951+
UnsupportedOpInfo::PartialPointerOverwrite(_) => const_eval_partial_pointer_overwrite,
952+
UnsupportedOpInfo::PartialPointerCopy(_) => const_eval_partial_pointer_copy,
953+
UnsupportedOpInfo::ReadPointerAsBytes => const_eval_read_pointer_as_bytes,
954+
UnsupportedOpInfo::ThreadLocalStatic(_) => const_eval_thread_local_static,
955+
UnsupportedOpInfo::ReadExternStatic(_) => const_eval_read_extern_static,
956+
};
957+
handler.struct_diagnostic(msg)
958+
}
959+
}
960+
961+
pub struct InterpErrorExt<'a>(pub InterpError<'a>);
962+
963+
impl IntoDiagnostic<'_> for InterpErrorExt<'_> {
964+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
965+
match self.0 {
966+
InterpError::UndefinedBehavior(ub) => {
967+
UndefinedBehaviorInfoExt(ub).into_diagnostic(handler)
968+
}
969+
InterpError::Unsupported(e) => UnsupportedExt(e).into_diagnostic(handler),
970+
InterpError::InvalidProgram(e) => InvalidProgramInfoExt(e).into_diagnostic(handler),
971+
InterpError::ResourceExhaustion(e) => ResourceExhaustionExt(e).into_diagnostic(handler),
972+
InterpError::MachineStop(_) => todo!("machine stop"),
973+
}
974+
}
975+
}
976+
800977
impl<'tcx> ReportErrorExt for InterpError<'tcx> {
801978
fn diagnostic_message(&self) -> DiagnosticMessage {
802979
match self {
@@ -824,6 +1001,25 @@ impl<'tcx> ReportErrorExt for InterpError<'tcx> {
8241001
}
8251002
}
8261003

1004+
pub struct InvalidProgramInfoExt<'a>(InvalidProgramInfo<'a>);
1005+
1006+
impl IntoDiagnostic<'_> for InvalidProgramInfoExt<'_> {
1007+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
1008+
use crate::fluent_generated::*;
1009+
let msg = match self.0 {
1010+
InvalidProgramInfo::TooGeneric => const_eval_too_generic,
1011+
InvalidProgramInfo::AlreadyReported(_) => const_eval_already_reported,
1012+
InvalidProgramInfo::Layout(e) => e.diagnostic_message(),
1013+
InvalidProgramInfo::FnAbiAdjustForForeignAbi(_) => {
1014+
rustc_middle::error::middle_adjust_for_foreign_abi_error
1015+
}
1016+
InvalidProgramInfo::SizeOfUnsizedType(_) => const_eval_size_of_unsized,
1017+
InvalidProgramInfo::UninitUnsizedLocal => const_eval_uninit_unsized_local,
1018+
};
1019+
handler.struct_diagnostic(msg)
1020+
}
1021+
}
1022+
8271023
impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> {
8281024
fn diagnostic_message(&self) -> DiagnosticMessage {
8291025
use crate::fluent_generated::*;
@@ -878,3 +1074,17 @@ impl ReportErrorExt for ResourceExhaustionInfo {
8781074
}
8791075
fn add_args<G: EmissionGuarantee>(self, _: &Handler, _: &mut DiagnosticBuilder<'_, G>) {}
8801076
}
1077+
1078+
pub struct ResourceExhaustionExt(ResourceExhaustionInfo);
1079+
1080+
impl IntoDiagnostic<'_> for ResourceExhaustionExt {
1081+
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
1082+
use crate::fluent_generated::*;
1083+
let msg = match self.0 {
1084+
ResourceExhaustionInfo::StackFrameLimitReached => const_eval_stack_frame_limit_reached,
1085+
ResourceExhaustionInfo::MemoryExhausted => const_eval_memory_exhausted,
1086+
ResourceExhaustionInfo::AddressSpaceFull => const_eval_address_space_full,
1087+
};
1088+
handler.struct_diagnostic(msg)
1089+
}
1090+
}

compiler/rustc_const_eval/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod interpret;
3434
pub mod transform;
3535
pub mod util;
3636

37+
pub use errors::InterpErrorExt;
3738
pub use errors::ReportErrorExt;
3839

3940
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};

0 commit comments

Comments
 (0)