Skip to main content

rustc_errors/
diagnostic_impls.rs

1use std::borrow::Cow;
2
3use rustc_error_messages::{DiagArgValue, IntoDiagArg};
4use rustc_macros::Subdiagnostic;
5use rustc_span::{Span, Symbol};
6
7use crate::diagnostic::DiagLocation;
8use crate::{Diag, EmissionGuarantee, Subdiagnostic};
9
10impl IntoDiagArg for DiagLocation {
11    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
12        DiagArgValue::Str(Cow::from(self.to_string()))
13    }
14}
15
16#[derive(#[automatically_derived]
impl<S: ::core::clone::Clone> ::core::clone::Clone for DiagSymbolList<S> {
    #[inline]
    fn clone(&self) -> DiagSymbolList<S> {
        DiagSymbolList(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
17pub struct DiagSymbolList<S = Symbol>(Vec<S>);
18
19impl<S> From<Vec<S>> for DiagSymbolList<S> {
20    fn from(v: Vec<S>) -> Self {
21        DiagSymbolList(v)
22    }
23}
24
25impl<S> FromIterator<S> for DiagSymbolList<S> {
26    fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
27        iter.into_iter().collect::<Vec<_>>().into()
28    }
29}
30
31impl<S: std::fmt::Display> IntoDiagArg for DiagSymbolList<S> {
32    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
33        DiagArgValue::StrListSepByAnd(
34            self.0.into_iter().map(|sym| Cow::Owned(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", sym))
    })format!("`{sym}`"))).collect(),
35        )
36    }
37}
38
39/// Utility struct used to apply a single label while highlighting multiple spans
40pub struct SingleLabelManySpans {
41    pub spans: Vec<Span>,
42    pub label: &'static str,
43}
44impl Subdiagnostic for SingleLabelManySpans {
45    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
46        diag.span_labels(self.spans, self.label);
47    }
48}
49
50#[derive(const _: () =
    {
        impl rustc_errors::Subdiagnostic for ExpectedLifetimeParameter {
            fn add_to_diag<__G>(self, diag: &mut rustc_errors::Diag<'_, __G>)
                where __G: rustc_errors::EmissionGuarantee {
                match self {
                    ExpectedLifetimeParameter {
                        span: __binding_0, count: __binding_1 } => {
                        let mut sub_args = rustc_errors::DiagArgMap::default();
                        sub_args.insert("count".into(),
                            rustc_errors::IntoDiagArg::into_diag_arg(__binding_1,
                                &mut diag.long_ty_path));
                        let __message =
                            rustc_errors::format_diag_message(&rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected lifetime {$count ->\n        [1] parameter\n        *[other] parameters\n    }")),
                                &sub_args);
                        diag.span_label(__binding_0, __message);
                    }
                }
            }
        }
    };Subdiagnostic)]
51#[label(
52    "expected lifetime {$count ->
53        [1] parameter
54        *[other] parameters
55    }"
56)]
57pub struct ExpectedLifetimeParameter {
58    #[primary_span]
59    pub span: Span,
60    pub count: usize,
61}
62
63#[derive(const _: () =
    {
        impl rustc_errors::Subdiagnostic for IndicateAnonymousLifetime {
            fn add_to_diag<__G>(self, diag: &mut rustc_errors::Diag<'_, __G>)
                where __G: rustc_errors::EmissionGuarantee {
                match self {
                    IndicateAnonymousLifetime {
                        span: __binding_0,
                        count: __binding_1,
                        suggestion: __binding_2 } => {
                        let __code_0 =
                            [::alloc::__export::must_use({
                                                ::alloc::fmt::format(format_args!("{0}", __binding_2))
                                            })].into_iter();
                        let mut sub_args = rustc_errors::DiagArgMap::default();
                        sub_args.insert("count".into(),
                            rustc_errors::IntoDiagArg::into_diag_arg(__binding_1,
                                &mut diag.long_ty_path));
                        sub_args.insert("suggestion".into(),
                            rustc_errors::IntoDiagArg::into_diag_arg(__binding_2,
                                &mut diag.long_ty_path));
                        let __message =
                            rustc_errors::format_diag_message(&rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("indicate the anonymous {$count ->\n        [1] lifetime\n        *[other] lifetimes\n    }")),
                                &sub_args);
                        diag.span_suggestions_with_style(__binding_0, __message,
                            __code_0, rustc_errors::Applicability::Unspecified,
                            rustc_errors::SuggestionStyle::ShowAlways);
                    }
                }
            }
        }
    };Subdiagnostic)]
64#[suggestion(
65    "indicate the anonymous {$count ->
66        [1] lifetime
67        *[other] lifetimes
68    }",
69    code = "{suggestion}",
70    style = "verbose"
71)]
72pub struct IndicateAnonymousLifetime {
73    #[primary_span]
74    pub span: Span,
75    pub count: usize,
76    pub suggestion: String,
77}
78
79#[derive(const _: () =
    {
        impl rustc_errors::Subdiagnostic for ElidedLifetimeInPathSubdiag {
            fn add_to_diag<__G>(self, diag: &mut rustc_errors::Diag<'_, __G>)
                where __G: rustc_errors::EmissionGuarantee {
                match self {
                    ElidedLifetimeInPathSubdiag {
                        expected: __binding_0, indicate: __binding_1 } => {
                        __binding_0.add_to_diag(diag);
                        if let Some(__binding_1) = __binding_1 {
                            __binding_1.add_to_diag(diag);
                        }
                        let mut sub_args = rustc_errors::DiagArgMap::default();
                    }
                }
            }
        }
    };Subdiagnostic)]
80pub struct ElidedLifetimeInPathSubdiag {
81    #[subdiagnostic]
82    pub expected: ExpectedLifetimeParameter,
83    #[subdiagnostic]
84    pub indicate: Option<IndicateAnonymousLifetime>,
85}