Skip to content

Commit 03c8665

Browse files
committed
find fluent resource from hash slug
1 parent 7b5cb45 commit 03c8665

File tree

9 files changed

+249
-198
lines changed

9 files changed

+249
-198
lines changed

Cargo.lock

+4-2
Original file line numberDiff line numberDiff line change
@@ -2338,10 +2338,11 @@ dependencies = [
23382338

23392339
[[package]]
23402340
name = "md-5"
2341-
version = "0.10.5"
2341+
version = "0.10.6"
23422342
source = "registry+https://github.com/rust-lang/crates.io-index"
2343-
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
2343+
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
23442344
dependencies = [
2345+
"cfg-if",
23452346
"digest",
23462347
]
23472348

@@ -3812,6 +3813,7 @@ dependencies = [
38123813
"annotate-snippets",
38133814
"derive_setters",
38143815
"fluent",
3816+
"md-5",
38153817
"rustc_ast",
38163818
"rustc_ast_pretty",
38173819
"rustc_data_structures",

compiler/rustc_errors/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
annotate-snippets = "0.9"
99
derive_setters = "0.1.6"
1010
fluent = "0.16.0"
11+
md-5 = "0.10.6"
1112
rustc_ast = { path = "../rustc_ast" }
1213
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_errors/src/translation.rs

+60-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::fluent_bundle::FluentResource;
33
use crate::snippet::Style;
44
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
55
use fluent::FluentBundle as RawFluentBundle;
6+
use md5::{Digest, Md5};
67
use rustc_data_structures::sync::Lrc;
78
use rustc_error_messages::FluentArgs;
89
use std::borrow::Cow;
@@ -68,24 +69,69 @@ pub trait Translate {
6869
return Ok(Cow::Borrowed(msg));
6970
}
7071
DiagnosticMessage::FluentRaw(msg) => {
71-
// FIXME(yukang): A hack for raw fluent content for new diagnostics proc format
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-
));
82-
}
72+
// FIXME(yukang): calculate the `slug` from the raw fluent content,
73+
// The fluent resources are generated by a simple standalone visitor:
74+
// https://github.com/chenyukang/fluent-utils/blob/main/src/visitor.rs#L13-L97
75+
// we may need to add fluent-utils into the tools directory of rustc
76+
let mut hasher = Md5::new();
77+
hasher.update(msg.to_string());
78+
let digest = hasher.finalize();
79+
let id = format!("{:x}", digest);
80+
let identifier = format!("slug-{}", id[0..8].to_string());
81+
let id = Cow::Borrowed(identifier.as_str());
8382

84-
// If the message is not a valid Fluent resource, just return the original
85-
return Ok(Cow::Borrowed(msg));
83+
let translate_with_bundle =
84+
|bundle: &'a FluentBundle| -> Result<Cow<'_, str>, TranslateError<'_>> {
85+
let message = bundle
86+
.get_message(&identifier)
87+
.ok_or(TranslateError::message(&id, args))?;
88+
let value = message.value().ok_or(TranslateError::value(&id, args))?;
89+
debug!(?message, ?value);
90+
91+
let mut errs = vec![];
92+
let translated = bundle.format_pattern(value, Some(args), &mut errs);
93+
debug!(?translated, ?errs);
94+
if errs.is_empty() {
95+
Ok(translated)
96+
} else {
97+
Err(TranslateError::fluent(&id, args, errs))
98+
}
99+
};
100+
101+
return {
102+
match self.fluent_bundle().map(|b| translate_with_bundle(b)) {
103+
// The primary bundle was present and translation succeeded
104+
Some(Ok(t)) => {
105+
// eprintln!("translated id OK: {} => {}", identifier, t);
106+
Ok(t)
107+
}
108+
109+
// If `translate_with_bundle` returns `Err` with the primary bundle, this is likely
110+
// just that the primary bundle doesn't contain the message being translated, so
111+
// proceed to the fallback bundle.
112+
_ => {
113+
// fallback to en-US, we don't need fluent bundle for raw fluent content in English
114+
// here we just interpret the variables in the fluent content.
115+
let fluent_text = format!("dummy = {}", msg);
116+
if let Ok(resource) = FluentResource::try_new(fluent_text) {
117+
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
118+
bundle.add_resource(resource).unwrap();
119+
let mut errors = vec![];
120+
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
121+
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
122+
Ok(Cow::Owned(
123+
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
124+
))
125+
} else {
126+
Ok(Cow::Owned(msg.to_string()))
127+
}
128+
}
129+
}
130+
};
86131
}
87132
DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
88133
};
134+
// FIXME(yukang): remove this part for fluent resource id after all diagnostics are migrated to Fluent
89135
let translate_with_bundle =
90136
|bundle: &'a FluentBundle| -> Result<Cow<'_, str>, TranslateError<'_>> {
91137
let message = bundle

