Skip to content

Commit fb00adb

Browse files
committed
forbid toggling x87 and fpregs on hard-float targets
1 parent 59b7a46 commit fb00adb

20 files changed

+129
-8
lines changed

compiler/rustc_feature/src/unstable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ declare_features! (
342342
(unstable, sse4a_target_feature, "1.27.0", Some(44839)),
343343
(unstable, tbm_target_feature, "1.27.0", Some(44839)),
344344
(unstable, wasm_target_feature, "1.30.0", Some(44839)),
345+
(unstable, x87_target_feature, "CURRENT_RUSTC_VERSION", Some(44839)),
345346
// !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!!
346347
// Features are listed in alphabetical order. Tidy will fail if you don't keep it this way.
347348
// !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!!

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,7 @@ symbols! {
21822182
writeln_macro,
21832183
x86_amx_intrinsics,
21842184
x87_reg,
2185+
x87_target_feature,
21852186
xer,
21862187
xmm_reg,
21872188
xop_target_feature,

compiler/rustc_target/src/spec/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,18 @@ impl TargetOptions {
26012601
.collect();
26022602
}
26032603
}
2604+
2605+
pub(crate) fn has_feature(&self, search_feature: &str) -> bool {
2606+
self.features.split(',').any(|f| {
2607+
if let Some(f) = f.strip_prefix('+')
2608+
&& f == search_feature
2609+
{
2610+
true
2611+
} else {
2612+
false
2613+
}
2614+
})
2615+
}
26042616
}
26052617

