Skip to content

Commit 1df013b

Browse files
committed
backup
1 parent 5ef37ed commit 1df013b

File tree

9 files changed

+73
-38
lines changed

9 files changed

+73
-38
lines changed

Diff for: compiler/rustc_error_messages/src/lib.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ type FluentId = Cow<'static, str>;
266266
pub enum SubdiagnosticMessage {
267267
/// Non-translatable diagnostic message.
268268
Str(Cow<'static, str>),
269+
/// Translatable diagnostic message in Fluent raw format.
270+
FluentRaw(Cow<'static, str>),
269271
/// Translatable message which has already been translated eagerly.
270272
///
271273
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
@@ -313,6 +315,8 @@ impl From<Cow<'static, str>> for SubdiagnosticMessage {
313315
pub enum DiagnosticMessage {
314316
/// Non-translatable diagnostic message.
315317
Str(Cow<'static, str>),
318+
/// Translatable diagnostic message in Fluent raw format.
319+
FluentRaw(Cow<'static, str>),
316320
/// Translatable message which has already been translated eagerly.
317321
///
318322
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
@@ -342,6 +346,7 @@ impl DiagnosticMessage {
342346
pub fn with_subdiagnostic_message(&self, sub: SubdiagnosticMessage) -> Self {
343347
let attr = match sub {
344348
SubdiagnosticMessage::Str(s) => return DiagnosticMessage::Str(s),
349+
SubdiagnosticMessage::FluentRaw(s) => return DiagnosticMessage::FluentRaw(s),
345350
SubdiagnosticMessage::Eager(s) => return DiagnosticMessage::Eager(s),
346351
SubdiagnosticMessage::FluentIdentifier(id) => {
347352
return DiagnosticMessage::FluentIdentifier(id, None);
@@ -351,6 +356,7 @@ impl DiagnosticMessage {
351356

352357
match self {
353358
DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()),
359+
DiagnosticMessage::FluentRaw(s) => DiagnosticMessage::FluentRaw(s.clone()),
354360
DiagnosticMessage::Eager(s) => DiagnosticMessage::Eager(s.clone()),
355361
DiagnosticMessage::FluentIdentifier(id, _) => {
356362
DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr))
@@ -360,7 +366,9 @@ impl DiagnosticMessage {
360366

361367
pub fn as_str(&self) -> Option<&str> {
362368
match self {
363-
DiagnosticMessage::Eager(s) | DiagnosticMessage::Str(s) => Some(s),
369+
DiagnosticMessage::Eager(s)
370+
| DiagnosticMessage::Str(s)
371+
| DiagnosticMessage::FluentRaw(s) => Some(s),
364372
DiagnosticMessage::FluentIdentifier(_, _) => None,
365373
}
366374
}
@@ -382,6 +390,13 @@ impl From<Cow<'static, str>> for DiagnosticMessage {
382390
}
383391
}
384392

393+
#[macro_export]
394+
macro_rules! fluent_raw {
395+
($str:expr) => {
396+
DiagnosticMessage::FluentRaw(Cow::Borrowed($str))
397+
};
398+
}
399+
385400
/// A workaround for "good path" ICEs when formatting types in disabled lints.
386401
///
387402
/// Delays formatting until `.into(): DiagnosticMessage` is used.
@@ -402,6 +417,7 @@ impl Into<SubdiagnosticMessage> for DiagnosticMessage {
402417
fn into(self) -> SubdiagnosticMessage {
403418
match self {
404419
DiagnosticMessage::Str(s) => SubdiagnosticMessage::Str(s),
420+
DiagnosticMessage::FluentRaw(s) => SubdiagnosticMessage::FluentRaw(s),
405421
DiagnosticMessage::Eager(s) => SubdiagnosticMessage::Eager(s),
406422
DiagnosticMessage::FluentIdentifier(id, None) => {
407423
SubdiagnosticMessage::FluentIdentifier(id)

Diff for: compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
3838
use rustc_data_structures::sync::{Lock, Lrc};
3939
use rustc_data_structures::AtomicRef;
4040
pub use rustc_error_messages::{
41-
fallback_fluent_bundle, fluent_bundle, DelayDm, DiagnosticMessage, FluentBundle,
41+
fallback_fluent_bundle, fluent_bundle, fluent_raw, DelayDm, DiagnosticMessage, FluentBundle,
4242
LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, SubdiagnosticMessage,
4343
};
4444
use rustc_fluent_macro::fluent_messages;

Diff for: compiler/rustc_errors/src/translation.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,23 @@ pub trait Translate {
6565
trace!(?message, ?args);
6666
let (identifier, attr) = match message {
6767
DiagnosticMessage::Str(msg) | DiagnosticMessage::Eager(msg) => {
68+
return Ok(Cow::Borrowed(msg));
69+
}
70+
DiagnosticMessage::FluentRaw(msg) => {
6871
// FIXME(yukang): A hack for raw fluent content for new diagnostics proc format
69-
let trimed = msg.replace(" ", "");
70-
if trimed.contains("$") || trimed.contains("{\"") || trimed.contains("\"}") {
71-
let fluent_text = format!("dummy = {}", msg);
72-
if let Ok(resource) = FluentResource::try_new(fluent_text) {
73-
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
74-
bundle.add_resource(resource).unwrap();
75-
let mut errors = vec![];
76-
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
77-
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
78-
return Ok(Cow::Owned(
79-
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
80-
));
81-
}
72+
let fluent_text = format!("dummy = {}", msg);
73+
if let Ok(resource) = FluentResource::try_new(fluent_text) {
74+
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
75+
bundle.add_resource(resource).unwrap();
76+
let mut errors = vec![];
77+
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
78+
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
79+
return Ok(Cow::Owned(
80+
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
81+
));
8282
}
83+
84+
// If the message is not a valid Fluent resource, just return the original
8385
return Ok(Cow::Borrowed(msg));
8486
}
8587
DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),

Diff for: compiler/rustc_macros/src/diagnostics/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a> DiagnosticDerive<'a> {
6969
}
7070
(None, Some(raw_label)) => {
7171
quote! {
72-
let mut #diag = #handler.struct_diagnostic(DiagnosticMessage::Str(#raw_label.into()));
72+
let mut #diag = #handler.struct_diagnostic(DiagnosticMessage::FluentRaw(#raw_label.into()));
7373
}
7474
}
7575
(Some(_slug), Some(_raw_label)) => {

Diff for: compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
509509

510510
let suggestion_label = if let Some(raw_label) = raw_label {
511511
quote! {
512-
#raw_label
512+
DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label))
513513
}
514514
} else {
515515
quote! {
@@ -547,15 +547,15 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
547547
return quote! {
548548
#diag.#fn_name(
549549
#field_binding,
550-
#raw_label
550+
DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label))
551551
);
552552
};
553553
}
554554
if let Some(raw_label) = self.get_attr(kind.to_string().as_str()) {
555555
quote! {
556556
#diag.#fn_name(
557557
#field_binding,
558-
#raw_label
558+
DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label))
559559
);
560560
}
561561
} else {
@@ -579,12 +579,12 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
579579
let diag = &self.parent.diag;
580580
if let Some(raw_label) = raw_label {
581581
return quote! {
582-
#diag.#kind(#raw_label);
582+
#diag.#kind(DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label)));
583583
};
584584
}
585585
if let Some(raw_label) = self.get_attr(kind.to_string().as_str()) {
586586
quote! {
587-
#diag.#kind(#raw_label);
587+
#diag.#kind(DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label)));
588588
}
589589
} else {
590590
quote! {

Diff for: compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
527527
quote! { let #message = #f(#diag, crate::fluent_generated::#slug.into()); },
528528
);
529529
} else {
530-
calls.extend(quote! { let #message = #f(#diag, #raw_label.into()); });
530+
calls.extend(quote! { let #message = #f(#diag, DiagnosticMessage::FluentRaw(Cow::Borrowed(#raw_label)).into()); });
531531
}
532532

533533
let name = format_ident!(

Diff for: compiler/rustc_parse/src/errors.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
// ignore-tidy-filelength
22
use std::borrow::Cow;
33

4+
use crate::parser::{ForbiddenLetReason, TokenDescription};
45
use rustc_ast::token::Token;
56
use rustc_ast::{Path, Visibility};
6-
use rustc_errors::DiagnosticMessage;
7+
use rustc_errors::{fluent_raw, DiagnosticMessage};
78
use rustc_errors::{AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
89
use rustc_macros::{Diagnostic, Subdiagnostic};
910
use rustc_session::errors::ExprParenthesesNeeded;
1011
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
1112
use rustc_span::symbol::Ident;
1213
use rustc_span::{Span, Symbol};
1314

14-
use crate::parser::{ForbiddenLetReason, TokenDescription};
15-
1615
#[derive(Diagnostic)]
1716
#[diag("ambiguous `+` in a type")]
1817
pub(crate) struct AmbiguousPlus {
@@ -1205,18 +1204,22 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
12051204

12061205
let mut diag = handler.struct_diagnostic(match token_descr {
12071206
Some(TokenDescription::ReservedIdentifier) => {
1208-
"expected identifier, found reserved identifier `{$token}`"
1207+
fluent_raw!("expected identifier, found reserved identifier `{$token}`")
12091208
}
1210-
Some(TokenDescription::Keyword) => "expected identifier, found keyword `{$token}`",
1209+
Some(TokenDescription::Keyword) => {
1210+
fluent_raw!("expected identifier, found keyword `{$token}`")
1211+
}
1212+
12111213
Some(TokenDescription::ReservedKeyword) => {
1212-
"expected identifier, found reserved keyword `{$token}`"
1214+
fluent_raw!("expected identifier, found reserved keyword `{$token}`")
12131215
}
12141216

12151217
Some(TokenDescription::DocComment) => {
1216-
"expected identifier, found doc comment `{$token}`"
1218+
fluent_raw!("expected identifier, found doc comment `{$token}`")
1219+
}
1220+
None => {
1221+
fluent_raw!("expected identifier, found `{$token}`")
12171222
}
1218-
1219-
None => "expected identifier, found `{$token}`",
12201223
});
12211224
diag.set_span(self.span);
12221225
diag.set_arg("token", self.token);
@@ -1264,14 +1267,18 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi {
12641267

12651268
let mut diag = handler.struct_diagnostic(match token_descr {
12661269
Some(TokenDescription::ReservedIdentifier) => {
1267-
"expected `;`, found reserved identifier `{$token}`"
1270+
fluent_raw!("expected `;`, found reserved identifier `{$token}`")
1271+
}
1272+
Some(TokenDescription::Keyword) => {
1273+
fluent_raw!("expected `;`, found keyword `{$token}`")
12681274
}
1269-
Some(TokenDescription::Keyword) => "expected `;`, found keyword `{$token}`",
12701275
Some(TokenDescription::ReservedKeyword) => {
1271-
"expected `;`, found reserved keyword `{$token}`"
1276+
fluent_raw!("expected `;`, found reserved keyword `{$token}`")
1277+
}
1278+
Some(TokenDescription::DocComment) => {
1279+
fluent_raw!("expected `;`, found doc comment `{$token}`")
12721280
}
1273-
Some(TokenDescription::DocComment) => "expected `;`, found doc comment `{$token}`",
1274-
None => "expected `;`, found `{$token}`",
1281+
None => fluent_raw!("expected `;`, found `{$token}`"),
12751282
});
12761283
diag.set_span(self.span);
12771284
diag.set_arg("token", self.token);

Diff for: compiler/rustc_parse/src/parser/attr.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
33
use rustc_ast as ast;
44
use rustc_ast::attr;
55
use rustc_ast::token::{self, Delimiter, Nonterminal};
6+
use rustc_errors::fluent_raw;
7+
use rustc_errors::DiagnosticMessage;
68
use rustc_errors::{error_code, Diagnostic, IntoDiagnostic, PResult};
79
use rustc_span::{sym, BytePos, Span};
10+
use std::borrow::Cow;
811
use std::convert::TryInto;
912
use thin_vec::ThinVec;
1013
use tracing::debug;
@@ -175,10 +178,15 @@ impl<'a> Parser<'a> {
175178
Ok(Some(item)) => {
176179
// FIXME(#100717)
177180
err.set_arg("item", item.kind.descr());
178-
err.span_label(item.span, "the inner {$item_type} doesn't annotate this {$item}");
181+
err.span_label(
182+
item.span,
183+
fluent_raw!("the inner {$item_type} doesn't annotate this {$item}"),
184+
);
179185
err.span_suggestion_verbose(
180186
replacement_span,
181-
"to annotate the {$item}, change the {$item_type} from inner to outer style",
187+
fluent_raw!(
188+
"to annotate the {$item}, change the {$item_type} from inner to outer style"
189+
),
182190
match attr_type {
183191
OuterAttributeType::Attribute => "",
184192
OuterAttributeType::DocBlockComment => "*",

Diff for: compiler/rustc_parse/src/parser/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, R
2424
use rustc_ast::{ClosureBinder, MetaItemLit, StmtKind};
2525
use rustc_ast_pretty::pprust;
2626
use rustc_data_structures::stack::ensure_sufficient_stack;
27+
use rustc_errors::DiagnosticMessage;
2728
use rustc_errors::{
2829
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
2930
PResult, StashKey,
@@ -36,6 +37,7 @@ use rustc_span::source_map::{self, Spanned};
3637
use rustc_span::symbol::kw::PathRoot;
3738
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3839
use rustc_span::{BytePos, Pos, Span};
40+
use std::borrow::Cow;
3941
use thin_vec::{thin_vec, ThinVec};
4042

4143
/// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression

0 commit comments

Comments
 (0)