Disallow enabling features without their implied features

This commit is contained in:
Caleb Zulawski 2024-08-06 00:35:32 -04:00
parent 0b98a0c727
commit 8818c95528
5 changed files with 11 additions and 23 deletions

View File

@ -277,7 +277,7 @@ pub fn check_tied_features(
/// Used to generate cfg variables and apply features /// Used to generate cfg variables and apply features
/// Must express features in the way Rust understands them /// Must express features in the way Rust understands them
pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> { pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
let mut features = FxHashSet::default(); let mut features = vec![];
// Add base features for the target // Add base features for the target
let target_machine = create_informational_target_machine(sess, true); let target_machine = create_informational_target_machine(sess, true);
@ -313,7 +313,9 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
if enabled { if enabled {
features.extend(sess.target.implied_target_features(std::iter::once(feature))); features.extend(sess.target.implied_target_features(std::iter::once(feature)));
} else { } else {
features.remove(&feature); features.retain(|f| {
!sess.target.implied_target_features(std::iter::once(*f)).contains(&feature)
});
} }
} }

View File

@ -1,7 +1,7 @@
use rustc_ast::ast; use rustc_ast::ast;
use rustc_attr::InstructionSetAttr; use rustc_attr::InstructionSetAttr;
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet}; use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
@ -108,8 +108,7 @@ pub fn from_target_feature(
// Add implied features // Add implied features
let mut implied_target_features = UnordSet::new(); let mut implied_target_features = UnordSet::new();
for feature in added_target_features.iter() { for feature in added_target_features.iter() {
implied_target_features implied_target_features.extend(tcx.implied_target_features(*feature).clone());
.extend_unord(tcx.implied_target_features(*feature).clone().into_items());
} }
for feature in added_target_features.iter() { for feature in added_target_features.iter() {
implied_target_features.remove(feature); implied_target_features.remove(feature);
@ -179,7 +178,8 @@ pub(crate) fn provide(providers: &mut Providers) {
} }
}, },
implied_target_features: |tcx, feature| { implied_target_features: |tcx, feature| {
tcx.sess.target.implied_target_features(std::iter::once(feature)).into() UnordSet::from(tcx.sess.target.implied_target_features(std::iter::once(feature)))
.into_sorted_stable_ord()
}, },
asm_target_features, asm_target_features,
..*providers ..*providers

View File

@ -319,18 +319,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
.iter() .iter()
.any(|feature| !self.tcx.sess.target_features.contains(&feature.name)) .any(|feature| !self.tcx.sess.target_features.contains(&feature.name))
{ {
// Don't include implicit features in the error, unless only implicit features are
// missing. This should be rare, because it can only happen when an implicit feature
// is disabled, e.g. `+avx2,-avx`
let missing_explicit_features = attrs.target_features.iter().any(|feature| {
!feature.implied && !self.tcx.sess.target_features.contains(&feature.name)
});
throw_ub_custom!( throw_ub_custom!(
fluent::const_eval_unavailable_target_features_for_fn, fluent::const_eval_unavailable_target_features_for_fn,
unavailable_feats = attrs unavailable_feats = attrs
.target_features .target_features
.iter() .iter()
.filter(|&feature| !(missing_explicit_features && feature.implied) .filter(|&feature| !feature.implied
&& !self.tcx.sess.target_features.contains(&feature.name)) && !self.tcx.sess.target_features.contains(&feature.name))
.fold(String::new(), |mut s, feature| { .fold(String::new(), |mut s, feature| {
if !s.is_empty() { if !s.is_empty() {

View File

@ -2183,7 +2183,7 @@ rustc_queries! {
desc { "looking up supported target features" } desc { "looking up supported target features" }
} }
query implied_target_features(feature: Symbol) -> &'tcx UnordSet<Symbol> { query implied_target_features(feature: Symbol) -> &'tcx Vec<Symbol> {
arena_cache arena_cache
eval_always eval_always
desc { "looking up implied target features" } desc { "looking up implied target features" }

View File

@ -447,19 +447,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
self.body_target_features.iter().any(|f| f.name == feature.name) self.body_target_features.iter().any(|f| f.name == feature.name)
}) })
{ {
// Don't include implicit features in the error, unless only implicit
// features are missing.
let missing_explicit_features = callee_features.iter().any(|feature| {
!feature.implied
&& !self.body_target_features.iter().any(|body_feature| {
!feature.implied && body_feature.name == feature.name
})
});
let missing: Vec<_> = callee_features let missing: Vec<_> = callee_features
.iter() .iter()
.copied() .copied()
.filter(|feature| { .filter(|feature| {
!(missing_explicit_features && feature.implied) !feature.implied
&& !self && !self
.body_target_features .body_target_features
.iter() .iter()