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, &msg,
|diag| { |diag| {
let mut help_span = MultiSpan::from_spans(vec![binding_span, inner_then_pat.span]); 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(binding_span, "replace this binding");
help_span.push_span_label(inner_then_pat.span, "with this pattern".into()); 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"); diag.span_help(help_span, "the outer pattern can be modified to include the inner pattern");
}, },
); );

View file

@ -621,7 +621,17 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
let filename = FileName::anon_source_code(&code); let filename = FileName::anon_source_code(&code);
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); 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 handler = Handler::with_emitter(false, None, Box::new(emitter));
let sess = ParseSess::with_span_handler(handler, sm); 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 // Suggest replacing iter_call with iter_replacement, and removing stmt
let mut span = MultiSpan::from_span(method_name.ident.span); 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( span_lint_hir_and_then(
cx, cx,
super::NEEDLESS_COLLECT, super::NEEDLESS_COLLECT,

View file

@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) { 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()) { 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 { } else {
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`"); span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");

View file

@ -235,11 +235,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
for (span, suggestion) in clone_spans { for (span, suggestion) in clone_spans {
diag.span_suggestion( diag.span_suggestion(
span, span,
&snippet_opt(cx, span) snippet_opt(cx, span)
.map_or( .map_or(
"change the call to".into(), "change the call to".into(),
|x| Cow::from(format!("change `{}` to", x)), |x| Cow::from(format!("change `{}` to", x)),
), )
.as_ref(),
suggestion.into(), suggestion.into(),
Applicability::Unspecified, Applicability::Unspecified,
); );
@ -264,11 +265,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
for (span, suggestion) in clone_spans { for (span, suggestion) in clone_spans {
diag.span_suggestion( diag.span_suggestion(
span, span,
&snippet_opt(cx, span) snippet_opt(cx, span)
.map_or( .map_or(
"change the call to".into(), "change the call to".into(),
|x| Cow::from(format!("change `{}` to", x)) |x| Cow::from(format!("change `{}` to", x))
), )
.as_ref(),
suggestion.into(), suggestion.into(),
Applicability::Unspecified, 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 // Separate the output with an empty line
eprintln!(); eprintln!();
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr( let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
rustc_errors::ColorConfig::Auto, rustc_errors::ColorConfig::Auto,
None, None,
fallback_bundle,
false, false,
false, false,
None, None,
@ -191,7 +193,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
]; ];
for note in &xs { 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 // If backtraces are enabled, also print the query stack