forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#125752 - jieyouxu:kaboom, r=<try>
run-make: enforce `#[must_use]` and arm command wrappers with drop bombs This PR is one in a series of cleanups to run-make tests and the run-make-support library. ### Summary It's easy to forget to actually executed constructed command wrappers, e.g. `rustc().input("foo.rs")` but forget the `run()`, so to help catch these mistakes, we: - Add `#[must_use]` annotations to functions where suitable and compile rmake.rs recipes with `-Dunused_must_use`. - Arm command wrappers with drop bombs on construction to force them to be executed by test code. ### Details Especially for command wrappers like `Rustc`, it's very easy to build up a command invocation but forget to actually execute it, e.g. by using `run()`. This commit adds "drop bombs" to command wrappers, which are armed on command wrapper construction, and only defused if the command is executed (through `run`, `run_fail` or `run_fail_assert_exit_code`). If the test writer forgets to execute the command, the drop bomb will "explode" and panic with an error message. This is so that tests don't silently pass with constructed-but-not-executed command wrappers. We don't add `#[must_use]` for command wrapper helper methods because they return `&mut Self` and can be annoying e.g. if a helper method is conditionally called, such as ``` if condition { cmd.arg("-Cprefer-dynamic"); // <- unused_must_use fires } cmd.run(); // <- even though cmd is eventually executed ``` This PR is best reviewed commit-by-commit. Fixes rust-lang#125703. Because `command_output()` doesn't defuse the drop bomb, it also fixes rust-lang#125617. try-job: x86_64-msvc
- Loading branch information
Showing
19 changed files
with
182 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! This module implements "drop bombs" intended for use by command wrappers to ensure that the | ||
//! constructed commands are *eventually* executed. This is exactly like `rustc_errors::Diag` | ||
//! where we force every `Diag` to be consumed or we emit a bug, but we panic instead. | ||
//! | ||
//! This is inspired by <https://docs.rs/drop_bomb/latest/drop_bomb/>. | ||
|
||
use std::borrow::Cow; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct DropBomb { | ||
msg: Cow<'static, str>, | ||
defused: bool, | ||
} | ||
|
||
impl DropBomb { | ||
/// Arm a [`DropBomb`]. If the value is dropped without being [`defused`][Self::defused], then | ||
/// it will panic. | ||
pub(crate) fn arm<S: Into<Cow<'static, str>>>(message: S) -> DropBomb { | ||
DropBomb { msg: message.into(), defused: false } | ||
} | ||
|
||
/// Defuse the [`DropBomb`]. This will prevent the drop bomb from panicking when dropped. | ||
pub(crate) fn defuse(&mut self) { | ||
self.defused = true; | ||
} | ||
} | ||
|
||
impl Drop for DropBomb { | ||
fn drop(&mut self) { | ||
if !self.defused && !std::thread::panicking() { | ||
panic!("{}", self.msg) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use super::DropBomb; | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_arm() { | ||
let bomb = DropBomb::arm("hi :3"); | ||
drop(bomb); // <- armed bomb should explode when not defused | ||
} | ||
|
||
#[test] | ||
fn test_defuse() { | ||
let mut bomb = DropBomb::arm("hi :3"); | ||
bomb.defuse(); | ||
drop(bomb); // <- defused bomb should not explode | ||
} |
Oops, something went wrong.