compiler/rustc_error_messages/src/lib.rs RUST 344 lines View on github.com → Search inside
1// tidy-alphabetical-start2#![allow(internal_features)]3#![feature(rustc_attrs)]4// tidy-alphabetical-end56use std::borrow::Cow;78pub use fluent_bundle::types::FluentType;9pub use fluent_bundle::{self, FluentArgs, FluentError, FluentValue};10use rustc_macros::{Decodable, Encodable, StableHash};11use rustc_span::Span;12pub use unic_langid::{LanguageIdentifier, langid};1314mod diagnostic_impls;15pub use diagnostic_impls::DiagArgFromDisplay;16use rustc_data_structures::fx::FxIndexMap;1718pub fn register_functions<R, M>(bundle: &mut fluent_bundle::bundle::FluentBundle<R, M>) {19    bundle20        .add_function("STREQ", |positional, _named| match positional {21            [FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),22            _ => FluentValue::Error,23        })24        .expect("Failed to add a function to the bundle.");25}2627/// Abstraction over a message in a diagnostic to support both translatable and non-translatable28/// diagnostic messages.29///30/// Intended to be removed once diagnostics are entirely translatable.31#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)]32#[rustc_diagnostic_item = "DiagMessage"]33pub enum DiagMessage {34    /// Non-translatable diagnostic message or a message that has been translated eagerly.35    ///36    /// Some diagnostics have repeated subdiagnostics where the same interpolated variables would37    /// be instantiated multiple times with different values. These subdiagnostics' messages38    /// are translated when they are added to the parent diagnostic. This is one of the ways39    /// this variant of `DiagMessage` is produced.40    Str(Cow<'static, str>),41    /// An inline Fluent message, containing the to be translated diagnostic message.42    Inline(Cow<'static, str>),43}4445impl DiagMessage {46    pub fn as_str(&self) -> Option<&str> {47        match self {48            DiagMessage::Str(s) => Some(s),49            DiagMessage::Inline(_) => None,50        }51    }52}5354impl From<String> for DiagMessage {55    fn from(s: String) -> Self {56        DiagMessage::Str(Cow::Owned(s))57    }58}59impl From<&'static str> for DiagMessage {60    fn from(s: &'static str) -> Self {61        DiagMessage::Str(Cow::Borrowed(s))62    }63}64impl From<Cow<'static, str>> for DiagMessage {65    fn from(s: Cow<'static, str>) -> Self {66        DiagMessage::Str(s)67    }68}6970/// A span together with some additional data.71#[derive(Clone, Debug)]72pub struct SpanLabel {73    /// The span we are going to include in the final snippet.74    pub span: Span,7576    /// Is this a primary span? This is the "locus" of the message,77    /// and is indicated with a `^^^^` underline, versus `----`.78    pub is_primary: bool,7980    /// What label should we attach to this span (if any)?81    pub label: Option<DiagMessage>,82}8384/// A collection of `Span`s.85///86/// Spans have two orthogonal attributes:87///88/// - They can be *primary spans*. In this case they are the locus of89///   the error, and would be rendered with `^^^`.90/// - They can have a *label*. In this case, the label is written next91///   to the mark in the snippet when we render.92#[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable, StableHash)]93pub struct MultiSpan {94    primary_spans: Vec<Span>,95    span_labels: Vec<(Span, DiagMessage)>,96}9798impl MultiSpan {99    #[inline]100    pub fn new() -> MultiSpan {101        MultiSpan { primary_spans: vec![], span_labels: vec![] }102    }103104    pub fn from_span(primary_span: Span) -> MultiSpan {105        MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }106    }107108    pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan {109        vec.sort();110        MultiSpan { primary_spans: vec, span_labels: vec![] }111    }112113    pub fn push_primary_span(&mut self, primary_span: Span) {114        self.primary_spans.push(primary_span);115    }116117    pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagMessage>) {118        self.span_labels.push((span, label.into()));119    }120121    pub fn push_span_diag(&mut self, span: Span, diag: DiagMessage) {122        self.span_labels.push((span, diag));123    }124125    /// Selects the first primary span (if any).126    pub fn primary_span(&self) -> Option<Span> {127        self.primary_spans.first().cloned()128    }129130    /// Returns all primary spans.131    pub fn primary_spans(&self) -> &[Span] {132        &self.primary_spans133    }134135    /// Returns `true` if any of the primary spans are displayable.136    pub fn has_primary_spans(&self) -> bool {137        !self.is_dummy()138    }139140    /// Returns `true` if this contains only a dummy primary span with any hygienic context.141    pub fn is_dummy(&self) -> bool {142        self.primary_spans.iter().all(|sp| sp.is_dummy())143    }144145    /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't146    /// display well (like std macros). Returns whether replacements occurred.147    pub fn replace(&mut self, before: Span, after: Span) -> bool {148        let mut replacements_occurred = false;149        for primary_span in &mut self.primary_spans {150            if *primary_span == before {151                *primary_span = after;152                replacements_occurred = true;153            }154        }155        for span_label in &mut self.span_labels {156            if span_label.0 == before {157                span_label.0 = after;158                replacements_occurred = true;159            }160        }161        replacements_occurred162    }163164    /// Returns the strings to highlight. We always ensure that there165    /// is an entry for each of the primary spans -- for each primary166    /// span `P`, if there is at least one label with span `P`, we return167    /// those labels (marked as primary). But otherwise we return168    /// `SpanLabel` instances with empty labels.169    pub fn span_labels(&self) -> Vec<SpanLabel> {170        let is_primary = |span| self.primary_spans.contains(&span);171172        let mut span_labels = self173            .span_labels174            .iter()175            .map(|&(span, ref label)| SpanLabel {176                span,177                is_primary: is_primary(span),178                label: Some(label.clone()),179            })180            .collect::<Vec<_>>();181182        for &span in &self.primary_spans {183            if !span_labels.iter().any(|sl| sl.span == span) {184                span_labels.push(SpanLabel { span, is_primary: true, label: None });185            }186        }187188        span_labels189    }190191    /// Returns the span labels as contained by `MultiSpan`.192    pub fn span_labels_raw(&self) -> &[(Span, DiagMessage)] {193        &self.span_labels194    }195196    /// Returns `true` if any of the span labels is displayable.197    pub fn has_span_labels(&self) -> bool {198        self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())199    }200201    /// Clone this `MultiSpan` without keeping any of the span labels - sometimes a `MultiSpan` is202    /// to be re-used in another diagnostic, but includes `span_labels` which have translated203    /// messages. These translated messages would fail to translate without their diagnostic204    /// arguments which are unlikely to be cloned alongside the `Span`.205    pub fn clone_ignoring_labels(&self) -> Self {206        Self { primary_spans: self.primary_spans.clone(), ..MultiSpan::new() }207    }208}209210impl From<Span> for MultiSpan {211    fn from(span: Span) -> MultiSpan {212        MultiSpan::from_span(span)213    }214}215216impl From<Vec<Span>> for MultiSpan {217    fn from(spans: Vec<Span>) -> MultiSpan {218        MultiSpan::from_spans(spans)219    }220}221222fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option<icu_locale::Locale> {223    icu_locale::Locale::try_from_str(&lang.to_string()).ok()224}225226pub fn fluent_value_from_str_list_sep_by_and(l: Vec<Cow<'_, str>>) -> FluentValue<'_> {227    // Fluent requires 'static value here for its AnyEq usages.228    #[derive(Clone, PartialEq, Debug)]229    struct FluentStrListSepByAnd(Vec<String>);230231    impl FluentType for FluentStrListSepByAnd {232        fn duplicate(&self) -> Box<dyn FluentType + Send> {233            Box::new(self.clone())234        }235236        fn as_string(&self, intls: &intl_memoizer::IntlLangMemoizer) -> Cow<'static, str> {237            let result = intls238                .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {239                    list_formatter.format_to_string(self.0.iter())240                })241                .unwrap();242            Cow::Owned(result)243        }244245        fn as_string_threadsafe(246            &self,247            intls: &intl_memoizer::concurrent::IntlLangMemoizer,248        ) -> Cow<'static, str> {249            let result = intls250                .with_try_get::<MemoizableListFormatter, _, _>((), |list_formatter| {251                    list_formatter.format_to_string(self.0.iter())252                })253                .unwrap();254            Cow::Owned(result)255        }256    }257258    struct MemoizableListFormatter(icu_list::ListFormatter);259260    impl std::ops::Deref for MemoizableListFormatter {261        type Target = icu_list::ListFormatter;262        fn deref(&self) -> &Self::Target {263            &self.0264        }265    }266267    impl intl_memoizer::Memoizable for MemoizableListFormatter {268        type Args = ();269        type Error = ();270271        fn construct(lang: LanguageIdentifier, _args: Self::Args) -> Result<Self, Self::Error> {272            let locale = icu_locale_from_unic_langid(lang)273                .unwrap_or_else(|| rustc_baked_icu_data::supported_locales::EN);274            let list_formatter = icu_list::ListFormatter::try_new_and_unstable(275                &rustc_baked_icu_data::BakedDataProvider,276                locale.into(),277                icu_list::options::ListFormatterOptions::default()278                    .with_length(icu_list::options::ListLength::Wide),279            )280            .expect("Failed to create list formatter");281282            Ok(MemoizableListFormatter(list_formatter))283        }284    }285286    let l = l.into_iter().map(|x| x.into_owned()).collect();287288    FluentValue::Custom(Box::new(FluentStrListSepByAnd(l)))289}290291/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of292/// `DiagArg` are converted to `FluentArgs` (consuming the collection) at the start of diagnostic293/// emission.294pub type DiagArg<'iter> = (&'iter DiagArgName, &'iter DiagArgValue);295296/// Name of a diagnostic argument.297pub type DiagArgName = Cow<'static, str>;298299/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted300/// to a `FluentValue` by the emitter to be used in diagnostic translation.301#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]302pub enum DiagArgValue {303    Str(Cow<'static, str>),304    // This gets converted to a `FluentNumber`, which is an `f64`. An `i32`305    // safely fits in an `f64`. Any integers bigger than that will be converted306    // to strings in `into_diag_arg` and stored using the `Str` variant.307    Number(i32),308    StrListSepByAnd(Vec<Cow<'static, str>>),309}310311/// A mapping from diagnostic argument names to their values.312/// This contains all the arguments necessary to format a diagnostic message.313pub type DiagArgMap = FxIndexMap<DiagArgName, DiagArgValue>;314315/// Converts a value of a type into a `DiagArg` (typically a field of an `Diag` struct).316/// Implemented as a custom trait rather than `From` so that it is implemented on the type being317/// converted rather than on `DiagArgValue`, which enables types from other `rustc_*` crates to318/// implement this.319pub trait IntoDiagArg {320    /// Convert `Self` into a `DiagArgValue` suitable for rendering in a diagnostic.321    ///322    /// It takes a `path` where "long values" could be written to, if the `DiagArgValue` is too big323    /// for displaying on the terminal. This path comes from the `Diag` itself. When rendering324    /// values that come from `TyCtxt`, like `Ty<'_>`, they can use `TyCtxt::short_string`. If a325    /// value has no shortening logic that could be used, the argument can be safely ignored.326    fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue;327}328329impl IntoDiagArg for DiagArgValue {330    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {331        self332    }333}334335impl From<DiagArgValue> for FluentValue<'static> {336    fn from(val: DiagArgValue) -> Self {337        match val {338            DiagArgValue::Str(s) => From::from(s),339            DiagArgValue::Number(n) => From::from(n),340            DiagArgValue::StrListSepByAnd(l) => fluent_value_from_str_list_sep_by_and(l),341        }342    }343}

Code quality findings 7

Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("Failed to add a function to the bundle.");
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
.unwrap();
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
.unwrap();
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("Failed to create list formatter");
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
.add_function("STREQ", |positional, _named| match positional {
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
label: Some(label.clone()),
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
span_labels.push(SpanLabel { span, is_primary: true, label: None });

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.