Skip to content

Commit be68ee4

Browse files
authored
Unrolled build for rust-lang#125970
Rollup merge of rust-lang#125970 - RalfJung:before_exec, r=m-ou-se CommandExt::before_exec: deprecate safety in edition 2024 Similar to `set_var`, we had to find out after 1.0 was released that `before_exec` should have been unsafe. We partially rectified this by deprecating that function a long time ago, but since rust-lang#124636 we have the ability to also deprecate the safety of the old function and make it a *hard error* to call the old function outside `unsafe` in the next edition. So just in case anyone still uses the old function, let's ensure this can't be ignored when moving code to the new edition. Cc `@rust-lang/libs-api` Tracking: - rust-lang#124866
2 parents 13a5289 + 5ae0386 commit be68ee4

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

library/std/src/os/unix/process.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,21 @@ pub trait CommandExt: Sealed {
109109
/// Schedules a closure to be run just before the `exec` function is
110110
/// invoked.
111111
///
112-
/// This method is stable and usable, but it should be unsafe. To fix
113-
/// that, it got deprecated in favor of the unsafe [`pre_exec`].
112+
/// `before_exec` used to be a safe method, but it needs to be unsafe since the closure may only
113+
/// perform operations that are *async-signal-safe*. Hence it got deprecated in favor of the
114+
/// unsafe [`pre_exec`]. Meanwhile, Rust gained the ability to make an existing safe method
115+
/// fully unsafe in a new edition, which is how `before_exec` became `unsafe`. It still also
116+
/// remains deprecated; `pre_exec` should be used instead.
114117
///
115118
/// [`pre_exec`]: CommandExt::pre_exec
116119
#[stable(feature = "process_exec", since = "1.15.0")]
117120
#[deprecated(since = "1.37.0", note = "should be unsafe, use `pre_exec` instead")]
118-
fn before_exec<F>(&mut self, f: F) -> &mut process::Command
121+
#[cfg_attr(bootstrap, rustc_deprecated_safe_2024)]
122+
#[cfg_attr(
123+
not(bootstrap),
124+
rustc_deprecated_safe_2024(audit_that = "the closure is async-signal-safe")
125+
)]
126+
unsafe fn before_exec<F>(&mut self, f: F) -> &mut process::Command
119127
where
120128
F: FnMut() -> io::Result<()> + Send + Sync + 'static,
121129
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: call to unsafe function `before_exec` is unsafe and requires unsafe block
2+
--> $DIR/unsafe-before_exec.rs:14:5
3+
|
4+
LL | cmd.before_exec(|| Ok(()));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ revisions: e2021 e2024
2+
//@ only-unix
3+
//@[e2021] edition: 2021
4+
//@[e2021] check-pass
5+
//@[e2024] edition: 2024
6+
//@[e2024] compile-flags: -Zunstable-options
7+
8+
use std::process::Command;
9+
use std::os::unix::process::CommandExt;
10+
11+
#[allow(deprecated)]
12+
fn main() {
13+
let mut cmd = Command::new("sleep");
14+
cmd.before_exec(|| Ok(()));
15+
//[e2024]~^ ERROR call to unsafe function `before_exec` is unsafe
16+
drop(cmd); // we don't actually run the command.
17+
}

0 commit comments

Comments
 (0)