about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-06-17 15:43:33 +0200
committerGitHub <noreply@github.com>2024-06-17 15:43:33 +0200
commitc768d6e57cdc9e47be548c57c47765299e9b269a (patch)
tree071163dd8b969e72be32c11aea24492d702672cc
parentba26af3e832745bb8be93097df3f93932ff3718c (diff)
parent4e5dfb61e449940f0a9bf97e2df6cfeef38d2f30 (diff)
downloadrust-c768d6e57cdc9e47be548c57c47765299e9b269a.tar.gz
rust-c768d6e57cdc9e47be548c57c47765299e9b269a.zip
Rollup merge of #126577 - oli-obk:static_valtrees, r=RalfJung
const_refs_to_static test and cleanup

r? ``@RalfJung``

test the existing behaviour of adt_const_params combined with const_refs_to_static.

also remove a dead error variant about consts referring to statics
-rw-r--r--compiler/rustc_const_eval/messages.ftl2
-rw-r--r--compiler/rustc_const_eval/src/errors.rs4
-rw-r--r--compiler/rustc_middle/src/mir/interpret/error.rs3
-rw-r--r--tests/ui/statics/const_generics.rs26
4 files changed, 26 insertions, 9 deletions
diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl
index 2dbeb7d5e0c..cb5aac7e560 100644
--- a/compiler/rustc_const_eval/messages.ftl
+++ b/compiler/rustc_const_eval/messages.ftl
@@ -399,7 +399,6 @@ const_eval_unwind_past_top =
 
 ## The `front_matter`s here refer to either `const_eval_front_matter_invalid_value` or `const_eval_front_matter_invalid_value_with_path`.
 ## (We'd love to sort this differently to make that more clear but tidy won't let us...)
-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_const_ref_to_extern = {$front_matter}: encountered reference to `extern` static in `const`
@@ -454,7 +453,6 @@ 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_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})
diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs
index e5ea4c3442e..91d17fdd895 100644
--- a/compiler/rustc_const_eval/src/errors.rs
+++ b/compiler/rustc_const_eval/src/errors.rs
@@ -640,9 +640,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
                 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,
-
             PointerAsInt { .. } => const_eval_validation_pointer_as_int,
             PartialPointer => const_eval_validation_partial_pointer,
             ConstRefToMutable => const_eval_validation_const_ref_to_mutable,
@@ -807,7 +804,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
                 );
             }
             NullPtr { .. }
-            | PtrToStatic { .. }
             | ConstRefToMutable
             | ConstRefToExtern
             | MutableRefToImmutable
diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs
index eabbcc2033f..23680f14397 100644
--- a/compiler/rustc_middle/src/mir/interpret/error.rs
+++ b/compiler/rustc_middle/src/mir/interpret/error.rs
@@ -438,9 +438,6 @@ pub enum ValidationErrorKind<'tcx> {
         ptr_kind: PointerKind,
         ty: Ty<'tcx>,
     },
-    PtrToStatic {
-        ptr_kind: PointerKind,
-    },
     ConstRefToMutable,
     ConstRefToExtern,
     MutableRefToImmutable,
diff --git a/tests/ui/statics/const_generics.rs b/tests/ui/statics/const_generics.rs
new file mode 100644
index 00000000000..70d9b933a76
--- /dev/null
+++ b/tests/ui/statics/const_generics.rs
@@ -0,0 +1,26 @@
+//! Check that we lose the information that `BAR` points to `FOO`
+//! when going through a const generic.
+//! This is not an intentional guarantee, it just describes the status quo.
+
+//@ run-pass
+// With optimizations, LLVM will deduplicate the constant `X` whose
+// value is `&42` to just be a reference to the static. This is correct,
+// but obscures the issue we're trying to show.
+//@ revisions: opt noopt
+//@[noopt] compile-flags: -Copt-level=0
+//@[opt] compile-flags: -O
+
+#![feature(const_refs_to_static)]
+#![feature(adt_const_params)]
+#![allow(incomplete_features)]
+
+static FOO: usize = 42;
+const BAR: &usize = &FOO;
+fn foo<const X: &'static usize>() {
+    // Without optimizations, `X` ends up pointing to a copy of `FOO` instead of `FOO` itself.
+    assert_eq!(cfg!(opt), std::ptr::eq(X, &FOO));
+}
+
+fn main() {
+    foo::<BAR>();
+}