about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret/machine.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-15 10:28:31 +0000
committerbors <bors@rust-lang.org>2024-02-15 10:28:31 +0000
commit6a4222b5119bce8c24e74dd3ad44006ce0c803e6 (patch)
tree54b3a5f75e9bafa2cee88edbe40c72f8913c06fe /compiler/rustc_const_eval/src/interpret/machine.rs
parent4ae1e79876fcee94a56c1edf7c029a677084d7fc (diff)
parent73b38c661d9bc0466547af16d20089e5383f754b (diff)
downloadrust-6a4222b5119bce8c24e74dd3ad44006ce0c803e6.tar.gz
rust-6a4222b5119bce8c24e74dd3ad44006ce0c803e6.zip
Auto merge of #116564 - oli-obk:evaluated_static_in_metadata, r=RalfJung,cjgillot
Store static initializers in metadata instead of the MIR of statics.

This means that adding generic statics would be even more difficult, as we can't evaluate statics from other crates anymore, but the subtle issue I have encountered make me think that having this be an explicit problem is better.

The issue is that

```rust
static mut FOO: &mut u32 = &mut 42;
static mut BAR = unsafe { FOO };
```

gets different allocations, instead of referring to the same one. This is also true for non-static mut, but promotion makes `static FOO: &u32 = &42;` annoying to demo.

Fixes https://github.com/rust-lang/rust/issues/61345

## Why is this being done?

In order to ensure all crates see the same nested allocations (which is the last issue that needs fixing before we can stabilize [`const_mut_refs`](https://github.com/rust-lang/rust/issues/57349)), I am working on creating anonymous (from the Rust side, to LLVM it's like a regular static item) static items for the nested allocations in a static. If we evaluate the static item in a downstream crate again, we will end up duplicating its nested allocations (and in some cases, like the `match` case, even duplicate the main allocation).
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret/machine.rs')
-rw-r--r--compiler/rustc_const_eval/src/interpret/machine.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs
index b981a1ee2ca..0106ec425bc 100644
--- a/compiler/rustc_const_eval/src/interpret/machine.rs
+++ b/compiler/rustc_const_eval/src/interpret/machine.rs
@@ -388,6 +388,8 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
     /// Takes read-only access to the allocation so we can keep all the memory read
     /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
     /// need to mutate.
+    ///
+    /// This is not invoked for ZST accesses, as no read actually happens.
     #[inline(always)]
     fn before_memory_read(
         _tcx: TyCtxtAt<'tcx>,
@@ -399,7 +401,20 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
         Ok(())
     }
 
+    /// Hook for performing extra checks on any memory read access,
+    /// that involves an allocation, even ZST reads.
+    ///
+    /// Used to prevent statics from self-initializing by reading from their own memory
+    /// as it is being initialized.
+    fn before_alloc_read(
+        _ecx: &InterpCx<'mir, 'tcx, Self>,
+        _alloc_id: AllocId,
+    ) -> InterpResult<'tcx> {
+        Ok(())
+    }
+
     /// Hook for performing extra checks on a memory write access.
+    /// This is not invoked for ZST accesses, as no write actually happens.
     #[inline(always)]
     fn before_memory_write(
         _tcx: TyCtxtAt<'tcx>,