Skip to content

Commit 921645c

Browse files
committed
Auto merge of #126197 - jieyouxu:rmake-must-use, r=Kobzol
run-make: annotate library with `#[must_use]` and enforce `unused_must_use` in rmake.rs This PR adds `#[must_use]` annotations to functions of the `run_make_support` library where it makes sense, and adjusts compiletest to compile rmake.rs with `-Dunused_must_use`. The rationale is that it's highly likely that unused `#[must_use]` values in rmake.rs test files are bugs. For example, unused fs/io results are often load-bearing to the correctness of the test and often unchecked fs/io results allow the test to silently pass where it would've failed if the result was checked. This PR is best reviewed commit-by-commit. try-job: test-various try-job: x86_64-msvc
2 parents 56e112a + 5b126ed commit 921645c

File tree

12 files changed

+48
-2
lines changed

12 files changed

+48
-2
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3452,7 +3452,7 @@ dependencies = [
34523452

34533453
[[package]]
34543454
name = "run_make_support"
3455-
version = "0.1.0"
3455+
version = "0.2.0"
34563456
dependencies = [
34573457
"gimli 0.28.1",
34583458
"object 0.34.0",

src/tools/compiletest/src/runtest.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,10 @@ impl<'test> TestCx<'test> {
35513551
.env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
35523552
.env("LLVM_COMPONENTS", &self.config.llvm_components);
35533553

3554+
// In test code we want to be very pedantic about values being silently discarded that are
3555+
// annotated with `#[must_use]`.
3556+
cmd.arg("-Dunused_must_use");
3557+
35543558
if std::env::var_os("COMPILETEST_FORCE_STAGE0").is_some() {
35553559
let mut stage0_sysroot = build_root.clone();
35563560
stage0_sysroot.push("stage0-sysroot");

src/tools/run-make-support/CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ changes to the support library).
1010
This support library will probably never reach 1.0. Please bump the minor version in `Cargo.toml` if
1111
you make any breaking changes or other significant changes, or bump the patch version for bug fixes.
1212

13+
## [0.2.0] - 2024-06-11
14+
15+
### Added
16+
17+
- Added `fs_wrapper` module which provides panic-on-fail helpers for their respective `std::fs`
18+
counterparts, the motivation is to:
19+
- Reduce littering `.unwrap()` or `.expect()` everywhere for fs operations
20+
- Help the test writer avoid forgetting to check fs results (even though enforced by
21+
`-Dunused_must_use`)
22+
- Provide better panic messages by default
23+
- Added `path()` helper which creates a `Path` relative to `cwd()` (but is less noisy).
24+
25+
### Changed
26+
27+
- Marked many functions with `#[must_use]`, and rmake.rs are now compiled with `-Dunused_must_use`.
28+
1329
## [0.1.0] - 2024-06-09
1430

1531
### Changed

src/tools/run-make-support/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "run_make_support"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[dependencies]

src/tools/run-make-support/src/cc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn cc() -> Cc {
1515
/// A platform-specific C compiler invocation builder. The specific C compiler used is
1616
/// passed down from compiletest.
1717
#[derive(Debug)]
18+
#[must_use]
1819
pub struct Cc {
1920
cmd: Command,
2021
}

src/tools/run-make-support/src/clang.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn clang() -> Clang {
1111

1212
/// A `clang` invocation builder.
1313
#[derive(Debug)]
14+
#[must_use]
1415
pub struct Clang {
1516
cmd: Command,
1617
}

src/tools/run-make-support/src/command.rs

+3
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,17 @@ pub struct CompletedProcess {
148148
}
149149

150150
impl CompletedProcess {
151+
#[must_use]
151152
pub fn stdout_utf8(&self) -> String {
152153
String::from_utf8(self.output.stdout.clone()).expect("stdout is not valid UTF-8")
153154
}
154155

156+
#[must_use]
155157
pub fn stderr_utf8(&self) -> String {
156158
String::from_utf8(self.output.stderr.clone()).expect("stderr is not valid UTF-8")
157159
}
158160

161+
#[must_use]
159162
pub fn status(&self) -> ExitStatus {
160163
self.output.status
161164
}

src/tools/run-make-support/src/diff/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn diff() -> Diff {
1313
}
1414

1515
#[derive(Debug)]
16+
#[must_use]
1617
pub struct Diff {
1718
expected: Option<String>,
1819
expected_name: Option<String>,

src/tools/run-make-support/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use rustc::{aux_build, rustc, Rustc};
3737
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
3838

3939
#[track_caller]
40+
#[must_use]
4041
pub fn env_var(name: &str) -> String {
4142
match env::var(name) {
4243
Ok(v) => v,
@@ -45,6 +46,7 @@ pub fn env_var(name: &str) -> String {
4546
}
4647

4748
#[track_caller]
49+
#[must_use]
4850
pub fn env_var_os(name: &str) -> OsString {
4951
match env::var_os(name) {
5052
Some(v) => v,
@@ -53,32 +55,38 @@ pub fn env_var_os(name: &str) -> OsString {
5355
}
5456

5557
/// `TARGET`
58+
#[must_use]
5659
pub fn target() -> String {
5760
env_var("TARGET")
5861
}
5962

6063
/// Check if target is windows-like.
64+
#[must_use]
6165
pub fn is_windows() -> bool {
6266
target().contains("windows")
6367
}
6468

6569
/// Check if target uses msvc.
70+
#[must_use]
6671
pub fn is_msvc() -> bool {
6772
target().contains("msvc")
6873
}
6974

7075
/// Check if target uses macOS.
76+
#[must_use]
7177
pub fn is_darwin() -> bool {
7278
target().contains("darwin")
7379
}
7480

7581
#[track_caller]
82+
#[must_use]
7683
pub fn python_command() -> Command {
7784
let python_path = env_var("PYTHON");
7885
Command::new(python_path)
7986
}
8087

8188
#[track_caller]
89+
#[must_use]
8290
pub fn htmldocck() -> Command {
8391
let mut python = python_command();
8492
python.arg(source_root().join("src/etc/htmldocck.py"));
@@ -91,6 +99,7 @@ pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
9199
}
92100

93101
/// Path to the root rust-lang/rust source checkout.
102+
#[must_use]
94103
pub fn source_root() -> PathBuf {
95104
env_var("SOURCE_ROOT").into()
96105
}
@@ -124,6 +133,7 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
124133
}
125134

126135
/// Construct the static library name based on the platform.
136+
#[must_use]
127137
pub fn static_lib_name(name: &str) -> String {
128138
// See tools.mk (irrelevant lines omitted):
129139
//
@@ -148,6 +158,7 @@ pub fn static_lib_name(name: &str) -> String {
148158
}
149159

150160
/// Construct the dynamic library name based on the platform.
161+
#[must_use]
151162
pub fn dynamic_lib_name(name: &str) -> String {
152163
// See tools.mk (irrelevant lines omitted):
153164
//
@@ -174,6 +185,7 @@ pub fn dynamic_lib_name(name: &str) -> String {
174185
}
175186
}
176187

188+
#[must_use]
177189
pub fn dynamic_lib_extension() -> &'static str {
178190
if is_darwin() {
179191
"dylib"
@@ -185,23 +197,27 @@ pub fn dynamic_lib_extension() -> &'static str {
185197
}
186198

187199
/// Generate the name a rust library (rlib) would have.
200+
#[must_use]
188201
pub fn rust_lib_name(name: &str) -> String {
189202
format!("lib{name}.rlib")
190203
}
191204

192205
/// Construct the binary name based on platform.
206+
#[must_use]
193207
pub fn bin_name(name: &str) -> String {
194208
if is_windows() { format!("{name}.exe") } else { name.to_string() }
195209
}
196210

197211
/// Return the current working directory.
212+
#[must_use]
198213
pub fn cwd() -> PathBuf {
199214
env::current_dir().unwrap()
200215
}
201216

202217
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
203218
/// available on the platform!
204219
#[track_caller]
220+
#[must_use]
205221
pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
206222
let caller = panic::Location::caller();
207223
let mut cygpath = Command::new("cygpath");
@@ -217,6 +233,7 @@ pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
217233

218234
/// Run `uname`. This assumes that `uname` is available on the platform!
219235
#[track_caller]
236+
#[must_use]
220237
pub fn uname() -> String {
221238
let caller = panic::Location::caller();
222239
let mut uname = Command::new("uname");

src/tools/run-make-support/src/llvm_readobj.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn llvm_readobj() -> LlvmReadobj {
1212

1313
/// A `llvm-readobj` invocation builder.
1414
#[derive(Debug)]
15+
#[must_use]
1516
pub struct LlvmReadobj {
1617
cmd: Command,
1718
}

src/tools/run-make-support/src/rustc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn aux_build() -> Rustc {
1818

1919
/// A `rustc` invocation builder.
2020
#[derive(Debug)]
21+
#[must_use]
2122
pub struct Rustc {
2223
cmd: Command,
2324
}

src/tools/run-make-support/src/rustdoc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub fn rustdoc() -> Rustdoc {
1717
}
1818

1919
#[derive(Debug)]
20+
#[must_use]
2021
pub struct Rustdoc {
2122
cmd: Command,
2223
}

0 commit comments

Comments
 (0)