Skip to content

Commit 291765f

Browse files
committed
Allow forbidden target features to be hard errors
1 parent d1ee5e5 commit 291765f

3 files changed

Lines changed: 66 additions & 20 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
@@ -299,12 +299,19 @@ pub fn cfg_target_feature<'a, const N: usize>(
299299
sess.dcx().emit_warn(unknown_feature);
300300
}
301301
Some((_, stability, _)) => {
302-
if let Err(reason) = stability.toggle_allowed() {
303-
sess.dcx().emit_warn(errors::ForbiddenCTargetFeature {
302+
if let Stability::Forbidden { reason, hard_error } = stability {
303+
let diag = errors::ForbiddenCTargetFeature {
304304
feature: base_feature,
305305
enabled: if enable { "enabled" } else { "disabled" },
306306
reason,
307-
});
307+
future_compat_note: !hard_error,
308+
};
309+
310+
if *hard_error {
311+
sess.dcx().emit_err(diag);
312+
} else {
313+
sess.dcx().emit_warn(diag);
314+
}
308315
} else if stability.requires_nightly().is_some() {
309316
// An unstable feature. Warn about using it. It makes little sense
310317
// to hard-error here since we just warn about fully unknown

compiler/rustc_target/src/target_features.rs

Lines changed: 49 additions & 13 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

@@ -57,12 +62,12 @@ impl Stability {
5762
}
5863

5964
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
60-
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
65+
/// (It might still be nightly-only even if this returns `Ok(())`, so make sure to also check
6166
/// `requires_nightly`.)
6267
pub fn toggle_allowed(&self) -> Result<(), &'static str> {
6368
match self {
6469
Stability::Unstable(_) | Stability::Stable { .. } => Ok(()),
65-
Stability::Forbidden { reason } => Err(reason),
70+
Stability::Forbidden { reason, hard_error: _ } => Err(reason),
6671
}
6772
}
6873
}
@@ -119,7 +124,10 @@ static ARM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
119124
("aes", Unstable(sym::arm_target_feature), &["neon"]),
120125
(
121126
"atomics-32",
122-
Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
127+
Stability::Forbidden {
128+
reason: "unsound because it changes the ABI of atomic operations",
129+
hard_error: false,
130+
},
123131
&[],
124132
),
125133
("crc", Unstable(sym::arm_target_feature), &[]),
@@ -195,7 +203,11 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
195203
// FEAT_FLAGM2
196204
("flagm2", Unstable(sym::aarch64_unstable_target_feature), &[]),
197205
// We forbid directly toggling just `fp-armv8`; it must be toggled with `neon`.
198-
("fp-armv8", Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`" }, &[]),
206+
(
207+
"fp-armv8",
208+
Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`", hard_error: false },
209+
&[],
210+
),
199211
// FEAT_FP8
200212
("fp8", Unstable(sym::aarch64_unstable_target_feature), &["faminmax", "lut", "bf16"]),
201213
// FEAT_FP8DOT2
@@ -258,7 +270,11 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
258270
("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
259271
// FEAT_RDM
260272
("rdm", Stable, &["neon"]),
261-
("reserve-x18", Forbidden { reason: "use `-Zfixed-x18` compiler flag instead" }, &[]),
273+
(
274+
"reserve-x18",
275+
Forbidden { reason: "use `-Zfixed-x18` compiler flag instead", hard_error: false },
276+
&[],
277+
),
262278
// FEAT_SB
263279
("sb", Stable, &[]),
264280
// FEAT_SHA1 & FEAT_SHA256
@@ -432,25 +448,38 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
432448
("rdseed", Stable, &[]),
433449
(
434450
"retpoline-external-thunk",
435-
Stability::Forbidden { reason: "use `-Zretpoline-external-thunk` compiler flag instead" },
451+
Stability::Forbidden {
452+
reason: "use `-Zretpoline-external-thunk` compiler flag instead",
453+
hard_error: false,
454+
},
436455
&[],
437456
),
438457
(
439458
"retpoline-indirect-branches",
440-
Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
459+
Stability::Forbidden {
460+
reason: "use `-Zretpoline` compiler flag instead",
461+
hard_error: false,
462+
},
441463
&[],
442464
),
443465
(
444466
"retpoline-indirect-calls",
445-
Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
467+
Stability::Forbidden {
468+
reason: "use `-Zretpoline` compiler flag instead",
469+
hard_error: false,
470+
},
446471
&[],
447472
),
448473
("rtm", Unstable(sym::rtm_target_feature), &[]),
449474
("sha", Stable, &["sse2"]),
450475
("sha512", Stable, &["avx2"]),
451476
("sm3", Stable, &["avx"]),
452477
("sm4", Stable, &["avx2"]),
453-
("soft-float", Stability::Forbidden { reason: "use a soft-float target instead" }, &[]),
478+
(
479+
"soft-float",
480+
Stability::Forbidden { reason: "use a soft-float target instead", hard_error: false },
481+
&[],
482+
),
454483
("sse", Stable, &[]),
455484
("sse2", Stable, &["sse"]),
456485
("sse3", Stable, &["sse2"]),
@@ -592,7 +621,10 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
592621
("f", Unstable(sym::riscv_target_feature), &["zicsr"]),
593622
(
594623
"forced-atomics",
595-
Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
624+
Stability::Forbidden {
625+
reason: "unsound because it changes the ABI of atomic operations",
626+
hard_error: false,
627+
},
596628
&[],
597629
),
598630
("m", Stable, &[]),
@@ -847,7 +879,7 @@ const IBMZ_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
847879
("miscellaneous-extensions-3", Stable, &[]),
848880
("miscellaneous-extensions-4", Stable, &[]),
849881
("nnp-assist", Stable, &["vector"]),
850-
("soft-float", Forbidden { reason: "unsupported ABI-configuration feature" }, &[]),
882+
("soft-float", Forbidden { reason: "unsupported ABI-configuration feature", hard_error: false }, &[]),
851883
("transactional-execution", Unstable(sym::s390x_target_feature), &[]),
852884
("vector", Stable, &[]),
853885
("vector-enhancements-1", Stable, &["vector"]),
@@ -899,7 +931,11 @@ static AVR_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
899931
("rmw", Unstable(sym::avr_target_feature), &[]),
900932
("spm", Unstable(sym::avr_target_feature), &[]),
901933
("spmx", Unstable(sym::avr_target_feature), &[]),
902-
("sram", Forbidden { reason: "devices that have no SRAM are unsupported" }, &[]),
934+
(
935+
"sram",
936+
Forbidden { reason: "devices that have no SRAM are unsupported", hard_error: false },
937+
&[],
938+
),
903939
("tinyencoding", Unstable(sym::avr_target_feature), &[]),
904940
// tidy-alphabetical-end
905941
];

0 commit comments

Comments
 (0)