about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-10-25 14:03:17 +0100
committerRalf Jung <post@ralfj.de>2020-10-26 08:56:54 +0100
commit744dfd8847e447edf7e612196d6eec665ce36c04 (patch)
tree86a44a33a80f48f70b7c6ca7079343cc27a42bd1
parent18fd58e9d1f5daea4b1ab6cc80b4251645842191 (diff)
downloadrust-744dfd8847e447edf7e612196d6eec665ce36c04.tar.gz
rust-744dfd8847e447edf7e612196d6eec665ce36c04.zip
explain why interning is not as trivial as it might seem
-rw-r--r--compiler/rustc_mir/src/interpret/intern.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/compiler/rustc_mir/src/interpret/intern.rs b/compiler/rustc_mir/src/interpret/intern.rs
index c8b7da90753..5e5c74a3723 100644
--- a/compiler/rustc_mir/src/interpret/intern.rs
+++ b/compiler/rustc_mir/src/interpret/intern.rs
@@ -2,6 +2,17 @@
 //!
 //! After a const evaluation has computed a value, before we destroy the const evaluator's session
 //! memory, we need to extract all memory allocations to the global memory pool so they stay around.
+//!
+//! In principle, this is not very complicated: we recursively walk the final value, follow all the
+//! pointers, and move all reachable allocations to the global `tcx` memory. The only complication
+//! is picking the right mutability for the allocations in a `static` initializer: we want to make
+//! as many allocations as possible immutable so LLVM can put them into read-only memory. At the
+//! same time, we need to make memory that could be mutated by the program mutable to avoid
+//! incorrect compilations. To achieve this, we do a type-based traversal of the final value,
+//! tracking mutable and shared references and `UnsafeCell` to determine the current mutability.
+//! (In principle, we could skip this type-based part for `const` and promoteds, as they need to be
+//! always immutable. At least for `const` however we use this opportunity to reject any `const`
+//! that contains allocations whose mutability we cannot identify.)
 
 use super::validity::RefTracking;
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};