1 #![feature(let_chains)]
3 #![feature(rustc_attrs)]
4 #![feature(type_alias_impl_trait)]
5 #![deny(rustc::untranslatable_diagnostic)]
6 #![deny(rustc::diagnostic_outside_of_impl)]
11 use fluent_bundle::FluentResource;
12 use fluent_syntax::parser::ParserError;
13 use icu_provider_adapters::fallback::{LocaleFallbackProvider, LocaleFallbacker};
14 use rustc_data_structures::sync::Lrc;
15 use rustc_macros::{fluent_messages, Decodable, Encodable};
18 use std::error::Error;
22 use std::path::{Path, PathBuf};
24 #[cfg(not(parallel_compiler))]
25 use std::cell::LazyCell as Lazy;
26 #[cfg(parallel_compiler)]
27 use std::sync::LazyLock as Lazy;
29 #[cfg(parallel_compiler)]
30 use intl_memoizer::concurrent::IntlLangMemoizer;
31 #[cfg(not(parallel_compiler))]
32 use intl_memoizer::IntlLangMemoizer;
34 pub use fluent_bundle::{self, types::FluentType, FluentArgs, FluentError, FluentValue};
35 pub use unic_langid::{langid, LanguageIdentifier};
37 // Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
39 // tidy-alphabetical-start
40 ast_lowering => "../locales/en-US/ast_lowering.ftl",
41 ast_passes => "../locales/en-US/ast_passes.ftl",
42 attr => "../locales/en-US/attr.ftl",
43 borrowck => "../locales/en-US/borrowck.ftl",
44 builtin_macros => "../locales/en-US/builtin_macros.ftl",
45 codegen_gcc => "../locales/en-US/codegen_gcc.ftl",
46 codegen_llvm => "../locales/en-US/codegen_llvm.ftl",
47 codegen_ssa => "../locales/en-US/codegen_ssa.ftl",
48 compiletest => "../locales/en-US/compiletest.ftl",
49 const_eval => "../locales/en-US/const_eval.ftl",
50 driver => "../locales/en-US/driver.ftl",
51 errors => "../locales/en-US/errors.ftl",
52 expand => "../locales/en-US/expand.ftl",
53 hir_analysis => "../locales/en-US/hir_analysis.ftl",
54 hir_typeck => "../locales/en-US/hir_typeck.ftl",
55 infer => "../locales/en-US/infer.ftl",
56 interface => "../locales/en-US/interface.ftl",
57 lint => "../locales/en-US/lint.ftl",
58 metadata => "../locales/en-US/metadata.ftl",
59 middle => "../locales/en-US/middle.ftl",
60 mir_build => "../locales/en-US/mir_build.ftl",
61 mir_dataflow => "../locales/en-US/mir_dataflow.ftl",
62 monomorphize => "../locales/en-US/monomorphize.ftl",
63 parse => "../locales/en-US/parse.ftl",
64 passes => "../locales/en-US/passes.ftl",
65 plugin_impl => "../locales/en-US/plugin_impl.ftl",
66 privacy => "../locales/en-US/privacy.ftl",
67 query_system => "../locales/en-US/query_system.ftl",
68 resolve => "../locales/en-US/resolve.ftl",
69 save_analysis => "../locales/en-US/save_analysis.ftl",
70 session => "../locales/en-US/session.ftl",
71 symbol_mangling => "../locales/en-US/symbol_mangling.ftl",
72 trait_selection => "../locales/en-US/trait_selection.ftl",
73 ty_utils => "../locales/en-US/ty_utils.ftl",
74 // tidy-alphabetical-end
77 pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};
79 pub type FluentBundle = fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>;
81 #[cfg(parallel_compiler)]
82 fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
83 FluentBundle::new_concurrent(locales)
86 #[cfg(not(parallel_compiler))]
87 fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
88 FluentBundle::new(locales)
92 pub enum TranslationBundleError {
93 /// Failed to read from `.ftl` file.
95 /// Failed to parse contents of `.ftl` file.
96 ParseFtl(ParserError),
97 /// Failed to add `FluentResource` to `FluentBundle`.
98 AddResource(FluentError),
99 /// `$sysroot/share/locale/$locale` does not exist.
101 /// Cannot read directory entries of `$sysroot/share/locale/$locale`.
102 ReadLocalesDir(io::Error),
103 /// Cannot read directory entry of `$sysroot/share/locale/$locale`.
104 ReadLocalesDirEntry(io::Error),
105 /// `$sysroot/share/locale/$locale` is not a directory.
109 impl fmt::Display for TranslationBundleError {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 TranslationBundleError::ReadFtl(e) => write!(f, "could not read ftl file: {}", e),
113 TranslationBundleError::ParseFtl(e) => {
114 write!(f, "could not parse ftl file: {}", e)
116 TranslationBundleError::AddResource(e) => write!(f, "failed to add resource: {}", e),
117 TranslationBundleError::MissingLocale => write!(f, "missing locale directory"),
118 TranslationBundleError::ReadLocalesDir(e) => {
119 write!(f, "could not read locales dir: {}", e)
121 TranslationBundleError::ReadLocalesDirEntry(e) => {
122 write!(f, "could not read locales dir entry: {}", e)
124 TranslationBundleError::LocaleIsNotDir => {
125 write!(f, "`$sysroot/share/locales/$locale` is not a directory")
131 impl Error for TranslationBundleError {
132 fn source(&self) -> Option<&(dyn Error + 'static)> {
134 TranslationBundleError::ReadFtl(e) => Some(e),
135 TranslationBundleError::ParseFtl(e) => Some(e),
136 TranslationBundleError::AddResource(e) => Some(e),
137 TranslationBundleError::MissingLocale => None,
138 TranslationBundleError::ReadLocalesDir(e) => Some(e),
139 TranslationBundleError::ReadLocalesDirEntry(e) => Some(e),
140 TranslationBundleError::LocaleIsNotDir => None,
145 impl From<(FluentResource, Vec<ParserError>)> for TranslationBundleError {
146 fn from((_, mut errs): (FluentResource, Vec<ParserError>)) -> Self {
147 TranslationBundleError::ParseFtl(errs.pop().expect("failed ftl parse with no errors"))
151 impl From<Vec<FluentError>> for TranslationBundleError {
152 fn from(mut errs: Vec<FluentError>) -> Self {
153 TranslationBundleError::AddResource(
154 errs.pop().expect("failed adding resource to bundle with no errors"),
159 /// Returns Fluent bundle with the user's locale resources from
160 /// `$sysroot/share/locale/$requested_locale/*.ftl`.
162 /// If `-Z additional-ftl-path` was provided, load that resource and add it to the bundle
163 /// (overriding any conflicting messages).
164 #[instrument(level = "trace")]
165 pub fn fluent_bundle(
166 mut user_provided_sysroot: Option<PathBuf>,
167 mut sysroot_candidates: Vec<PathBuf>,
168 requested_locale: Option<LanguageIdentifier>,
169 additional_ftl_path: Option<&Path>,
170 with_directionality_markers: bool,
171 ) -> Result<Option<Lrc<FluentBundle>>, TranslationBundleError> {
172 if requested_locale.is_none() && additional_ftl_path.is_none() {
176 let fallback_locale = langid!("en-US");
177 let requested_fallback_locale = requested_locale.as_ref() == Some(&fallback_locale);
179 // If there is only `-Z additional-ftl-path`, assume locale is "en-US", otherwise use user
181 let locale = requested_locale.clone().unwrap_or(fallback_locale);
183 let mut bundle = new_bundle(vec![locale]);
185 // Fluent diagnostics can insert directionality isolation markers around interpolated variables
186 // indicating that there may be a shift from right-to-left to left-to-right text (or
187 // vice-versa). These are disabled because they are sometimes visible in the error output, but
188 // may be worth investigating in future (for example: if type names are left-to-right and the
189 // surrounding diagnostic messages are right-to-left, then these might be helpful).
190 bundle.set_use_isolating(with_directionality_markers);
192 // If the user requests the default locale then don't try to load anything.
193 if !requested_fallback_locale && let Some(requested_locale) = requested_locale {
194 let mut found_resources = false;
195 for sysroot in user_provided_sysroot.iter_mut().chain(sysroot_candidates.iter_mut()) {
196 sysroot.push("share");
197 sysroot.push("locale");
198 sysroot.push(requested_locale.to_string());
201 if !sysroot.exists() {
206 if !sysroot.is_dir() {
207 return Err(TranslationBundleError::LocaleIsNotDir);
210 for entry in sysroot.read_dir().map_err(TranslationBundleError::ReadLocalesDir)? {
211 let entry = entry.map_err(TranslationBundleError::ReadLocalesDirEntry)?;
212 let path = entry.path();
214 if path.extension().and_then(|s| s.to_str()) != Some("ftl") {
220 fs::read_to_string(path).map_err(TranslationBundleError::ReadFtl)?;
222 FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
224 bundle.add_resource(resource).map_err(TranslationBundleError::from)?;
225 found_resources = true;
229 if !found_resources {
230 return Err(TranslationBundleError::MissingLocale);
234 if let Some(additional_ftl_path) = additional_ftl_path {
236 fs::read_to_string(additional_ftl_path).map_err(TranslationBundleError::ReadFtl)?;
238 FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
240 bundle.add_resource_overriding(resource);
243 let bundle = Lrc::new(bundle);
247 /// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily
248 /// evaluated fluent bundle.
249 pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBundle>>;
251 /// Return the default `FluentBundle` with standard "en-US" diagnostic messages.
252 #[instrument(level = "trace")]
253 pub fn fallback_fluent_bundle(
254 resources: &'static [&'static str],
255 with_directionality_markers: bool,
256 ) -> LazyFallbackBundle {
257 Lrc::new(Lazy::new(move || {
258 let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);
259 // See comment in `fluent_bundle`.
260 fallback_bundle.set_use_isolating(with_directionality_markers);
262 for resource in resources {
263 let resource = FluentResource::try_new(resource.to_string())
264 .expect("failed to parse fallback fluent resource");
266 fallback_bundle.add_resource_overriding(resource);
273 /// Identifier for the Fluent message/attribute corresponding to a diagnostic message.
274 type FluentId = Cow<'static, str>;
276 /// Abstraction over a message in a subdiagnostic (i.e. label, note, help, etc) to support both
277 /// translatable and non-translatable diagnostic messages.
279 /// Translatable messages for subdiagnostics are typically attributes attached to a larger Fluent
280 /// message so messages of this type must be combined with a `DiagnosticMessage` (using
281 /// `DiagnosticMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
282 /// the `Subdiagnostic` derive refer to Fluent identifiers directly.
283 #[rustc_diagnostic_item = "SubdiagnosticMessage"]
284 pub enum SubdiagnosticMessage {
285 /// Non-translatable diagnostic message.
286 // FIXME(davidtwco): can a `Cow<'static, str>` be used here?
288 /// Translatable message which has already been translated eagerly.
290 /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
291 /// be instantiated multiple times with different values. As translation normally happens
292 /// immediately prior to emission, after the diagnostic and subdiagnostic derive logic has run,
293 /// the setting of diagnostic arguments in the derived code will overwrite previous variable
294 /// values and only the final value will be set when translation occurs - resulting in
295 /// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
296 /// happening immediately after the subdiagnostic derive's logic has been run. This variant
297 /// stores messages which have been translated eagerly.
298 // FIXME(#100717): can a `Cow<'static, str>` be used here?
300 /// Identifier of a Fluent message. Instances of this variant are generated by the
301 /// `Subdiagnostic` derive.
302 FluentIdentifier(FluentId),
303 /// Attribute of a Fluent message. Needs to be combined with a Fluent identifier to produce an
304 /// actual translated message. Instances of this variant are generated by the `fluent_messages`
307 /// <https://projectfluent.org/fluent/guide/attributes.html>
308 FluentAttr(FluentId),
311 /// `From` impl that enables existing diagnostic calls to functions which now take
312 /// `impl Into<SubdiagnosticMessage>` to continue to work as before.
313 impl<S: Into<String>> From<S> for SubdiagnosticMessage {
314 fn from(s: S) -> Self {
315 SubdiagnosticMessage::Str(s.into())
319 /// Abstraction over a message in a diagnostic to support both translatable and non-translatable
320 /// diagnostic messages.
322 /// Intended to be removed once diagnostics are entirely translatable.
323 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
324 #[rustc_diagnostic_item = "DiagnosticMessage"]
325 pub enum DiagnosticMessage {
326 /// Non-translatable diagnostic message.
327 // FIXME(#100717): can a `Cow<'static, str>` be used here?
329 /// Translatable message which has already been translated eagerly.
331 /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
332 /// be instantiated multiple times with different values. As translation normally happens
333 /// immediately prior to emission, after the diagnostic and subdiagnostic derive logic has run,
334 /// the setting of diagnostic arguments in the derived code will overwrite previous variable
335 /// values and only the final value will be set when translation occurs - resulting in
336 /// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
337 /// happening immediately after the subdiagnostic derive's logic has been run. This variant
338 /// stores messages which have been translated eagerly.
339 // FIXME(#100717): can a `Cow<'static, str>` be used here?
341 /// Identifier for a Fluent message (with optional attribute) corresponding to the diagnostic
344 /// <https://projectfluent.org/fluent/guide/hello.html>
345 /// <https://projectfluent.org/fluent/guide/attributes.html>
346 FluentIdentifier(FluentId, Option<FluentId>),
349 impl DiagnosticMessage {
350 /// Given a `SubdiagnosticMessage` which may contain a Fluent attribute, create a new
351 /// `DiagnosticMessage` that combines that attribute with the Fluent identifier of `self`.
353 /// - If the `SubdiagnosticMessage` is non-translatable then return the message as a
354 /// `DiagnosticMessage`.
355 /// - If `self` is non-translatable then return `self`'s message.
356 pub fn with_subdiagnostic_message(&self, sub: SubdiagnosticMessage) -> Self {
357 let attr = match sub {
358 SubdiagnosticMessage::Str(s) => return DiagnosticMessage::Str(s),
359 SubdiagnosticMessage::Eager(s) => return DiagnosticMessage::Eager(s),
360 SubdiagnosticMessage::FluentIdentifier(id) => {
361 return DiagnosticMessage::FluentIdentifier(id, None);
363 SubdiagnosticMessage::FluentAttr(attr) => attr,
367 DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()),
368 DiagnosticMessage::Eager(s) => DiagnosticMessage::Eager(s.clone()),
369 DiagnosticMessage::FluentIdentifier(id, _) => {
370 DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr))
376 /// `From` impl that enables existing diagnostic calls to functions which now take
377 /// `impl Into<DiagnosticMessage>` to continue to work as before.
378 impl<S: Into<String>> From<S> for DiagnosticMessage {
379 fn from(s: S) -> Self {
380 DiagnosticMessage::Str(s.into())
384 /// A workaround for "good path" ICEs when formatting types in disabled lints.
386 /// Delays formatting until `.into(): DiagnosticMessage` is used.
387 pub struct DelayDm<F>(pub F);
389 impl<F: FnOnce() -> String> From<DelayDm<F>> for DiagnosticMessage {
390 fn from(DelayDm(f): DelayDm<F>) -> Self {
391 DiagnosticMessage::from(f())
395 /// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
396 /// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagnosticMessage` and the
397 /// subdiagnostic derive refers to typed identifiers that are `DiagnosticMessage`s, so need to be
398 /// able to convert between these, as much as they'll be converted back into `DiagnosticMessage`
399 /// using `with_subdiagnostic_message` eventually. Don't use this other than for the derive.
400 impl Into<SubdiagnosticMessage> for DiagnosticMessage {
401 fn into(self) -> SubdiagnosticMessage {
403 DiagnosticMessage::Str(s) => SubdiagnosticMessage::Str(s),
404 DiagnosticMessage::Eager(s) => SubdiagnosticMessage::Eager(s),
405 DiagnosticMessage::FluentIdentifier(id, None) => {
406 SubdiagnosticMessage::FluentIdentifier(id)
408 // There isn't really a sensible behaviour for this because it loses information but
409 // this is the most sensible of the behaviours.
410 DiagnosticMessage::FluentIdentifier(_, Some(attr)) => {
411 SubdiagnosticMessage::FluentAttr(attr)
417 /// A span together with some additional data.
418 #[derive(Clone, Debug)]
419 pub struct SpanLabel {
420 /// The span we are going to include in the final snippet.
423 /// Is this a primary span? This is the "locus" of the message,
424 /// and is indicated with a `^^^^` underline, versus `----`.
425 pub is_primary: bool,
427 /// What label should we attach to this span (if any)?
428 pub label: Option<DiagnosticMessage>,
431 /// A collection of `Span`s.
433 /// Spans have two orthogonal attributes:
435 /// - They can be *primary spans*. In this case they are the locus of
436 /// the error, and would be rendered with `^^^`.
437 /// - They can have a *label*. In this case, the label is written next
438 /// to the mark in the snippet when we render.
439 #[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)]
440 pub struct MultiSpan {
441 primary_spans: Vec<Span>,
442 span_labels: Vec<(Span, DiagnosticMessage)>,
447 pub fn new() -> MultiSpan {
448 MultiSpan { primary_spans: vec![], span_labels: vec![] }
451 pub fn from_span(primary_span: Span) -> MultiSpan {
452 MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }
455 pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan {
457 MultiSpan { primary_spans: vec, span_labels: vec![] }
460 pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagnosticMessage>) {
461 self.span_labels.push((span, label.into()));
464 /// Selects the first primary span (if any).
465 pub fn primary_span(&self) -> Option<Span> {
466 self.primary_spans.first().cloned()
469 /// Returns all primary spans.
470 pub fn primary_spans(&self) -> &[Span] {
474 /// Returns `true` if any of the primary spans are displayable.
475 pub fn has_primary_spans(&self) -> bool {
479 /// Returns `true` if this contains only a dummy primary span with any hygienic context.
480 pub fn is_dummy(&self) -> bool {
481 self.primary_spans.iter().all(|sp| sp.is_dummy())
484 /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't
485 /// display well (like std macros). Returns whether replacements occurred.
486 pub fn replace(&mut self, before: Span, after: Span) -> bool {
487 let mut replacements_occurred = false;
488 for primary_span in &mut self.primary_spans {
489 if *primary_span == before {
490 *primary_span = after;
491 replacements_occurred = true;
494 for span_label in &mut self.span_labels {
495 if span_label.0 == before {
496 span_label.0 = after;
497 replacements_occurred = true;
500 replacements_occurred
503 /// Returns the strings to highlight. We always ensure that there
504 /// is an entry for each of the primary spans -- for each primary
505 /// span `P`, if there is at least one label with span `P`, we return
506 /// those labels (marked as primary). But otherwise we return
507 /// `SpanLabel` instances with empty labels.
508 pub fn span_labels(&self) -> Vec<SpanLabel> {
509 let is_primary = |span| self.primary_spans.contains(&span);
511 let mut span_labels = self
514 .map(|&(span, ref label)| SpanLabel {
516 is_primary: is_primary(span),
517 label: Some(label.clone()),
519 .collect::<Vec<_>>();
521 for &span in &self.primary_spans {
522 if !span_labels.iter().any(|sl| sl.span == span) {
523 span_labels.push(SpanLabel { span, is_primary: true, label: None });
530 /// Returns `true` if any of the span labels is displayable.
531 pub fn has_span_labels(&self) -> bool {
532 self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
536 impl From<Span> for MultiSpan {
537 fn from(span: Span) -> MultiSpan {
538 MultiSpan::from_span(span)
542 impl From<Vec<Span>> for MultiSpan {
543 fn from(spans: Vec<Span>) -> MultiSpan {
544 MultiSpan::from_spans(spans)
548 fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option<icu_locid::Locale> {
549 icu_locid::Locale::try_from_bytes(lang.to_string().as_bytes()).ok()
552 pub fn fluent_value_from_str_list_sep_by_and(l: Vec<Cow<'_, str>>) -> FluentValue<'_> {
553 // Fluent requires 'static value here for its AnyEq usages.
554 #[derive(Clone, PartialEq, Debug)]
555 struct FluentStrListSepByAnd(Vec<String>);
557 impl FluentType for FluentStrListSepByAnd {
558 fn duplicate(&self) -> Box<dyn FluentType + Send> {
559 Box::new(self.clone())
562 fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {
564 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
565 list_formatter.format_to_string(self.0.iter())
571 #[cfg(not(parallel_compiler))]
572 fn as_string_threadsafe(
574 _intls: &intl_memoizer::concurrent::IntlLangMemoizer,
575 ) -> Cow<'static, str> {
576 unreachable!("`as_string_threadsafe` is not used in non-parallel rustc")
579 #[cfg(parallel_compiler)]
580 fn as_string_threadsafe(
582 intls: &intl_memoizer::concurrent::IntlLangMemoizer,
583 ) -> Cow<'static, str> {
585 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
586 list_formatter.format_to_string(self.0.iter())
593 struct MemoizableListFormatter(icu_list::ListFormatter);
595 impl std::ops::Deref for MemoizableListFormatter {
596 type Target = icu_list::ListFormatter;
597 fn deref(&self) -> &Self::Target {
602 impl intl_memoizer::Memoizable for MemoizableListFormatter {
606 fn construct(lang: LanguageIdentifier, _args: Self::Args) -> Result<Self, Self::Error>
610 let baked_data_provider = rustc_baked_icu_data::baked_data_provider();
611 let locale_fallbacker =
612 LocaleFallbacker::try_new_with_any_provider(&baked_data_provider)
613 .expect("Failed to create fallback provider");
615 LocaleFallbackProvider::new_with_fallbacker(baked_data_provider, locale_fallbacker);
616 let locale = icu_locale_from_unic_langid(lang)
617 .unwrap_or_else(|| rustc_baked_icu_data::supported_locales::EN);
619 icu_list::ListFormatter::try_new_and_with_length_with_any_provider(
622 icu_list::ListLength::Wide,
624 .expect("Failed to create list formatter");
626 Ok(MemoizableListFormatter(list_formatter))
630 let l = l.into_iter().map(|x| x.into_owned()).collect();
632 FluentValue::Custom(Box::new(FluentStrListSepByAnd(l)))