compiler/rustc_parse/src/errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,10 @@ pub(crate) struct ParenthesesInMatchPat {
15281528
}
15291529

15301530
#[derive(Subdiagnostic)]
1531-
#[multipart_suggestion(label = "remove parentheses surrounding the pattern", applicability = "machine-applicable")]
1531+
#[multipart_suggestion(
1532+
label = "remove parentheses surrounding the pattern",
1533+
applicability = "machine-applicable"
1534+
)]
15321535
pub(crate) struct ParenthesesInMatchPatSugg {
15331536
#[suggestion_part(code = "")]
15341537
pub left: Span,
@@ -3472,7 +3475,10 @@ pub(crate) struct TransposeDynOrImpl<'a> {
34723475
}
34733476

34743477
#[derive(Subdiagnostic)]
3475-
#[multipart_suggestion(label = "move `{$kw}` before the `for<...>`", applicability = "machine-applicable")]
3478+
#[multipart_suggestion(
3479+
label = "move `{$kw}` before the `for<...>`",
3480+
applicability = "machine-applicable"
3481+
)]
34763482
pub(crate) struct TransposeDynOrImplSugg<'a> {
34773483
#[suggestion_part(code = "")]
34783484
pub removal_span: Span,

src/tools/rust-analyzer/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -2000,9 +2000,9 @@ dependencies = [
20002000

20012001
[[package]]
20022002
name = "triomphe"
2003-
version = "0.1.11"
2003+
version = "0.1.10"
20042004
source = "registry+https://github.com/rust-lang/crates.io-index"
2005-
checksum = "859eb650cfee7434994602c3a68b25d77ad9e68c8a6cd491616ef86661382eb3"
2005+
checksum = "d0c5a71827ac326072b6405552093e2ad2accd25a32fd78d4edc82d98c7f2409"
20062006

20072007
[[package]]
20082008
name = "tt"

tests/run-make/translation/Makefile

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ FAKEROOT=$(TMPDIR)/fakeroot
99
RUSTC_LOG:=rustc_error_messages
1010
export RUSTC_TRANSLATION_NO_DEBUG_ASSERT:=1
1111

12-
all: normal missing broken sysroot-invalid sysroot-missing
12+
all: normal custom missing broken sysroot sysroot-invalid sysroot-missing
1313

1414
# Check that the test works normally, using the built-in fallback bundle.
1515
normal: test.rs
1616
$(RUSTC) $< 2>&1 | $(CGREP) "struct literal body without path"
1717

1818
# Check that a primary bundle can be loaded and will be preferentially used
1919
# where possible.
20-
# FIXME(yukang): This test is broken because the compiler doesn't look for the fluent slugs now
21-
# we need to fix it after we have implemented the new way to find the fluent resources
2220
custom: test.rs working.ftl
2321
$(RUSTC) $< -Ztranslate-additional-ftl=$(CURDIR)/working.ftl 2>&1 | $(CGREP) "this is a test message"
2422

@@ -36,8 +34,6 @@ broken: test.rs broken.ftl
3634
# identifier by making a local copy of the sysroot and adding the custom locale
3735
# to it.
3836

39-
# FIXME(yukang): This test is broken because the compiler doesn't look for the fluent slugs now
40-
# we need to fix it after we have implemented the new way to find the fluent resources
4137
sysroot: test.rs working.ftl
4238
rm -rf $(FAKEROOT)
4339
mkdir $(FAKEROOT)
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
parse_struct_literal_body_without_path = this is a test message
2-
.suggestion = this is a test suggestion
1+
slug-687d6246 = this is a test message
2+
slug-7d40154a = this is a test suggestion

0 commit comments

Comments
 (0)