Skip to content

Commit ffad9aa

Browse files
committed
mark some target features as 'forbidden' so they cannot be (un)set
For now, this is just a warning, but should become a hard error in the future
1 parent 2dece5b commit ffad9aa

23 files changed

+371
-157
lines changed

compiler/rustc_codegen_gcc/messages.ftl

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ codegen_gcc_invalid_minimum_alignment =
88
codegen_gcc_lto_not_supported =
99
LTO is not supported. You may get a linker error.
1010
11+
codegen_gcc_forbidden_ctarget_feature =
12+
target feature `{$feature}` cannot be toggled with `-Ctarget-feature`
13+
1114
codegen_gcc_unwinding_inline_asm =
1215
GCC backend does not support unwinding from inline asm
1316
@@ -24,11 +27,15 @@ codegen_gcc_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdyl
2427
codegen_gcc_lto_bitcode_from_rlib = failed to get bitcode from object file for LTO ({$gcc_err})
2528
2629
codegen_gcc_unknown_ctarget_feature =
27-
unknown feature specified for `-Ctarget-feature`: `{$feature}`
28-
.note = it is still passed through to the codegen backend
30+
unknown and unstable feature specified for `-Ctarget-feature`: `{$feature}`
31+
.note = it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
2932
.possible_feature = you might have meant: `{$rust_feature}`
3033
.consider_filing_feature_request = consider filing a feature request
3134
35+
codegen_gcc_unstable_ctarget_feature =
36+
unstable feature specified for `-Ctarget-feature`: `{$feature}`
37+
.note = this feature is not stably supported; its behavior can change in the future
38+
3239
codegen_gcc_missing_features =
3340
add the missing features in a `target_feature` attribute
3441

compiler/rustc_codegen_gcc/src/errors.rs

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ pub(crate) struct UnknownCTargetFeature<'a> {
1717
pub rust_feature: PossibleFeature<'a>,
1818
}
1919

20+
#[derive(Diagnostic)]
21+
#[diag(codegen_gcc_unstable_ctarget_feature)]
22+
#[note]
23+
pub(crate) struct UnstableCTargetFeature<'a> {
24+
pub feature: &'a str,
25+
}
26+
27+
#[derive(Diagnostic)]
28+
#[diag(codegen_gcc_forbidden_ctarget_feature)]
29+
pub(crate) struct ForbiddenCTargetFeature<'a> {
30+
pub feature: &'a str,
31+
}
32+
2033
#[derive(Subdiagnostic)]
2134
pub(crate) enum PossibleFeature<'a> {
2235
#[help(codegen_gcc_possible_feature)]

compiler/rustc_codegen_gcc/src/gcc_util.rs

+40-25
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ use rustc_codegen_ssa::errors::TargetFeatureDisableOrEnable;
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_middle::bug;
77
use rustc_session::Session;
8-
use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES;
8+
use rustc_target::target_features::{RUSTC_SPECIFIC_FEATURES, Stability};
99
use smallvec::{SmallVec, smallvec};
1010

11-
use crate::errors::{PossibleFeature, UnknownCTargetFeature, UnknownCTargetFeaturePrefix};
11+
use crate::errors::{
12+
ForbiddenCTargetFeature, PossibleFeature, UnknownCTargetFeature, UnknownCTargetFeaturePrefix,
13+
UnstableCTargetFeature,
14+
};
1215

1316
/// The list of GCC features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
1417
/// `--target` and similar).
@@ -43,7 +46,7 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
4346
);
4447

4548
// -Ctarget-features
46-
let supported_features = sess.target.supported_target_features();
49+
let known_features = sess.target.rust_target_features();
4750
let mut featsmap = FxHashMap::default();
4851
let feats = sess
4952
.opts
@@ -62,37 +65,49 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
6265
}
6366
};
6467

