Skip to content

Commit b15b279

Browse files
authored
Move experimental Rust features behind a --cfg=. (#754)
Instead of just autodetecting nightly-only Rust features, put them behind a `--cfg` so that they're not feature-detected for users.
1 parent 88a53f2 commit b15b279

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ jobs:
221221
runs-on: ${{ matrix.os }}
222222
env:
223223
QEMU_BUILD_VERSION: 8.0.2
224+
# Enabling testing of experimental features.
225+
RUSTFLAGS: --cfg rustix_use_experimental_features
224226
strategy:
225227
matrix:
226228
build: [ubuntu, ubuntu-20.04, i686-linux, aarch64-linux, powerpc64le-linux, riscv64-linux, s390x-linux, arm-linux, ubuntu-stable, ubuntu-1.63, i686-linux-stable, aarch64-linux-stable, riscv64-linux-stable, s390x-linux-stable, mipsel-linux-stable, mips64el-linux-stable, powerpc64le-linux-stable, arm-linux-stable, ubuntu-1.63, i686-linux-1.63, aarch64-linux-1.63, riscv64-linux-1.63, s390x-linux-1.63, mipsel-linux-1.63, mips64el-linux-1.63, powerpc64le-linux-1.63, arm-linux-1.63, macos-latest, macos-11, windows, windows-2019]

build.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ fn main() {
99
// the rustc version.
1010
println!("cargo:rerun-if-changed=build.rs");
1111

12-
use_feature_or_nothing("rustc_attrs");
13-
14-
// Features only used in no-std configurations.
15-
#[cfg(not(feature = "std"))]
16-
{
17-
use_feature_or_nothing("core_c_str");
18-
use_feature_or_nothing("core_ffi_c");
19-
use_feature_or_nothing("alloc_c_string");
20-
use_feature_or_nothing("alloc_ffi");
21-
}
22-
2312
// Gather target information.
2413
let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
2514
let env = var("CARGO_CFG_TARGET_ENV").unwrap();
@@ -45,6 +34,13 @@ fn main() {
4534
// enable the libc backend even if rustix is depended on transitively.
4635
let cfg_use_libc = var("CARGO_CFG_RUSTIX_USE_LIBC").is_ok();
4736

37+
// Check for eg. `RUSTFLAGS=--cfg=rustix_use_experimental_features`. This
38+
// is a rustc flag rather than a cargo feature flag because it's
39+
// experimental and not something we want accidentally enabled via
40+
// `--all-features`.
41+
let rustix_use_experimental_features =
42+
var("CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_FEATURES").is_ok();
43+
4844
// Check for eg. `RUSTFLAGS=--cfg=rustix_use_experimental_asm`. This is a
4945
// rustc flag rather than a cargo feature flag because it's experimental
5046
// and not something we want accidentally enabled via `--all-features`.
@@ -54,6 +50,27 @@ fn main() {
5450
// libc FFI calls, so if we're running under miri, use the libc backend.
5551
let miri = var("CARGO_CFG_MIRI").is_ok();
5652

53+
// If experimental features are enabled, auto-detect and use available
54+
// features.
55+
if rustix_use_experimental_features {
56+
use_feature_or_nothing("rustc_attrs");
57+
use_feature_or_nothing("core_intrinsics");
58+
}
59+
60+
// Features needed only in no-std configurations.
61+
#[cfg(not(feature = "std"))]
62+
{
63+
use_feature_or_nothing("core_c_str");
64+
use_feature_or_nothing("core_ffi_c");
65+
use_feature_or_nothing("alloc_c_string");
66+
use_feature_or_nothing("alloc_ffi");
67+
}
68+
69+
// Feature needed for testing.
70+
if use_static_assertions() {
71+
use_feature("static_assertions");
72+
}
73+
5774
// If the libc backend is requested, or if we're not on a platform for
5875
// which we have linux_raw support, use the libc backend.
5976
//
@@ -74,7 +91,6 @@ fn main() {
7491
} else {
7592
// Use the linux_raw backend.
7693
use_feature("linux_raw");
77-
use_feature_or_nothing("core_intrinsics");
7894
if rustix_use_experimental_asm {
7995
use_feature("asm_experimental_arch");
8096
}
@@ -133,14 +149,6 @@ fn main() {
133149
use_feature("fix_y2038");
134150
}
135151

136-
if os == "wasi" {
137-
use_feature_or_nothing("wasi_ext");
138-
}
139-
140-
if use_static_assertions() {
141-
use_feature("static_assertions");
142-
}
143-
144152
println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM");
145153
println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_LIBC");
146154

0 commit comments

Comments
 (0)