Skip to main content

rustc_errors/
diagnostic_impls.rs

1use std::borrow::Cow;
2
3use rustc_abi::TargetDataLayoutErrors;
4use rustc_error_messages::{DiagArgValue, IntoDiagArg};
5use rustc_macros::Subdiagnostic;
6use rustc_span::{Span, Symbol};
7
8use crate::diagnostic::DiagLocation;
9use crate::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, Subdiagnostic, msg};
10
11impl IntoDiagArg for DiagLocation {
12    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
13        DiagArgValue::Str(Cow::from(self.to_string()))
14    }
15}
16
17#[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)]
18pub struct DiagSymbolList<S = Symbol>(Vec<S>);
19
20impl<S> From<Vec<S>> for DiagSymbolList<S> {
21    fn from(v: Vec<S>) -> Self {
22        DiagSymbolList(v)
23    }
24}
25
26impl<S> FromIterator<S> for DiagSymbolList<S> {
27    fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
28        iter.into_iter().collect::<Vec<_>>().into()
29    }
30}
31
32impl<S: std::fmt::Display> IntoDiagArg for DiagSymbolList<S> {
33    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
34        DiagArgValue::StrListSepByAnd(
35            self.0.into_iter().map(|sym| Cow::Owned(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", sym))
    })format!("`{sym}`"))).collect(),
36        )
37    }
38}
39
40impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
41    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
42        match self {
43            TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
44                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("invalid address space `{$addr_space}` for `{$cause}` in \"data-layout\": {$err}"))msg!("invalid address space `{$addr_space}` for `{$cause}` in \"data-layout\": {$err}"))
45                    .with_arg("addr_space", addr_space)
46                    .with_arg("cause", cause)
47                    .with_arg("err", err)
48            }
49            TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => {
50                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("invalid {$kind} `{$bit}` for `{$cause}` in \"data-layout\": {$err}"))msg!("invalid {$kind} `{$bit}` for `{$cause}` in \"data-layout\": {$err}"))
51                    .with_arg("kind", kind)
52                    .with_arg("bit", bit)
53                    .with_arg("cause", cause)
54                    .with_arg("err", err)
55            }
56            TargetDataLayoutErrors::MissingAlignment { cause } => {
57                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("missing alignment for `{$cause}` in \"data-layout\""))msg!("missing alignment for `{$cause}` in \"data-layout\""))
58                    .with_arg("cause", cause)
59            }
60            TargetDataLayoutErrors::InvalidAlignment { cause, err } => {
61                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("invalid alignment for `{$cause}` in \"data-layout\": {$err}"))msg!(
62                    "invalid alignment for `{$cause}` in \"data-layout\": {$err}"
63                ))
64                .with_arg("cause", cause)
65                .with_arg("err", err.to_string())
66            }
67            TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => {
68                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("inconsistent target specification: \"data-layout\" claims architecture is {$dl}-endian, while \"target-endian\" is `{$target}`"))msg!(
69                    "inconsistent target specification: \"data-layout\" claims architecture is {$dl}-endian, while \"target-endian\" is `{$target}`"
70                ))
71                .with_arg("dl", dl).with_arg("target", target)
72            }
73            TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => {
74                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("inconsistent target specification: \"data-layout\" claims pointers are {$pointer_size}-bit, while \"target-pointer-width\" is `{$target}`"))msg!(
75                    "inconsistent target specification: \"data-layout\" claims pointers are {$pointer_size}-bit, while \"target-pointer-width\" is `{$target}`"
76                )).with_arg("pointer_size", pointer_size).with_arg("target", target)
77            }
78            TargetDataLayoutErrors::InvalidBitsSize { err } => {
79                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("{$err}"))msg!("{$err}")).with_arg("err", err)
80            }
81            TargetDataLayoutErrors::UnknownPointerSpecification { err } => {
82                Diag::new(dcx, level, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("unknown pointer specification `{$err}` in datalayout string"))msg!("unknown pointer specification `{$err}` in datalayout string"))
83                    .with_arg("err", err)
84            }
85        }
86    }
87}
88
89/// Utility struct used to apply a single label while highlighting multiple spans
90pub struct SingleLabelManySpans {
91    pub spans: Vec<Span>,
92    pub label: &'static str,
93}
94impl Subdiagnostic for SingleLabelManySpans {
95    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
96        diag.span_labels(self.spans, self.label);
97    }
98}
99
100#[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)]
101#[label(
102    "expected lifetime {$count ->
103        [1] parameter
104        *[other] parameters
105    }"
106)]
107pub struct ExpectedLifetimeParameter {
108    #[primary_span]
109    pub span: Span,
110    pub count: usize,
111}
112
113#[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)]
114#[suggestion(
115    "indicate the anonymous {$count ->
116        [1] lifetime
117        *[other] lifetimes
118    }",
119    code = "{suggestion}",
120    style = "verbose"
121)]
122pub struct IndicateAnonymousLifetime {
123    #[primary_span]
124    pub span: Span,
125    pub count: usize,
126    pub suggestion: String,
127}
128
129#[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)]
130pub struct ElidedLifetimeInPathSubdiag {
131    #[subdiagnostic]
132    pub expected: ExpectedLifetimeParameter,
133    #[subdiagnostic]
134    pub indicate: Option<IndicateAnonymousLifetime>,
135}