68+
// Get the backend feature name, if any.
69+
// This excludes rustc-specific features, that do not get passed down to GCC.
6570
let feature = backend_feature_name(s)?;
6671
// Warn against use of GCC specific feature names on the CLI.
67-
if diagnostics && !supported_features.iter().any(|&(v, _, _)| v == feature) {
68-
let rust_feature = supported_features.iter().find_map(|&(rust_feature, _, _)| {
69-
let gcc_features = to_gcc_features(sess, rust_feature);
70-
if gcc_features.contains(&feature) && !gcc_features.contains(&rust_feature) {
71-
Some(rust_feature)
72-
} else {
73-
None
72+
if diagnostics {
73+
let feature_state = known_features.iter().find(|&&(v, _, _)| v == feature);
74+
match feature_state {
75+
None => {
76+
let rust_feature =
77+
known_features.iter().find_map(|&(rust_feature, _, _)| {
78+
let gcc_features = to_gcc_features(sess, rust_feature);
79+
if gcc_features.contains(&feature)
80+
&& !gcc_features.contains(&rust_feature)
81+
{
82+
Some(rust_feature)
83+
} else {
84+
None
85+
}
86+
});
87+
let unknown_feature = if let Some(rust_feature) = rust_feature {
88+
UnknownCTargetFeature {
89+
feature,
90+
rust_feature: PossibleFeature::Some { rust_feature },
91+
}
92+
} else {
93+
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
94+
};
95+
sess.dcx().emit_warn(unknown_feature);
7496
}
75-
});
76-
let unknown_feature = if let Some(rust_feature) = rust_feature {
77-
UnknownCTargetFeature {
78-
feature,
79-
rust_feature: PossibleFeature::Some { rust_feature },
97+
Some((_, Stability::Stable, _)) => {}
98+
Some((_, Stability::Unstable(_), _)) => {
99+
// An unstable feature. Warn about using it.
100+
sess.dcx().emit_warn(UnstableCTargetFeature { feature });
80101
}
81-
} else {
82-
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
83-
};
84-
sess.dcx().emit_warn(unknown_feature);
85-
}
102+
Some((_, Stability::Forbidden { .. }, _)) => {
103+
sess.dcx().emit_err(ForbiddenCTargetFeature { feature });
104+
}
105+
}
86106

87-
if diagnostics {
88107
// FIXME(nagisa): figure out how to not allocate a full hashset here.
89108
featsmap.insert(feature, enable_disable == '+');
90109
}
91110

92-
// rustc-specific features do not get passed down to GCC…
93-
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
94-
return None;
95-
}
96111
// ... otherwise though we run through `to_gcc_features` when
97112
// passing requests down to GCC. This means that all in-language
98113
// features also work on the command line instead of having two

compiler/rustc_codegen_gcc/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,9 @@ pub fn target_features(
491491
) -> Vec<Symbol> {
492492
// TODO(antoyo): use global_gcc_features.
493493
sess.target
494-
.supported_target_features()
494+
.rust_target_features()
495495
.iter()
496+
.filter(|(_, gate, _)| gate.is_supported())
496497
.filter_map(|&(feature, gate, _)| {
497498
if sess.is_nightly_build() || allow_unstable || gate.is_stable() {
498499
Some(feature)

compiler/rustc_codegen_llvm/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ codegen_llvm_dynamic_linking_with_lto =
77
88
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
99
10+
codegen_llvm_forbidden_ctarget_feature =
11+
target feature `{$feature}` cannot be toggled with `-Ctarget-feature`: {$reason}
12+
.note = this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
codegen_llvm_forbidden_ctarget_feature_issue = for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
14+
1015
codegen_llvm_from_llvm_diag = {$message}
1116
1217
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}

compiler/rustc_codegen_llvm/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ pub(crate) struct UnstableCTargetFeature<'a> {
3131
pub feature: &'a str,
3232
}
3333

34+
#[derive(Diagnostic)]
35+
#[diag(codegen_llvm_forbidden_ctarget_feature)]
36+
#[note]
37+
#[note(codegen_llvm_forbidden_ctarget_feature_issue)]
38+
pub(crate) struct ForbiddenCTargetFeature<'a> {
39+
pub feature: &'a str,
40+
pub reason: &'a str,
41+
}
42+
3443
#[derive(Subdiagnostic)]
3544
pub(crate) enum PossibleFeature<'a> {
3645
#[help(codegen_llvm_possible_feature)]

compiler/rustc_codegen_llvm/src/llvm_util.rs

+71-48
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use rustc_session::Session;
1616
use rustc_session::config::{PrintKind, PrintRequest};
1717
use rustc_span::symbol::Symbol;
1818
use rustc_target::spec::{MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
19-
use rustc_target::target_features::{RUSTC_SPECIAL_FEATURES, RUSTC_SPECIFIC_FEATURES};
19+
use rustc_target::target_features::{RUSTC_SPECIAL_FEATURES, RUSTC_SPECIFIC_FEATURES, Stability};
2020

2121
use crate::back::write::create_informational_target_machine;
2222
use crate::errors::{
23-
FixedX18InvalidArch, InvalidTargetFeaturePrefix, PossibleFeature, UnknownCTargetFeature,
24-
UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
23+
FixedX18InvalidArch, ForbiddenCTargetFeature, InvalidTargetFeaturePrefix, PossibleFeature,
24+
UnknownCTargetFeature, UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
2525
};
2626
use crate::llvm;
2727

@@ -280,19 +280,29 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
280280
}
281281
}
282282

283-
/// Used to generate cfg variables and apply features
284-
/// Must express features in the way Rust understands them
283+
/// Used to generate cfg variables and apply features.
284+
/// Must express features in the way Rust understands them.
285+
///
286+
/// We do not have to worry about RUSTC_SPECIFIC_FEATURES here, those are handled outside codegen.
285287
pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
286-
let mut features = vec![];
287-
288-
// Add base features for the target
288+
let mut features: FxHashSet<Symbol> = Default::default();
289+
290+
// Add base features for the target.
291+
// We do *not* add the -Ctarget-features there, and instead duplicate the logic for that below.
292+
// The reason is that if LLVM considers a feature implied but we do not, we don't want that to
293+
// show up in `cfg`. That way, `cfg` is entirely under our control -- except for the handling of
294+
// the target CPU, that is still expanded to target features (with all their implied features) by
295+
// LLVM.
289296
let target_machine = create_informational_target_machine(sess, true);
297+
// Compute which of the known target features are enabled in the 'base' target machine.
298+
// We only consider "supported" features; "forbidden" features are not reflected in `cfg` as of now.
290299
features.extend(
291300
sess.target
292-
.supported_target_features()
301+
.rust_target_features()
293302
.iter()
303+
.filter(|(_, gate, _)| gate.is_supported())
294304
.filter(|(feature, _, _)| {
295-
// skip checking special features, as LLVM may not understands them
305+
// skip checking special features, as LLVM may not understand them
296306
if RUSTC_SPECIAL_FEATURES.contains(feature) {
297307
return true;
298308
}
@@ -323,16 +333,22 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
323333
if enabled {
324334
features.extend(sess.target.implied_target_features(std::iter::once(feature)));
325335
} else {
336+
// We don't care about the order in `features` since the only thing we use it for is the
337+
// `features.contains` below.
338+
#[allow(rustc::potential_query_instability)]
326339
features.retain(|f| {
340+
// Keep a feature if it does not imply `feature`. Or, equivalently,
341+
// remove the reverse-dependencies of `feature`.
327342
!sess.target.implied_target_features(std::iter::once(*f)).contains(&feature)
328343
});
329344
}
330345
}
331346

332347
// Filter enabled features based on feature gates
333348
sess.target
334-
.supported_target_features()
349+
.rust_target_features()
335350
.iter()
351+
.filter(|(_, gate, _)| gate.is_supported())
336352
.filter_map(|&(feature, gate, _)| {
337353
if sess.is_nightly_build() || allow_unstable || gate.is_stable() {
338354
Some(feature)
@@ -392,9 +408,13 @@ fn print_target_features(out: &mut String, sess: &Session, tm: &llvm::TargetMach
392408
let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
393409
let mut rustc_target_features = sess
394410
.target
395-
.supported_target_features()
411+
.rust_target_features()
396412
.iter()
397-
.filter_map(|(feature, _gate, _implied)| {
413+
.filter_map(|(feature, gate, _implied)| {
414+
if !gate.is_supported() {
415+
// Only list (experimentally) supported features.
416+
return None;
417+
}
398418
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these
399419
// strings.
400420
let llvm_feature = to_llvm_features(sess, *feature)?.llvm_feature_name;
@@ -567,7 +587,7 @@ pub(crate) fn global_llvm_features(
567587

568588
// -Ctarget-features
569589
if !only_base_features {
570-
let supported_features = sess.target.supported_target_features();
590+
let known_features = sess.target.rust_target_features();
571591
let mut featsmap = FxHashMap::default();
572592

573593
// insert implied features
@@ -601,50 +621,53 @@ pub(crate) fn global_llvm_features(
601621
}
602622
};
603623

624+
// Get the backend feature name, if any.
625+
// This excludes rustc-specific features, which do not get passed to LLVM.
604626
let feature = backend_feature_name(sess, s)?;
605627
// Warn against use of LLVM specific feature names and unstable features on the CLI.
606628
if diagnostics {
607-
let feature_state = supported_features.iter().find(|&&(v, _, _)| v == feature);
608-
if feature_state.is_none() {
609-
let rust_feature =
610-
supported_features.iter().find_map(|&(rust_feature, _, _)| {
611-
let llvm_features = to_llvm_features(sess, rust_feature)?;
612-
if llvm_features.contains(feature)
613-
&& !llvm_features.contains(rust_feature)
614-
{
615-
Some(rust_feature)
616-
} else {
617-
None
629+
let feature_state = known_features.iter().find(|&&(v, _, _)| v == feature);
630+
match feature_state {
631+
None => {
632+
let rust_feature =
633+
known_features.iter().find_map(|&(rust_feature, _, _)| {
634+
let llvm_features = to_llvm_features(sess, rust_feature)?;
635+
if llvm_features.contains(feature)
636+
&& !llvm_features.contains(rust_feature)
637+
{
638+
Some(rust_feature)
639+
} else {
640+
None
641+
}
642+
});
643+
let unknown_feature = if let Some(rust_feature) = rust_feature {
644+
UnknownCTargetFeature {
645+
feature,
646+
rust_feature: PossibleFeature::Some { rust_feature },
618647
}
619-
});
620-
let unknown_feature = if let Some(rust_feature) = rust_feature {
621-
UnknownCTargetFeature {
622-
feature,
623-
rust_feature: PossibleFeature::Some { rust_feature },
624-
}
625-
} else {
626-
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
627-
};
628-
sess.dcx().emit_warn(unknown_feature);
629-
} else if feature_state
630-
.is_some_and(|(_name, feature_gate, _implied)| !feature_gate.is_stable())
631-
{
632-
// An unstable feature. Warn about using it.
633-
sess.dcx().emit_warn(UnstableCTargetFeature { feature });
648+
} else {
649+
UnknownCTargetFeature {
650+
feature,
651+
rust_feature: PossibleFeature::None,
652+
}
653+
};
654+
sess.dcx().emit_warn(unknown_feature);
655+
}
656+
Some((_, Stability::Stable, _)) => {}
657+
Some((_, Stability::Unstable(_), _)) => {
658+
// An unstable feature. Warn about using it.
659+
sess.dcx().emit_warn(UnstableCTargetFeature { feature });
660+
}
661+
Some((_, Stability::Forbidden { reason }, _)) => {
662+
sess.dcx().emit_warn(ForbiddenCTargetFeature { feature, reason });
663+
}
634664
}
635-
}
636665

637-
if diagnostics {
638666
// FIXME(nagisa): figure out how to not allocate a full hashset here.
639667
featsmap.insert(feature, enable_disable == '+');
640668
}
641669

642-
// rustc-specific features do not get passed down to LLVM…
643-
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
644-
return None;
645-
}
646-
647-
// ... otherwise though we run through `to_llvm_features` when
670+
// We run through `to_llvm_features` when
648671
// passing requests down to LLVM. This means that all in-language
649672
// features also work on the command line instead of having two
650673
// different names when the LLVM name and the Rust name differ.

compiler/rustc_codegen_ssa/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ codegen_ssa_failed_to_write = failed to write {$path}: {$error}
5858
5959
codegen_ssa_field_associated_value_expected = associated value expected for `{$name}`
6060
61+
codegen_ssa_forbidden_target_feature_attr =
62+
target feature `{$feature}` cannot be toggled with `#[target_feature]`: {$reason}
63+
6164
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced
6265
6366
codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced

0 commit comments

Comments
 (0)