about summary refs log tree commit diff
path: root/compiler/rustc_error_codes/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_error_codes/src')
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0796.md26
1 files changed, 15 insertions, 11 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0796.md b/compiler/rustc_error_codes/src/error_codes/E0796.md
index cea18f8db85..7ac429e5215 100644
--- a/compiler/rustc_error_codes/src/error_codes/E0796.md
+++ b/compiler/rustc_error_codes/src/error_codes/E0796.md
@@ -1,22 +1,26 @@
-Reference of mutable static.
+You have created a reference to a mutable static.
 
 Erroneous code example:
 
 ```compile_fail,edition2024,E0796
 static mut X: i32 = 23;
-static mut Y: i32 = 24;
 
-unsafe {
-  let y = &X;
-  let ref x = X;
-  let (x, y) = (&X, &Y);
-  foo(&X);
+fn work() {
+  let _val = unsafe { X };
 }
 
-fn foo<'a>(_x: &'a i32) {}
+let x_ref = unsafe { &mut X };
+work();
+// The next line has Undefined Behavior!
+// `x_ref` is a mutable reference and allows no aliases,
+// but `work` has been reading the reference between
+// the moment `x_ref` was created and when it was used.
+// This violates the uniqueness of `x_ref`.
+*x_ref = 42;
 ```
 
-Mutable statics can be written to by multiple threads: aliasing violations or
-data races will cause undefined behavior.
+A reference to a mutable static has lifetime `'static`. This is very dangerous
+as it is easy to accidentally overlap the lifetime of that reference with
+other, conflicting accesses to the same static.
 
-Reference of mutable static is a hard error from 2024 edition.
+References to mutable statics are a hard error in the 2024 edition.