Skip to content

Commit 945ed66

Browse files
committed
Add 2024 unsafe functions
1 parent bbaabbe commit 945ed66

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
- [Rust 2024 🚧](rust-2024/index.md)
4141
- [Additions to the prelude](rust-2024/prelude.md)
42+
- [Unsafe functions](rust-2024/unsafe-functions.md)
4243
- [`unsafe_op_in_unsafe_fn` warning](rust-2024/unsafe-op-in-unsafe-fn.md)
4344
- [RPIT lifetime capture](rust-2024/rpit-lifetime-capture.md)
4445
- [Disallow references to `static mut`](rust-2024/static-mut-reference.md)

src/rust-2024/unsafe-functions.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Unsafe functions
2+
3+
🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".
4+
More information may be found in the tracking issue at <https://github.com/rust-lang/rust/issues/124866>.
5+
6+
## Summary
7+
8+
- The following functions are now marked [`unsafe`]:
9+
- [`std::env::set_var`]
10+
- [`std::env::remove_var`]
11+
12+
[`unsafe`]: ../../reference/unsafe-keyword.html#unsafe-functions-unsafe-fn
13+
[`std::env::set_var`]: ../../std/env/fn.set_var.html
14+
[`std::env::remove_var`]: ../../std/env/fn.remove_var.html
15+
16+
## Details
17+
18+
It can be unsound to call [`std::env::set_var`] or [`std::env::remove_var`] in a multi-threaded program due to safety limitations of the way the process environment is handled on some platforms. The standard library originally defined these as safe functions, but it was later determined that was not correct.
19+
20+
It is important to ensure that these functions are not called in a multi-threaded program. See the [Safety] section of the function documentation for more details.
21+
22+
Ordinarily it would be a backwards-incompatible change to add `unsafe` to these functions. To address that problem, they are marked as `unsafe` only in the 2024 Edition.
23+
24+
[Safety]: ../../std/env/fn.set_var.html#safety
25+
26+
## Migration
27+
28+
To make your code compile in both the 2021 and 2024 editions, you will need to make sure that `set_var` and `remove_var` is called from an `unsafe` blocks.
29+
30+
**⚠ Caution**: It is important that you manually inspect the calls to `set_var` and `remove_var` and possibly rewrite your code to satisfy the preconditions of those functions. In particular, they should not be called if there might be multiple threads running. You may need to elect to use a different mechanism other than environment variables to manage your use case.
31+
32+
The [`deprecated_safe_2024`] lint will automatically modify any use of `set_var` or `remove_var` to be wrapped in an `unsafe` block so that it can compile on both editions. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:
33+
34+
```sh
35+
cargo fix --edition
36+
```
37+
38+
For example, this will change:
39+
40+
```rust
41+
fn main() {
42+
std::env::set_var("FOO", "123");
43+
}
44+
```
45+
46+
to be:
47+
48+
```rust
49+
fn main() {
50+
unsafe { std::env::set_var("FOO", "123") };
51+
}
52+
```
53+
54+
Just beware that this automatic migration will not be able to verify that these functions are being used correctly. It is still your responsibility to manually review their usage.
55+
56+
Alternatively, you can manually enable the lint to find places these functions are called:
57+
58+
```rust
59+
// Add this to the root of your crate to do a manual migration.
60+
#![warn(deprecated_safe_2024)]
61+
```
62+
63+
[`deprecated_safe_2024`]: ../../rustc/lints/listing/allowed-by-default.html#deprecated-safe

0 commit comments

Comments
 (0)