Skip to content

Commit 453fe91

Browse files
Rollup merge of #153228 - JonathanBrouwer:remove_translate_error, r=lqd
Remove `TranslationError` This function is now infallible, since the diagnostic message and presence of variables is already parsed in the `msg!` or `derive(Diagnostic)` macro. In practice, the result of this function was just unwrapped everywhere anyways... r? @Kivooeo if you feel like reviewing diagnostics PRs again, I have a few more coming :) (Tho other people are also free to steal these PRs)
2 parents f462a5b + 6e8d9c1 commit 453fe91

7 files changed

Lines changed: 13 additions & 177 deletions

File tree

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/
77
88
use std::borrow::Cow;
9-
use std::error::Report;
109
use std::fmt::Debug;
1110
use std::io;
1211
use std::io::Write;
@@ -361,10 +360,7 @@ impl AnnotateSnippetEmitter {
361360
if substitutions.is_empty() {
362361
continue;
363362
}
364-
let mut msg = format_diag_message(&suggestion.msg, args)
365-
.map_err(Report::new)
366-
.unwrap()
367-
.to_string();
363+
let mut msg = format_diag_message(&suggestion.msg, args).to_string();
368364

369365
let lo = substitutions
370366
.iter()
@@ -547,7 +543,7 @@ impl AnnotateSnippetEmitter {
547543
) -> String {
548544
msgs.iter()
549545
.filter_map(|(m, style)| {
550-
let text = format_diag_message(m, args).map_err(Report::new).unwrap();
546+
let text = format_diag_message(m, args);
551547
let style = style.anstyle(level);
552548
if text.is_empty() { None } else { Some(format!("{style}{text}{style:#}")) }
553549
})
@@ -694,9 +690,7 @@ fn collect_annotations(
694690

695691
let kind = if is_primary { AnnotationKind::Primary } else { AnnotationKind::Context };
696692

697-
let label = label.as_ref().map(|m| {
698-
normalize_whitespace(&format_diag_message(m, args).map_err(Report::new).unwrap())
699-
});
693+
let label = label.as_ref().map(|m| normalize_whitespace(&format_diag_message(m, args)));
700694

701695
let ann = Annotation { kind, span, label };
702696
if sm.is_valid_span(ann.span).is_ok() {

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! The output types are defined in `rustc_session::config::ErrorOutputType`.
99
1010
use std::borrow::Cow;
11-
use std::error::Report;
1211
use std::io::prelude::*;
1312
use std::io::{self, IsTerminal};
1413
use std::iter;
@@ -106,7 +105,7 @@ pub trait Emitter {
106105
fluent_args: &FluentArgs<'_>,
107106
) {
108107
if let Some((sugg, rest)) = suggestions.split_first() {
109-
let msg = format_diag_message(&sugg.msg, fluent_args).map_err(Report::new).unwrap();
108+
let msg = format_diag_message(&sugg.msg, fluent_args);
110109
if rest.is_empty()
111110
// ^ if there is only one suggestion
112111
// don't display multi-suggestions as labels

compiler/rustc_errors/src/error.rs

Lines changed: 0 additions & 139 deletions
This file was deleted.

compiler/rustc_errors/src/json.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
1010
// FIXME: spec the JSON output properly.
1111

12-
use std::error::Report;
1312
use std::io::{self, Write};
1413
use std::path::{Path, PathBuf};
1514
use std::sync::{Arc, Mutex};
@@ -301,8 +300,7 @@ impl Diagnostic {
301300
fn from_errors_diagnostic(diag: crate::DiagInner, je: &JsonEmitter) -> Diagnostic {
302301
let args = to_fluent_args(diag.args.iter());
303302
let sugg_to_diag = |sugg: &CodeSuggestion| {
304-
let translated_message =
305-
format_diag_message(&sugg.msg, &args).map_err(Report::new).unwrap();
303+
let translated_message = format_diag_message(&sugg.msg, &args);
306304
Diagnostic {
307305
message: translated_message.to_string(),
308306
code: None,
@@ -417,10 +415,7 @@ impl DiagnosticSpan {
417415
Self::from_span_etc(
418416
span.span,
419417
span.is_primary,
420-
span.label
421-
.as_ref()
422-
.map(|m| format_diag_message(m, args).unwrap())
423-
.map(|m| m.to_string()),
418+
span.label.as_ref().map(|m| format_diag_message(m, args)).map(|m| m.to_string()),
424419
suggestion,
425420
je,
426421
)

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ extern crate self as rustc_errors;
2121
use std::backtrace::{Backtrace, BacktraceStatus};
2222
use std::borrow::Cow;
2323
use std::cell::Cell;
24-
use std::error::Report;
2524
use std::ffi::OsStr;
2625
use std::hash::Hash;
2726
use std::io::Write;
@@ -78,7 +77,6 @@ mod decorate_diag;
7877
mod diagnostic;
7978
mod diagnostic_impls;
8079
pub mod emitter;
81-
pub mod error;
8280
pub mod json;
8381
mod lock;
8482
pub mod markdown;
@@ -1437,7 +1435,7 @@ impl DiagCtxtInner {
14371435
args: impl Iterator<Item = DiagArg<'a>>,
14381436
) -> String {
14391437
let args = crate::translation::to_fluent_args(args);
1440-
format_diag_message(&message, &args).map_err(Report::new).unwrap().to_string()
1438+
format_diag_message(&message, &args).to_string()
14411439
}
14421440

14431441
fn eagerly_translate_for_subdiag(

compiler/rustc_errors/src/translation.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use std::borrow::Cow;
2-
use std::error::Report;
32

43
pub use rustc_error_messages::FluentArgs;
54
use rustc_error_messages::{langid, register_functions};
65
use tracing::{debug, trace};
76

8-
use crate::error::TranslateError;
97
use crate::fluent_bundle::FluentResource;
108
use crate::{DiagArg, DiagMessage, Style, fluent_bundle};
119

@@ -33,22 +31,14 @@ pub fn format_diag_messages(
3331
messages: &[(DiagMessage, Style)],
3432
args: &FluentArgs<'_>,
3533
) -> Cow<'static, str> {
36-
Cow::Owned(
37-
messages
38-
.iter()
39-
.map(|(m, _)| format_diag_message(m, args).map_err(Report::new).unwrap())
40-
.collect::<String>(),
41-
)
34+
Cow::Owned(messages.iter().map(|(m, _)| format_diag_message(m, args)).collect::<String>())
4235
}
4336

4437
/// Convert a `DiagMessage` to a string
45-
pub fn format_diag_message<'a>(
46-
message: &'a DiagMessage,
47-
args: &'a FluentArgs<'_>,
48-
) -> Result<Cow<'a, str>, TranslateError<'a>> {
38+
pub fn format_diag_message<'a>(message: &'a DiagMessage, args: &'a FluentArgs<'_>) -> Cow<'a, str> {
4939
trace!(?message, ?args);
5040
match message {
51-
DiagMessage::Str(msg) => Ok(Cow::Borrowed(msg)),
41+
DiagMessage::Str(msg) => Cow::Borrowed(msg),
5242
// This translates an inline fluent diagnostic message
5343
// It does this by creating a new `FluentBundle` with only one message,
5444
// and then translating using this bundle.
@@ -67,9 +57,9 @@ pub fn format_diag_message<'a>(
6757
let translated = bundle.format_pattern(value, Some(args), &mut errs).to_string();
6858
debug!(?translated, ?errs);
6959
if errs.is_empty() {
70-
Ok(Cow::Owned(translated))
60+
Cow::Owned(translated)
7161
} else {
72-
Err(TranslateError::fluent(&Cow::Borrowed(GENERATED_MSG_ID), args, errs))
62+
panic!("Fluent errors while formatting message: {errs:?}");
7363
}
7464
}
7565
}

src/librustdoc/passes/lint/check_code_block_syntax.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ impl Emitter for BufferEmitter {
151151
let mut buffer = self.buffer.borrow_mut();
152152

153153
let fluent_args = to_fluent_args(diag.args.iter());
154-
let translated_main_message = format_diag_message(&diag.messages[0].0, &fluent_args)
155-
.unwrap_or_else(|e| panic!("{e}"));
154+
let translated_main_message = format_diag_message(&diag.messages[0].0, &fluent_args);
156155

157156
buffer.messages.push(format!("error from rustc: {translated_main_message}"));
158157
if diag.is_error() {

0 commit comments

Comments
 (0)