Skip to content

Commit 744dfd8

Browse files
committed
explain why interning is not as trivial as it might seem
1 parent 18fd58e commit 744dfd8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

compiler/rustc_mir/src/interpret/intern.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
//!
33
//! After a const evaluation has computed a value, before we destroy the const evaluator's session
44
//! memory, we need to extract all memory allocations to the global memory pool so they stay around.
5+
//!
6+
//! In principle, this is not very complicated: we recursively walk the final value, follow all the
7+
//! pointers, and move all reachable allocations to the global `tcx` memory. The only complication
8+
//! is picking the right mutability for the allocations in a `static` initializer: we want to make
9+
//! as many allocations as possible immutable so LLVM can put them into read-only memory. At the
10+
//! same time, we need to make memory that could be mutated by the program mutable to avoid
11+
//! incorrect compilations. To achieve this, we do a type-based traversal of the final value,
12+
//! tracking mutable and shared references and `UnsafeCell` to determine the current mutability.
13+
//! (In principle, we could skip this type-based part for `const` and promoteds, as they need to be
14+
//! always immutable. At least for `const` however we use this opportunity to reject any `const`
15+
//! that contains allocations whose mutability we cannot identify.)
516
617
use super::validity::RefTracking;
718
use rustc_data_structures::fx::{FxHashMap, FxHashSet};

0 commit comments

Comments
 (0)