about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
diff options
context:
space:
mode:
authorDavid Wood <david.wood@huawei.com>2022-03-26 07:27:43 +0000
committerDavid Wood <david.wood@huawei.com>2022-04-05 07:01:02 +0100
commit7f91697b5035f8620df4de47057024c3539b55a6 (patch)
treea7f6e303b30ec5b3d675c362d474e1de0f3419ad /compiler/rustc_parse/src
parentc45f29595df6f6a178b7998bc33c76099f3c12b7 (diff)
downloadrust-7f91697b5035f8620df4de47057024c3539b55a6.tar.gz
rust-7f91697b5035f8620df4de47057024c3539b55a6.zip
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.
Diffstat (limited to 'compiler/rustc_parse/src')
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs36
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs4
-rw-r--r--compiler/rustc_parse/src/parser/item.rs2
3 files changed, 25 insertions, 17 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 2999db79785..ed264045170 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -18,7 +18,9 @@ use rustc_ast::{
 use rustc_ast_pretty::pprust;
 use rustc_data_structures::fx::FxHashSet;
 use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, ErrorGuaranteed};
-use rustc_errors::{Applicability, DiagnosticBuilder, Handler, MultiSpan, PResult};
+use rustc_errors::{
+    Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult,
+};
 use rustc_span::source_map::Spanned;
 use rustc_span::symbol::{kw, Ident};
 use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
@@ -273,12 +275,12 @@ impl<'a> Parser<'a> {
     pub fn struct_span_err<S: Into<MultiSpan>>(
         &self,
         sp: S,
-        m: &str,
+        m: impl Into<DiagnosticMessage>,
     ) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
         self.sess.span_diagnostic.struct_span_err(sp, m)
     }
 
-    pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> ! {
+    pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, m: impl Into<DiagnosticMessage>) -> ! {
         self.sess.span_diagnostic.span_bug(sp, m)
     }
 
@@ -584,16 +586,22 @@ impl<'a> Parser<'a> {
                     //     field: value,
                     // } }
                     err.delay_as_bug();
-                    self.struct_span_err(expr.span, "struct literal body without path")
-                        .multipart_suggestion(
-                            "you might have forgotten to add the struct literal inside the block",
-                            vec![
-                                (expr.span.shrink_to_lo(), "{ SomeStruct ".to_string()),
-                                (expr.span.shrink_to_hi(), " }".to_string()),
-                            ],
-                            Applicability::MaybeIncorrect,
-                        )
-                        .emit();
+                    self.struct_span_err(
+                        expr.span,
+                        DiagnosticMessage::fluent("parser-struct-literal-body-without-path"),
+                    )
+                    .multipart_suggestion(
+                        DiagnosticMessage::fluent_attr(
+                            "parser-struct-literal-body-without-path",
+                            "suggestion",
+                        ),
+                        vec![
+                            (expr.span.shrink_to_lo(), "{ SomeStruct ".to_string()),
+                            (expr.span.shrink_to_hi(), " }".to_string()),
+                        ],
+                        Applicability::MaybeIncorrect,
+                    )
+                    .emit();
                     self.restore_snapshot(snapshot);
                     let mut tail = self.mk_block(
                         vec![self.mk_stmt_err(expr.span)],
@@ -1752,7 +1760,7 @@ impl<'a> Parser<'a> {
                     let mut primary_span: MultiSpan = primary_span.into();
                     for span_label in err.span.span_labels() {
                         if let Some(label) = span_label.label {
-                            primary_span.push_span_message(span_label.span, label);
+                            primary_span.push_span_label(span_label.span, label);
                         }
                     }
                     err.set_span(primary_span);
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 7c407cbe465..7978a1a7f5f 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -1703,7 +1703,7 @@ impl<'a> Parser<'a> {
                     if matches!(expr.kind, ExprKind::Err) {
                         let mut err = self
                             .diagnostic()
-                            .struct_span_err(self.token.span, &"invalid interpolated expression");
+                            .struct_span_err(self.token.span, "invalid interpolated expression");
                         err.downgrade_to_delayed_bug();
                         return err;
                     }
@@ -1820,7 +1820,7 @@ impl<'a> Parser<'a> {
                 } else if let Some(fixed) = fix_base_capitalisation(suf) {
                     let msg = "invalid base prefix for number literal";
 
-                    self.struct_span_err(span, &msg)
+                    self.struct_span_err(span, msg)
                         .note("base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase")
                         .span_suggestion(
                             span,
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index fd4b00de392..5bf6f22b5d0 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -1011,7 +1011,7 @@ impl<'a> Parser<'a> {
                 let current_qual_sp = current_qual_sp.to(sp_start);
                 if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) {
                     // FIXME(davidtwco): avoid depending on the error message text
-                    if err.message().as_str() == "expected `{`, found keyword `unsafe`" {
+                    if err.message[0].0.expect_str() == "expected `{`, found keyword `unsafe`" {
                         let invalid_qual_sp = self.token.uninterpolated_span();
                         let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap();