Make std::env::{set_var, remove_var} unsafe in edition 2024 - #124636
Conversation
|
Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt Some changes occurred in src/tools/compiletest cc @jieyouxu The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt The Miri subtree was changed cc @rust-lang/miri Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred in src/tools/compiletest cc @jieyouxu |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Should there be a warning on older editions, to make this not come entirely out of the blue when doing edition migration? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I agree that this would be quite useful. It seems hard to do right now, though, because a stage 0 compiler will detect a lot of unused |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I wasn't talking about unused_unsafe, but a lint that warns against using set_var/remove_var outside an unsafe block on old editions. Also, I am not sure if it's the best idea to add new lang items for these functions. In the past the proposal was to add an attribute for "deprecating safety". Then we could e.g. also add that attribute to before_exec. |
| /// that no other thread will read the environment, so the only safe option is | ||
| /// to not use `set_var` or `remove_var` in multi-threaded programs at all. |
There was a problem hiding this comment.
so the only safe option is to not use
set_varorremove_varin multi-threaded programs at all
Can't we use a static Mutex to guard env reads and writes? If so, saying that "the only option is not to use this" sounds misleading.
There was a problem hiding this comment.
No we cannot. There's a lot of discussion about that on the linked issues but in short that would only make it safe for people sharing the same Mutex and then only if all FFI was guarded by that Mutex (because any libc function is allowed to read the environment).
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// ```no_run |
There was a problem hiding this comment.
Would have been good to add a comment explaining why this is no_run. Are you worried about UB in the doctest?
There was a problem hiding this comment.
Yes, I'm worried about that. The doctest has no safety documentation, and I wouldn't know how I could prove that no other thread is running concurrently to it.
There was a problem hiding this comment.
It is not safe to use in a doctest. We would need to use an integration test.
A doctest could work on platforms where set_var is thread safe but I'm not sure there's much utility in a test only for those platforms if we can have something that works cross-platform.
Allow calling these functions without
unsafeblocks in editions up until 2021, but don't trigger theunused_unsafelint forunsafeblocks containing these functions.Fixes #27970.
Fixes #90308.
CC #124866.