From dfcfaa4ec1dd8a5bda33874dc180509b74bb625f Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Fri, 23 Jul 2021 14:42:36 +0300 Subject: [PATCH] Do not pass through features without +/- prefix LLVM really dislikes this and will assert, saying something along the lines of: ``` rustc: llvm/lib/MC/MCSubtargetInfo.cpp:60: void ApplyFeatureFlag( llvm::FeatureBitset&, llvm::StringRef, llvm::ArrayRef ): Assertion `SubtargetFeatures::hasFlag(Feature) && "Feature flags should start with '+' or '-'"` failed. ``` --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 33 +++++++++---------- .../ui/target-feature/missing-plusminus-2.rs | 6 ++++ .../target-feature/missing-plusminus-2.stderr | 6 ++++ 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 src/test/ui/target-feature/missing-plusminus-2.rs create mode 100644 src/test/ui/target-feature/missing-plusminus-2.stderr diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index b1c14c7e44bd0..69eacafb1b594 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -221,7 +221,11 @@ pub fn target_features(sess: &Session) -> Vec { supported_target_features(sess) .iter() .filter_map(|&(feature, gate)| { - if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None } + if sess.is_nightly_build() || gate.is_none() { + Some(feature) + } else { + None + } }) .filter(|feature| { for llvm_feature in to_llvm_feature(sess, feature) { @@ -428,20 +432,15 @@ pub fn llvm_global_features(sess: &Session) -> Vec { } let filter = |s: &str| { - if s.is_empty() { - return vec![]; - } - let feature = strip(s); - if feature == s { - return vec![s.to_string()]; - } - - // Rustc-specific feature requests like `+crt-static` or `-crt-static` - // are not passed down to LLVM. - if RUSTC_SPECIFIC_FEATURES.contains(&feature) { - return vec![]; - } - // ... otherwise though we run through `to_llvm_feature` feature when + // features must start with a `+` or `-`. + let feature = match s.strip_prefix(&['+', '-'][..]) { + None => return vec![], + // Rustc-specific feature requests like `+crt-static` or `-crt-static` + // are not passed down to LLVM. + Some(feature) if RUSTC_SPECIFIC_FEATURES.contains(&feature) => return vec![], + Some(feature) => feature, + }; + // ... otherwise though we run through `to_llvm_feature` when // passing requests down to LLVM. This means that all in-language // features also work on the command line instead of having two // different names when the LLVM name and the Rust name differ. @@ -458,11 +457,11 @@ pub fn llvm_global_features(sess: &Session) -> Vec { check_tied_features(sess, &feats.iter().map(|f| (strip(f), !f.starts_with("-"))).collect()) { sess.err(&format!( - "Target features {} must all be enabled or disabled together", + "target features {} must all be enabled or disabled together", f.join(", ") )); } - features.extend(feats.iter().flat_map(|&f| filter(f))); + features.extend(feats.iter().flat_map(&filter)); features } diff --git a/src/test/ui/target-feature/missing-plusminus-2.rs b/src/test/ui/target-feature/missing-plusminus-2.rs new file mode 100644 index 0000000000000..1316872890279 --- /dev/null +++ b/src/test/ui/target-feature/missing-plusminus-2.rs @@ -0,0 +1,6 @@ +// compile-flags: -Ctarget-feature=rdrand --crate-type=rlib --target=x86_64-unknown-linux-gnu +// build-pass +// needs-llvm-components: x86 + +#![feature(no_core)] +#![no_core] diff --git a/src/test/ui/target-feature/missing-plusminus-2.stderr b/src/test/ui/target-feature/missing-plusminus-2.stderr new file mode 100644 index 0000000000000..5ed2652a06df2 --- /dev/null +++ b/src/test/ui/target-feature/missing-plusminus-2.stderr @@ -0,0 +1,6 @@ +warning: unknown feature specified for `-Ctarget-feature`: `rdrand` + | + = note: features must begin with a `+` to enable or `-` to disable it + +warning: 1 warning emitted +