errors: implement fallback diagnostic translation

This commit updates the signatures of all diagnostic functions to accept
types that can be converted into a `DiagnosticMessage`. This enables
existing diagnostic calls to continue to work as before and Fluent
identifiers to be provided. The `SessionDiagnostic` derive just
generates normal diagnostic calls, so these APIs had to be modified to
accept Fluent identifiers.

In addition, loading of the "fallback" Fluent bundle, which contains the
built-in English messages, has been implemented.

Each diagnostic now has "arguments" which correspond to variables in the
Fluent messages (necessary to render a Fluent message) but no API for
adding arguments has been added yet. Therefore, diagnostics (that do not
require interpolation) can be converted to use Fluent identifiers and
will be output as before.
This commit is contained in:
David Wood 2022-03-26 07:27:43 +00:00
parent c8b9e85b20
commit 41d1340505
6 changed files with 24 additions and 10 deletions

View File

@ -130,8 +130,8 @@ fn check_arm<'tcx>(
&msg,
|diag| {
let mut help_span = MultiSpan::from_spans(vec![binding_span, inner_then_pat.span]);
help_span.push_span_label(binding_span, "replace this binding".into());
help_span.push_span_label(inner_then_pat.span, "with this pattern".into());
help_span.push_span_label(binding_span, "replace this binding");
help_span.push_span_label(inner_then_pat.span, "with this pattern");
diag.span_help(help_span, "the outer pattern can be modified to include the inner pattern");
},
);

View File

@ -621,7 +621,17 @@ fn has_needless_main(code: String, edition: Edition) -> bool {
let filename = FileName::anon_source_code(&code);
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let emitter = EmitterWriter::new(Box::new(io::sink()), None, false, false, false, None, false);
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
let emitter = EmitterWriter::new(
Box::new(io::sink()),
None,
fallback_bundle,
false,
false,
false,
None,
false,
);
let handler = Handler::with_emitter(false, None, Box::new(emitter));
let sess = ParseSess::with_span_handler(handler, sm);

View File

@ -102,7 +102,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
// Suggest replacing iter_call with iter_replacement, and removing stmt
let mut span = MultiSpan::from_span(method_name.ident.span);
span.push_span_label(iter_call.span, "the iterator could be used here instead".into());
span.push_span_label(iter_call.span, "the iterator could be used here instead");
span_lint_hir_and_then(
cx,
super::NEEDLESS_COLLECT,

View File

@ -148,7 +148,7 @@ fn check_fn(
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) {
if cx.tcx.is_const_fn_raw(def_id.to_def_id()) {
cx.tcx.sess.span_err(span, &err);
cx.tcx.sess.span_err(span, err.as_ref());
}
} else {
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");

View File

@ -235,11 +235,12 @@ fn check_fn(
for (span, suggestion) in clone_spans {
diag.span_suggestion(
span,
&snippet_opt(cx, span)
snippet_opt(cx, span)
.map_or(
"change the call to".into(),
|x| Cow::from(format!("change `{}` to", x)),
),
)
.as_ref(),
suggestion.into(),
Applicability::Unspecified,
);
@ -264,11 +265,12 @@ fn check_fn(
for (span, suggestion) in clone_spans {
diag.span_suggestion(
span,
&snippet_opt(cx, span)
snippet_opt(cx, span)
.map_or(
"change the call to".into(),
|x| Cow::from(format!("change `{}` to", x))
),
)
.as_ref(),
suggestion.into(),
Applicability::Unspecified,
);

View File

@ -165,9 +165,11 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
// Separate the output with an empty line
eprintln!();
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
rustc_errors::ColorConfig::Auto,
None,
fallback_bundle,
false,
false,
None,
@ -191,7 +193,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
];
for note in &xs {
handler.note_without_error(note);
handler.note_without_error(note.as_ref());
}
// If backtraces are enabled, also print the query stack