Skip to content

Commit 934618f

Browse files
committed
Emit a diagnostic for invalid target options
1 parent 11f32b7 commit 934618f

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

compiler/rustc_codegen_llvm/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ codegen_llvm_invalid_minimum_alignment_not_power_of_two =
2828
codegen_llvm_invalid_minimum_alignment_too_large =
2929
invalid minimum global alignment: {$align} is too large
3030
31+
codegen_llvm_invalid_target_feature_prefix = target feature `{$feature}` must begin with a `+` or `-`"
32+
3133
codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}"
3234
codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err}
3335

compiler/rustc_codegen_llvm/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,9 @@ pub struct MismatchedDataLayout<'a> {
253253
pub llvm_target: &'a str,
254254
pub llvm_layout: &'a str,
255255
}
256+
257+
#[derive(Diagnostic)]
258+
#[diag(codegen_llvm_invalid_target_feature_prefix)]
259+
pub(crate) struct InvalidTargetFeaturePrefix<'a> {
260+
pub feature: &'a str,
261+
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::back::write::create_informational_target_machine;
22
use crate::errors::{
3-
PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature,
4-
UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
3+
InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable,
4+
UnknownCTargetFeature, UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
55
};
66
use crate::llvm;
77
use libc::c_int;
@@ -511,7 +511,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
511511
sess.target
512512
.features
513513
.split(',')
514-
.filter(|v| !v.is_empty() && backend_feature_name(v).is_some())
514+
.filter(|v| !v.is_empty() && backend_feature_name(sess, v).is_some())
515515
.map(String::from),
516516
);
517517

@@ -535,7 +535,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
535535
}
536536
};
537537

538-
let feature = backend_feature_name(s)?;
538+
let feature = backend_feature_name(sess, s)?;
539539
// Warn against use of LLVM specific feature names and unstable features on the CLI.
540540
if diagnostics {
541541
let feature_state = supported_features.iter().find(|&&(v, _)| v == feature);
@@ -611,11 +611,11 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
611611
/// Returns a feature name for the given `+feature` or `-feature` string.
612612
///
613613
/// Only allows features that are backend specific (i.e. not [`RUSTC_SPECIFIC_FEATURES`].)
614-
fn backend_feature_name(s: &str) -> Option<&str> {
614+
fn backend_feature_name<'a>(sess: &Session, s: &'a str) -> Option<&'a str> {
615615
// features must start with a `+` or `-`.
616-
let feature = s.strip_prefix(&['+', '-'][..]).unwrap_or_else(|| {
617-
bug!("target feature `{}` must begin with a `+` or `-`", s);
618-
});
616+
let feature = s
617+
.strip_prefix(&['+', '-'][..])
618+
.unwrap_or_else(|| sess.dcx().emit_fatal(InvalidTargetFeaturePrefix { feature: s }));
619619
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
620620
// are not passed down to LLVM.
621621
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {

0 commit comments

Comments
 (0)