About static variables

I was just reading this RFC and I realized that there were some things that I don't understand.

What is the use of an immutable static? Isn't this a const?

static X: i32 = 0;

I also didn't understand this part:

[...] creating a mutable reference to shared memory is undefined behavior, even if you never dereference that reference.

What is the undefined behavior there? Isn't that just like having a pointer that you never use?

Thanks for illuminating this wannabe Rustacean :slight_smile:

Undefined Behavior (UB) occurs when, via unsafe, you violate the input constraints of the LLVM backend of the compiler. Once you violate those constraints LLVM is free to "miscompile" (from your perspective) every last bit of your code in its quest for hyper-optimization. It doesn't matter whether you actually try to execute the offending code; its mere existence in LLVM's input is what triggers the "miscompilation".


Rust has interior mutability, so a shared static can hold e.g. a Mutex that makes it safely mutable (Mutex and other such types contain special magic UnsafeCell that isn't optimized the way references are).

The UB comes from aliasing information given to LLVM. As soon as the reference exists, LLVM is informed about it, and it can make assumptions about this memory, and everything related to it. Optimizer is evaluating all the "what-ifs" about the code, so even if you don't dereference an invalid reference, LLVM can see what would happen if you did.


To elaborate further, interior mutability works by replacing the rustc compiler's compile-time checks with run-time checking, which require real code and execution time, and which usually result in a panic if/when Rust's exclusive-access constraint is violated.

Thanks, I guess I should go check out the interior mutability chapter in the book again.

I just want to point out that interior mutability in itself just prevents some optimizations. The types built on top of it often have a run-time cost however.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.