Skip to content

Commit 8752ce9

Browse files
committed
begin on no-fluent-errors
1 parent ba7c7a3 commit 8752ce9

File tree

12 files changed

+1486
-640
lines changed

12 files changed

+1486
-640
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,16 @@ dependencies = [
13691369
"miniz_oxide",
13701370
]
13711371

1372+
[[package]]
1373+
name = "fluent"
1374+
version = "0.16.0"
1375+
source = "registry+https://github.com/rust-lang/crates.io-index"
1376+
checksum = "61f69378194459db76abd2ce3952b790db103ceb003008d3d50d97c41ff847a7"
1377+
dependencies = [
1378+
"fluent-bundle",
1379+
"unic-langid",
1380+
]
1381+
13721382
[[package]]
13731383
name = "fluent-bundle"
13741384
version = "0.15.2"
@@ -3858,6 +3868,7 @@ version = "0.0.0"
38583868
dependencies = [
38593869
"annotate-snippets",
38603870
"derive_setters",
3871+
"fluent",
38613872
"rustc_ast",
38623873
"rustc_ast_pretty",
38633874
"rustc_data_structures",
@@ -3875,6 +3886,7 @@ dependencies = [
38753886
"termcolor",
38763887
"termize",
38773888
"tracing",
3889+
"unic-langid",
38783890
"unicode-width",
38793891
"windows",
38803892
]
@@ -4190,6 +4202,7 @@ version = "0.0.0"
41904202
dependencies = [
41914203
"proc-macro2",
41924204
"quote",
4205+
"regex",
41934206
"syn 2.0.29",
41944207
"synstructure",
41954208
]

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: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,27 @@ impl<'a> DiagnosticDerive<'a> {
3030
pub(crate) fn into_tokens(self) -> TokenStream {
3131
let DiagnosticDerive { mut structure, mut builder } = self;
3232

33-
let slugs = RefCell::new(Vec::new());
3433
let implementation = builder.each_variant(&mut structure, |mut builder, variant| {
35-
let preamble = builder.preamble(variant);
36-
let body = builder.body(variant);
37-
3834
let diag = &builder.parent.diag;
3935
let DiagnosticDeriveKind::Diagnostic { handler } = &builder.parent.kind else {
36+
eprintln!("BUG: DiagnosticDeriveKind::Diagnostic expected");
4037
unreachable!()
4138
};
42-
let init = match builder.slug.value_ref() {
43-
None => {
44-
span_err(builder.span, "diagnostic slug not specified")
39+
40+
let preamble = builder.preamble(variant);
41+
let body = builder.body(variant);
42+
43+
let init = match (builder.slug.value_ref(), builder.label.value_ref()) {
44+
(None, None) => {
45+
span_err(builder.span, "diagnostic slug or label is not specified")
4546
.help(
4647
"specify the slug as the first argument to the `#[diag(...)]` \
47-
attribute, such as `#[diag(hir_analysis_example_error)]`",
48+
attribute, such as `#[diag(hir_analysis_example_error)]`, or use format #[diag(label = \"the message ..\")]",
4849
)
4950
.emit();
5051
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
5152
}
52-
Some(slug)
53+
(Some(slug), None)
5354
if let Some(Mismatch { slug_name, crate_name, slug_prefix }) =
5455
Mismatch::check(slug) =>
5556
{
@@ -59,12 +60,19 @@ impl<'a> DiagnosticDerive<'a> {
5960
.emit();
6061
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
6162
}
62-
Some(slug) => {
63-
slugs.borrow_mut().push(slug.clone());
63+
(Some(slug), None) => {
6464
quote! {
6565
let mut #diag = #handler.struct_diagnostic(crate::fluent_generated::#slug);
6666
}
6767
}
68+
(None, Some(text)) => {
69+
quote! {
70+
let mut #diag = #handler.struct_diagnostic(DiagnosticMessage::Str(#text.into()));
71+
}
72+
}
73+
(Some(_slug), Some(_text)) => {
74+
unreachable!("BUG: slug and text specified");
75+
}
6876
};
6977

7078
let formatting_init = &builder.formatting_init;
@@ -77,9 +85,11 @@ impl<'a> DiagnosticDerive<'a> {
7785
}
7886
});
7987

80-
let DiagnosticDeriveKind::Diagnostic { handler } = &builder.kind else { unreachable!() };
88+
let DiagnosticDeriveKind::Diagnostic { handler } = &builder.kind else {
89+
unreachable!();
90+
};
8191

82-
let mut imp = structure.gen_impl(quote! {
92+
let imp = structure.gen_impl(quote! {
8393
gen impl<'__diagnostic_handler_sess, G>
8494
rustc_errors::IntoDiagnostic<'__diagnostic_handler_sess, G>
8595
for @Self
@@ -96,9 +106,6 @@ impl<'a> DiagnosticDerive<'a> {
96106
}
97107
}
98108
});
99-
for test in slugs.borrow().iter().map(|s| generate_test(s, &structure)) {
100-
imp.extend(test);
101-
}
102109
imp
103110
}
104111
}

0 commit comments

Comments
 (0)