Skip to content

Commit ef987b4

Browse files
committed
Update static-mut-refs now that it is just a lint
Updates for the changes from rust-lang/rust#124895
1 parent 373844c commit ef987b4

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/rust-2024/static-mut-references.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,41 @@ More information may be found in the tracking issue at <https://github.com/rust-
55

66
## Summary
77

8-
- The [`static_mut_refs`] lint is now a hard error that cannot be disabled.
9-
This prevents taking a shared or mutable reference to a `static mut`.
8+
- The [`static_mut_refs`] lint level is now `deny` by default.
9+
This checks for taking a shared or mutable reference to a `static mut`.
1010

1111
[`static_mut_refs`]: ../../rustc/lints/listing/warn-by-default.html#static-mut-refs
1212

1313
## Details
1414

15-
Taking a reference to a [`static mut`] is no longer allowed:
15+
The [`static_mut_refs`] lint detects taking a reference to a [`static mut`]. In the 2024 Edition, this lint is now `deny` by default to emphasize that you should avoid making these references.
1616

17-
<!-- edition2024,E0796 -->
17+
<!-- edition2024 -->
1818
```rust
1919
static mut X: i32 = 23;
2020
static mut Y: i32 = 24;
2121

2222
unsafe {
23-
let y = &X; // ERROR: reference of mutable static
24-
let ref x = X; // ERROR: reference of mutable static
25-
let (x, y) = (&X, &Y); // ERROR: reference of mutable static
23+
let y = &X; // ERROR: shared reference to mutable static
24+
let ref x = X; // ERROR: shared reference to mutable static
25+
let (x, y) = (&X, &Y); // ERROR: shared reference to mutable static
2626
}
2727
```
2828

2929
Merely taking such a reference in violation of Rust's mutability XOR aliasing requirement has always been *instantaneous* [undefined behavior], **even if the reference is never read from or written to**. Furthermore, upholding mutability XOR aliasing for a `static mut` requires *reasoning about your code globally*, which can be particularly difficult in the face of reentrancy and/or multithreading.
3030

31+
Note that there are some cases where implicit references are automatically created without a visible `&` operator. For example, these situations will also trigger the lint:
32+
33+
<!-- edition2024 -->
34+
```rust
35+
static mut NUMS: &[u8; 3] = &[0, 1, 2];
36+
37+
unsafe {
38+
println!("{NUMS:?}"); // ERROR: shared reference to mutable static
39+
let n = NUMS.len(); // ERROR: shared reference to mutable static
40+
}
41+
```
42+
3143
## Alternatives
3244

3345
Wherever possible, it is **strongly recommended** to use instead an *immutable* `static` of a type that provides *interior mutability* behind some *locally-reasoned abstraction* (which greatly reduces the complexity of ensuring that Rust's mutability XOR aliasing requirement is upheld).
@@ -40,6 +52,4 @@ In situations where no locally-reasoned abstraction is possible and you are ther
4052

4153
## Migration
4254

43-
🚧 The automatic migration for this has not yet been implemented.
44-
45-
<!-- TODO: Discuss alternatives around rewriting your code. -->
55+
There is no automatic migration to fix these references to `static mut`. To avoid undefined behavior you must rewrite your code to use a different approach as recommended in the [Alternatives](#alternatives) section.

0 commit comments

Comments
 (0)