Skip to content

Commit 57bb38b

Browse files
authored
Rollup merge of #142297 - jieyouxu:needs-target-std, r=Kobzol
Implement `//@ needs-target-std` compiletest directive Closes #141863. Needed to unblock #139244 and #141856. ### Summary This PR implements a `//@ needs-target-std` compiletest directive that gates test execution based on whether the target supports std or not. For some cases, this should be preferred over e.g. some combination of `//@ ignore-none`, `//@ ignore-nvptx` and more[^none-limit]. ### Implementation limitation Unfortunately, since there is currently [no reliable way to determine from metadata whether a given target supports std or not](#142296), we have to resort to a hack. Bootstrap currently determines whether or not a target supports std by a naive target tuple substring comparison: a target supports std if its target tuple does *not* contain one of `["-none", "nvptx", "switch"]` substrings. This PR simply pulls that hack out into `build_helpers` to avoid reimplementing the same hack in compiletest, and uses that logic to inform `//@ needs-target-std`. ### Auxiliary changes This PR additionally changes a few run-make tests to use `//@ needs-target-std` over an inconsistent combination of target-based `ignore`s. This should help with #139244. --- r? bootstrap [^none-limit]: Notably, `target_os = "none"` is **not** a sufficient condition for "target does not support std"
2 parents 407e568 + c558db3 commit 57bb38b

File tree

13 files changed

+37
-25
lines changed

13 files changed

+37
-25
lines changed

src/bootstrap/src/core/config/toml/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct Target {
8484
impl Target {
8585
pub fn from_triple(triple: &str) -> Self {
8686
let mut target: Self = Default::default();
87-
if triple.contains("-none") || triple.contains("nvptx") || triple.contains("switch") {
87+
if !build_helper::targets::target_supports_std(triple) {
8888
target.no_std = true;
8989
}
9090
if triple.contains("emscripten") {

src/build_helper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod fs;
66
pub mod git;
77
pub mod metrics;
88
pub mod stage0_parser;
9+
pub mod targets;
910
pub mod util;
1011

1112
/// The default set of crates for opt-dist to collect LLVM profiles.

src/build_helper/src/targets.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// FIXME(#142296): this hack is because there is no reliable way (yet) to determine whether a given
2+
// target supports std. In the long-term, we should try to implement a way to *reliably* determine
3+
// target (std) metadata.
4+
//
5+
// NOTE: this is pulled out to `build_helpers` to share this hack between `bootstrap` and
6+
// `compiletest`.
7+
pub fn target_supports_std(target_tuple: &str) -> bool {
8+
!(target_tuple.contains("-none")
9+
|| target_tuple.contains("nvptx")
10+
|| target_tuple.contains("switch"))
11+
}

src/doc/rustc-dev-guide/src/tests/directives.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ settings:
202202
`//@ needs-crate-type: cdylib, proc-macro` will cause the test to be ignored
203203
on `wasm32-unknown-unknown` target because the target does not support the
204204
`proc-macro` crate type.
205+
- `needs-target-std` — ignores if target platform does not have std support.
205206

206207
The following directives will check LLVM support:
207208

src/tools/compiletest/src/directive-list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
166166
"needs-subprocess",
167167
"needs-symlink",
168168
"needs-target-has-atomic",
169+
"needs-target-std",
169170
"needs-threads",
170171
"needs-unwind",
171172
"needs-wasmtime",

src/tools/compiletest/src/header/needs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ pub(super) fn handle_needs(
174174
condition: config.with_std_debug_assertions,
175175
ignore_reason: "ignored if std wasn't built with debug assertions",
176176
},
177+
Need {
178+
name: "needs-target-std",
179+
condition: build_helper::targets::target_supports_std(&config.target),
180+
ignore_reason: "ignored if target does not support std",
181+
},
177182
];
178183

179184
let (name, rest) = match ln.split_once([':', ' ']) {

src/tools/compiletest/src/header/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,14 @@ fn test_ignore_auxiliary() {
945945
let config = cfg().build();
946946
assert!(check_ignore(&config, "//@ ignore-auxiliary"));
947947
}
948+
949+
#[test]
950+
fn test_needs_target_std() {
951+
// Cherry-picks two targets:
952+
// 1. `x86_64-unknown-none`: Tier 2, intentionally never supports std.
953+
// 2. `x86_64-unknown-linux-gnu`: Tier 1, always supports std.
954+
let config = cfg().target("x86_64-unknown-none").build();
955+
assert!(check_ignore(&config, "//@ needs-target-std"));
956+
let config = cfg().target("x86_64-unknown-linux-gnu").build();
957+
assert!(!check_ignore(&config, "//@ needs-target-std"));
958+
}

tests/run-make/cpp-global-destructors/rmake.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
//@ ignore-cross-compile
77
// Reason: the compiled binary is executed
88

9-
//@ ignore-none
10-
// Reason: no-std is not supported.
119
//@ ignore-wasm32
1210
//@ ignore-wasm64
1311
// Reason: compiling C++ to WASM may cause problems.
1412

15-
// Neither of these are tested in full CI.
16-
//@ ignore-nvptx64-nvidia-cuda
17-
// Reason: can't find crate "std"
13+
// Not exercised in full CI, but sgx technically supports std.
1814
//@ ignore-sgx
1915

2016
use run_make_support::{build_native_static_lib_cxx, run, rustc};

tests/run-make/export-executable-symbols/rmake.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
// (See #85673)
1111
//@ ignore-wasm32
1212
//@ ignore-wasm64
13-
//@ ignore-none
14-
// Reason: no-std is not supported
13+
//@ needs-target-std
1514

1615
use run_make_support::{bin_name, llvm_readobj, rustc};
1716

tests/run-make/incr-foreign-head-span/rmake.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
// source file from disk during compilation of a downstream crate.
66
// See https://github.com/rust-lang/rust/issues/86480
77

8-
//@ ignore-none
9-
// Reason: no-std is not supported
10-
//@ ignore-nvptx64-nvidia-cuda
11-
// Reason: can't find crate for 'std'
8+
//@ needs-target-std
129

1310
use run_make_support::{rfs, rust_lib_name, rustc};
1411

tests/run-make/incr-prev-body-beyond-eof/rmake.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
// was hashed by rustc in addition to the span length, and the fix still
88
// works.
99

10-
//@ ignore-none
11-
// reason: no-std is not supported
12-
13-
//@ ignore-nvptx64-nvidia-cuda
14-
// FIXME: can't find crate for `std`
10+
//@ needs-target-std
1511

1612
use run_make_support::{rfs, rustc};
1713

tests/run-make/incr-test-moved-file/rmake.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
// for successful compilation.
1010
// See https://github.com/rust-lang/rust/issues/83112
1111

12-
//@ ignore-none
13-
// Reason: no-std is not supported
14-
//@ ignore-nvptx64-nvidia-cuda
15-
// FIXME: can't find crate for 'std'
12+
//@ needs-target-std
1613

1714
use run_make_support::{rfs, rustc};
1815

tests/run-make/moved-src-dir-fingerprint-ice/rmake.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
// sessions.
1313
// See https://github.com/rust-lang/rust/issues/85019
1414

15-
//@ ignore-none
16-
// Reason: no-std is not supported
17-
//@ ignore-nvptx64-nvidia-cuda
18-
// FIXME: can't find crate for 'std'
15+
//@ needs-target-std
1916

2017
use run_make_support::{rfs, rust_lib_name, rustc};
2118

0 commit comments

Comments
 (0)