diff options
| author | Obei Sideg <obei.sideg@gmail.com> | 2024-02-17 22:01:56 +0300 |
|---|---|---|
| committer | Obei Sideg <obei.sideg@gmail.com> | 2024-02-18 06:01:40 +0300 |
| commit | 408eeae59d35cbcaab2cfb345d24373954e74fc5 (patch) | |
| tree | 96f35aa95f6fba769db2c292965e8a7ec9c09498 /compiler/rustc_error_codes/src | |
| parent | eeeb021954e30a6b16fc12cc4b8a1bcf7910a40e (diff) | |
| download | rust-408eeae59d35cbcaab2cfb345d24373954e74fc5.tar.gz rust-408eeae59d35cbcaab2cfb345d24373954e74fc5.zip | |
Improve wording of static_mut_ref
Rename `static_mut_ref` lint to `static_mut_refs`.
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. |
