From 7024249f27b58a7995e357728f4418a54146cc04 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 15 Oct 2023 17:52:44 -0700 Subject: [PATCH 1/3] 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. --- compiler/rustc_const_eval/messages.ftl | 51 ++++++++++----- compiler/rustc_const_eval/src/errors.rs | 84 ++++++++----------------- 2 files changed, 63 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index d23e2a9f3e4e0..f2dc47601d56b 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -406,12 +406,18 @@ const_eval_upcast_mismatch = const_eval_validation_box_to_mut = {$front_matter}: encountered a box pointing to mutable memory in a constant const_eval_validation_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant const_eval_validation_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty} -const_eval_validation_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance) -const_eval_validation_dangling_box_out_of_bounds = {$front_matter}: encountered a dangling box (going beyond the bounds of its allocation) -const_eval_validation_dangling_box_use_after_free = {$front_matter}: encountered a dangling box (use-after-free) -const_eval_validation_dangling_ref_no_provenance = {$front_matter}: encountered a dangling reference ({$pointer} has no provenance) -const_eval_validation_dangling_ref_out_of_bounds = {$front_matter}: encountered a dangling reference (going beyond the bounds of its allocation) -const_eval_validation_dangling_ref_use_after_free = {$front_matter}: encountered a dangling reference (use-after-free) +const_eval_validation_dangling_no_provenance = {$front_matter}: encountered a dangling {$ptr_kind -> + [box] box + *[ref] reference +} ({$pointer} has no provenance) +const_eval_validation_dangling_out_of_bounds = {$front_matter}: encountered a dangling {$ptr_kind -> + [box] box + *[ref] reference +} (going beyond the bounds of its allocation) +const_eval_validation_dangling_use_after_free = {$front_matter}: encountered a dangling {$ptr_kind -> + [box] box + *[ref] reference +} (use-after-free) const_eval_validation_expected_bool = expected a boolean const_eval_validation_expected_box = expected a box @@ -429,14 +435,10 @@ const_eval_validation_front_matter_invalid_value = constructing invalid value const_eval_validation_front_matter_invalid_value_with_path = constructing invalid value at {$path} const_eval_validation_invalid_bool = {$front_matter}: encountered {$value}, but expected a boolean -const_eval_validation_invalid_box_meta = {$front_matter}: encountered invalid box metadata: total size is bigger than largest supported object -const_eval_validation_invalid_box_slice_meta = {$front_matter}: encountered invalid box metadata: slice is bigger than largest supported object const_eval_validation_invalid_char = {$front_matter}: encountered {$value}, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) const_eval_validation_invalid_enum_tag = {$front_matter}: encountered {$value}, but expected a valid enum tag const_eval_validation_invalid_fn_ptr = {$front_matter}: encountered {$value}, but expected a function pointer -const_eval_validation_invalid_ref_meta = {$front_matter}: encountered invalid reference metadata: total size is bigger than largest supported object -const_eval_validation_invalid_ref_slice_meta = {$front_matter}: encountered invalid reference metadata: slice is bigger than largest supported object const_eval_validation_invalid_vtable_ptr = {$front_matter}: encountered {$value}, but expected a vtable pointer const_eval_validation_mutable_ref_in_const = {$front_matter}: encountered mutable reference in a `const` 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 const_eval_validation_partial_pointer = {$front_matter}: encountered a partial pointer or a mix of pointers const_eval_validation_pointer_as_int = {$front_matter}: encountered a pointer, but {$expected} const_eval_validation_ptr_out_of_range = {$front_matter}: encountered a pointer, but expected something that cannot possibly fail to be {$in_range} -const_eval_validation_ref_to_mut = {$front_matter}: encountered a reference pointing to mutable memory in a constant -const_eval_validation_ref_to_static = {$front_matter}: encountered a reference pointing to a static variable in a constant -const_eval_validation_ref_to_uninhabited = {$front_matter}: encountered a reference pointing to uninhabited type {$ty} -const_eval_validation_unaligned_box = {$front_matter}: encountered an unaligned box (required {$required_bytes} byte alignment but found {$found_bytes}) -const_eval_validation_unaligned_ref = {$front_matter}: encountered an unaligned reference (required {$required_bytes} byte alignment but found {$found_bytes}) +const_eval_validation_ptr_to_mut = {$front_matter}: encountered a {$ptr_kind -> + [box] box + *[ref] reference +} pointing to mutable memory in a constant +const_eval_validation_ptr_to_static = {$front_matter}: encountered a {$ptr_kind -> + [box] box + *[ref] reference +} pointing to a static variable in a constant +const_eval_validation_ptr_to_uninhabited = {$front_matter}: encountered a {$ptr_kind -> + [box] box + *[ref] reference +} pointing to uninhabited type {$ty} +const_eval_validation_too_big_meta = {$front_matter}: encountered invalid {$ptr_kind -> + [box] box + *[ref] reference +} metadata: total size is bigger than largest supported object +const_eval_validation_too_big_slice_meta = {$front_matter}: {$ptr_kind -> + [box] encountered invalid box metadata: slice is bigger than largest supported object + *[ref] encountered invalid reference metadata: slice is bigger than largest supported object +} +const_eval_validation_unaligned = {$front_matter}: encountered an unaligned {$ptr_kind -> + [box] box + *[ref] reference +} (required {$required_bytes} byte alignment but found {$found_bytes}) const_eval_validation_uninhabited_enum_variant = {$front_matter}: encountered an uninhabited enum variant const_eval_validation_uninhabited_val = {$front_matter}: encountered a value of uninhabited type `{$ty}` const_eval_validation_uninit = {$front_matter}: encountered uninitialized memory, but {$expected} diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index b1599dd689482..7cc27296edbb7 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -610,19 +610,9 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { use crate::fluent_generated::*; use rustc_middle::mir::interpret::ValidationErrorKind::*; match self.kind { - PtrToUninhabited { ptr_kind: PointerKind::Box, .. } => { - const_eval_validation_box_to_uninhabited - } - PtrToUninhabited { ptr_kind: PointerKind::Ref, .. } => { - const_eval_validation_ref_to_uninhabited - } - - PtrToStatic { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_static, - PtrToStatic { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_static, - - PtrToMut { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_mut, - PtrToMut { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_mut, - + PtrToUninhabited { .. } => const_eval_validation_ptr_to_uninhabited, + PtrToStatic { .. } => const_eval_validation_ptr_to_static, + PtrToMut { .. } => const_eval_validation_ptr_to_mut, PointerAsInt { .. } => const_eval_validation_pointer_as_int, PartialPointer => const_eval_validation_partial_pointer, MutableRefInConst => const_eval_validation_mutable_ref_in_const, @@ -637,42 +627,14 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { UninhabitedEnumVariant => const_eval_validation_uninhabited_enum_variant, Uninit { .. } => const_eval_validation_uninit, InvalidVTablePtr { .. } => const_eval_validation_invalid_vtable_ptr, - InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Box } => { - const_eval_validation_invalid_box_slice_meta - } - InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Ref } => { - const_eval_validation_invalid_ref_slice_meta - } - - InvalidMetaTooLarge { ptr_kind: PointerKind::Box } => { - const_eval_validation_invalid_box_meta - } - InvalidMetaTooLarge { ptr_kind: PointerKind::Ref } => { - const_eval_validation_invalid_ref_meta - } - UnalignedPtr { ptr_kind: PointerKind::Ref, .. } => const_eval_validation_unaligned_ref, - UnalignedPtr { ptr_kind: PointerKind::Box, .. } => const_eval_validation_unaligned_box, - + InvalidMetaSliceTooLarge { .. } => const_eval_validation_too_big_slice_meta, + InvalidMetaTooLarge { .. } => const_eval_validation_too_big_meta, + UnalignedPtr { .. } => const_eval_validation_unaligned, NullPtr { ptr_kind: PointerKind::Box } => const_eval_validation_null_box, NullPtr { ptr_kind: PointerKind::Ref } => const_eval_validation_null_ref, - DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => { - const_eval_validation_dangling_box_no_provenance - } - DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => { - const_eval_validation_dangling_ref_no_provenance - } - DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => { - const_eval_validation_dangling_box_out_of_bounds - } - DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => { - const_eval_validation_dangling_ref_out_of_bounds - } - DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => { - const_eval_validation_dangling_box_use_after_free - } - DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => { - const_eval_validation_dangling_ref_use_after_free - } + DanglingPtrNoProvenance { .. } => const_eval_validation_dangling_no_provenance, + DanglingPtrOutOfBounds { .. } => const_eval_validation_dangling_out_of_bounds, + DanglingPtrUseAfterFree { .. } => const_eval_validation_dangling_use_after_free, InvalidBool { .. } => const_eval_validation_invalid_bool, InvalidChar { .. } => const_eval_validation_invalid_char, InvalidFnPtr { .. } => const_eval_validation_invalid_fn_ptr, @@ -734,7 +696,11 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { } match self.kind { - PtrToUninhabited { ty, .. } | UninhabitedVal { ty } => { + UninhabitedVal { ty } => { + err.set_arg("ty", ty); + } + PtrToUninhabited { ty, ptr_kind, .. } => { + err.set_arg("ptr_kind", ptr_kind); err.set_arg("ty", ty); } PointerAsInt { expected } | Uninit { expected } => { @@ -768,24 +734,28 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { err.set_arg("value", value); add_range_arg(range, max_value, handler, err); } - UnalignedPtr { required_bytes, found_bytes, .. } => { - err.set_arg("required_bytes", required_bytes); + UnalignedPtr { required_bytes, found_bytes, ptr_kind } => { err.set_arg("found_bytes", found_bytes); + err.set_arg("ptr_kind", ptr_kind); + err.set_arg("required_bytes", required_bytes); } - DanglingPtrNoProvenance { pointer, .. } => { + DanglingPtrNoProvenance { pointer, ptr_kind } => { err.set_arg("pointer", pointer); + err.set_arg("ptr_kind", ptr_kind); + } + DanglingPtrUseAfterFree { ptr_kind } + | DanglingPtrOutOfBounds { ptr_kind } + | InvalidMetaSliceTooLarge { ptr_kind } + | InvalidMetaTooLarge { ptr_kind } + | PtrToStatic { ptr_kind } + | PtrToMut { ptr_kind } => { + err.set_arg("ptr_kind", ptr_kind); } NullPtr { .. } - | PtrToStatic { .. } - | PtrToMut { .. } | MutableRefInConst | NullFnPtr | NeverVal | UnsafeCell - | InvalidMetaSliceTooLarge { .. } - | InvalidMetaTooLarge { .. } - | DanglingPtrUseAfterFree { .. } - | DanglingPtrOutOfBounds { .. } | UninhabitedEnumVariant | PartialPointer => {} } From 78d37dcfec88a769262cf96b5d56039f446db68c Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 15 Oct 2023 20:47:22 -0700 Subject: [PATCH 2/3] Revert "Dedup Fluent invalid ptr errors in const eval" This reverts commit 7024249f27b58a7995e357728f4418a54146cc04. --- compiler/rustc_const_eval/messages.ftl | 51 +++++---------- compiler/rustc_const_eval/src/errors.rs | 84 +++++++++++++++++-------- 2 files changed, 72 insertions(+), 63 deletions(-) diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index f2dc47601d56b..d23e2a9f3e4e0 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -406,18 +406,12 @@ const_eval_upcast_mismatch = const_eval_validation_box_to_mut = {$front_matter}: encountered a box pointing to mutable memory in a constant const_eval_validation_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant const_eval_validation_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty} -const_eval_validation_dangling_no_provenance = {$front_matter}: encountered a dangling {$ptr_kind -> - [box] box - *[ref] reference -} ({$pointer} has no provenance) -const_eval_validation_dangling_out_of_bounds = {$front_matter}: encountered a dangling {$ptr_kind -> - [box] box - *[ref] reference -} (going beyond the bounds of its allocation) -const_eval_validation_dangling_use_after_free = {$front_matter}: encountered a dangling {$ptr_kind -> - [box] box - *[ref] reference -} (use-after-free) +const_eval_validation_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance) +const_eval_validation_dangling_box_out_of_bounds = {$front_matter}: encountered a dangling box (going beyond the bounds of its allocation) +const_eval_validation_dangling_box_use_after_free = {$front_matter}: encountered a dangling box (use-after-free) +const_eval_validation_dangling_ref_no_provenance = {$front_matter}: encountered a dangling reference ({$pointer} has no provenance) +const_eval_validation_dangling_ref_out_of_bounds = {$front_matter}: encountered a dangling reference (going beyond the bounds of its allocation) +const_eval_validation_dangling_ref_use_after_free = {$front_matter}: encountered a dangling reference (use-after-free) const_eval_validation_expected_bool = expected a boolean const_eval_validation_expected_box = expected a box @@ -435,10 +429,14 @@ const_eval_validation_front_matter_invalid_value = constructing invalid value const_eval_validation_front_matter_invalid_value_with_path = constructing invalid value at {$path} const_eval_validation_invalid_bool = {$front_matter}: encountered {$value}, but expected a boolean +const_eval_validation_invalid_box_meta = {$front_matter}: encountered invalid box metadata: total size is bigger than largest supported object +const_eval_validation_invalid_box_slice_meta = {$front_matter}: encountered invalid box metadata: slice is bigger than largest supported object const_eval_validation_invalid_char = {$front_matter}: encountered {$value}, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) const_eval_validation_invalid_enum_tag = {$front_matter}: encountered {$value}, but expected a valid enum tag const_eval_validation_invalid_fn_ptr = {$front_matter}: encountered {$value}, but expected a function pointer +const_eval_validation_invalid_ref_meta = {$front_matter}: encountered invalid reference metadata: total size is bigger than largest supported object +const_eval_validation_invalid_ref_slice_meta = {$front_matter}: encountered invalid reference metadata: slice is bigger than largest supported object const_eval_validation_invalid_vtable_ptr = {$front_matter}: encountered {$value}, but expected a vtable pointer const_eval_validation_mutable_ref_in_const = {$front_matter}: encountered mutable reference in a `const` const_eval_validation_never_val = {$front_matter}: encountered a value of the never type `!` @@ -450,30 +448,11 @@ const_eval_validation_out_of_range = {$front_matter}: encountered {$value}, but const_eval_validation_partial_pointer = {$front_matter}: encountered a partial pointer or a mix of pointers const_eval_validation_pointer_as_int = {$front_matter}: encountered a pointer, but {$expected} const_eval_validation_ptr_out_of_range = {$front_matter}: encountered a pointer, but expected something that cannot possibly fail to be {$in_range} -const_eval_validation_ptr_to_mut = {$front_matter}: encountered a {$ptr_kind -> - [box] box - *[ref] reference -} pointing to mutable memory in a constant -const_eval_validation_ptr_to_static = {$front_matter}: encountered a {$ptr_kind -> - [box] box - *[ref] reference -} pointing to a static variable in a constant -const_eval_validation_ptr_to_uninhabited = {$front_matter}: encountered a {$ptr_kind -> - [box] box - *[ref] reference -} pointing to uninhabited type {$ty} -const_eval_validation_too_big_meta = {$front_matter}: encountered invalid {$ptr_kind -> - [box] box - *[ref] reference -} metadata: total size is bigger than largest supported object -const_eval_validation_too_big_slice_meta = {$front_matter}: {$ptr_kind -> - [box] encountered invalid box metadata: slice is bigger than largest supported object - *[ref] encountered invalid reference metadata: slice is bigger than largest supported object -} -const_eval_validation_unaligned = {$front_matter}: encountered an unaligned {$ptr_kind -> - [box] box - *[ref] reference -} (required {$required_bytes} byte alignment but found {$found_bytes}) +const_eval_validation_ref_to_mut = {$front_matter}: encountered a reference pointing to mutable memory in a constant +const_eval_validation_ref_to_static = {$front_matter}: encountered a reference pointing to a static variable in a constant +const_eval_validation_ref_to_uninhabited = {$front_matter}: encountered a reference pointing to uninhabited type {$ty} +const_eval_validation_unaligned_box = {$front_matter}: encountered an unaligned box (required {$required_bytes} byte alignment but found {$found_bytes}) +const_eval_validation_unaligned_ref = {$front_matter}: encountered an unaligned reference (required {$required_bytes} byte alignment but found {$found_bytes}) const_eval_validation_uninhabited_enum_variant = {$front_matter}: encountered an uninhabited enum variant const_eval_validation_uninhabited_val = {$front_matter}: encountered a value of uninhabited type `{$ty}` const_eval_validation_uninit = {$front_matter}: encountered uninitialized memory, but {$expected} diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index 7cc27296edbb7..b1599dd689482 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -610,9 +610,19 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { use crate::fluent_generated::*; use rustc_middle::mir::interpret::ValidationErrorKind::*; match self.kind { - PtrToUninhabited { .. } => const_eval_validation_ptr_to_uninhabited, - PtrToStatic { .. } => const_eval_validation_ptr_to_static, - PtrToMut { .. } => const_eval_validation_ptr_to_mut, + PtrToUninhabited { ptr_kind: PointerKind::Box, .. } => { + const_eval_validation_box_to_uninhabited + } + PtrToUninhabited { ptr_kind: PointerKind::Ref, .. } => { + const_eval_validation_ref_to_uninhabited + } + + PtrToStatic { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_static, + PtrToStatic { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_static, + + PtrToMut { ptr_kind: PointerKind::Box } => const_eval_validation_box_to_mut, + PtrToMut { ptr_kind: PointerKind::Ref } => const_eval_validation_ref_to_mut, + PointerAsInt { .. } => const_eval_validation_pointer_as_int, PartialPointer => const_eval_validation_partial_pointer, MutableRefInConst => const_eval_validation_mutable_ref_in_const, @@ -627,14 +637,42 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { UninhabitedEnumVariant => const_eval_validation_uninhabited_enum_variant, Uninit { .. } => const_eval_validation_uninit, InvalidVTablePtr { .. } => const_eval_validation_invalid_vtable_ptr, - InvalidMetaSliceTooLarge { .. } => const_eval_validation_too_big_slice_meta, - InvalidMetaTooLarge { .. } => const_eval_validation_too_big_meta, - UnalignedPtr { .. } => const_eval_validation_unaligned, + InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Box } => { + const_eval_validation_invalid_box_slice_meta + } + InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Ref } => { + const_eval_validation_invalid_ref_slice_meta + } + + InvalidMetaTooLarge { ptr_kind: PointerKind::Box } => { + const_eval_validation_invalid_box_meta + } + InvalidMetaTooLarge { ptr_kind: PointerKind::Ref } => { + const_eval_validation_invalid_ref_meta + } + UnalignedPtr { ptr_kind: PointerKind::Ref, .. } => const_eval_validation_unaligned_ref, + UnalignedPtr { ptr_kind: PointerKind::Box, .. } => const_eval_validation_unaligned_box, + NullPtr { ptr_kind: PointerKind::Box } => const_eval_validation_null_box, NullPtr { ptr_kind: PointerKind::Ref } => const_eval_validation_null_ref, - DanglingPtrNoProvenance { .. } => const_eval_validation_dangling_no_provenance, - DanglingPtrOutOfBounds { .. } => const_eval_validation_dangling_out_of_bounds, - DanglingPtrUseAfterFree { .. } => const_eval_validation_dangling_use_after_free, + DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => { + const_eval_validation_dangling_box_no_provenance + } + DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => { + const_eval_validation_dangling_ref_no_provenance + } + DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => { + const_eval_validation_dangling_box_out_of_bounds + } + DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => { + const_eval_validation_dangling_ref_out_of_bounds + } + DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => { + const_eval_validation_dangling_box_use_after_free + } + DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => { + const_eval_validation_dangling_ref_use_after_free + } InvalidBool { .. } => const_eval_validation_invalid_bool, InvalidChar { .. } => const_eval_validation_invalid_char, InvalidFnPtr { .. } => const_eval_validation_invalid_fn_ptr, @@ -696,11 +734,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { } match self.kind { - UninhabitedVal { ty } => { - err.set_arg("ty", ty); - } - PtrToUninhabited { ty, ptr_kind, .. } => { - err.set_arg("ptr_kind", ptr_kind); + PtrToUninhabited { ty, .. } | UninhabitedVal { ty } => { err.set_arg("ty", ty); } PointerAsInt { expected } | Uninit { expected } => { @@ -734,28 +768,24 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { err.set_arg("value", value); add_range_arg(range, max_value, handler, err); } - UnalignedPtr { required_bytes, found_bytes, ptr_kind } => { - err.set_arg("found_bytes", found_bytes); - err.set_arg("ptr_kind", ptr_kind); + UnalignedPtr { required_bytes, found_bytes, .. } => { err.set_arg("required_bytes", required_bytes); + err.set_arg("found_bytes", found_bytes); } - DanglingPtrNoProvenance { pointer, ptr_kind } => { + DanglingPtrNoProvenance { pointer, .. } => { err.set_arg("pointer", pointer); - err.set_arg("ptr_kind", ptr_kind); - } - DanglingPtrUseAfterFree { ptr_kind } - | DanglingPtrOutOfBounds { ptr_kind } - | InvalidMetaSliceTooLarge { ptr_kind } - | InvalidMetaTooLarge { ptr_kind } - | PtrToStatic { ptr_kind } - | PtrToMut { ptr_kind } => { - err.set_arg("ptr_kind", ptr_kind); } NullPtr { .. } + | PtrToStatic { .. } + | PtrToMut { .. } | MutableRefInConst | NullFnPtr | NeverVal | UnsafeCell + | InvalidMetaSliceTooLarge { .. } + | InvalidMetaTooLarge { .. } + | DanglingPtrUseAfterFree { .. } + | DanglingPtrOutOfBounds { .. } | UninhabitedEnumVariant | PartialPointer => {} } From 0fc64958c6393cd0d3decc21fb36366c188443cb Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 15 Oct 2023 21:24:44 -0700 Subject: [PATCH 3/3] Try another abstraction for dangling ptr messages --- compiler/rustc_const_eval/messages.ftl | 11 +++--- compiler/rustc_const_eval/src/errors.rs | 48 +++++++++++++++---------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index d23e2a9f3e4e0..2cab2bdd3411b 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -406,12 +406,11 @@ const_eval_upcast_mismatch = const_eval_validation_box_to_mut = {$front_matter}: encountered a box pointing to mutable memory in a constant const_eval_validation_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant const_eval_validation_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty} -const_eval_validation_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance) -const_eval_validation_dangling_box_out_of_bounds = {$front_matter}: encountered a dangling box (going beyond the bounds of its allocation) -const_eval_validation_dangling_box_use_after_free = {$front_matter}: encountered a dangling box (use-after-free) -const_eval_validation_dangling_ref_no_provenance = {$front_matter}: encountered a dangling reference ({$pointer} has no provenance) -const_eval_validation_dangling_ref_out_of_bounds = {$front_matter}: encountered a dangling reference (going beyond the bounds of its allocation) -const_eval_validation_dangling_ref_use_after_free = {$front_matter}: encountered a dangling reference (use-after-free) +const_eval_validation_dangling_err_no_provenance = {$pointer} has no provenance +const_eval_validation_dangling_err_out_of_bounds = going beyond the bounds of its allocation +const_eval_validation_dangling_err_use_after_free = use-after-free +const_eval_validation_dangling_ptr_box = {$front_matter}: encountered a dangling box ({$subdiagnostic}) +const_eval_validation_dangling_ptr_ref = {$front_matter}: encountered a dangling reference ({$subdiagnostic}) const_eval_validation_expected_bool = expected a boolean const_eval_validation_expected_box = expected a box diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index b1599dd689482..ef684b324ee84 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -655,23 +655,15 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { NullPtr { ptr_kind: PointerKind::Box } => const_eval_validation_null_box, NullPtr { ptr_kind: PointerKind::Ref } => const_eval_validation_null_ref, - DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => { - const_eval_validation_dangling_box_no_provenance + DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } + | DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } + | DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => { + const_eval_validation_dangling_ptr_box } - DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => { - const_eval_validation_dangling_ref_no_provenance - } - DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => { - const_eval_validation_dangling_box_out_of_bounds - } - DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => { - const_eval_validation_dangling_ref_out_of_bounds - } - DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => { - const_eval_validation_dangling_box_use_after_free - } - DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => { - const_eval_validation_dangling_ref_use_after_free + DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } + | DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } + | DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => { + const_eval_validation_dangling_ptr_ref } InvalidBool { .. } => const_eval_validation_invalid_bool, InvalidChar { .. } => const_eval_validation_invalid_char, @@ -773,7 +765,27 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { err.set_arg("found_bytes", found_bytes); } DanglingPtrNoProvenance { pointer, .. } => { - err.set_arg("pointer", pointer); + let subdiagnostic = handler.eagerly_translate_to_string( + fluent::const_eval_validation_dangling_err_no_provenance, + [("pointer".into(), DiagnosticArgValue::Str(pointer.into()))] + .iter() + .map(|(a, b)| (a, b)), + ); + err.set_arg("subdiagnostic", subdiagnostic); + } + DanglingPtrUseAfterFree { .. } => { + let subdiagnostic = handler.eagerly_translate_to_string( + fluent::const_eval_validation_dangling_err_use_after_free, + [].into_iter(), + ); + err.set_arg("subdiagnostic", subdiagnostic); + } + DanglingPtrOutOfBounds { .. } => { + let subdiagnostic = handler.eagerly_translate_to_string( + fluent::const_eval_validation_dangling_err_out_of_bounds, + [].into_iter(), + ); + err.set_arg("subdiagnostic", subdiagnostic); } NullPtr { .. } | PtrToStatic { .. } @@ -784,8 +796,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { | UnsafeCell | InvalidMetaSliceTooLarge { .. } | InvalidMetaTooLarge { .. } - | DanglingPtrUseAfterFree { .. } - | DanglingPtrOutOfBounds { .. } | UninhabitedEnumVariant | PartialPointer => {} }