Skip to content

Commit 31a3497

Browse files
committed
Allow forbidden target features to be hard errors
1 parent c043085 commit 31a3497

3 files changed

Lines changed: 68 additions & 21 deletions

File tree

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,14 +1203,17 @@ pub(crate) struct UnstableCTargetFeature<'a> {
12031203

12041204
#[derive(Diagnostic)]
12051205
#[diag("target feature `{$feature}` cannot be {$enabled} with `-Ctarget-feature`: {$reason}")]
1206-
#[note(
1207-
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
1208-
)]
1209-
#[note("for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>")]
12101206
pub(crate) struct ForbiddenCTargetFeature<'a> {
12111207
pub feature: &'a str,
12121208
pub enabled: &'a str,
12131209
pub reason: &'a str,
1210+
#[note(
1211+
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
1212+
)]
1213+
#[note(
1214+
"for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>"
1215+
)]
1216+
pub future_compat_note: bool,
12141217
}
12151218

12161219
pub struct TargetFeatureDisableOrEnable<'a> {

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,19 @@ pub fn cfg_target_feature<'a, const N: usize>(
298298
sess.dcx().emit_warn(unknown_feature);
299299
}
300300
Some((_, stability, _)) => {
301-
if let Err(reason) = stability.toggle_allowed() {
302-
sess.dcx().emit_warn(errors::ForbiddenCTargetFeature {
301+
if let Stability::Forbidden { reason, hard_error } = stability {
302+
let diag = errors::ForbiddenCTargetFeature {
303303
feature: base_feature,
304304
enabled: if enable { "enabled" } else { "disabled" },
305305
reason,
306-
});
306+
future_compat_note: !hard_error,
307+
};
308+
309+
if *hard_error {
310+
sess.dcx().emit_err(diag);
311+
} else {
312+
sess.dcx().emit_warn(diag);
313+
}
307314
} else if stability.requires_nightly().is_some() {
308315
// An unstable feature. Warn about using it. It makes little sense
309316
// to hard-error here since we just warn about fully unknown

compiler/rustc_target/src/target_features.rs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ pub enum Stability {
2828
/// set in the target spec. It is never set in `cfg(target_feature)`. Used in
2929
/// particular for features are actually ABI configuration flags (not all targets are as nice as
3030
/// RISC-V and have an explicit way to set the ABI separate from target features).
31-
Forbidden { reason: &'static str },
31+
Forbidden {
32+
reason: &'static str,
33+
/// True if this is always an error, false if this can be reported as a warning when set via
34+
/// `-Ctarget-feature`.
35+
hard_error: bool,
36+
},
3237
}
3338
use Stability::*;
3439

@@ -41,8 +46,9 @@ impl<CTX> HashStable<CTX> for Stability {
4146
Stability::Unstable(nightly_feature) => {
4247
nightly_feature.hash_stable(hcx, hasher);
4348
}
44-
Stability::Forbidden { reason } => {
49+
Stability::Forbidden { reason, hard_error } => {
4550
reason.hash_stable(hcx, hasher);
51+
hard_error.hash_stable(hcx, hasher);
4652
}
4753
}
4854
}
@@ -73,12 +79,12 @@ impl Stability {
7379
}
7480

7581
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
76-
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
82+
/// (It might still be nightly-only even if this returns `Ok(())`, so make sure to also check
7783
/// `requires_nightly`.)
7884
pub fn toggle_allowed(&self) -> Result<(), &'static str> {
7985
match self {
8086
Stability::Unstable(_) | Stability::Stable { .. } => Ok(()),
81-
Stability::Forbidden { reason } => Err(reason),
87+
Stability::Forbidden { reason, hard_error: _ } => Err(reason),
8288
}
8389
}
8490
}
@@ -135,7 +141,10 @@ static ARM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
135141
("aes", Unstable(sym::arm_target_feature), &["neon"]),
136142
(
137143
"atomics-32",
138-
Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
144+
Stability::Forbidden {
145+
reason: "unsound because it changes the ABI of atomic operations",
146+
hard_error: false,
147+
},
139148
&[],
140149
),
141150
("crc", Unstable(sym::arm_target_feature), &[]),
@@ -211,7 +220,11 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
211220
// FEAT_FLAGM2
212221
("flagm2", Unstable(sym::aarch64_unstable_target_feature), &[]),
213222
// We forbid directly toggling just `fp-armv8`; it must be toggled with `neon`.
214-
("fp-armv8", Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`" }, &[]),
223+
(
224+
"fp-armv8",
225+
Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`", hard_error: false },
226+
&[],
227+
),
215228
// FEAT_FP8
216229
("fp8", Unstable(sym::aarch64_unstable_target_feature), &["faminmax", "lut", "bf16"]),
217230
// FEAT_FP8DOT2
@@ -274,7 +287,11 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
274287
("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
275288
// FEAT_RDM
276289
("rdm", Stable, &["neon"]),
277-
("reserve-x18", Forbidden { reason: "use `-Zfixed-x18` compiler flag instead" }, &[]),
290+
(
291+
"reserve-x18",
292+
Forbidden { reason: "use `-Zfixed-x18` compiler flag instead", hard_error: false },
293+
&[],
294+
),
278295
// FEAT_SB
279296
("sb", Stable, &[]),
280297
// FEAT_SHA1 & FEAT_SHA256
@@ -448,25 +465,38 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
448465
("rdseed", Stable, &[]),
449466
(
450467
"retpoline-external-thunk",
451-
Stability::Forbidden { reason: "use `-Zretpoline-external-thunk` compiler flag instead" },
468+
Stability::Forbidden {
469+
reason: "use `-Zretpoline-external-thunk` compiler flag instead",
470+
hard_error: false,
471+
},
452472
&[],
453473
),
454474
(
455475
"retpoline-indirect-branches",
456-
Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
476+
Stability::Forbidden {
477+
reason: "use `-Zretpoline` compiler flag instead",
478+
hard_error: false,
479+
},
457480
&[],
458481
),
459482
(
460483
"retpoline-indirect-calls",
461-
Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
484+
Stability::Forbidden {
485+
reason: "use `-Zretpoline` compiler flag instead",
486+
hard_error: false,
487+
},
462488
&[],
463489
),
464490
("rtm", Unstable(sym::rtm_target_feature), &[]),
465491
("sha", Stable, &["sse2"]),
466492
("sha512", Stable, &["avx2"]),
467493
("sm3", Stable, &["avx"]),
468494
("sm4", Stable, &["avx2"]),
469-
("soft-float", Stability::Forbidden { reason: "use a soft-float target instead" }, &[]),
495+
(
496+
"soft-float",
497+
Stability::Forbidden { reason: "use a soft-float target instead", hard_error: false },
498+
&[],
499+
),
470500
("sse", Stable, &[]),
471501
("sse2", Stable, &["sse"]),
472502
("sse3", Stable, &["sse2"]),
@@ -608,7 +638,10 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
608638
("f", Unstable(sym::riscv_target_feature), &["zicsr"]),
609639
(
610640
"forced-atomics",
611-
Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
641+
Stability::Forbidden {
642+
reason: "unsound because it changes the ABI of atomic operations",
643+
hard_error: false,
644+
},
612645
&[],
613646
),
614647
("m", Stable, &[]),
@@ -863,7 +896,7 @@ const IBMZ_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
863896
("miscellaneous-extensions-3", Stable, &[]),
864897
("miscellaneous-extensions-4", Stable, &[]),
865898
("nnp-assist", Stable, &["vector"]),
866-
("soft-float", Forbidden { reason: "unsupported ABI-configuration feature" }, &[]),
899+
("soft-float", Forbidden { reason: "unsupported ABI-configuration feature", hard_error: false }, &[]),
867900
("transactional-execution", Unstable(sym::s390x_target_feature), &[]),
868901
("vector", Stable, &[]),
869902
("vector-enhancements-1", Stable, &["vector"]),
@@ -915,7 +948,11 @@ static AVR_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
915948
("rmw", Unstable(sym::avr_target_feature), &[]),
916949
("spm", Unstable(sym::avr_target_feature), &[]),
917950
("spmx", Unstable(sym::avr_target_feature), &[]),
918-
("sram", Forbidden { reason: "devices that have no SRAM are unsupported" }, &[]),
951+
(
952+
"sram",
953+
Forbidden { reason: "devices that have no SRAM are unsupported", hard_error: false },
954+
&[],
955+
),
919956
("tinyencoding", Unstable(sym::avr_target_feature), &[]),
920957
// tidy-alphabetical-end
921958
];

0 commit comments

Comments
 (0)