Skip to content

Commit 9a15458

Browse files
committed
Simplify existing Diagnostic implementations
1 parent 3728e95 commit 9a15458

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,11 @@ pub fn from_fn_attrs<'ll, 'tcx>(
396396
.map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
397397
cx.tcx
398398
.sess
399-
.create_err(TargetFeatureDisableOrEnable { features: f, span: Some(span) })
400-
.subdiagnostic(MissingFeatures)
399+
.create_err(TargetFeatureDisableOrEnable {
400+
features: f,
401+
span: Some(span),
402+
missing_features: Some(MissingFeatures),
403+
})
401404
.emit();
402405
return;
403406
}

compiler/rustc_codegen_llvm/src/errors.rs

+28-33
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,28 @@ use rustc_errors::IntoDiagnostic;
88
use rustc_macros::{Diagnostic, Subdiagnostic};
99
use rustc_span::Span;
1010

11-
pub(crate) enum UnknownCTargetFeature<'a> {
12-
UnknownFeaturePrefix { feature: &'a str },
13-
UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> },
14-
}
15-
16-
impl IntoDiagnostic<'_, ()> for UnknownCTargetFeature<'_> {
17-
fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ()> {
18-
match self {
19-
UnknownCTargetFeature::UnknownFeaturePrefix { feature } => {
20-
let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature);
21-
diag.set_arg("feature", feature);
22-
diag.note(fluent::codegen_llvm_unknown_feature_prefix);
23-
diag
24-
}
25-
UnknownCTargetFeature::UnknownFeature { feature, rust_feature } => {
26-
let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature);
27-
diag.set_arg("feature", feature);
28-
diag.note(fluent::codegen_llvm_unknown_feature);
29-
if let Some(rust_feature) = rust_feature {
30-
diag.help(fluent::codegen_llvm_rust_feature);
31-
diag.set_arg("rust_feature", rust_feature);
32-
} else {
33-
diag.note(fluent::codegen_llvm_unknown_feature_fill_request);
34-
}
35-
diag
36-
}
37-
}
38-
}
11+
#[derive(Diagnostic)]
12+
#[diag(codegen_llvm_unknown_ctarget_feature_prefix)]
13+
#[note]
14+
pub(crate) struct UnknownCTargetFeaturePrefix<'a> {
15+
pub feature: &'a str,
16+
}
17+
18+
#[derive(Diagnostic)]
19+
#[diag(codegen_llvm_unknown_ctarget_feature)]
20+
#[note]
21+
pub(crate) struct UnknownCTargetFeature<'a> {
22+
pub feature: &'a str,
23+
#[subdiagnostic]
24+
pub rust_feature: PossibleFeature<'a>,
25+
}
26+
27+
#[derive(Subdiagnostic)]
28+
pub(crate) enum PossibleFeature<'a> {
29+
#[help(possible_feature)]
30+
Some { rust_feature: &'a str },
31+
#[help(consider_filing_feature_request)]
32+
None,
3933
}
4034

4135
#[derive(Diagnostic)]
@@ -131,6 +125,7 @@ pub(crate) struct FailParsingTargetMachineConfigToTargetMachine {
131125
pub(crate) struct TargetFeatureDisableOrEnable<'a> {
132126
pub features: &'a [&'a str],
133127
pub span: Option<Span>,
128+
pub missing_features: Option<MissingFeatures>,
134129
}
135130

136131
#[derive(Subdiagnostic)]
@@ -139,13 +134,13 @@ pub(crate) struct MissingFeatures;
139134

140135
impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> {
141136
fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
142-
let mut diag = if let Some(span) = self.span {
143-
let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable);
137+
let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable);
138+
if let Some(span) = self.span {
144139
diag.set_span(span);
145-
diag
146-
} else {
147-
sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable)
148140
};
141+
if let Some(missing_features) = self.missing_features {
142+
diag.subdiagnostic(missing_features);
143+
}
149144
diag.set_arg("features", self.features.join(", "));
150145
diag
151146
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::back::write::create_informational_target_machine;
2-
use crate::errors::{TargetFeatureDisableOrEnable, UnknownCTargetFeature};
2+
use crate::errors::{
3+
PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature,
4+
UnknownCTargetFeaturePrefix,
5+
};
36
use crate::llvm;
47
use libc::c_int;
58
use rustc_codegen_ssa::target_features::{
@@ -435,9 +438,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
435438
Some(c @ '+' | c @ '-') => c,
436439
Some(_) => {
437440
if diagnostics {
438-
sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix {
439-
feature: s,
440-
});
441+
sess.emit_warning(UnknownCTargetFeaturePrefix { feature: s });
441442
}
442443
return None;
443444
}
@@ -454,7 +455,15 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
454455
None
455456
}
456457
});
457-
sess.emit_warning(UnknownCTargetFeature::UnknownFeature { feature, rust_feature });
458+
let unknown_feature = if let Some(rust_feature) = rust_feature {
459+
UnknownCTargetFeature {
460+
feature,
461+
rust_feature: PossibleFeature::Some { rust_feature },
462+
}
463+
} else {
464+
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
465+
};
466+
sess.emit_warning(unknown_feature);
458467
}
459468

460469
if diagnostics {
@@ -482,7 +491,8 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
482491
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
483492
sess.emit_err(TargetFeatureDisableOrEnable {
484493
features: f,
485-
span: None
494+
span: None,
495+
missing_features: None,
486496
});
487497
}
488498

compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
codegen_llvm_unknown_ctarget_feature =
22
unknown feature specified for `-Ctarget-feature`: `{$feature}`
3+
.note = it is still passed through to the codegen backend
4+
.possible_feature = you might have meant: `{$rust_feature}`
5+
.consider_filing_feature_request = consider filing a feature request
36
4-
codegen_llvm_unknown_feature_prefix =
5-
features must begin with a `+` to enable or `-` to disable it
6-
7-
codegen_llvm_unknown_feature =
8-
it is still passed through to the codegen backend
9-
10-
codegen_llvm_rust_feature =
11-
you might have meant: `{$rust_feature}`
12-
13-
codegen_llvm_unknown_feature_fill_request =
14-
consider filing a feature request
7+
codegen_llvm_unknown_ctarget_feature_prefix =
8+
unknown feature specified for `-Ctarget-feature`: `{$feature}`
9+
.note = features must begin with a `+` to enable or `-` to disable it
1510
1611
codegen_llvm_error_creating_import_library =
1712
Error creating import library for {$lib_name}: {$error}

0 commit comments

Comments
 (0)