Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 86a9a75

Browse files
authored
Merge pull request #342 from tgross35/adjust-features
Adjust how intrinsics are enabled
2 parents 2063e36 + da5144f commit 86a9a75

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ default = []
1818

1919
# This tells the compiler to assume that a Nightly toolchain is being used and
2020
# that it should activate any useful Nightly things accordingly.
21-
unstable = []
21+
unstable = ["unstable-intrinsics"]
22+
23+
# Enable calls to functions in `core::intrinsics`
24+
unstable-intrinsics = []
2225

2326
# Used to prevent using any intrinsics or arch-specific code.
27+
#
28+
# HACK: this is a negative feature which is generally a bad idea in Cargo, but
29+
# we need it to be able to forbid other features when this crate is used in
30+
# Rust dependencies. Setting this overrides all features that may enable
31+
# hard float operations.
2432
force-soft-floats = []
2533

2634
[workspace]

build.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::env;
33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
55
println!("cargo:rustc-check-cfg=cfg(assert_no_panic)");
6-
println!("cargo:rustc-check-cfg=cfg(feature, values(\"unstable\"))");
76

87
println!("cargo:rustc-check-cfg=cfg(feature, values(\"checked\"))");
98

@@ -14,4 +13,18 @@ fn main() {
1413
println!("cargo:rustc-cfg=assert_no_panic");
1514
}
1615
}
16+
17+
configure_intrinsics();
18+
}
19+
20+
/// Simplify the feature logic for enabling intrinsics so code only needs to use
21+
/// `cfg(intrinsics_enabled)`.
22+
fn configure_intrinsics() {
23+
println!("cargo:rustc-check-cfg=cfg(intrinsics_enabled)");
24+
25+
// Disabled by default; `unstable-intrinsics` enables again; `force-soft-floats` overrides
26+
// to disable.
27+
if cfg!(feature = "unstable-intrinsics") && !cfg!(feature = "force-soft-floats") {
28+
println!("cargo:rustc-cfg=intrinsics_enabled");
29+
}
1730
}

ci/run.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ if [ "$(uname -a)" = "Linux" ]; then
4646
extra_flags="$extra_flags --features libm-test/test-musl-serialized"
4747
fi
4848

49+
# Make sure we can build with overriding features. We test the indibidual
50+
# features it controls separately.
51+
cargo check --features "force-soft-floats"
52+
4953
if [ "${BUILD_ONLY:-}" = "1" ]; then
5054
cmd="cargo build --target $target --package libm"
5155
$cmd
52-
$cmd --features 'unstable'
56+
$cmd --features "unstable-intrinsics"
5357

5458
echo "can't run tests on $target"
5559
else
@@ -60,6 +64,6 @@ else
6064
$cmd --release
6165

6266
# unstable with a feature
63-
$cmd --features 'unstable'
64-
$cmd --release --features 'unstable'
67+
$cmd --features "unstable-intrinsics"
68+
$cmd --release --features "unstable-intrinsics"
6569
fi

crates/compiler-builtins-smoke-test/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ test = false
1010
bench = false
1111

1212
[features]
13+
# Duplicated from libm's Cargo.toml
1314
unstable = []
15+
unstable-intrinsics = []
1416
checked = []
1517
force-soft-floats = []
18+
19+
[lints.rust]
20+
unexpected_cfgs = { level = "warn", check-cfg = [
21+
"cfg(assert_no_panic)",
22+
"cfg(intrinsics_enabled)",
23+
] }

crates/compiler-builtins-smoke-test/build.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! libm in pure Rust
22
#![no_std]
3-
#![cfg_attr(feature = "unstable", allow(internal_features))]
4-
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
3+
#![cfg_attr(intrinsics_enabled, allow(internal_features))]
4+
#![cfg_attr(intrinsics_enabled, feature(core_intrinsics))]
55
#![allow(clippy::assign_op_pattern)]
66
#![allow(clippy::deprecated_cfg_attr)]
77
#![allow(clippy::eq_op)]

src/math/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ macro_rules! i {
6060
// the time of this writing this is only used in a few places, and once
6161
// rust-lang/rust#72751 is fixed then this macro will no longer be necessary and
6262
// the native `/` operator can be used and panics won't be codegen'd.
63-
#[cfg(any(debug_assertions, not(feature = "unstable")))]
63+
#[cfg(any(debug_assertions, not(intrinsics_enabled)))]
6464
macro_rules! div {
6565
($a:expr, $b:expr) => {
6666
$a / $b
6767
};
6868
}
6969

70-
#[cfg(all(not(debug_assertions), feature = "unstable"))]
70+
#[cfg(all(not(debug_assertions), intrinsics_enabled))]
7171
macro_rules! div {
7272
($a:expr, $b:expr) => {
7373
unsafe { core::intrinsics::unchecked_div($a, $b) }
@@ -76,7 +76,7 @@ macro_rules! div {
7676

7777
macro_rules! llvm_intrinsically_optimized {
7878
(#[cfg($($clause:tt)*)] $e:expr) => {
79-
#[cfg(all(feature = "unstable", not(feature = "force-soft-floats"), $($clause)*))]
79+
#[cfg(all(intrinsics_enabled, not(feature = "force-soft-floats"), $($clause)*))]
8080
{
8181
if true { // thwart the dead code lint
8282
$e

0 commit comments

Comments
 (0)