Skip to content

Commit 7347655

Browse files
authored
Rollup merge of #106427 - mejrs:translation_errors, r=davidtwco
Improve fluent error messages These have been really frustrating me while migrating diagnostics.
2 parents 1693891 + 4c0c32c commit 7347655

File tree

7 files changed

+404
-79
lines changed

7 files changed

+404
-79
lines changed

compiler/rustc_errors/src/emitter.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_error_messages::{FluentArgs, SpanLabel};
2828
use rustc_span::hygiene::{ExpnKind, MacroKind};
2929
use std::borrow::Cow;
3030
use std::cmp::{max, min, Reverse};
31+
use std::error::Report;
3132
use std::io::prelude::*;
3233
use std::io::{self, IsTerminal};
3334
use std::iter;
@@ -250,7 +251,7 @@ pub trait Emitter: Translate {
250251
let mut primary_span = diag.span.clone();
251252
let suggestions = diag.suggestions.as_deref().unwrap_or(&[]);
252253
if let Some((sugg, rest)) = suggestions.split_first() {
253-
let msg = self.translate_message(&sugg.msg, fluent_args);
254+
let msg = self.translate_message(&sugg.msg, fluent_args).map_err(Report::new).unwrap();
254255
if rest.is_empty() &&
255256
// ^ if there is only one suggestion
256257
// don't display multi-suggestions as labels
@@ -1325,7 +1326,7 @@ impl EmitterWriter {
13251326
// very *weird* formats
13261327
// see?
13271328
for (text, style) in msg.iter() {
1328-
let text = self.translate_message(text, args);
1329+
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13291330
let lines = text.split('\n').collect::<Vec<_>>();
13301331
if lines.len() > 1 {
13311332
for (i, line) in lines.iter().enumerate() {
@@ -1387,7 +1388,7 @@ impl EmitterWriter {
13871388
label_width += 2;
13881389
}
13891390
for (text, _) in msg.iter() {
1390-
let text = self.translate_message(text, args);
1391+
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13911392
// Account for newlines to align output to its label.
13921393
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
13931394
buffer.append(
@@ -2301,7 +2302,9 @@ impl FileWithAnnotatedLines {
23012302
hi.col_display += 1;
23022303
}
23032304

2304-
let label = label.as_ref().map(|m| emitter.translate_message(m, args).to_string());
2305+
let label = label.as_ref().map(|m| {
2306+
emitter.translate_message(m, args).map_err(Report::new).unwrap().to_string()
2307+
});
23052308

23062309
if lo.line != hi.line {
23072310
let ml = MultilineAnnotation {

compiler/rustc_errors/src/error.rs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use rustc_error_messages::{
2+
fluent_bundle::resolver::errors::{ReferenceKind, ResolverError},
3+
FluentArgs, FluentError,
4+
};
5+
use std::borrow::Cow;
6+
use std::error::Error;
7+
use std::fmt;
8+
9+
#[derive(Debug)]
10+
pub enum TranslateError<'args> {
11+
One {
12+
id: &'args Cow<'args, str>,
13+
args: &'args FluentArgs<'args>,
14+
kind: TranslateErrorKind<'args>,
15+
},
16+
Two {
17+
primary: Box<TranslateError<'args>>,
18+
fallback: Box<TranslateError<'args>>,
19+
},
20+
}
21+
22+
impl<'args> TranslateError<'args> {
23+
pub fn message(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
24+
Self::One { id, args, kind: TranslateErrorKind::MessageMissing }
25+
}
26+
pub fn primary(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
27+
Self::One { id, args, kind: TranslateErrorKind::PrimaryBundleMissing }
28+
}
29+
pub fn attribute(
30+
id: &'args Cow<'args, str>,
31+
args: &'args FluentArgs<'args>,
32+
attr: &'args str,
33+
) -> Self {
34+
Self::One { id, args, kind: TranslateErrorKind::AttributeMissing { attr } }
35+
}
36+
pub fn value(id: &'args Cow<'args, str>, args: &'args FluentArgs<'args>) -> Self {
37+
Self::One { id, args, kind: TranslateErrorKind::ValueMissing }
38+
}
39+
40+
pub fn fluent(
41+
id: &'args Cow<'args, str>,
42+
args: &'args FluentArgs<'args>,
43+
errs: Vec<FluentError>,
44+
) -> Self {
45+
Self::One { id, args, kind: TranslateErrorKind::Fluent { errs } }
46+
}
47+
48+
pub fn and(self, fallback: TranslateError<'args>) -> TranslateError<'args> {
49+
Self::Two { primary: Box::new(self), fallback: Box::new(fallback) }
50+
}
51+
}
52+
53+
#[derive(Debug)]
54+
pub enum TranslateErrorKind<'args> {
55+
MessageMissing,
56+
PrimaryBundleMissing,
57+
AttributeMissing { attr: &'args str },
58+
ValueMissing,
59+
Fluent { errs: Vec<FluentError> },
60+
}
61+
62+
impl fmt::Display for TranslateError<'_> {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
use TranslateErrorKind::*;
65+
66+
match self {
67+
Self::One { id, args, kind } => {
68+
writeln!(f, "failed while formatting fluent string `{id}`: ")?;
69+
match kind {
70+
MessageMissing => writeln!(f, "message was missing")?,
71+
PrimaryBundleMissing => writeln!(f, "the primary bundle was missing")?,
72+
AttributeMissing { attr } => {
73+
writeln!(f, "the attribute `{attr}` was missing")?;
74+
writeln!(f, "help: add `.{attr} = <message>`")?;
75+
}
76+
ValueMissing => writeln!(f, "the value was missing")?,
77+
Fluent { errs } => {
78+
for err in errs {
79+
match err {
80+
FluentError::ResolverError(ResolverError::Reference(
81+
ReferenceKind::Message { id, .. }
82+
| ReferenceKind::Variable { id, .. },
83+
)) => {
84+
if args.iter().any(|(arg_id, _)| arg_id == id) {
85+
writeln!(
86+
f,
87+
"argument `{id}` exists but was not referenced correctly"
88+
)?;
89+
writeln!(f, "help: try using `{{${id}}}` instead")?;
90+
} else {
91+
writeln!(
92+
f,
93+
"the fluent string has an argument `{id}` that was not found."
94+
)?;
95+
let vars: Vec<&str> =
96+
args.iter().map(|(a, _v)| a).collect();
97+
match &*vars {
98+
[] => writeln!(f, "help: no arguments are available")?,
99+
[one] => writeln!(
100+
f,
101+
"help: the argument `{one}` is available"
102+
)?,
103+
[first, middle @ .., last] => {
104+
write!(f, "help: the arguments `{first}`")?;
105+
for a in middle {
106+
write!(f, ", `{a}`")?;
107+
}
108+
writeln!(f, " and `{last}` are available")?;
109+
}
110+
}
111+
}
112+
}
113+
_ => writeln!(f, "{err}")?,
114+
}
115+
}
116+
}
117+
}
118+
}
119+
// If someone cares about primary bundles, they'll probably notice it's missing
120+
// regardless or will be using `debug_assertions`
121+
// so we skip the arm below this one to avoid confusing the regular user.
122+
Self::Two { primary: box Self::One { kind: PrimaryBundleMissing, .. }, fallback } => {
123+
fmt::Display::fmt(fallback, f)?;
124+
}
125+
Self::Two { primary, fallback } => {
126+
writeln!(
127+
f,
128+
"first, fluent formatting using the primary bundle failed:\n {primary}\n \
129+
while attempting to recover by using the fallback bundle instead, another error occurred:\n{fallback}"
130+
)?;
131+
}
132+
}
133+
Ok(())
134+
}
135+
}
136+
137+
impl Error for TranslateError<'_> {}

compiler/rustc_errors/src/json.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_data_structures::sync::Lrc;
2424
use rustc_error_messages::FluentArgs;
2525
use rustc_span::hygiene::ExpnData;
2626
use rustc_span::Span;
27+
use std::error::Report;
2728
use std::io::{self, Write};
2829
use std::path::Path;
2930
use std::sync::{Arc, Mutex};
@@ -321,7 +322,8 @@ impl Diagnostic {
321322
fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
322323
let args = to_fluent_args(diag.args());
323324
let sugg = diag.suggestions.iter().flatten().map(|sugg| {
324-
let translated_message = je.translate_message(&sugg.msg, &args);
325+
let translated_message =
326+
je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();
325327
Diagnostic {
326328
message: translated_message.to_string(),
327329
code: None,
@@ -411,7 +413,10 @@ impl DiagnosticSpan {
411413
Self::from_span_etc(
412414
span.span,
413415
span.is_primary,
414-
span.label.as_ref().map(|m| je.translate_message(m, args)).map(|m| m.to_string()),
416+
span.label
417+
.as_ref()
418+
.map(|m| je.translate_message(m, args).unwrap())
419+
.map(|m| m.to_string()),
415420
suggestion,
416421
je,
417422
)

compiler/rustc_errors/src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#![feature(never_type)]
1212
#![feature(result_option_inspect)]
1313
#![feature(rustc_attrs)]
14+
#![feature(yeet_expr)]
15+
#![feature(try_blocks)]
16+
#![feature(box_patterns)]
17+
#![feature(error_reporter)]
1418
#![allow(incomplete_features)]
1519

1620
#[macro_use]
@@ -41,6 +45,7 @@ use rustc_span::HashStableContext;
4145
use rustc_span::{Loc, Span};
4246

4347
use std::borrow::Cow;
48+
use std::error::Report;
4449
use std::fmt;
4550
use std::hash::Hash;
4651
use std::num::NonZeroUsize;
@@ -54,11 +59,14 @@ mod diagnostic;
5459
mod diagnostic_builder;
5560
mod diagnostic_impls;
5661
pub mod emitter;
62+
pub mod error;
5763
pub mod json;
5864
mod lock;
5965
pub mod registry;
6066
mod snippet;
6167
mod styled_buffer;
68+
#[cfg(test)]
69+
mod tests;
6270
pub mod translation;
6371

6472
pub use diagnostic_builder::IntoDiagnostic;
@@ -616,7 +624,14 @@ impl Handler {
616624
) -> SubdiagnosticMessage {
617625
let inner = self.inner.borrow();
618626
let args = crate::translation::to_fluent_args(args);
619-
SubdiagnosticMessage::Eager(inner.emitter.translate_message(&message, &args).to_string())
627+
SubdiagnosticMessage::Eager(
628+
inner
629+
.emitter
630+
.translate_message(&message, &args)
631+
.map_err(Report::new)
632+
.unwrap()
633+
.to_string(),
634+
)
620635
}
621636

622637
// This is here to not allow mutation of flags;

0 commit comments

Comments
 (0)