Simplify existing Diagnostic implementations

This commit is contained in:
SLASHLogin 2022-10-30 19:26:12 +01:00
parent 3728e95596
commit 9a1545861e
4 changed files with 53 additions and 50 deletions

View file

@ -396,8 +396,11 @@ pub fn from_fn_attrs<'ll, 'tcx>(
.map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
cx.tcx
.sess
.create_err(TargetFeatureDisableOrEnable { features: f, span: Some(span) })
.subdiagnostic(MissingFeatures)
.create_err(TargetFeatureDisableOrEnable {
features: f,
span: Some(span),
missing_features: Some(MissingFeatures),
})
.emit();
return;
}

View file

@ -8,34 +8,28 @@ use rustc_errors::IntoDiagnostic;
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span;
pub(crate) enum UnknownCTargetFeature<'a> {
UnknownFeaturePrefix { feature: &'a str },
UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> },
#[derive(Diagnostic)]
#[diag(codegen_llvm_unknown_ctarget_feature_prefix)]
#[note]
pub(crate) struct UnknownCTargetFeaturePrefix<'a> {
pub feature: &'a str,
}
impl IntoDiagnostic<'_, ()> for UnknownCTargetFeature<'_> {
fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ()> {
match self {
UnknownCTargetFeature::UnknownFeaturePrefix { feature } => {
let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature);
diag.set_arg("feature", feature);
diag.note(fluent::codegen_llvm_unknown_feature_prefix);
diag
}
UnknownCTargetFeature::UnknownFeature { feature, rust_feature } => {
let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature);
diag.set_arg("feature", feature);
diag.note(fluent::codegen_llvm_unknown_feature);
if let Some(rust_feature) = rust_feature {
diag.help(fluent::codegen_llvm_rust_feature);
diag.set_arg("rust_feature", rust_feature);
} else {
diag.note(fluent::codegen_llvm_unknown_feature_fill_request);
}
diag
}
}
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_unknown_ctarget_feature)]
#[note]
pub(crate) struct UnknownCTargetFeature<'a> {
pub feature: &'a str,
#[subdiagnostic]
pub rust_feature: PossibleFeature<'a>,
}
#[derive(Subdiagnostic)]
pub(crate) enum PossibleFeature<'a> {
#[help(possible_feature)]
Some { rust_feature: &'a str },
#[help(consider_filing_feature_request)]
None,
}
#[derive(Diagnostic)]
@ -131,6 +125,7 @@ pub(crate) struct FailParsingTargetMachineConfigToTargetMachine {
pub(crate) struct TargetFeatureDisableOrEnable<'a> {
pub features: &'a [&'a str],
pub span: Option<Span>,
pub missing_features: Option<MissingFeatures>,
}
#[derive(Subdiagnostic)]
@ -139,13 +134,13 @@ pub(crate) struct MissingFeatures;
impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> {
fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = if let Some(span) = self.span {
let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable);
let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable);
if let Some(span) = self.span {
diag.set_span(span);
diag
} else {
sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable)
};
if let Some(missing_features) = self.missing_features {
diag.subdiagnostic(missing_features);
}
diag.set_arg("features", self.features.join(", "));
diag
}

View file

@ -1,5 +1,8 @@
use crate::back::write::create_informational_target_machine;
use crate::errors::{TargetFeatureDisableOrEnable, UnknownCTargetFeature};
use crate::errors::{
PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature,
UnknownCTargetFeaturePrefix,
};
use crate::llvm;
use libc::c_int;
use rustc_codegen_ssa::target_features::{
@ -435,9 +438,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
Some(c @ '+' | c @ '-') => c,
Some(_) => {
if diagnostics {
sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix {
feature: s,
});
sess.emit_warning(UnknownCTargetFeaturePrefix { feature: s });
}
return None;
}
@ -454,7 +455,15 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
None
}
});
sess.emit_warning(UnknownCTargetFeature::UnknownFeature { feature, rust_feature });
let unknown_feature = if let Some(rust_feature) = rust_feature {
UnknownCTargetFeature {
feature,
rust_feature: PossibleFeature::Some { rust_feature },
}
} else {
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
};
sess.emit_warning(unknown_feature);
}
if diagnostics {
@ -482,7 +491,8 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
sess.emit_err(TargetFeatureDisableOrEnable {
features: f,
span: None
span: None,
missing_features: None,
});
}

View file

@ -1,17 +1,12 @@
codegen_llvm_unknown_ctarget_feature =
unknown feature specified for `-Ctarget-feature`: `{$feature}`
.note = it is still passed through to the codegen backend
.possible_feature = you might have meant: `{$rust_feature}`
.consider_filing_feature_request = consider filing a feature request
codegen_llvm_unknown_feature_prefix =
features must begin with a `+` to enable or `-` to disable it
codegen_llvm_unknown_feature =
it is still passed through to the codegen backend
codegen_llvm_rust_feature =
you might have meant: `{$rust_feature}`
codegen_llvm_unknown_feature_fill_request =
consider filing a feature request
codegen_llvm_unknown_ctarget_feature_prefix =
unknown feature specified for `-Ctarget-feature`: `{$feature}`
.note = features must begin with a `+` to enable or `-` to disable it
codegen_llvm_error_creating_import_library =
Error creating import library for {$lib_name}: {$error}