Skip to content

Commit ce78bfb

Browse files
committed
begin on no-fluent-errors
1 parent f4d794e commit ce78bfb

File tree

13 files changed

+1502
-636
lines changed

13 files changed

+1502
-636
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,16 @@ dependencies = [
13241324
"miniz_oxide",
13251325
]
13261326

1327+
[[package]]
1328+
name = "fluent"
1329+
version = "0.16.0"
1330+
source = "registry+https://github.com/rust-lang/crates.io-index"
1331+
checksum = "61f69378194459db76abd2ce3952b790db103ceb003008d3d50d97c41ff847a7"
1332+
dependencies = [
1333+
"fluent-bundle",
1334+
"unic-langid",
1335+
]
1336+
13271337
[[package]]
13281338
name = "fluent-bundle"
13291339
version = "0.15.2"
@@ -3772,6 +3782,7 @@ version = "0.0.0"
37723782
dependencies = [
37733783
"annotate-snippets",
37743784
"derive_setters",
3785+
"fluent",
37753786
"rustc_ast",
37763787
"rustc_ast_pretty",
37773788
"rustc_data_structures",
@@ -3789,6 +3800,7 @@ dependencies = [
37893800
"termcolor",
37903801
"termize",
37913802
"tracing",
3803+
"unic-langid",
37923804
"unicode-width",
37933805
"windows",
37943806
]
@@ -4113,6 +4125,7 @@ version = "0.0.0"
41134125
dependencies = [
41144126
"proc-macro2",
41154127
"quote",
4128+
"regex",
41164129
"syn 2.0.32",
41174130
"synstructure",
41184131
]

compiler/rustc_errors/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ termcolor = "1.2.0"
2525
termize = "0.1.1"
2626
tracing = "0.1"
2727
unicode-width = "0.1.4"
28+
fluent = "0.16.0"
29+
unic-langid = {version = "0.9.1", features = ["unic-langid-macros"]}
30+
2831
# tidy-alphabetical-end
2932

3033
[target.'cfg(windows)'.dependencies.windows]

compiler/rustc_errors/src/translation.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use crate::error::{TranslateError, TranslateErrorKind};
2+
use crate::fluent_bundle::FluentResource;
23
use crate::snippet::Style;
34
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
5+
use fluent::FluentBundle as RawFluentBundle;
46
use rustc_data_structures::sync::Lrc;
57
use rustc_error_messages::FluentArgs;
68
use std::borrow::Cow;
79
use std::env;
810
use std::error::Report;
11+
use unic_langid::langid;
912

1013
/// Convert diagnostic arguments (a rustc internal type that exists to implement
1114
/// `Encodable`/`Decodable`) into `FluentArgs` which is necessary to perform translation.
@@ -62,7 +65,29 @@ pub trait Translate {
6265
trace!(?message, ?args);
6366
let (identifier, attr) = match message {
6467
DiagnosticMessage::Str(msg) | DiagnosticMessage::Eager(msg) => {
65-
return Ok(Cow::Borrowed(msg));
68+
if args.iter().next().is_none() || (!msg.contains("$") && !msg.contains("`{")) {
69+
return Ok(Cow::Borrowed(msg));
70+
} else {
71+
// FIXME(yukang) A hacky for raw fluent content
72+
let fluent_text = format!("dummy = {}", msg);
73+
if let Ok(resource) = FluentResource::try_new(fluent_text) {
74+
let langid_en = langid!("en-US");
75+
let mut bundle = RawFluentBundle::new(vec![langid_en]);
76+
bundle.add_resource(resource).unwrap();
77+
let mut errors = vec![];
78+
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
79+
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
80+
//eprintln!("translated: {:?}", msg);
81+
//eprintln!("args: {:?}", args);
82+
//eprintln!("res: {:?}", res);
83+
return Ok(Cow::Owned(
84+
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
85+
));
86+
} else {
87+
//eprintln!("translate error: {}, args: {:?}", msg, args);
88+
return Ok(Cow::Borrowed(msg));
89+
}
90+
}
6691
}
6792
DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
6893
};

compiler/rustc_macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ proc-macro2 = "1"
1212
quote = "1"
1313
syn = { version = "2.0.9", features = ["full"] }
1414
synstructure = "0.13.0"
15+
regex = "1.3.3"
1516
# tidy-alphabetical-end

compiler/rustc_macros/src/diagnostics/diagnostic.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ impl<'a> DiagnosticDerive<'a> {
2828
let preamble = builder.preamble(variant);
2929
let body = builder.body(variant);
3030

31-
let init = match builder.slug.value_ref() {
32-
None => {
33-
span_err(builder.span, "diagnostic slug not specified")
31+
let init = match (builder.slug.value_ref(), builder.label.value_ref()) {
32+
(None, None) => {
33+
span_err(builder.span, "diagnostic slug or label is not specified")
3434
.help(
3535
"specify the slug as the first argument to the `#[diag(...)]` \
36-
attribute, such as `#[diag(hir_analysis_example_error)]`",
36+
attribute, such as `#[diag(hir_analysis_example_error)]`, or use format #[diag(label = \"the message ..\")]",
3737
)
3838
.emit();
3939
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
4040
}
41-
Some(slug)
41+
(Some(slug), None)
4242
if let Some(Mismatch { slug_name, crate_name, slug_prefix }) =
4343
Mismatch::check(slug) =>
4444
{
@@ -48,8 +48,7 @@ impl<'a> DiagnosticDerive<'a> {
4848
.emit();
4949
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
5050
}
51-
Some(slug) => {
52-
slugs.borrow_mut().push(slug.clone());
51+
(Some(slug), None) => {
5352
quote! {
5453
let mut diag = rustc_errors::DiagnosticBuilder::new(
5554
dcx,
@@ -58,6 +57,14 @@ impl<'a> DiagnosticDerive<'a> {
5857
);
5958
}
6059
}
60+
(None, Some(text)) => {
61+
quote! {
62+
let mut #diag = #handler.struct_diagnostic(DiagnosticMessage::Str(#text.into()));
63+
}
64+
}
65+
(Some(_slug), Some(_text)) => {
66+
unreachable!("BUG: slug and text specified");
67+
}
6168
};
6269

6370
let formatting_init = &builder.formatting_init;
@@ -88,9 +95,6 @@ impl<'a> DiagnosticDerive<'a> {
8895
}
8996
}
9097
});
91-
for test in slugs.borrow().iter().map(|s| generate_test(s, &structure)) {
92-
imp.extend(test);
93-
}
9498
imp
9599
}
96100
}

0 commit comments

Comments
 (0)