diff options
| author | bors <bors@rust-lang.org> | 2024-02-18 08:00:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-18 08:00:34 +0000 |
| commit | bcb3545164d7621353c7b152b6339ce44c640add (patch) | |
| tree | 7e0a71eaca15e746565fb899187a69f0547dad53 /compiler/rustc_error_codes/src | |
| parent | 23a3d777c8a95715977608c827de63e7738fa228 (diff) | |
| parent | 408eeae59d35cbcaab2cfb345d24373954e74fc5 (diff) | |
| download | rust-bcb3545164d7621353c7b152b6339ce44c640add.tar.gz rust-bcb3545164d7621353c7b152b6339ce44c640add.zip | |
Auto merge of #121034 - obeis:improve-static-mut-ref, r=RalfJung
Improve wording of `static_mut_ref` Close #120964
Diffstat (limited to 'compiler/rustc_error_codes/src')
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0796.md | 26 |
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. |
