Skip to content

Commit 7024249

Browse files
Dedup Fluent invalid ptr errors in const eval
I believe these forms aren't unbearable to work with, and Fluent's ability to use selectors allows us to avoid enumerating every case laboriously.
1 parent f70779b commit 7024249

File tree

2 files changed

+63
-72
lines changed

2 files changed

+63
-72
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,18 @@ const_eval_upcast_mismatch =
406406
const_eval_validation_box_to_mut = {$front_matter}: encountered a box pointing to mutable memory in a constant
407407
const_eval_validation_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant
408408
const_eval_validation_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty}
409-
const_eval_validation_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance)
410-
const_eval_validation_dangling_box_out_of_bounds = {$front_matter}: encountered a dangling box (going beyond the bounds of its allocation)
411-
const_eval_validation_dangling_box_use_after_free = {$front_matter}: encountered a dangling box (use-after-free)
412-
const_eval_validation_dangling_ref_no_provenance = {$front_matter}: encountered a dangling reference ({$pointer} has no provenance)
413-
const_eval_validation_dangling_ref_out_of_bounds = {$front_matter}: encountered a dangling reference (going beyond the bounds of its allocation)
414-
const_eval_validation_dangling_ref_use_after_free = {$front_matter}: encountered a dangling reference (use-after-free)
409+
const_eval_validation_dangling_no_provenance = {$front_matter}: encountered a dangling {$ptr_kind ->
410+
[box] box
411+
*[ref] reference
412+
} ({$pointer} has no provenance)
413+
const_eval_validation_dangling_out_of_bounds = {$front_matter}: encountered a dangling {$ptr_kind ->
414+
[box] box
415+
*[ref] reference
416+
} (going beyond the bounds of its allocation)
417+
const_eval_validation_dangling_use_after_free = {$front_matter}: encountered a dangling {$ptr_kind ->
418+
[box] box
419+
*[ref] reference
420+
} (use-after-free)
415421
416422
const_eval_validation_expected_bool = expected a boolean
417423
const_eval_validation_expected_box = expected a box
@@ -429,14 +435,10 @@ const_eval_validation_front_matter_invalid_value = constructing invalid value
429435
const_eval_validation_front_matter_invalid_value_with_path = constructing invalid value at {$path}
430436
431437
const_eval_validation_invalid_bool = {$front_matter}: encountered {$value}, but expected a boolean
432-
const_eval_validation_invalid_box_meta = {$front_matter}: encountered invalid box metadata: total size is bigger than largest supported object
433-
const_eval_validation_invalid_box_slice_meta = {$front_matter}: encountered invalid box metadata: slice is bigger than largest supported object
434438
const_eval_validation_invalid_char = {$front_matter}: encountered {$value}, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
435439
436440
const_eval_validation_invalid_enum_tag = {$front_matter}: encountered {$value}, but expected a valid enum tag
437441
const_eval_validation_invalid_fn_ptr = {$front_matter}: encountered {$value}, but expected a function pointer
438-
const_eval_validation_invalid_ref_meta = {$front_matter}: encountered invalid reference metadata: total size is bigger than largest supported object
439-
const_eval_validation_invalid_ref_slice_meta = {$front_matter}: encountered invalid reference metadata: slice is bigger than largest supported object
440442
const_eval_validation_invalid_vtable_ptr = {$front_matter}: encountered {$value}, but expected a vtable pointer
441443
const_eval_validation_mutable_ref_in_const = {$front_matter}: encountered mutable reference in a `const`
442444
const_eval_validation_never_val = {$front_matter}: encountered a value of the never type `!`
@@ -448,11 +450,30 @@ const_eval_validation_out_of_range = {$front_matter}: encountered {$value}, but
448450
const_eval_validation_partial_pointer = {$front_matter}: encountered a partial pointer or a mix of pointers
449451
const_eval_validation_pointer_as_int = {$front_matter}: encountered a pointer, but {$expected}
450452
const_eval_validation_ptr_out_of_range = {$front_matter}: encountered a pointer, but expected something that cannot possibly fail to be {$in_range}
451-
const_eval_validation_ref_to_mut = {$front_matter}: encountered a reference pointing to mutable memory in a constant
452-
const_eval_validation_ref_to_static = {$front_matter}: encountered a reference pointing to a static variable in a constant
453-
const_eval_validation_ref_to_uninhabited = {$front_matter}: encountered a reference pointing to uninhabited type {$ty}
454-
const_eval_validation_unaligned_box = {$front_matter}: encountered an unaligned box (required {$required_bytes} byte alignment but found {$found_bytes})
455-
const_eval_validation_unaligned_ref = {$front_matter}: encountered an unaligned reference (required {$required_bytes} byte alignment but found {$found_bytes})
453+
const_eval_validation_ptr_to_mut = {$front_matter}: encountered a {$ptr_kind ->
454+
[box] box
455+
*[ref] reference
456+
} pointing to mutable memory in a constant
457+
const_eval_validation_ptr_to_static = {$front_matter}: encountered a {$ptr_kind ->
458+
[box] box
459+
*[ref] reference
460+
} pointing to a static variable in a constant
461+
const_eval_validation_ptr_to_uninhabited = {$front_matter}: encountered a {$ptr_kind ->
462+
[box] box
463+
*[ref] reference
464+
} pointing to uninhabited type {$ty}
465+
const_eval_validation_too_big_meta = {$front_matter}: encountered invalid {$ptr_kind ->
466+
[box] box
467+
*[ref] reference
468+
} metadata: total size is bigger than largest supported object
469+
const_eval_validation_too_big_slice_meta = {$front_matter}: {$ptr_kind ->
470+
[box] encountered invalid box metadata: slice is bigger than largest supported object
471+
*[ref] encountered invalid reference metadata: slice is bigger than largest supported object
472+
}
473+
const_eval_validation_unaligned = {$front_matter}: encountered an unaligned {$ptr_kind ->
474+
[box] box
475+
*[ref] reference
476+
} (required {$required_bytes} byte alignment but found {$found_bytes})
456477
const_eval_validation_uninhabited_enum_variant = {$front_matter}: encountered an uninhabited enum variant
457478
const_eval_validation_uninhabited_val = {$front_matter}: encountered a value of uninhabited type `{$ty}`
458479
const_eval_validation_uninit = {$front_matter}: encountered uninitialized memory, but {$expected}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,9 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
610610
use crate::fluent_generated::*;
611611
use rustc_middle::mir::interpret::ValidationErrorKind::*;
612612
match self.kind {
613-
PtrToUninhabited { ptr_kind: PointerKind::Box, .. } => {
614-
const_eval_validation_box_to_uninhabited
615-
}
616-
PtrToUninhabited { ptr_kind: PointerKind::Ref, .. } => {
617-
const_eval_validation_ref_to_uninhabited
618-
}
619-
620-
PtrToStatic { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_static,
621-
PtrToStatic { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_static,
622-
623-
PtrToMut { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_mut,
624-
PtrToMut { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_mut,
625-
613+
PtrToUninhabited { .. } => const_eval_validation_ptr_to_uninhabited,
614+
PtrToStatic { .. } => const_eval_validation_ptr_to_static,
615+
PtrToMut { .. } => const_eval_validation_ptr_to_mut,
626616
PointerAsInt { .. } => const_eval_validation_pointer_as_int,
627617
PartialPointer => const_eval_validation_partial_pointer,
628618
MutableRefInConst => const_eval_validation_mutable_ref_in_const,
@@ -637,42 +627,14 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
637627
UninhabitedEnumVariant => const_eval_validation_uninhabited_enum_variant,
638628
Uninit { .. } => const_eval_validation_uninit,
639629
InvalidVTablePtr { .. } => const_eval_validation_invalid_vtable_ptr,
640-
InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Box } => {
641-
const_eval_validation_invalid_box_slice_meta
642-
}
643-
InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Ref } => {
644-
const_eval_validation_invalid_ref_slice_meta
645-
}
646-
647-
InvalidMetaTooLarge { ptr_kind: PointerKind::Box } => {
648-
const_eval_validation_invalid_box_meta
649-
}
650-
InvalidMetaTooLarge { ptr_kind: PointerKind::Ref } => {
651-
const_eval_validation_invalid_ref_meta
652-
}
653-
UnalignedPtr { ptr_kind: PointerKind::Ref, .. } => const_eval_validation_unaligned_ref,
654-
UnalignedPtr { ptr_kind: PointerKind::Box, .. } => const_eval_validation_unaligned_box,
655-
630+
InvalidMetaSliceTooLarge { .. } => const_eval_validation_too_big_slice_meta,
631+
InvalidMetaTooLarge { .. } => const_eval_validation_too_big_meta,
632+
UnalignedPtr { .. } => const_eval_validation_unaligned,
656633
NullPtr { ptr_kind: PointerKind::Box } => const_eval_validation_null_box,
657634
NullPtr { ptr_kind: PointerKind::Ref } => const_eval_validation_null_ref,
658-
DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => {
659-
const_eval_validation_dangling_box_no_provenance
660-
}
661-
DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => {
662-
const_eval_validation_dangling_ref_no_provenance
663-
}
664-
DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => {
665-
const_eval_validation_dangling_box_out_of_bounds
666-
}
667-
DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => {
668-
const_eval_validation_dangling_ref_out_of_bounds
669-
}
670-
DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => {
671-
const_eval_validation_dangling_box_use_after_free
672-
}
673-
DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => {
674-
const_eval_validation_dangling_ref_use_after_free
675-
}
635+
DanglingPtrNoProvenance { .. } => const_eval_validation_dangling_no_provenance,
636+
DanglingPtrOutOfBounds { .. } => const_eval_validation_dangling_out_of_bounds,
637+
DanglingPtrUseAfterFree { .. } => const_eval_validation_dangling_use_after_free,
676638
InvalidBool { .. } => const_eval_validation_invalid_bool,
677639
InvalidChar { .. } => const_eval_validation_invalid_char,
678640
InvalidFnPtr { .. } => const_eval_validation_invalid_fn_ptr,
@@ -734,7 +696,11 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
734696
}
735697

736698
match self.kind {
737-
PtrToUninhabited { ty, .. } | UninhabitedVal { ty } => {
699+
UninhabitedVal { ty } => {
700+
err.set_arg("ty", ty);
701+
}
702+
PtrToUninhabited { ty, ptr_kind, .. } => {
703+
err.set_arg("ptr_kind", ptr_kind);
738704
err.set_arg("ty", ty);
739705
}
740706
PointerAsInt { expected } | Uninit { expected } => {
@@ -768,24 +734,28 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
768734
err.set_arg("value", value);
769735
add_range_arg(range, max_value, handler, err);
770736
}
771-
UnalignedPtr { required_bytes, found_bytes, .. } => {
772-
err.set_arg("required_bytes", required_bytes);
737+
UnalignedPtr { required_bytes, found_bytes, ptr_kind } => {
773738
err.set_arg("found_bytes", found_bytes);
739+
err.set_arg("ptr_kind", ptr_kind);
740+
err.set_arg("required_bytes", required_bytes);
774741
}
775-
DanglingPtrNoProvenance { pointer, .. } => {
742+
DanglingPtrNoProvenance { pointer, ptr_kind } => {
776743
err.set_arg("pointer", pointer);
744+
err.set_arg("ptr_kind", ptr_kind);
745+
}
746+
DanglingPtrUseAfterFree { ptr_kind }
747+
| DanglingPtrOutOfBounds { ptr_kind }
748+
| InvalidMetaSliceTooLarge { ptr_kind }
749+
| InvalidMetaTooLarge { ptr_kind }
750+
| PtrToStatic { ptr_kind }
751+
| PtrToMut { ptr_kind } => {
752+
err.set_arg("ptr_kind", ptr_kind);
777753
}
778754
NullPtr { .. }
779-
| PtrToStatic { .. }
780-
| PtrToMut { .. }
781755
| MutableRefInConst
782756
| NullFnPtr
783757
| NeverVal
784758
| UnsafeCell
785-
| InvalidMetaSliceTooLarge { .. }
786-
| InvalidMetaTooLarge { .. }
787-
| DanglingPtrUseAfterFree { .. }
788-
| DanglingPtrOutOfBounds { .. }
789759
| UninhabitedEnumVariant
790760
| PartialPointer => {}
791761
}

0 commit comments

Comments
 (0)