]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_messages/src/lib.rs
Rollup merge of #105784 - yanns:update_stdarch, r=Amanieu
[rust.git] / compiler / rustc_error_messages / src / lib.rs
1 #![feature(let_chains)]
2 #![feature(once_cell)]
3 #![feature(rustc_attrs)]
4 #![feature(type_alias_impl_trait)]
5 #![deny(rustc::untranslatable_diagnostic)]
6 #![deny(rustc::diagnostic_outside_of_impl)]
7
8 #[macro_use]
9 extern crate tracing;
10
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};
16 use rustc_span::Span;
17 use std::borrow::Cow;
18 use std::error::Error;
19 use std::fmt;
20 use std::fs;
21 use std::io;
22 use std::path::{Path, PathBuf};
23
24 #[cfg(not(parallel_compiler))]
25 use std::cell::LazyCell as Lazy;
26 #[cfg(parallel_compiler)]
27 use std::sync::LazyLock as Lazy;
28
29 #[cfg(parallel_compiler)]
30 use intl_memoizer::concurrent::IntlLangMemoizer;
31 #[cfg(not(parallel_compiler))]
32 use intl_memoizer::IntlLangMemoizer;
33
34 pub use fluent_bundle::{self, types::FluentType, FluentArgs, FluentError, FluentValue};
35 pub use unic_langid::{langid, LanguageIdentifier};
36
37 // Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
38 fluent_messages! {
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
75 }
76
77 pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};
78
79 pub type FluentBundle = fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>;
80
81 #[cfg(parallel_compiler)]
82 fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
83     FluentBundle::new_concurrent(locales)
84 }
85
86 #[cfg(not(parallel_compiler))]
87 fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
88     FluentBundle::new(locales)
89 }
90
91 #[derive(Debug)]
92 pub enum TranslationBundleError {
93     /// Failed to read from `.ftl` file.
94     ReadFtl(io::Error),
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.
100     MissingLocale,
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.
106     LocaleIsNotDir,
107 }
108
109 impl fmt::Display for TranslationBundleError {
110     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111         match self {
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)
115             }
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)
120             }
121             TranslationBundleError::ReadLocalesDirEntry(e) => {
122                 write!(f, "could not read locales dir entry: {}", e)
123             }
124             TranslationBundleError::LocaleIsNotDir => {
125                 write!(f, "`$sysroot/share/locales/$locale` is not a directory")
126             }
127         }
128     }
129 }
130
131 impl Error for TranslationBundleError {
132     fn source(&self) -> Option<&(dyn Error + 'static)> {
133         match self {
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,
141         }
142     }
143 }
144
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"))
148     }
149 }
150
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"),
155         )
156     }
157 }
158
159 /// Returns Fluent bundle with the user's locale resources from
160 /// `$sysroot/share/locale/$requested_locale/*.ftl`.
161 ///
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() {
173         return Ok(None);
174     }
175
176     let fallback_locale = langid!("en-US");
177     let requested_fallback_locale = requested_locale.as_ref() == Some(&fallback_locale);
178
179     // If there is only `-Z additional-ftl-path`, assume locale is "en-US", otherwise use user
180     // provided locale.
181     let locale = requested_locale.clone().unwrap_or(fallback_locale);
182     trace!(?locale);
183     let mut bundle = new_bundle(vec![locale]);
184
185     // Add convenience functions available to ftl authors.
186     register_functions(&mut bundle);
187
188     // Fluent diagnostics can insert directionality isolation markers around interpolated variables
189     // indicating that there may be a shift from right-to-left to left-to-right text (or
190     // vice-versa). These are disabled because they are sometimes visible in the error output, but
191     // may be worth investigating in future (for example: if type names are left-to-right and the
192     // surrounding diagnostic messages are right-to-left, then these might be helpful).
193     bundle.set_use_isolating(with_directionality_markers);
194
195     // If the user requests the default locale then don't try to load anything.
196     if !requested_fallback_locale && let Some(requested_locale) = requested_locale {
197         let mut found_resources = false;
198         for sysroot in user_provided_sysroot.iter_mut().chain(sysroot_candidates.iter_mut()) {
199             sysroot.push("share");
200             sysroot.push("locale");
201             sysroot.push(requested_locale.to_string());
202             trace!(?sysroot);
203
204             if !sysroot.exists() {
205                 trace!("skipping");
206                 continue;
207             }
208
209             if !sysroot.is_dir() {
210                 return Err(TranslationBundleError::LocaleIsNotDir);
211             }
212
213             for entry in sysroot.read_dir().map_err(TranslationBundleError::ReadLocalesDir)? {
214                 let entry = entry.map_err(TranslationBundleError::ReadLocalesDirEntry)?;
215                 let path = entry.path();
216                 trace!(?path);
217                 if path.extension().and_then(|s| s.to_str()) != Some("ftl") {
218                     trace!("skipping");
219                     continue;
220                 }
221
222                 let resource_str =
223                     fs::read_to_string(path).map_err(TranslationBundleError::ReadFtl)?;
224                 let resource =
225                     FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
226                 trace!(?resource);
227                 bundle.add_resource(resource).map_err(TranslationBundleError::from)?;
228                 found_resources = true;
229             }
230         }
231
232         if !found_resources {
233             return Err(TranslationBundleError::MissingLocale);
234         }
235     }
236
237     if let Some(additional_ftl_path) = additional_ftl_path {
238         let resource_str =
239             fs::read_to_string(additional_ftl_path).map_err(TranslationBundleError::ReadFtl)?;
240         let resource =
241             FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
242         trace!(?resource);
243         bundle.add_resource_overriding(resource);
244     }
245
246     let bundle = Lrc::new(bundle);
247     Ok(Some(bundle))
248 }
249
250 fn register_functions(bundle: &mut FluentBundle) {
251     bundle
252         .add_function("STREQ", |positional, _named| match positional {
253             [FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),
254             _ => FluentValue::Error,
255         })
256         .expect("Failed to add a function to the bundle.");
257 }
258
259 /// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily
260 /// evaluated fluent bundle.
261 pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBundle>>;
262
263 /// Return the default `FluentBundle` with standard "en-US" diagnostic messages.
264 #[instrument(level = "trace")]
265 pub fn fallback_fluent_bundle(
266     resources: &'static [&'static str],
267     with_directionality_markers: bool,
268 ) -> LazyFallbackBundle {
269     Lrc::new(Lazy::new(move || {
270         let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);
271
272         register_functions(&mut fallback_bundle);
273
274         // See comment in `fluent_bundle`.
275         fallback_bundle.set_use_isolating(with_directionality_markers);
276
277         for resource in resources {
278             let resource = FluentResource::try_new(resource.to_string())
279                 .expect("failed to parse fallback fluent resource");
280             trace!(?resource);
281             fallback_bundle.add_resource_overriding(resource);
282         }
283
284         fallback_bundle
285     }))
286 }
287
288 /// Identifier for the Fluent message/attribute corresponding to a diagnostic message.
289 type FluentId = Cow<'static, str>;
290
291 /// Abstraction over a message in a subdiagnostic (i.e. label, note, help, etc) to support both
292 /// translatable and non-translatable diagnostic messages.
293 ///
294 /// Translatable messages for subdiagnostics are typically attributes attached to a larger Fluent
295 /// message so messages of this type must be combined with a `DiagnosticMessage` (using
296 /// `DiagnosticMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
297 /// the `Subdiagnostic` derive refer to Fluent identifiers directly.
298 #[rustc_diagnostic_item = "SubdiagnosticMessage"]
299 pub enum SubdiagnosticMessage {
300     /// Non-translatable diagnostic message.
301     // FIXME(davidtwco): can a `Cow<'static, str>` be used here?
302     Str(String),
303     /// Translatable message which has already been translated eagerly.
304     ///
305     /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
306     /// be instantiated multiple times with different values. As translation normally happens
307     /// immediately prior to emission, after the diagnostic and subdiagnostic derive logic has run,
308     /// the setting of diagnostic arguments in the derived code will overwrite previous variable
309     /// values and only the final value will be set when translation occurs - resulting in
310     /// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
311     /// happening immediately after the subdiagnostic derive's logic has been run. This variant
312     /// stores messages which have been translated eagerly.
313     // FIXME(#100717): can a `Cow<'static, str>` be used here?
314     Eager(String),
315     /// Identifier of a Fluent message. Instances of this variant are generated by the
316     /// `Subdiagnostic` derive.
317     FluentIdentifier(FluentId),
318     /// Attribute of a Fluent message. Needs to be combined with a Fluent identifier to produce an
319     /// actual translated message. Instances of this variant are generated by the `fluent_messages`
320     /// macro.
321     ///
322     /// <https://projectfluent.org/fluent/guide/attributes.html>
323     FluentAttr(FluentId),
324 }
325
326 /// `From` impl that enables existing diagnostic calls to functions which now take
327 /// `impl Into<SubdiagnosticMessage>` to continue to work as before.
328 impl<S: Into<String>> From<S> for SubdiagnosticMessage {
329     fn from(s: S) -> Self {
330         SubdiagnosticMessage::Str(s.into())
331     }
332 }
333
334 /// Abstraction over a message in a diagnostic to support both translatable and non-translatable
335 /// diagnostic messages.
336 ///
337 /// Intended to be removed once diagnostics are entirely translatable.
338 #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
339 #[rustc_diagnostic_item = "DiagnosticMessage"]
340 pub enum DiagnosticMessage {
341     /// Non-translatable diagnostic message.
342     // FIXME(#100717): can a `Cow<'static, str>` be used here?
343     Str(String),
344     /// Translatable message which has already been translated eagerly.
345     ///
346     /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
347     /// be instantiated multiple times with different values. As translation normally happens
348     /// immediately prior to emission, after the diagnostic and subdiagnostic derive logic has run,
349     /// the setting of diagnostic arguments in the derived code will overwrite previous variable
350     /// values and only the final value will be set when translation occurs - resulting in
351     /// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
352     /// happening immediately after the subdiagnostic derive's logic has been run. This variant
353     /// stores messages which have been translated eagerly.
354     // FIXME(#100717): can a `Cow<'static, str>` be used here?
355     Eager(String),
356     /// Identifier for a Fluent message (with optional attribute) corresponding to the diagnostic
357     /// message.
358     ///
359     /// <https://projectfluent.org/fluent/guide/hello.html>
360     /// <https://projectfluent.org/fluent/guide/attributes.html>
361     FluentIdentifier(FluentId, Option<FluentId>),
362 }
363
364 impl DiagnosticMessage {
365     /// Given a `SubdiagnosticMessage` which may contain a Fluent attribute, create a new
366     /// `DiagnosticMessage` that combines that attribute with the Fluent identifier of `self`.
367     ///
368     /// - If the `SubdiagnosticMessage` is non-translatable then return the message as a
369     /// `DiagnosticMessage`.
370     /// - If `self` is non-translatable then return `self`'s message.
371     pub fn with_subdiagnostic_message(&self, sub: SubdiagnosticMessage) -> Self {
372         let attr = match sub {
373             SubdiagnosticMessage::Str(s) => return DiagnosticMessage::Str(s),
374             SubdiagnosticMessage::Eager(s) => return DiagnosticMessage::Eager(s),
375             SubdiagnosticMessage::FluentIdentifier(id) => {
376                 return DiagnosticMessage::FluentIdentifier(id, None);
377             }
378             SubdiagnosticMessage::FluentAttr(attr) => attr,
379         };
380
381         match self {
382             DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()),
383             DiagnosticMessage::Eager(s) => DiagnosticMessage::Eager(s.clone()),
384             DiagnosticMessage::FluentIdentifier(id, _) => {
385                 DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr))
386             }
387         }
388     }
389 }
390
391 /// `From` impl that enables existing diagnostic calls to functions which now take
392 /// `impl Into<DiagnosticMessage>` to continue to work as before.
393 impl<S: Into<String>> From<S> for DiagnosticMessage {
394     fn from(s: S) -> Self {
395         DiagnosticMessage::Str(s.into())
396     }
397 }
398
399 /// A workaround for "good path" ICEs when formatting types in disabled lints.
400 ///
401 /// Delays formatting until `.into(): DiagnosticMessage` is used.
402 pub struct DelayDm<F>(pub F);
403
404 impl<F: FnOnce() -> String> From<DelayDm<F>> for DiagnosticMessage {
405     fn from(DelayDm(f): DelayDm<F>) -> Self {
406         DiagnosticMessage::from(f())
407     }
408 }
409
410 /// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
411 /// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagnosticMessage` and the
412 /// subdiagnostic derive refers to typed identifiers that are `DiagnosticMessage`s, so need to be
413 /// able to convert between these, as much as they'll be converted back into `DiagnosticMessage`
414 /// using `with_subdiagnostic_message` eventually. Don't use this other than for the derive.
415 impl Into<SubdiagnosticMessage> for DiagnosticMessage {
416     fn into(self) -> SubdiagnosticMessage {
417         match self {
418             DiagnosticMessage::Str(s) => SubdiagnosticMessage::Str(s),
419             DiagnosticMessage::Eager(s) => SubdiagnosticMessage::Eager(s),
420             DiagnosticMessage::FluentIdentifier(id, None) => {
421                 SubdiagnosticMessage::FluentIdentifier(id)
422             }
423             // There isn't really a sensible behaviour for this because it loses information but
424             // this is the most sensible of the behaviours.
425             DiagnosticMessage::FluentIdentifier(_, Some(attr)) => {
426                 SubdiagnosticMessage::FluentAttr(attr)
427             }
428         }
429     }
430 }
431
432 /// A span together with some additional data.
433 #[derive(Clone, Debug)]
434 pub struct SpanLabel {
435     /// The span we are going to include in the final snippet.
436     pub span: Span,
437
438     /// Is this a primary span? This is the "locus" of the message,
439     /// and is indicated with a `^^^^` underline, versus `----`.
440     pub is_primary: bool,
441
442     /// What label should we attach to this span (if any)?
443     pub label: Option<DiagnosticMessage>,
444 }
445
446 /// A collection of `Span`s.
447 ///
448 /// Spans have two orthogonal attributes:
449 ///
450 /// - They can be *primary spans*. In this case they are the locus of
451 ///   the error, and would be rendered with `^^^`.
452 /// - They can have a *label*. In this case, the label is written next
453 ///   to the mark in the snippet when we render.
454 #[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)]
455 pub struct MultiSpan {
456     primary_spans: Vec<Span>,
457     span_labels: Vec<(Span, DiagnosticMessage)>,
458 }
459
460 impl MultiSpan {
461     #[inline]
462     pub fn new() -> MultiSpan {
463         MultiSpan { primary_spans: vec![], span_labels: vec![] }
464     }
465
466     pub fn from_span(primary_span: Span) -> MultiSpan {
467         MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }
468     }
469
470     pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan {
471         vec.sort();
472         MultiSpan { primary_spans: vec, span_labels: vec![] }
473     }
474
475     pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagnosticMessage>) {
476         self.span_labels.push((span, label.into()));
477     }
478
479     /// Selects the first primary span (if any).
480     pub fn primary_span(&self) -> Option<Span> {
481         self.primary_spans.first().cloned()
482     }
483
484     /// Returns all primary spans.
485     pub fn primary_spans(&self) -> &[Span] {
486         &self.primary_spans
487     }
488
489     /// Returns `true` if any of the primary spans are displayable.
490     pub fn has_primary_spans(&self) -> bool {
491         !self.is_dummy()
492     }
493
494     /// Returns `true` if this contains only a dummy primary span with any hygienic context.
495     pub fn is_dummy(&self) -> bool {
496         self.primary_spans.iter().all(|sp| sp.is_dummy())
497     }
498
499     /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't
500     /// display well (like std macros). Returns whether replacements occurred.
501     pub fn replace(&mut self, before: Span, after: Span) -> bool {
502         let mut replacements_occurred = false;
503         for primary_span in &mut self.primary_spans {
504             if *primary_span == before {
505                 *primary_span = after;
506                 replacements_occurred = true;
507             }
508         }
509         for span_label in &mut self.span_labels {
510             if span_label.0 == before {
511                 span_label.0 = after;
512                 replacements_occurred = true;
513             }
514         }
515         replacements_occurred
516     }
517
518     /// Returns the strings to highlight. We always ensure that there
519     /// is an entry for each of the primary spans -- for each primary
520     /// span `P`, if there is at least one label with span `P`, we return
521     /// those labels (marked as primary). But otherwise we return
522     /// `SpanLabel` instances with empty labels.
523     pub fn span_labels(&self) -> Vec<SpanLabel> {
524         let is_primary = |span| self.primary_spans.contains(&span);
525
526         let mut span_labels = self
527             .span_labels
528             .iter()
529             .map(|&(span, ref label)| SpanLabel {
530                 span,
531                 is_primary: is_primary(span),
532                 label: Some(label.clone()),
533             })
534             .collect::<Vec<_>>();
535
536         for &span in &self.primary_spans {
537             if !span_labels.iter().any(|sl| sl.span == span) {
538                 span_labels.push(SpanLabel { span, is_primary: true, label: None });
539             }
540         }
541
542         span_labels
543     }
544
545     /// Returns `true` if any of the span labels is displayable.
546     pub fn has_span_labels(&self) -> bool {
547         self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
548     }
549 }
550
551 impl From<Span> for MultiSpan {
552     fn from(span: Span) -> MultiSpan {
553         MultiSpan::from_span(span)
554     }
555 }
556
557 impl From<Vec<Span>> for MultiSpan {
558     fn from(spans: Vec<Span>) -> MultiSpan {
559         MultiSpan::from_spans(spans)
560     }
561 }
562
563 fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option<icu_locid::Locale> {
564     icu_locid::Locale::try_from_bytes(lang.to_string().as_bytes()).ok()
565 }
566
567 pub fn fluent_value_from_str_list_sep_by_and(l: Vec<Cow<'_, str>>) -> FluentValue<'_> {
568     // Fluent requires 'static value here for its AnyEq usages.
569     #[derive(Clone, PartialEq, Debug)]
570     struct FluentStrListSepByAnd(Vec<String>);
571
572     impl FluentType for FluentStrListSepByAnd {
573         fn duplicate(&self) -> Box<dyn FluentType + Send> {
574             Box::new(self.clone())
575         }
576
577         fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {
578             let result = intls
579                 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
580                     list_formatter.format_to_string(self.0.iter())
581                 })
582                 .unwrap();
583             Cow::Owned(result)
584         }
585
586         #[cfg(not(parallel_compiler))]
587         fn as_string_threadsafe(
588             &self,
589             _intls: &intl_memoizer::concurrent::IntlLangMemoizer,
590         ) -> Cow<'static, str> {
591             unreachable!("`as_string_threadsafe` is not used in non-parallel rustc")
592         }
593
594         #[cfg(parallel_compiler)]
595         fn as_string_threadsafe(
596             &self,
597             intls: &intl_memoizer::concurrent::IntlLangMemoizer,
598         ) -> Cow<'static, str> {
599             let result = intls
600                 .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
601                     list_formatter.format_to_string(self.0.iter())
602                 })
603                 .unwrap();
604             Cow::Owned(result)
605         }
606     }
607
608     struct MemoizableListFormatter(icu_list::ListFormatter);
609
610     impl std::ops::Deref for MemoizableListFormatter {
611         type Target = icu_list::ListFormatter;
612         fn deref(&self) -> &Self::Target {
613             &self.0
614         }
615     }
616
617     impl intl_memoizer::Memoizable for MemoizableListFormatter {
618         type Args = ();
619         type Error = ();
620
621         fn construct(lang: LanguageIdentifier, _args: Self::Args) -> Result<Self, Self::Error>
622         where
623             Self: Sized,
624         {
625             let baked_data_provider = rustc_baked_icu_data::baked_data_provider();
626             let locale_fallbacker =
627                 LocaleFallbacker::try_new_with_any_provider(&baked_data_provider)
628                     .expect("Failed to create fallback provider");
629             let data_provider =
630                 LocaleFallbackProvider::new_with_fallbacker(baked_data_provider, locale_fallbacker);
631             let locale = icu_locale_from_unic_langid(lang)
632                 .unwrap_or_else(|| rustc_baked_icu_data::supported_locales::EN);
633             let list_formatter =
634                 icu_list::ListFormatter::try_new_and_with_length_with_any_provider(
635                     &data_provider,
636                     &locale.into(),
637                     icu_list::ListLength::Wide,
638                 )
639                 .expect("Failed to create list formatter");
640
641             Ok(MemoizableListFormatter(list_formatter))
642         }
643     }
644
645     let l = l.into_iter().map(|x| x.into_owned()).collect();
646
647     FluentValue::Custom(Box::new(FluentStrListSepByAnd(l)))
648 }