rustc_error_messages/
lib.rs

1// tidy-alphabetical-start
2#![allow(internal_features)]
3#![doc(rust_logo)]
4#![feature(rustc_attrs)]
5#![feature(rustdoc_internals)]
6// tidy-alphabetical-end
7
8use std::borrow::Cow;
9use std::error::Error;
10use std::path::Path;
11use std::sync::{Arc, LazyLock};
12use std::{fmt, fs, io};
13
14use fluent_bundle::FluentResource;
15pub use fluent_bundle::types::FluentType;
16pub use fluent_bundle::{self, FluentArgs, FluentError, FluentValue};
17use fluent_syntax::parser::ParserError;
18use intl_memoizer::concurrent::IntlLangMemoizer;
19use rustc_data_structures::sync::{DynSend, IntoDynSyncSend};
20use rustc_macros::{Decodable, Encodable};
21use rustc_span::Span;
22use tracing::{instrument, trace};
23pub use unic_langid::{LanguageIdentifier, langid};
24
25mod diagnostic_impls;
26pub use diagnostic_impls::DiagArgFromDisplay;
27
28pub type FluentBundle =
29    IntoDynSyncSend<fluent_bundle::bundle::FluentBundle<FluentResource, IntlLangMemoizer>>;
30
31fn new_bundle(locales: Vec<LanguageIdentifier>) -> FluentBundle {
32    IntoDynSyncSend(fluent_bundle::bundle::FluentBundle::new_concurrent(locales))
33}
34
35#[derive(Debug)]
36pub enum TranslationBundleError {
37    /// Failed to read from `.ftl` file.
38    ReadFtl(io::Error),
39    /// Failed to parse contents of `.ftl` file.
40    ParseFtl(ParserError),
41    /// Failed to add `FluentResource` to `FluentBundle`.
42    AddResource(FluentError),
43    /// `$sysroot/share/locale/$locale` does not exist.
44    MissingLocale,
45    /// Cannot read directory entries of `$sysroot/share/locale/$locale`.
46    ReadLocalesDir(io::Error),
47    /// Cannot read directory entry of `$sysroot/share/locale/$locale`.
48    ReadLocalesDirEntry(io::Error),
49    /// `$sysroot/share/locale/$locale` is not a directory.
50    LocaleIsNotDir,
51}
52
53impl fmt::Display for TranslationBundleError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            TranslationBundleError::ReadFtl(e) => write!(f, "could not read ftl file: {e}"),
57            TranslationBundleError::ParseFtl(e) => {
58                write!(f, "could not parse ftl file: {e}")
59            }
60            TranslationBundleError::AddResource(e) => write!(f, "failed to add resource: {e}"),
61            TranslationBundleError::MissingLocale => write!(f, "missing locale directory"),
62            TranslationBundleError::ReadLocalesDir(e) => {
63                write!(f, "could not read locales dir: {e}")
64            }
65            TranslationBundleError::ReadLocalesDirEntry(e) => {
66                write!(f, "could not read locales dir entry: {e}")
67            }
68            TranslationBundleError::LocaleIsNotDir => {
69                write!(f, "`$sysroot/share/locales/$locale` is not a directory")
70            }
71        }
72    }
73}
74
75impl Error for TranslationBundleError {
76    fn source(&self) -> Option<&(dyn Error + 'static)> {
77        match self {
78            TranslationBundleError::ReadFtl(e) => Some(e),
79            TranslationBundleError::ParseFtl(e) => Some(e),
80            TranslationBundleError::AddResource(e) => Some(e),
81            TranslationBundleError::MissingLocale => None,
82            TranslationBundleError::ReadLocalesDir(e) => Some(e),
83            TranslationBundleError::ReadLocalesDirEntry(e) => Some(e),
84            TranslationBundleError::LocaleIsNotDir => None,
85        }
86    }
87}
88
89impl From<(FluentResource, Vec<ParserError>)> for TranslationBundleError {
90    fn from((_, mut errs): (FluentResource, Vec<ParserError>)) -> Self {
91        TranslationBundleError::ParseFtl(errs.pop().expect("failed ftl parse with no errors"))
92    }
93}
94
95impl From<Vec<FluentError>> for TranslationBundleError {
96    fn from(mut errs: Vec<FluentError>) -> Self {
97        TranslationBundleError::AddResource(
98            errs.pop().expect("failed adding resource to bundle with no errors"),
99        )
100    }
101}
102
103/// Returns Fluent bundle with the user's locale resources from
104/// `$sysroot/share/locale/$requested_locale/*.ftl`.
105///
106/// If `-Z additional-ftl-path` was provided, load that resource and add it  to the bundle
107/// (overriding any conflicting messages).
108#[instrument(level = "trace")]
109pub fn fluent_bundle(
110    sysroot_candidates: &[&Path],
111    requested_locale: Option<LanguageIdentifier>,
112    additional_ftl_path: Option<&Path>,
113    with_directionality_markers: bool,
114) -> Result<Option<Arc<FluentBundle>>, TranslationBundleError> {
115    if requested_locale.is_none() && additional_ftl_path.is_none() {
116        return Ok(None);
117    }
118
119    let fallback_locale = langid!("en-US");
120    let requested_fallback_locale = requested_locale.as_ref() == Some(&fallback_locale);
121    trace!(?requested_fallback_locale);
122    if requested_fallback_locale && additional_ftl_path.is_none() {
123        return Ok(None);
124    }
125    // If there is only `-Z additional-ftl-path`, assume locale is "en-US", otherwise use user
126    // provided locale.
127    let locale = requested_locale.clone().unwrap_or(fallback_locale);
128    trace!(?locale);
129    let mut bundle = new_bundle(vec![locale]);
130
131    // Add convenience functions available to ftl authors.
132    register_functions(&mut bundle);
133
134    // Fluent diagnostics can insert directionality isolation markers around interpolated variables
135    // indicating that there may be a shift from right-to-left to left-to-right text (or
136    // vice-versa). These are disabled because they are sometimes visible in the error output, but
137    // may be worth investigating in future (for example: if type names are left-to-right and the
138    // surrounding diagnostic messages are right-to-left, then these might be helpful).
139    bundle.set_use_isolating(with_directionality_markers);
140
141    // If the user requests the default locale then don't try to load anything.
142    if let Some(requested_locale) = requested_locale {
143        let mut found_resources = false;
144        for sysroot in sysroot_candidates {
145            let mut sysroot = sysroot.to_path_buf();
146            sysroot.push("share");
147            sysroot.push("locale");
148            sysroot.push(requested_locale.to_string());
149            trace!(?sysroot);
150
151            if !sysroot.exists() {
152                trace!("skipping");
153                continue;
154            }
155
156            if !sysroot.is_dir() {
157                return Err(TranslationBundleError::LocaleIsNotDir);
158            }
159
160            for entry in sysroot.read_dir().map_err(TranslationBundleError::ReadLocalesDir)? {
161                let entry = entry.map_err(TranslationBundleError::ReadLocalesDirEntry)?;
162                let path = entry.path();
163                trace!(?path);
164                if path.extension().and_then(|s| s.to_str()) != Some("ftl") {
165                    trace!("skipping");
166                    continue;
167                }
168
169                let resource_str =
170                    fs::read_to_string(path).map_err(TranslationBundleError::ReadFtl)?;
171                let resource =
172                    FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
173                trace!(?resource);
174                bundle.add_resource(resource).map_err(TranslationBundleError::from)?;
175                found_resources = true;
176            }
177        }
178
179        if !found_resources {
180            return Err(TranslationBundleError::MissingLocale);
181        }
182    }
183
184    if let Some(additional_ftl_path) = additional_ftl_path {
185        let resource_str =
186            fs::read_to_string(additional_ftl_path).map_err(TranslationBundleError::ReadFtl)?;
187        let resource =
188            FluentResource::try_new(resource_str).map_err(TranslationBundleError::from)?;
189        trace!(?resource);
190        bundle.add_resource_overriding(resource);
191    }
192
193    let bundle = Arc::new(bundle);
194    Ok(Some(bundle))
195}
196
197fn register_functions(bundle: &mut FluentBundle) {
198    bundle
199        .add_function("STREQ", |positional, _named| match positional {
200            [FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),
201            _ => FluentValue::Error,
202        })
203        .expect("Failed to add a function to the bundle.");
204}
205
206/// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily
207/// evaluated fluent bundle.
208pub type LazyFallbackBundle =
209    Arc<LazyLock<FluentBundle, Box<dyn FnOnce() -> FluentBundle + DynSend>>>;
210
211/// Return the default `FluentBundle` with standard "en-US" diagnostic messages.
212#[instrument(level = "trace", skip(resources))]
213pub fn fallback_fluent_bundle(
214    resources: Vec<&'static str>,
215    with_directionality_markers: bool,
216) -> LazyFallbackBundle {
217    Arc::new(LazyLock::new(Box::new(move || {
218        let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);
219
220        register_functions(&mut fallback_bundle);
221
222        // See comment in `fluent_bundle`.
223        fallback_bundle.set_use_isolating(with_directionality_markers);
224
225        for resource in resources {
226            let resource = FluentResource::try_new(resource.to_string())
227                .expect("failed to parse fallback fluent resource");
228            fallback_bundle.add_resource_overriding(resource);
229        }
230
231        fallback_bundle
232    })))
233}
234
235/// Identifier for the Fluent message/attribute corresponding to a diagnostic message.
236type FluentId = Cow<'static, str>;
237
238/// Abstraction over a message in a subdiagnostic (i.e. label, note, help, etc) to support both
239/// translatable and non-translatable diagnostic messages.
240///
241/// Translatable messages for subdiagnostics are typically attributes attached to a larger Fluent
242/// message so messages of this type must be combined with a `DiagMessage` (using
243/// `DiagMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from
244/// the `Subdiagnostic` derive refer to Fluent identifiers directly.
245#[rustc_diagnostic_item = "SubdiagMessage"]
246pub enum SubdiagMessage {
247    /// Non-translatable diagnostic message.
248    Str(Cow<'static, str>),
249    /// Translatable message which has already been translated eagerly.
250    ///
251    /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
252    /// be instantiated multiple times with different values. These subdiagnostics' messages
253    /// are translated when they are added to the parent diagnostic, producing this variant of
254    /// `DiagMessage`.
255    Translated(Cow<'static, str>),
256    /// Identifier of a Fluent message. Instances of this variant are generated by the
257    /// `Subdiagnostic` derive.
258    FluentIdentifier(FluentId),
259    /// Attribute of a Fluent message. Needs to be combined with a Fluent identifier to produce an
260    /// actual translated message. Instances of this variant are generated by the `fluent_messages`
261    /// macro.
262    ///
263    /// <https://projectfluent.org/fluent/guide/attributes.html>
264    FluentAttr(FluentId),
265}
266
267impl From<String> for SubdiagMessage {
268    fn from(s: String) -> Self {
269        SubdiagMessage::Str(Cow::Owned(s))
270    }
271}
272impl From<&'static str> for SubdiagMessage {
273    fn from(s: &'static str) -> Self {
274        SubdiagMessage::Str(Cow::Borrowed(s))
275    }
276}
277impl From<Cow<'static, str>> for SubdiagMessage {
278    fn from(s: Cow<'static, str>) -> Self {
279        SubdiagMessage::Str(s)
280    }
281}
282
283/// Abstraction over a message in a diagnostic to support both translatable and non-translatable
284/// diagnostic messages.
285///
286/// Intended to be removed once diagnostics are entirely translatable.
287#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
288#[rustc_diagnostic_item = "DiagMessage"]
289pub enum DiagMessage {
290    /// Non-translatable diagnostic message.
291    Str(Cow<'static, str>),
292    /// Translatable message which has been already translated.
293    ///
294    /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
295    /// be instantiated multiple times with different values. These subdiagnostics' messages
296    /// are translated when they are added to the parent diagnostic, producing this variant of
297    /// `DiagMessage`.
298    Translated(Cow<'static, str>),
299    /// Identifier for a Fluent message (with optional attribute) corresponding to the diagnostic
300    /// message. Yet to be translated.
301    ///
302    /// <https://projectfluent.org/fluent/guide/hello.html>
303    /// <https://projectfluent.org/fluent/guide/attributes.html>
304    FluentIdentifier(FluentId, Option<FluentId>),
305}
306
307impl DiagMessage {
308    /// Given a `SubdiagMessage` which may contain a Fluent attribute, create a new
309    /// `DiagMessage` that combines that attribute with the Fluent identifier of `self`.
310    ///
311    /// - If the `SubdiagMessage` is non-translatable then return the message as a `DiagMessage`.
312    /// - If `self` is non-translatable then return `self`'s message.
313    pub fn with_subdiagnostic_message(&self, sub: SubdiagMessage) -> Self {
314        let attr = match sub {
315            SubdiagMessage::Str(s) => return DiagMessage::Str(s),
316            SubdiagMessage::Translated(s) => return DiagMessage::Translated(s),
317            SubdiagMessage::FluentIdentifier(id) => {
318                return DiagMessage::FluentIdentifier(id, None);
319            }
320            SubdiagMessage::FluentAttr(attr) => attr,
321        };
322
323        match self {
324            DiagMessage::Str(s) => DiagMessage::Str(s.clone()),
325            DiagMessage::Translated(s) => DiagMessage::Translated(s.clone()),
326            DiagMessage::FluentIdentifier(id, _) => {
327                DiagMessage::FluentIdentifier(id.clone(), Some(attr))
328            }
329        }
330    }
331
332    pub fn as_str(&self) -> Option<&str> {
333        match self {
334            DiagMessage::Translated(s) | DiagMessage::Str(s) => Some(s),
335            DiagMessage::FluentIdentifier(_, _) => None,
336        }
337    }
338}
339
340impl From<String> for DiagMessage {
341    fn from(s: String) -> Self {
342        DiagMessage::Str(Cow::Owned(s))
343    }
344}
345impl From<&'static str> for DiagMessage {
346    fn from(s: &'static str) -> Self {
347        DiagMessage::Str(Cow::Borrowed(s))
348    }
349}
350impl From<Cow<'static, str>> for DiagMessage {
351    fn from(s: Cow<'static, str>) -> Self {
352        DiagMessage::Str(s)
353    }
354}
355
356/// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
357/// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagMessage` and the
358/// subdiagnostic derive refers to typed identifiers that are `DiagMessage`s, so need to be
359/// able to convert between these, as much as they'll be converted back into `DiagMessage`
360/// using `with_subdiagnostic_message` eventually. Don't use this other than for the derive.
361impl From<DiagMessage> for SubdiagMessage {
362    fn from(val: DiagMessage) -> Self {
363        match val {
364            DiagMessage::Str(s) => SubdiagMessage::Str(s),
365            DiagMessage::Translated(s) => SubdiagMessage::Translated(s),
366            DiagMessage::FluentIdentifier(id, None) => SubdiagMessage::FluentIdentifier(id),
367            // There isn't really a sensible behaviour for this because it loses information but
368            // this is the most sensible of the behaviours.
369            DiagMessage::FluentIdentifier(_, Some(attr)) => SubdiagMessage::FluentAttr(attr),
370        }
371    }
372}
373
374/// A span together with some additional data.
375#[derive(Clone, Debug)]
376pub struct SpanLabel {
377    /// The span we are going to include in the final snippet.
378    pub span: Span,
379
380    /// Is this a primary span? This is the "locus" of the message,
381    /// and is indicated with a `^^^^` underline, versus `----`.
382    pub is_primary: bool,
383
384    /// What label should we attach to this span (if any)?
385    pub label: Option<DiagMessage>,
386}
387
388/// A collection of `Span`s.
389///
390/// Spans have two orthogonal attributes:
391///
392/// - They can be *primary spans*. In this case they are the locus of
393///   the error, and would be rendered with `^^^`.
394/// - They can have a *label*. In this case, the label is written next
395///   to the mark in the snippet when we render.
396#[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)]
397pub struct MultiSpan {
398    primary_spans: Vec<Span>,
399    span_labels: Vec<(Span, DiagMessage)>,
400}
401
402impl MultiSpan {
403    #[inline]
404    pub fn new() -> MultiSpan {
405        MultiSpan { primary_spans: vec![], span_labels: vec![] }
406    }
407
408    pub fn from_span(primary_span: Span) -> MultiSpan {
409        MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }
410    }
411
412    pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan {
413        vec.sort();
414        MultiSpan { primary_spans: vec, span_labels: vec![] }
415    }
416
417    pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagMessage>) {
418        self.span_labels.push((span, label.into()));
419    }
420
421    /// Selects the first primary span (if any).
422    pub fn primary_span(&self) -> Option<Span> {
423        self.primary_spans.first().cloned()
424    }
425
426    /// Returns all primary spans.
427    pub fn primary_spans(&self) -> &[Span] {
428        &self.primary_spans
429    }
430
431    /// Returns `true` if any of the primary spans are displayable.
432    pub fn has_primary_spans(&self) -> bool {
433        !self.is_dummy()
434    }
435
436    /// Returns `true` if this contains only a dummy primary span with any hygienic context.
437    pub fn is_dummy(&self) -> bool {
438        self.primary_spans.iter().all(|sp| sp.is_dummy())
439    }
440
441    /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't
442    /// display well (like std macros). Returns whether replacements occurred.
443    pub fn replace(&mut self, before: Span, after: Span) -> bool {
444        let mut replacements_occurred = false;
445        for primary_span in &mut self.primary_spans {
446            if *primary_span == before {
447                *primary_span = after;
448                replacements_occurred = true;
449            }
450        }
451        for span_label in &mut self.span_labels {
452            if span_label.0 == before {
453                span_label.0 = after;
454                replacements_occurred = true;
455            }
456        }
457        replacements_occurred
458    }
459
460    pub fn pop_span_label(&mut self) -> Option<(Span, DiagMessage)> {
461        self.span_labels.pop()
462    }
463
464    /// Returns the strings to highlight. We always ensure that there
465    /// is an entry for each of the primary spans -- for each primary
466    /// span `P`, if there is at least one label with span `P`, we return
467    /// those labels (marked as primary). But otherwise we return
468    /// `SpanLabel` instances with empty labels.
469    pub fn span_labels(&self) -> Vec<SpanLabel> {
470        let is_primary = |span| self.primary_spans.contains(&span);
471
472        let mut span_labels = self
473            .span_labels
474            .iter()
475            .map(|&(span, ref label)| SpanLabel {
476                span,
477                is_primary: is_primary(span),
478                label: Some(label.clone()),
479            })
480            .collect::<Vec<_>>();
481
482        for &span in &self.primary_spans {
483            if !span_labels.iter().any(|sl| sl.span == span) {
484                span_labels.push(SpanLabel { span, is_primary: true, label: None });
485            }
486        }
487
488        span_labels
489    }
490
491    /// Returns `true` if any of the span labels is displayable.
492    pub fn has_span_labels(&self) -> bool {
493        self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
494    }
495
496    /// Clone this `MultiSpan` without keeping any of the span labels - sometimes a `MultiSpan` is
497    /// to be re-used in another diagnostic, but includes `span_labels` which have translated
498    /// messages. These translated messages would fail to translate without their diagnostic
499    /// arguments which are unlikely to be cloned alongside the `Span`.
500    pub fn clone_ignoring_labels(&self) -> Self {
501        Self { primary_spans: self.primary_spans.clone(), ..MultiSpan::new() }
502    }
503}
504
505impl From<Span> for MultiSpan {
506    fn from(span: Span) -> MultiSpan {
507        MultiSpan::from_span(span)
508    }
509}
510
511impl From<Vec<Span>> for MultiSpan {
512    fn from(spans: Vec<Span>) -> MultiSpan {
513        MultiSpan::from_spans(spans)
514    }
515}
516
517fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option<icu_locale::Locale> {
518    icu_locale::Locale::try_from_str(&lang.to_string()).ok()
519}
520
521pub fn fluent_value_from_str_list_sep_by_and(l: Vec<Cow<'_, str>>) -> FluentValue<'_> {
522    // Fluent requires 'static value here for its AnyEq usages.
523    #[derive(Clone, PartialEq, Debug)]
524    struct FluentStrListSepByAnd(Vec<String>);
525
526    impl FluentType for FluentStrListSepByAnd {
527        fn duplicate(&self) -> Box<dyn FluentType + Send> {
528            Box::new(self.clone())
529        }
530
531        fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {
532            let result = intls
533                .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
534                    list_formatter.format_to_string(self.0.iter())
535                })
536                .unwrap();
537            Cow::Owned(result)
538        }
539
540        fn as_string_threadsafe(
541            &self,
542            intls: &intl_memoizer::concurrent::IntlLangMemoizer,
543        ) -> Cow<'static, str> {
544            let result = intls
545                .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {
546                    list_formatter.format_to_string(self.0.iter())
547                })
548                .unwrap();
549            Cow::Owned(result)
550        }
551    }
552
553    struct MemoizableListFormatter(icu_list::ListFormatter);
554
555    impl std::ops::Deref for MemoizableListFormatter {
556        type Target = icu_list::ListFormatter;
557        fn deref(&self) -> &Self::Target {
558            &self.0
559        }
560    }
561
562    impl intl_memoizer::Memoizable for MemoizableListFormatter {
563        type Args = ();
564        type Error = ();
565
566        fn construct(lang: LanguageIdentifier, _args: Self::Args) -> Result<Self, Self::Error>
567        where
568            Self: Sized,
569        {
570            let locale = icu_locale_from_unic_langid(lang)
571                .unwrap_or_else(|| rustc_baked_icu_data::supported_locales::EN);
572            let list_formatter = icu_list::ListFormatter::try_new_and_unstable(
573                &rustc_baked_icu_data::BakedDataProvider,
574                locale.into(),
575                icu_list::options::ListFormatterOptions::default()
576                    .with_length(icu_list::options::ListLength::Wide),
577            )
578            .expect("Failed to create list formatter");
579
580            Ok(MemoizableListFormatter(list_formatter))
581        }
582    }
583
584    let l = l.into_iter().map(|x| x.into_owned()).collect();
585
586    FluentValue::Custom(Box::new(FluentStrListSepByAnd(l)))
587}
588
589/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
590/// `DiagArg` are converted to `FluentArgs` (consuming the collection) at the start of diagnostic
591/// emission.
592pub type DiagArg<'iter> = (&'iter DiagArgName, &'iter DiagArgValue);
593
594/// Name of a diagnostic argument.
595pub type DiagArgName = Cow<'static, str>;
596
597/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted
598/// to a `FluentValue` by the emitter to be used in diagnostic translation.
599#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
600pub enum DiagArgValue {
601    Str(Cow<'static, str>),
602    // This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
603    // safely fits in an `f64`. Any integers bigger than that will be converted
604    // to strings in `into_diag_arg` and stored using the `Str` variant.
605    Number(i32),
606    StrListSepByAnd(Vec<Cow<'static, str>>),
607}
608
609/// Converts a value of a type into a `DiagArg` (typically a field of an `Diag` struct).
610/// Implemented as a custom trait rather than `From` so that it is implemented on the type being
611/// converted rather than on `DiagArgValue`, which enables types from other `rustc_*` crates to
612/// implement this.
613pub trait IntoDiagArg {
614    /// Convert `Self` into a `DiagArgValue` suitable for rendering in a diagnostic.
615    ///
616    /// It takes a `path` where "long values" could be written to, if the `DiagArgValue` is too big
617    /// for displaying on the terminal. This path comes from the `Diag` itself. When rendering
618    /// values that come from `TyCtxt`, like `Ty<'_>`, they can use `TyCtxt::short_string`. If a
619    /// value has no shortening logic that could be used, the argument can be safely ignored.
620    fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue;
621}
622
623impl IntoDiagArg for DiagArgValue {
624    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
625        self
626    }
627}
628
629impl From<DiagArgValue> for FluentValue<'static> {
630    fn from(val: DiagArgValue) -> Self {
631        match val {
632            DiagArgValue::Str(s) => From::from(s),
633            DiagArgValue::Number(n) => From::from(n),
634            DiagArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),
635        }
636    }
637}