Skip to content

Commit

Permalink
Disallow enabling features without their implied features
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Aug 7, 2024
1 parent 0b98a0c commit 8818c95
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn check_tied_features(
/// Used to generate cfg variables and apply features
/// Must express features in the way Rust understands them
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
let target_machine = create_informational_target_machine(sess, true);
Expand Down Expand Up @@ -313,7 +313,9 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
if enabled {
features.extend(sess.target.implied_target_features(std::iter::once(feature)));
} else {
features.remove(&feature);
features.retain(|f| {
!sess.target.implied_target_features(std::iter::once(*f)).contains(&feature)
});
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/target_features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast::ast;
use rustc_attr::InstructionSetAttr;
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_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
Expand Down Expand Up @@ -108,8 +108,7 @@ pub fn from_target_feature(
// Add implied features
let mut implied_target_features = UnordSet::new();
for feature in added_target_features.iter() {
implied_target_features
.extend_unord(tcx.implied_target_features(*feature).clone().into_items());
implied_target_features.extend(tcx.implied_target_features(*feature).clone());
}
for feature in added_target_features.iter() {
implied_target_features.remove(feature);
Expand Down Expand Up @@ -179,7 +178,8 @@ pub(crate) fn provide(providers: &mut Providers) {
}
},
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,
..*providers
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
.iter()
.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!(
fluent::const_eval_unavailable_target_features_for_fn,
unavailable_feats = attrs
.target_features
.iter()
.filter(|&feature| !(missing_explicit_features && feature.implied)
.filter(|&feature| !feature.implied
&& !self.tcx.sess.target_features.contains(&feature.name))
.fold(String::new(), |mut s, feature| {
if !s.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,7 @@ rustc_queries! {
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
eval_always
desc { "looking up implied target features" }
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
{
// 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
.iter()
.copied()
.filter(|feature| {
!(missing_explicit_features && feature.implied)
!feature.implied
&& !self
.body_target_features
.iter()
Expand Down

0 comments on commit 8818c95

Please sign in to comment.