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); .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
cx.tcx cx.tcx
.sess .sess
.create_err(TargetFeatureDisableOrEnable { features: f, span: Some(span) }) .create_err(TargetFeatureDisableOrEnable {
.subdiagnostic(MissingFeatures) features: f,
span: Some(span),
missing_features: Some(MissingFeatures),
})
.emit(); .emit();
return; return;
} }

View File

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

View File

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

View File

@@ -1,17 +1,12 @@
codegen_llvm_unknown_ctarget_feature = codegen_llvm_unknown_ctarget_feature =
unknown feature specified for `-Ctarget-feature`: `{$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 = codegen_llvm_unknown_ctarget_feature_prefix =
features must begin with a `+` to enable or `-` to disable it unknown feature specified for `-Ctarget-feature`: `{$feature}`
.note = 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_error_creating_import_library = codegen_llvm_error_creating_import_library =
Error creating import library for {$lib_name}: {$error} Error creating import library for {$lib_name}: {$error}