26062618
impl Default for TargetOptions {

compiler/rustc_target/src/target_features.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub enum Stability<AllowToggle> {
3232
/// This target feature is unstable. It is only present in `#[cfg(target_feature)]` on
3333
/// nightly and using it in `#[target_feature]` requires enabling the given nightly feature.
3434
Unstable {
35+
/// This must be a *language* feature, or else rustc will ICE when reporting a missing
36+
/// feature gate!
3537
nightly_feature: Symbol,
3638
/// See `Stable::allow_toggle` comment above.
3739
allow_toggle: AllowToggle,
@@ -168,6 +170,22 @@ const ARM_FEATURES: &[(&str, StabilityUncomputed, ImpliedFeatures)] = &[
168170
("dotprod", unstable(sym::arm_target_feature), &["neon"]),
169171
("dsp", unstable(sym::arm_target_feature), &[]),
170172
("fp-armv8", unstable(sym::arm_target_feature), &["vfp4"]),
173+
(
174+
"fpregs",
175+
Stability::Unstable {
176+
nightly_feature: sym::arm_target_feature,
177+
allow_toggle: |target: &Target| {
178+
// Only allow toggling this if the target has `soft-float` set. With `soft-float`,
179+
// `fpregs` isn't needed so changing it cannot affect the ABI.
180+
if target.has_feature("soft-float") {
181+
Ok(())
182+
} else {
183+
Err("unsound on hard-float targets because it changes float ABI")
184+
}
185+
},
186+
},
187+
&[],
188+
),
171189
("i8mm", unstable(sym::arm_target_feature), &["neon"]),
172190
("mclass", unstable(sym::arm_target_feature), &[]),
173191
("neon", unstable(sym::arm_target_feature), &["vfp3"]),
@@ -191,7 +209,6 @@ const ARM_FEATURES: &[(&str, StabilityUncomputed, ImpliedFeatures)] = &[
191209
("vfp4", unstable(sym::arm_target_feature), &["vfp3"]),
192210
("virtualization", unstable(sym::arm_target_feature), &[]),
193211
// tidy-alphabetical-end
194-
// FIXME: need to also forbid turning off `fpregs` on hardfloat targets
195212
];
196213

197214
const AARCH64_FEATURES: &[(&str, StabilityUncomputed, ImpliedFeatures)] = &[
@@ -450,13 +467,28 @@ const X86_FEATURES: &[(&str, StabilityUncomputed, ImpliedFeatures)] = &[
450467
("tbm", unstable(sym::tbm_target_feature), &[]),
451468
("vaes", unstable(sym::avx512_target_feature), &["avx2", "aes"]),
452469
("vpclmulqdq", unstable(sym::avx512_target_feature), &["avx", "pclmulqdq"]),
470+
(
471+
"x87",
472+
Stability::Unstable {
473+
nightly_feature: sym::x87_target_feature,
474+
allow_toggle: |target: &Target| {
475+
// Only allow toggling this if the target has `soft-float` set. With `soft-float`,
476+
// `fpregs` isn't needed so changing it cannot affect the ABI.
477+
if target.has_feature("soft-float") {
478+
Ok(())
479+
} else {
480+
Err("unsound on hard-float targets because it changes float ABI")
481+
}
482+
},
483+
},
484+
&[],
485+
),
453486
("xop", unstable(sym::xop_target_feature), &[/*"fma4", */ "avx", "sse4a"]),
454487
("xsave", STABLE, &[]),
455488
("xsavec", STABLE, &["xsave"]),
456489
("xsaveopt", STABLE, &["xsave"]),
457490
("xsaves", STABLE, &["xsave"]),
458491
// tidy-alphabetical-end
459-
// FIXME: need to also forbid turning off `x87` on hardfloat targets
460492
];
461493

462494
const HEXAGON_FEATURES: &[(&str, StabilityUncomputed, ImpliedFeatures)] = &[

tests/ui/check-cfg/target_feature.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
9898
`fp8dot2`
9999
`fp8dot4`
100100
`fp8fma`
101+
`fpregs`
101102
`fpuv2_df`
102103
`fpuv2_sf`
103104
`fpuv3_df`
@@ -260,6 +261,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
260261
`vsx`
261262
`wfxt`
262263
`wide-arithmetic`
264+
`x87`
263265
`xop`
264266
`xsave`
265267
`xsavec`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: --target=x86_64-unknown-none --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
//@ build-pass
4+
#![feature(no_core, lang_items, x87_target_feature)]
5+
#![no_core]
6+
7+
#[lang = "sized"]
8+
pub trait Sized {}
9+
10+
#[target_feature(enable = "x87")]
11+
pub unsafe fn my_fun() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ compile-flags: --target=x86_64-unknown-none --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
//@ compile-flags: -Ctarget-feature=-x87
4+
//@ build-pass
5+
#![feature(no_core, lang_items)]
6+
#![no_core]
7+
8+
#[lang = "sized"]
9+
pub trait Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: unstable feature specified for `-Ctarget-feature`: `x87`
2+
|
3+
= note: this feature is not stably supported; its behavior can change in the future
4+
5+
warning: 1 warning emitted
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
#![feature(no_core, lang_items)]
4+
#![no_core]
5+
6+
#[lang = "sized"]
7+
pub trait Sized {}
8+
9+
#[target_feature(enable = "x87")]
10+
//~^ERROR: cannot be toggled with
11+
pub unsafe fn my_fun() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: target feature `x87` cannot be toggled with `#[target_feature]`: unsound on hard-float targets because it changes float ABI
2+
--> $DIR/forbidden-hardfloat-target-feature-attribute.rs:9:18
3+
|
4+
LL | #[target_feature(enable = "x87")]
5+
| ^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
//@ check-pass
4+
#![feature(no_core, lang_items)]
5+
#![no_core]
6+
#![allow(unexpected_cfgs)]
7+
8+
#[lang = "sized"]
9+
pub trait Sized {}
10+
11+
// The compile_error macro does not exist, so if the `cfg` evaluates to `true` this
12+
// complains about the missing macro rather than showing the error... but that's good enough.
13+
#[cfg(not(target_feature = "x87"))]
14+
compile_error!("the x87 feature *should* be exposed in `cfg`");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
//@ compile-flags: -Ctarget-feature=-x87
4+
// For now this is just a warning.
5+
//@ build-pass
6+
#![feature(no_core, lang_items)]
7+
#![no_core]
8+
9+
#[lang = "sized"]
10+
pub trait Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `x87` cannot be toggled with `-Ctarget-feature`: unsound on hard-float targets because it changes float ABI
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+

tests/ui/target-feature/forbidden-target-feature-attribute.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
22
//@ needs-llvm-components: x86
33
#![feature(no_core, lang_items)]
4-
#![no_std]
54
#![no_core]
65

76
#[lang = "sized"]

tests/ui/target-feature/forbidden-target-feature-attribute.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: target feature `soft-float` cannot be toggled with `#[target_feature]`: unsound because it changes float ABI
2-
--> $DIR/forbidden-target-feature-attribute.rs:10:18
2+
--> $DIR/forbidden-target-feature-attribute.rs:9:18
33
|
44
LL | #[target_feature(enable = "soft-float")]
55
| ^^^^^^^^^^^^^^^^^^^^^

tests/ui/target-feature/forbidden-target-feature-cfg.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//@ needs-llvm-components: x86
33
//@ check-pass
44
#![feature(no_core, lang_items)]
5-
#![no_std]
65
#![no_core]
76
#![allow(unexpected_cfgs)]
87

tests/ui/target-feature/forbidden-target-feature-flag-disable.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// For now this is just a warning.
55
//@ build-pass
66
#![feature(no_core, lang_items)]
7-
#![no_std]
87
#![no_core]
98

109
#[lang = "sized"]

tests/ui/target-feature/forbidden-target-feature-flag.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// For now this is just a warning.
55
//@ build-pass
66
#![feature(no_core, lang_items)]
7-
#![no_std]
87
#![no_core]
98

109
#[lang = "sized"]

tests/ui/target-feature/gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// gate-test-prfchw_target_feature
2525
// gate-test-s390x_target_feature
2626
// gate-test-sparc_target_feature
27+
// gate-test-x87_target_feature
2728

2829
#[target_feature(enable = "avx512bw")]
2930
//~^ ERROR: currently unstable

tests/ui/target-feature/gate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: the target feature `avx512bw` is currently unstable
2-
--> $DIR/gate.rs:28:18
2+
--> $DIR/gate.rs:29:18
33
|
44
LL | #[target_feature(enable = "avx512bw")]
55
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)