Skip to main content

rustc_errors/
diagnostic.rs

1use std::borrow::Cow;
2use std::fmt::{self, Debug};
3use std::hash::{Hash, Hasher};
4use std::marker::PhantomData;
5use std::ops::{Deref, DerefMut};
6use std::panic;
7use std::path::PathBuf;
8use std::thread::panicking;
9
10use rustc_ast::attr::version::RustcVersion;
11use rustc_data_structures::sync::{DynSend, DynSync};
12use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg};
13use rustc_lint_defs::{Applicability, LintExpectationId};
14use rustc_macros::{Decodable, Encodable};
15use rustc_span::{DUMMY_SP, Span, Spanned, Symbol};
16use tracing::debug;
17
18use crate::{
19    CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
20    MultiSpan, StashKey, Style, Substitution, SubstitutionPart, SuggestionStyle, Suggestions,
21};
22
23/// Trait for types that `Diag::emit` can return as a "guarantee" (or "proof")
24/// token that the emission happened.
25pub trait EmissionGuarantee: Sized {
26    /// This exists so that bugs and fatal errors can both result in `!` (an
27    /// abort) when emitted, but have different aborting behaviour.
28    type EmitResult = Self;
29
30    /// Implementation of `Diag::emit`, fully controlled by each `impl` of
31    /// `EmissionGuarantee`, to make it impossible to create a value of
32    /// `Self::EmitResult` without actually performing the emission.
33    #[track_caller]
34    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult;
35}
36
37impl EmissionGuarantee for ErrorGuaranteed {
38    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
39        diag.emit_producing_error_guaranteed()
40    }
41}
42
43impl EmissionGuarantee for () {
44    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
45        diag.emit_producing_nothing();
46    }
47}
48
49/// Marker type which enables implementation of `create_bug` and `emit_bug` functions for
50/// bug diagnostics.
51#[derive(#[automatically_derived]
impl ::core::marker::Copy for BugAbort { }Copy, #[automatically_derived]
impl ::core::clone::Clone for BugAbort {
    #[inline]
    fn clone(&self) -> BugAbort { *self }
}Clone)]
52pub struct BugAbort;
53
54impl EmissionGuarantee for BugAbort {
55    type EmitResult = !;
56
57    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
58        diag.emit_producing_nothing();
59        panic::panic_any(ExplicitBug);
60    }
61}
62
63/// Marker type which enables implementation of `create_fatal` and `emit_fatal` functions for
64/// fatal diagnostics.
65#[derive(#[automatically_derived]
impl ::core::marker::Copy for FatalAbort { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FatalAbort {
    #[inline]
    fn clone(&self) -> FatalAbort { *self }
}Clone)]
66pub struct FatalAbort;
67
68impl EmissionGuarantee for FatalAbort {
69    type EmitResult = !;
70
71    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
72        diag.emit_producing_nothing();
73        crate::FatalError.raise()
74    }
75}
76
77impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
78    fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
79        diag.emit_producing_nothing();
80        rustc_span::fatal_error::FatalError
81    }
82}
83
84/// Trait implemented by error types. This is rarely implemented manually. Instead, use
85/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
86///
87/// When implemented manually, it should be generic over the emission
88/// guarantee, i.e.:
89/// ```ignore (fragment)
90/// impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for Foo { ... }
91/// ```
92/// rather than being specific:
93/// ```ignore (fragment)
94/// impl<'a> Diagnostic<'a> for Bar { ... }  // the default type param is `ErrorGuaranteed`
95/// impl<'a> Diagnostic<'a, ()> for Baz { ... }
96/// ```
97/// There are two reasons for this.
98/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is
99///   passed in to `into_diag` from outside. Even if in practice it is
100///   always emitted at a single level, we let the diagnostic creation/emission
101///   site determine the level (by using `create_err`, `emit_warn`, etc.)
102///   rather than the `Diagnostic` impl.
103/// - Derived impls are always generic, and it's good for the hand-written
104///   impls to be consistent with them.
105#[rustc_diagnostic_item = "Diagnostic"]
106pub trait Diagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
107    /// Write out as a diagnostic out of `DiagCtxt`.
108    #[must_use]
109    #[track_caller]
110    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G>;
111}
112
113impl<'a, T, G> Diagnostic<'a, G> for Spanned<T>
114where
115    T: Diagnostic<'a, G>,
116    G: EmissionGuarantee,
117{
118    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
119        self.node.into_diag(dcx, level).with_span(self.span)
120    }
121}
122
123impl<'a> Diagnostic<'a, ()>
124    for Box<
125        dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSync + DynSend + 'static,
126    >
127{
128    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
129        self(dcx, level)
130    }
131}
132
133/// Type used to emit diagnostic through a closure instead of implementing the `Diagnostic` trait.
134pub struct DiagDecorator<F: FnOnce(&mut Diag<'_, ()>)>(pub F);
135
136impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for DiagDecorator<F> {
137    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
138        let mut diag = Diag::new(dcx, level, "");
139        (self.0)(&mut diag);
140        diag
141    }
142}
143
144/// Trait implemented by error types. This should not be implemented manually. Instead, use
145/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
146#[rustc_diagnostic_item = "Subdiagnostic"]
147pub trait Subdiagnostic {
148    /// Add a subdiagnostic to an existing diagnostic.
149    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>);
150}
151
152#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagLocation {
    #[inline]
    fn clone(&self) -> DiagLocation {
        DiagLocation {
            file: ::core::clone::Clone::clone(&self.file),
            line: ::core::clone::Clone::clone(&self.line),
            col: ::core::clone::Clone::clone(&self.col),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DiagLocation {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "DiagLocation",
            "file", &self.file, "line", &self.line, "col", &&self.col)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DiagLocation {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DiagLocation {
                        file: ref __binding_0,
                        line: ref __binding_1,
                        col: ref __binding_2 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for DiagLocation {
            fn decode(__decoder: &mut __D) -> Self {
                DiagLocation {
                    file: ::rustc_serialize::Decodable::decode(__decoder),
                    line: ::rustc_serialize::Decodable::decode(__decoder),
                    col: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
153pub struct DiagLocation {
154    file: Cow<'static, str>,
155    line: u32,
156    col: u32,
157}
158
159impl DiagLocation {
160    #[track_caller]
161    pub fn caller() -> Self {
162        let loc = panic::Location::caller();
163        DiagLocation { file: loc.file().into(), line: loc.line(), col: loc.column() }
164    }
165}
166
167impl fmt::Display for DiagLocation {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        f.write_fmt(format_args!("{0}:{1}:{2}", self.file, self.line, self.col))write!(f, "{}:{}:{}", self.file, self.line, self.col)
170    }
171}
172
173#[derive(#[automatically_derived]
impl ::core::clone::Clone for IsLint {
    #[inline]
    fn clone(&self) -> IsLint {
        IsLint {
            name: ::core::clone::Clone::clone(&self.name),
            has_future_breakage: ::core::clone::Clone::clone(&self.has_future_breakage),
            rust_version: ::core::clone::Clone::clone(&self.rust_version),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for IsLint {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "IsLint",
            "name", &self.name, "has_future_breakage",
            &self.has_future_breakage, "rust_version", &&self.rust_version)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for IsLint {
    #[inline]
    fn eq(&self, other: &IsLint) -> bool {
        self.has_future_breakage == other.has_future_breakage &&
                self.name == other.name &&
            self.rust_version == other.rust_version
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for IsLint {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<String>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
        let _: ::core::cmp::AssertParamIsEq<Option<RustcVersion>>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for IsLint {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.name, state);
        ::core::hash::Hash::hash(&self.has_future_breakage, state);
        ::core::hash::Hash::hash(&self.rust_version, state)
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for IsLint {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    IsLint {
                        name: ref __binding_0,
                        has_future_breakage: ref __binding_1,
                        rust_version: ref __binding_2 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for IsLint {
            fn decode(__decoder: &mut __D) -> Self {
                IsLint {
                    name: ::rustc_serialize::Decodable::decode(__decoder),
                    has_future_breakage: ::rustc_serialize::Decodable::decode(__decoder),
                    rust_version: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
174pub struct IsLint {
175    /// The lint name.
176    pub(crate) name: String,
177    /// Indicates whether this lint should show up in cargo's future breakage report.
178    has_future_breakage: bool,
179    /// Indicates the minimum rust version this lint applies to
180    rust_version: Option<RustcVersion>,
181}
182
183#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DiagStyledString {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "DiagStyledString", &&self.0)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for DiagStyledString {
    #[inline]
    fn eq(&self, other: &DiagStyledString) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DiagStyledString {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Vec<StringPart>>;
    }
}Eq)]
184pub struct DiagStyledString(pub Vec<StringPart>);
185
186impl DiagStyledString {
187    pub fn new() -> DiagStyledString {
188        DiagStyledString(::alloc::vec::Vec::new()vec![])
189    }
190    pub fn push_normal<S: Into<String>>(&mut self, t: S) {
191        self.0.push(StringPart::normal(t));
192    }
193    pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
194        self.0.push(StringPart::highlighted(t));
195    }
196    pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
197        if highlight {
198            self.push_highlighted(t);
199        } else {
200            self.push_normal(t);
201        }
202    }
203    pub fn normal<S: Into<String>>(t: S) -> DiagStyledString {
204        DiagStyledString(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [StringPart::normal(t)]))vec![StringPart::normal(t)])
205    }
206
207    pub fn highlighted<S: Into<String>>(t: S) -> DiagStyledString {
208        DiagStyledString(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [StringPart::highlighted(t)]))vec![StringPart::highlighted(t)])
209    }
210
211    pub fn content(&self) -> String {
212        self.0.iter().map(|x| x.content.as_str()).collect::<String>()
213    }
214}
215
216#[derive(#[automatically_derived]
impl ::core::fmt::Debug for StringPart {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "StringPart",
            "content", &self.content, "style", &&self.style)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for StringPart {
    #[inline]
    fn eq(&self, other: &StringPart) -> bool {
        self.content == other.content && self.style == other.style
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for StringPart {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<String>;
        let _: ::core::cmp::AssertParamIsEq<Style>;
    }
}Eq)]
217pub struct StringPart {
218    content: String,
219    style: Style,
220}
221
222impl StringPart {
223    pub fn normal<S: Into<String>>(content: S) -> StringPart {
224        StringPart { content: content.into(), style: Style::NoStyle }
225    }
226
227    pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
228        StringPart { content: content.into(), style: Style::Highlight }
229    }
230}
231
232/// The main part of a diagnostic. Note that `Diag`, which wraps this type, is
233/// used for most operations, and should be used instead whenever possible.
234/// This type should only be used when `Diag`'s lifetime causes difficulties,
235/// e.g. when storing diagnostics within `DiagCtxt`.
236#[must_use]
237#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagInner {
    #[inline]
    fn clone(&self) -> DiagInner {
        DiagInner {
            level: ::core::clone::Clone::clone(&self.level),
            messages: ::core::clone::Clone::clone(&self.messages),
            code: ::core::clone::Clone::clone(&self.code),
            lint_id: ::core::clone::Clone::clone(&self.lint_id),
            span: ::core::clone::Clone::clone(&self.span),
            children: ::core::clone::Clone::clone(&self.children),
            suggestions: ::core::clone::Clone::clone(&self.suggestions),
            args: ::core::clone::Clone::clone(&self.args),
            sort_span: ::core::clone::Clone::clone(&self.sort_span),
            is_lint: ::core::clone::Clone::clone(&self.is_lint),
            long_ty_path: ::core::clone::Clone::clone(&self.long_ty_path),
            emitted_at: ::core::clone::Clone::clone(&self.emitted_at),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DiagInner {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["level", "messages", "code", "lint_id", "span", "children",
                        "suggestions", "args", "sort_span", "is_lint",
                        "long_ty_path", "emitted_at"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.level, &self.messages, &self.code, &self.lint_id,
                        &self.span, &self.children, &self.suggestions, &self.args,
                        &self.sort_span, &self.is_lint, &self.long_ty_path,
                        &&self.emitted_at];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "DiagInner",
            names, values)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DiagInner {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DiagInner {
                        level: ref __binding_0,
                        messages: ref __binding_1,
                        code: ref __binding_2,
                        lint_id: ref __binding_3,
                        span: ref __binding_4,
                        children: ref __binding_5,
                        suggestions: ref __binding_6,
                        args: ref __binding_7,
                        sort_span: ref __binding_8,
                        is_lint: ref __binding_9,
                        long_ty_path: ref __binding_10,
                        emitted_at: ref __binding_11 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_3,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_4,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_5,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_6,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_7,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_8,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_9,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_10,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_11,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for DiagInner {
            fn decode(__decoder: &mut __D) -> Self {
                DiagInner {
                    level: ::rustc_serialize::Decodable::decode(__decoder),
                    messages: ::rustc_serialize::Decodable::decode(__decoder),
                    code: ::rustc_serialize::Decodable::decode(__decoder),
                    lint_id: ::rustc_serialize::Decodable::decode(__decoder),
                    span: ::rustc_serialize::Decodable::decode(__decoder),
                    children: ::rustc_serialize::Decodable::decode(__decoder),
                    suggestions: ::rustc_serialize::Decodable::decode(__decoder),
                    args: ::rustc_serialize::Decodable::decode(__decoder),
                    sort_span: ::rustc_serialize::Decodable::decode(__decoder),
                    is_lint: ::rustc_serialize::Decodable::decode(__decoder),
                    long_ty_path: ::rustc_serialize::Decodable::decode(__decoder),
                    emitted_at: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
238pub struct DiagInner {
239    // NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
240    // outside of what methods in this crate themselves allow.
241    pub(crate) level: Level,
242
243    pub messages: Vec<(DiagMessage, Style)>,
244    pub code: Option<ErrCode>,
245    pub lint_id: Option<LintExpectationId>,
246    pub span: MultiSpan,
247    pub children: Vec<Subdiag>,
248    pub suggestions: Suggestions,
249    pub args: DiagArgMap,
250
251    /// This is not used for highlighting or rendering any error message. Rather, it can be used
252    /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
253    /// `span` if there is one. Otherwise, it is `DUMMY_SP`.
254    pub sort_span: Span,
255
256    pub is_lint: Option<IsLint>,
257
258    pub long_ty_path: Option<PathBuf>,
259    /// With `-Ztrack_diagnostics` enabled,
260    /// we print where in rustc this error was emitted.
261    pub emitted_at: DiagLocation,
262}
263
264impl DiagInner {
265    #[track_caller]
266    pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
267        DiagInner::new_with_messages(level, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(message.into(), Style::NoStyle)]))vec![(message.into(), Style::NoStyle)])
268    }
269
270    #[track_caller]
271    pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
272        DiagInner {
273            level,
274            lint_id: None,
275            messages,
276            code: None,
277            span: MultiSpan::new(),
278            children: ::alloc::vec::Vec::new()vec![],
279            suggestions: Suggestions::Enabled(::alloc::vec::Vec::new()vec![]),
280            args: Default::default(),
281            sort_span: DUMMY_SP,
282            is_lint: None,
283            long_ty_path: None,
284            emitted_at: DiagLocation::caller(),
285        }
286    }
287
288    #[inline(always)]
289    pub fn level(&self) -> Level {
290        self.level
291    }
292
293    pub fn is_error(&self) -> bool {
294        match self.level {
295            Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
296
297            Level::ForceWarning
298            | Level::Warning
299            | Level::Note
300            | Level::OnceNote
301            | Level::Help
302            | Level::OnceHelp
303            | Level::FailureNote
304            | Level::Allow
305            | Level::Expect => false,
306        }
307    }
308
309    /// Indicates whether this diagnostic should show up in cargo's future breakage report.
310    pub(crate) fn has_future_breakage(&self) -> bool {
311        #[allow(non_exhaustive_omitted_patterns)] match self.is_lint {
    Some(IsLint { has_future_breakage: true, .. }) => true,
    _ => false,
}matches!(self.is_lint, Some(IsLint { has_future_breakage: true, .. }))
312    }
313
314    /// Indicates the minimum rust version this lint applies to.
315    pub(crate) fn rust_version(&self) -> Option<RustcVersion> {
316        self.is_lint.as_ref().and_then(|is| is.rust_version)
317    }
318
319    pub(crate) fn is_force_warn(&self) -> bool {
320        match self.level {
321            Level::ForceWarning => {
322                if !self.is_lint.is_some() {
    ::core::panicking::panic("assertion failed: self.is_lint.is_some()")
};assert!(self.is_lint.is_some());
323                true
324            }
325            _ => false,
326        }
327    }
328
329    pub(crate) fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
330        let sub = Subdiag { level, messages: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(message.into(), Style::NoStyle)]))vec![(message.into(), Style::NoStyle)], span };
331        self.children.push(sub);
332    }
333
334    pub(crate) fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg) {
335        let name = name.into();
336        let value = arg.into_diag_arg(&mut self.long_ty_path);
337        // This assertion is to avoid subdiagnostics overwriting an existing diagnostic arg.
338        if true {
    if !(!self.args.contains_key(&name) ||
                self.args.get(&name) == Some(&value)) {
        {
            ::core::panicking::panic_fmt(format_args!("arg {0} already exists",
                    name));
        }
    };
};debug_assert!(
339            !self.args.contains_key(&name) || self.args.get(&name) == Some(&value),
340            "arg {} already exists",
341            name
342        );
343        self.args.insert(name, value);
344    }
345
346    pub fn remove_arg(&mut self, name: &str) {
347        self.args.swap_remove(name);
348    }
349
350    pub fn emitted_at_sub_diag(&self) -> Subdiag {
351        let track = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("-Ztrack-diagnostics: created at {0}",
                self.emitted_at))
    })format!("-Ztrack-diagnostics: created at {}", self.emitted_at);
352        Subdiag {
353            level: crate::Level::Note,
354            messages: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)]))vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
355            span: MultiSpan::new(),
356        }
357    }
358
359    /// Fields used for Hash, and PartialEq trait.
360    fn keys(
361        &self,
362    ) -> (
363        &Level,
364        &[(DiagMessage, Style)],
365        &Option<ErrCode>,
366        &MultiSpan,
367        &[Subdiag],
368        &Suggestions,
369        Vec<(&DiagArgName, &DiagArgValue)>,
370        &Option<IsLint>,
371    ) {
372        (
373            &self.level,
374            &self.messages,
375            &self.code,
376            &self.span,
377            &self.children,
378            &self.suggestions,
379            self.args.iter().collect(),
380            // omit self.sort_span
381            &self.is_lint,
382            // omit self.emitted_at
383        )
384    }
385}
386
387impl Hash for DiagInner {
388    fn hash<H>(&self, state: &mut H)
389    where
390        H: Hasher,
391    {
392        self.keys().hash(state);
393    }
394}
395
396impl PartialEq for DiagInner {
397    fn eq(&self, other: &Self) -> bool {
398        self.keys() == other.keys()
399    }
400}
401
402/// A "sub"-diagnostic attached to a parent diagnostic.
403/// For example, a note attached to an error.
404#[derive(#[automatically_derived]
impl ::core::clone::Clone for Subdiag {
    #[inline]
    fn clone(&self) -> Subdiag {
        Subdiag {
            level: ::core::clone::Clone::clone(&self.level),
            messages: ::core::clone::Clone::clone(&self.messages),
            span: ::core::clone::Clone::clone(&self.span),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Subdiag {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "Subdiag",
            "level", &self.level, "messages", &self.messages, "span",
            &&self.span)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Subdiag {
    #[inline]
    fn eq(&self, other: &Subdiag) -> bool {
        self.level == other.level && self.messages == other.messages &&
            self.span == other.span
    }
}PartialEq, #[automatically_derived]
impl ::core::hash::Hash for Subdiag {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.level, state);
        ::core::hash::Hash::hash(&self.messages, state);
        ::core::hash::Hash::hash(&self.span, state)
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for Subdiag {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Subdiag {
                        level: ref __binding_0,
                        messages: ref __binding_1,
                        span: ref __binding_2 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for Subdiag {
            fn decode(__decoder: &mut __D) -> Self {
                Subdiag {
                    level: ::rustc_serialize::Decodable::decode(__decoder),
                    messages: ::rustc_serialize::Decodable::decode(__decoder),
                    span: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
405pub struct Subdiag {
406    pub level: Level,
407    pub messages: Vec<(DiagMessage, Style)>,
408    pub span: MultiSpan,
409}
410
411/// Used for emitting structured error messages and other diagnostic information.
412/// Wraps a `DiagInner`, adding some useful things.
413/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
414///   that it has been emitted or cancelled.
415/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
416///
417/// Each constructed `Diag` must be consumed by a function such as `emit`,
418/// `cancel`, `delay_as_bug`, or `into_diag`. A panic occurs if a `Diag`
419/// is dropped without being consumed by one of these functions.
420///
421/// If there is some state in a downstream crate you would like to access in
422/// the methods of `Diag` here, consider extending `DiagCtxtFlags`.
423#[must_use]
424pub struct Diag<'a, G: EmissionGuarantee = ErrorGuaranteed> {
425    pub dcx: DiagCtxtHandle<'a>,
426
427    /// Why the `Option`? It is always `Some` until the `Diag` is consumed via
428    /// `emit`, `cancel`, etc. At that point it is consumed and replaced with
429    /// `None`. Then `drop` checks that it is `None`; if not, it panics because
430    /// a diagnostic was built but not used.
431    ///
432    /// Why the Box? `DiagInner` is a large type, and `Diag` is often used as a
433    /// return value, especially within the frequently-used `PResult` type. In
434    /// theory, return value optimization (RVO) should avoid unnecessary
435    /// copying. In practice, it does not (at the time of writing).
436    diag: Option<Box<DiagInner>>,
437
438    _marker: PhantomData<G>,
439}
440
441// Cloning a `Diag` is a recipe for a diagnostic being emitted twice, which
442// would be bad.
443impl<G> !Clone for Diag<'_, G> {}
444
445const _: [(); 3 * size_of::<usize>()] =
    [(); ::std::mem::size_of::<Diag<'_, ()>>()];rustc_data_structures::static_assert_size!(Diag<'_, ()>, 3 * size_of::<usize>());
446
447impl<G: EmissionGuarantee> Deref for Diag<'_, G> {
448    type Target = DiagInner;
449
450    fn deref(&self) -> &DiagInner {
451        self.diag.as_ref().unwrap()
452    }
453}
454
455impl<G: EmissionGuarantee> DerefMut for Diag<'_, G> {
456    fn deref_mut(&mut self) -> &mut DiagInner {
457        self.diag.as_mut().unwrap()
458    }
459}
460
461impl<G: EmissionGuarantee> Debug for Diag<'_, G> {
462    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
463        self.diag.fmt(f)
464    }
465}
466
467/// `Diag` impls many `&mut self -> &mut Self` methods. Each one modifies an
468/// existing diagnostic, either in a standalone fashion, e.g.
469/// `err.code(code);`, or in a chained fashion to make multiple modifications,
470/// e.g. `err.code(code).span(span);`.
471///
472/// This macro creates an equivalent `self -> Self` method, with a `with_`
473/// prefix. This can be used in a chained fashion when making a new diagnostic,
474/// e.g. `let err = struct_err(msg).with_code(code);`, or emitting a new
475/// diagnostic, e.g. `struct_err(msg).with_code(code).emit();`.
476///
477/// Although the latter method can be used to modify an existing diagnostic,
478/// e.g. `err = err.with_code(code);`, this should be avoided because the former
479/// method gives shorter code, e.g. `err.code(code);`.
480///
481/// Note: the `with_` methods are added only when needed. If you want to use
482/// one and it's not defined, feel free to add it.
483///
484/// Note: any doc comments must be within the `with_fn!` call.
485macro_rules! with_fn {
486    {
487        $with_f:ident,
488        $(#[$attrs:meta])*
489        pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
490            $($body:tt)*
491        }
492    } => {
493        // The original function.
494        $(#[$attrs])*
495        #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
496        pub fn $f(&mut $self, $($name: $ty),*) -> &mut Self {
497            $($body)*
498        }
499
500        // The `with_*` variant.
501        $(#[$attrs])*
502        #[doc = concat!("See [`Diag::", stringify!($f), "()`].")]
503        pub fn $with_f(mut $self, $($name: $ty),*) -> Self {
504            $self.$f($($name),*);
505            $self
506        }
507    };
508}
509
510impl<'a, G: EmissionGuarantee> Diag<'a, G> {
511    #[track_caller]
512    pub fn new(dcx: DiagCtxtHandle<'a>, level: Level, message: impl Into<DiagMessage>) -> Self {
513        Self::new_diagnostic(dcx, DiagInner::new(level, message))
514    }
515
516    /// Allow moving diagnostics between different error tainting contexts
517    pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_, G> {
518        Diag { dcx, diag: self.diag.take(), _marker: PhantomData }
519    }
520
521    /// Creates a new `Diag` with an already constructed diagnostic.
522    #[track_caller]
523    pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {
524        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_errors/src/diagnostic.rs:524",
                        "rustc_errors::diagnostic", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_errors/src/diagnostic.rs"),
                        ::tracing_core::__macro_support::Option::Some(524u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_errors::diagnostic"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("Created new diagnostic")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("Created new diagnostic");
525        Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
526    }
527
528    /// Delay emission of this diagnostic as a bug.
529    ///
530    /// This can be useful in contexts where an error indicates a bug but
531    /// typically this only happens when other compilation errors have already
532    /// happened. In those cases this can be used to defer emission of this
533    /// diagnostic as a bug in the compiler only if no other errors have been
534    /// emitted.
535    ///
536    /// In the meantime, though, callsites are required to deal with the "bug"
537    /// locally in whichever way makes the most sense.
538    #[track_caller]
539    pub fn downgrade_to_delayed_bug(&mut self) {
540        if !#[allow(non_exhaustive_omitted_patterns)] match self.level {
            Level::Error | Level::DelayedBug => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("downgrade_to_delayed_bug: cannot downgrade {0:?} to DelayedBug: not an error",
                self.level));
    }
};assert!(
541            matches!(self.level, Level::Error | Level::DelayedBug),
542            "downgrade_to_delayed_bug: cannot downgrade {:?} to DelayedBug: not an error",
543            self.level
544        );
545        self.level = Level::DelayedBug;
546    }
547
548    /// Make emitting this diagnostic fatal
549    ///
550    /// Changes the level of this diagnostic to Fatal, and importantly also changes the emission guarantee.
551    /// This is sound for errors that would otherwise be printed, but now simply exit the process instead.
552    /// This function still gives an emission guarantee, the guarantee is now just that it exits fatally.
553    /// For delayed bugs this is different, since those are buffered. If we upgrade one to fatal, another
554    /// might now be ignored.
555    #[track_caller]
556    pub fn upgrade_to_fatal(mut self) -> Diag<'a, FatalAbort> {
557        if !#[allow(non_exhaustive_omitted_patterns)] match self.level {
            Level::Error => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("upgrade_to_fatal: cannot upgrade {0:?} to Fatal: not an error",
                self.level));
    }
};assert!(
558            matches!(self.level, Level::Error),
559            "upgrade_to_fatal: cannot upgrade {:?} to Fatal: not an error",
560            self.level
561        );
562        self.level = Level::Fatal;
563
564        // Take is okay since we immediately rewrap it in another diagnostic.
565        // i.e. we do emit it despite defusing the original diagnostic's drop bomb.
566        let diag = self.diag.take();
567        Diag { dcx: self.dcx, diag, _marker: PhantomData }
568    }
569
570    #[doc = r" Appends a labeled span to the diagnostic."]
#[doc = r""]
#[doc =
r" Labels are used to convey additional context for the diagnostic's primary span. They will"]
#[doc =
r" be shown together with the original diagnostic's span, *not* with spans added by"]
#[doc =
r" `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because"]
#[doc =
r" the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed"]
#[doc = r" either."]
#[doc = r""]
#[doc =
r" Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when"]
#[doc =
r" the diagnostic was constructed. However, the label span is *not* considered a"]
#[doc =
r#" ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is"#]
#[doc = r" primary."]
#[doc = "See [`Diag::span_label()`]."]
pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>)
    -> &mut Self {
    self.span.push_span_label(span, label.into());
    self
}
#[doc = r" Appends a labeled span to the diagnostic."]
#[doc = r""]
#[doc =
r" Labels are used to convey additional context for the diagnostic's primary span. They will"]
#[doc =
r" be shown together with the original diagnostic's span, *not* with spans added by"]
#[doc =
r" `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because"]
#[doc =
r" the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed"]
#[doc = r" either."]
#[doc = r""]
#[doc =
r" Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when"]
#[doc =
r" the diagnostic was constructed. However, the label span is *not* considered a"]
#[doc =
r#" ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is"#]
#[doc = r" primary."]
#[doc = "See [`Diag::span_label()`]."]
pub fn with_span_label(mut self, span: Span, label: impl Into<DiagMessage>)
    -> Self {
    self.span_label(span, label);
    self
}with_fn! { with_span_label,
571    /// Appends a labeled span to the diagnostic.
572    ///
573    /// Labels are used to convey additional context for the diagnostic's primary span. They will
574    /// be shown together with the original diagnostic's span, *not* with spans added by
575    /// `span_note`, `span_help`, etc. Therefore, if the primary span is not displayable (because
576    /// the span is `DUMMY_SP` or the source code isn't found), labels will not be displayed
577    /// either.
578    ///
579    /// Implementation-wise, the label span is pushed onto the [`MultiSpan`] that was created when
580    /// the diagnostic was constructed. However, the label span is *not* considered a
581    /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
582    /// primary.
583    pub fn span_label(&mut self, span: Span, label: impl Into<DiagMessage>) -> &mut Self {
584        self.span.push_span_label(span, label.into());
585        self
586    } }
587
588    #[doc = r" Labels all the given spans with the provided label."]
#[doc = r" See [`Self::span_label()`] for more information."]
#[doc = "See [`Diag::span_labels()`]."]
pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>,
    label: &str) -> &mut Self {
    for span in spans { self.span_label(span, label.to_string()); }
    self
}
#[doc = r" Labels all the given spans with the provided label."]
#[doc = r" See [`Self::span_label()`] for more information."]
#[doc = "See [`Diag::span_labels()`]."]
pub fn with_span_labels(mut self, spans: impl IntoIterator<Item = Span>,
    label: &str) -> Self {
    self.span_labels(spans, label);
    self
}with_fn! { with_span_labels,
589    /// Labels all the given spans with the provided label.
590    /// See [`Self::span_label()`] for more information.
591    pub fn span_labels(&mut self, spans: impl IntoIterator<Item = Span>, label: &str) -> &mut Self {
592        for span in spans {
593            self.span_label(span, label.to_string());
594        }
595        self
596    } }
597
598    pub fn replace_span_with(&mut self, after: Span, keep_label: bool) -> &mut Self {
599        let before = self.span.clone();
600        self.span(after);
601        for span_label in before.span_labels() {
602            if let Some(label) = span_label.label {
603                if span_label.is_primary && keep_label {
604                    self.span.push_span_label(after, label);
605                } else {
606                    self.span.push_span_label(span_label.span, label);
607                }
608            }
609        }
610        self
611    }
612
613    pub fn note_expected_found(
614        &mut self,
615        expected_label: &str,
616        expected: DiagStyledString,
617        found_label: &str,
618        found: DiagStyledString,
619    ) -> &mut Self {
620        self.note_expected_found_extra(
621            expected_label,
622            expected,
623            found_label,
624            found,
625            DiagStyledString::normal(""),
626            DiagStyledString::normal(""),
627        )
628    }
629
630    pub fn note_expected_found_extra(
631        &mut self,
632        expected_label: &str,
633        expected: DiagStyledString,
634        found_label: &str,
635        found: DiagStyledString,
636        expected_extra: DiagStyledString,
637        found_extra: DiagStyledString,
638    ) -> &mut Self {
639        let expected_label = expected_label.to_string();
640        let expected_label = if expected_label.is_empty() {
641            "expected".to_string()
642        } else {
643            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}", expected_label))
    })format!("expected {expected_label}")
644        };
645        let found_label = found_label.to_string();
646        let found_label = if found_label.is_empty() {
647            "found".to_string()
648        } else {
649            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("found {0}", found_label))
    })format!("found {found_label}")
650        };
651        let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
652            (expected_label.len() - found_label.len(), 0)
653        } else {
654            (0, found_label.len() - expected_label.len())
655        };
656        let mut msg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [StringPart::normal(::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0}{1} `",
                                    " ".repeat(expected_padding), expected_label))
                        }))]))vec![StringPart::normal(format!(
657            "{}{} `",
658            " ".repeat(expected_padding),
659            expected_label
660        ))];
661        msg.extend(expected.0);
662        msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
663        msg.extend(expected_extra.0);
664        msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("\n")) })format!("\n")));
665        msg.push(StringPart::normal(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1} `",
                " ".repeat(found_padding), found_label))
    })format!("{}{} `", " ".repeat(found_padding), found_label)));
666        msg.extend(found.0);
667        msg.push(StringPart::normal(::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("`")) })format!("`")));
668        msg.extend(found_extra.0);
669
670        // For now, just attach these as notes.
671        self.highlighted_note(msg);
672        self
673    }
674
675    pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
676        self.highlighted_note(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [StringPart::normal(::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` from trait: `",
                                    name))
                        })), StringPart::highlighted(signature),
                StringPart::normal("`")]))vec![
677            StringPart::normal(format!("`{name}` from trait: `")),
678            StringPart::highlighted(signature),
679            StringPart::normal("`"),
680        ]);
681        self
682    }
683
684    #[doc = r" Add a note attached to this diagnostic."]
#[doc = "See [`Diag::note()`]."]
pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
    self.sub(Level::Note, msg, MultiSpan::new());
    self
}
#[doc = r" Add a note attached to this diagnostic."]
#[doc = "See [`Diag::note()`]."]
pub fn with_note(mut self, msg: impl Into<DiagMessage>) -> Self {
    self.note(msg);
    self
}with_fn! { with_note,
685    /// Add a note attached to this diagnostic.
686    pub fn note(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
687        self.sub(Level::Note, msg, MultiSpan::new());
688        self
689    } }
690
691    pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
692        self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
693        self
694    }
695
696    pub fn highlighted_span_note(
697        &mut self,
698        span: impl Into<MultiSpan>,
699        msg: Vec<StringPart>,
700    ) -> &mut Self {
701        self.sub_with_highlights(Level::Note, msg, span.into());
702        self
703    }
704
705    /// This is like [`Diag::note()`], but it's only printed once.
706    pub fn note_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
707        self.sub(Level::OnceNote, msg, MultiSpan::new());
708        self
709    }
710
711    #[doc = r" Prints the span with a note above it."]
#[doc = r" This is like [`Diag::note()`], but it gets its own span."]
#[doc = "See [`Diag::span_note()`]."]
pub fn span_note(&mut self, sp: impl Into<MultiSpan>,
    msg: impl Into<DiagMessage>) -> &mut Self {
    self.sub(Level::Note, msg, sp.into());
    self
}
#[doc = r" Prints the span with a note above it."]
#[doc = r" This is like [`Diag::note()`], but it gets its own span."]
#[doc = "See [`Diag::span_note()`]."]
pub fn with_span_note(mut self, sp: impl Into<MultiSpan>,
    msg: impl Into<DiagMessage>) -> Self {
    self.span_note(sp, msg);
    self
}with_fn! { with_span_note,
712    /// Prints the span with a note above it.
713    /// This is like [`Diag::note()`], but it gets its own span.
714    pub fn span_note(
715        &mut self,
716        sp: impl Into<MultiSpan>,
717        msg: impl Into<DiagMessage>,
718    ) -> &mut Self {
719        self.sub(Level::Note, msg, sp.into());
720        self
721    } }
722
723    /// Prints the span with a note above it.
724    /// This is like [`Diag::note_once()`], but it gets its own span.
725    pub fn span_note_once<S: Into<MultiSpan>>(
726        &mut self,
727        sp: S,
728        msg: impl Into<DiagMessage>,
729    ) -> &mut Self {
730        self.sub(Level::OnceNote, msg, sp.into());
731        self
732    }
733
734    #[doc = r" Add a warning attached to this diagnostic."]
#[doc = "See [`Diag::warn()`]."]
pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
    self.sub(Level::Warning, msg, MultiSpan::new());
    self
}
#[doc = r" Add a warning attached to this diagnostic."]
#[doc = "See [`Diag::warn()`]."]
pub fn with_warn(mut self, msg: impl Into<DiagMessage>) -> Self {
    self.warn(msg);
    self
}with_fn! { with_warn,
735    /// Add a warning attached to this diagnostic.
736    pub fn warn(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
737        self.sub(Level::Warning, msg, MultiSpan::new());
738        self
739    } }
740
741    /// Prints the span with a warning above it.
742    /// This is like [`Diag::warn()`], but it gets its own span.
743    pub fn span_warn<S: Into<MultiSpan>>(
744        &mut self,
745        sp: S,
746        msg: impl Into<DiagMessage>,
747    ) -> &mut Self {
748        self.sub(Level::Warning, msg, sp.into());
749        self
750    }
751
752    #[doc = r" Add a help message attached to this diagnostic."]
#[doc = "See [`Diag::help()`]."]
pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
    self.sub(Level::Help, msg, MultiSpan::new());
    self
}
#[doc = r" Add a help message attached to this diagnostic."]
#[doc = "See [`Diag::help()`]."]
pub fn with_help(mut self, msg: impl Into<DiagMessage>) -> Self {
    self.help(msg);
    self
}with_fn! { with_help,
753    /// Add a help message attached to this diagnostic.
754    pub fn help(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
755        self.sub(Level::Help, msg, MultiSpan::new());
756        self
757    } }
758
759    /// This is like [`Diag::help()`], but it's only printed once.
760    pub fn help_once(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
761        self.sub(Level::OnceHelp, msg, MultiSpan::new());
762        self
763    }
764
765    /// Add a help message attached to this diagnostic with a customizable highlighted message.
766    pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
767        self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
768        self
769    }
770
771    /// Add a help message attached to this diagnostic with a customizable highlighted message.
772    pub fn highlighted_span_help(
773        &mut self,
774        span: impl Into<MultiSpan>,
775        msg: Vec<StringPart>,
776    ) -> &mut Self {
777        self.sub_with_highlights(Level::Help, msg, span.into());
778        self
779    }
780
781    #[doc = r" Prints the span with some help above it."]
#[doc = r" This is like [`Diag::help()`], but it gets its own span."]
#[doc = "See [`Diag::span_help()`]."]
pub fn span_help(&mut self, sp: impl Into<MultiSpan>,
    msg: impl Into<DiagMessage>) -> &mut Self {
    self.sub(Level::Help, msg, sp.into());
    self
}
#[doc = r" Prints the span with some help above it."]
#[doc = r" This is like [`Diag::help()`], but it gets its own span."]
#[doc = "See [`Diag::span_help()`]."]
pub fn with_span_help(mut self, sp: impl Into<MultiSpan>,
    msg: impl Into<DiagMessage>) -> Self {
    self.span_help(sp, msg);
    self
}with_fn! { with_span_help,
782    /// Prints the span with some help above it.
783    /// This is like [`Diag::help()`], but it gets its own span.
784    pub fn span_help(
785        &mut self,
786        sp: impl Into<MultiSpan>,
787        msg: impl Into<DiagMessage>,
788    ) -> &mut Self {
789        self.sub(Level::Help, msg, sp.into());
790        self
791    } }
792
793    /// Disallow attaching suggestions to this diagnostic.
794    /// Any suggestions attached e.g. with the `span_suggestion_*` methods
795    /// (before and after the call to `disable_suggestions`) will be ignored.
796    pub fn disable_suggestions(&mut self) -> &mut Self {
797        self.suggestions = Suggestions::Disabled;
798        self
799    }
800
801    /// Prevent new suggestions from being added to this diagnostic.
802    ///
803    /// Suggestions added before the call to `.seal_suggestions()` will be preserved
804    /// and new suggestions will be ignored.
805    pub fn seal_suggestions(&mut self) -> &mut Self {
806        if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
807            let suggestions_slice = std::mem::take(suggestions).into_boxed_slice();
808            self.suggestions = Suggestions::Sealed(suggestions_slice);
809        }
810        self
811    }
812
813    /// Helper for pushing to `self.suggestions`.
814    ///
815    /// A new suggestion is added if suggestions are enabled for this diagnostic.
816    /// Otherwise, they are ignored.
817    fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
818        for subst in &suggestion.substitutions {
819            for part in &subst.parts {
820                let span = part.span;
821                let call_site = span.ctxt().outer_expn_data().call_site;
822                if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
823                    // Ignore if spans is from derive macro.
824                    return;
825                }
826            }
827        }
828
829        if let Suggestions::Enabled(suggestions) = &mut self.suggestions {
830            suggestions.push(suggestion);
831        }
832    }
833
834    #[doc =
r" Show a suggestion that has multiple parts to it, always as its own subdiagnostic."]
#[doc =
r" In other words, multiple changes need to be applied as part of this suggestion."]
#[doc = "See [`Diag::multipart_suggestion()`]."]
pub fn multipart_suggestion(&mut self, msg: impl Into<DiagMessage>,
    suggestion: Vec<(Span, String)>, applicability: Applicability)
    -> &mut Self {
    self.multipart_suggestion_with_style(msg, suggestion, applicability,
        SuggestionStyle::ShowAlways)
}
#[doc =
r" Show a suggestion that has multiple parts to it, always as its own subdiagnostic."]
#[doc =
r" In other words, multiple changes need to be applied as part of this suggestion."]
#[doc = "See [`Diag::multipart_suggestion()`]."]
pub fn with_multipart_suggestion(mut self, msg: impl Into<DiagMessage>,
    suggestion: Vec<(Span, String)>, applicability: Applicability) -> Self {
    self.multipart_suggestion(msg, suggestion, applicability);
    self
}with_fn! { with_multipart_suggestion,
835    /// Show a suggestion that has multiple parts to it, always as its own subdiagnostic.
836    /// In other words, multiple changes need to be applied as part of this suggestion.
837    pub fn multipart_suggestion(
838        &mut self,
839        msg: impl Into<DiagMessage>,
840        suggestion: Vec<(Span, String)>,
841        applicability: Applicability,
842    ) -> &mut Self {
843        self.multipart_suggestion_with_style(
844            msg,
845            suggestion,
846            applicability,
847            SuggestionStyle::ShowAlways,
848        )
849    } }
850
851    /// [`Diag::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
852    pub fn multipart_suggestion_with_style(
853        &mut self,
854        msg: impl Into<DiagMessage>,
855        mut suggestion: Vec<(Span, String)>,
856        applicability: Applicability,
857        style: SuggestionStyle,
858    ) -> &mut Self {
859        let mut seen = crate::FxHashSet::default();
860        suggestion.retain(|(span, msg)| seen.insert((span.lo(), span.hi(), msg.clone())));
861
862        let parts = suggestion
863            .into_iter()
864            .map(|(span, snippet)| SubstitutionPart { snippet, span })
865            .collect::<Vec<_>>();
866
867        if !!parts.is_empty() {
    ::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
868        if true {
    match (&parts.iter().find(|part|
                        part.span.is_empty() && part.snippet.is_empty()), &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("Span must not be empty and have no suggestion")));
            }
        }
    };
};debug_assert_eq!(
869            parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
870            None,
871            "Span must not be empty and have no suggestion",
872        );
873        if true {
    match (&parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
            &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("suggestion must not have overlapping parts")));
            }
        }
    };
};debug_assert_eq!(
874            parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
875            None,
876            "suggestion must not have overlapping parts",
877        );
878
879        self.push_suggestion(CodeSuggestion {
880            substitutions: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Substitution { parts }]))vec![Substitution { parts }],
881            msg: msg.into(),
882            style,
883            applicability,
884        });
885        self
886    }
887
888    /// Prints out a message with for a multipart suggestion without showing the suggested code.
889    ///
890    /// This is intended to be used for suggestions that are obvious in what the changes need to
891    /// be from the message, showing the span label inline would be visually unpleasant
892    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
893    /// improve understandability.
894    pub fn tool_only_multipart_suggestion(
895        &mut self,
896        msg: impl Into<DiagMessage>,
897        suggestion: Vec<(Span, String)>,
898        applicability: Applicability,
899    ) -> &mut Self {
900        self.multipart_suggestion_with_style(
901            msg,
902            suggestion,
903            applicability,
904            SuggestionStyle::CompletelyHidden,
905        )
906    }
907
908    #[doc = r" Prints out a message with a suggested edit of the code."]
#[doc = r""]
#[doc =
r" In case of short messages and a simple suggestion, rustc displays it as a label:"]
#[doc = r""]
#[doc = r" ```text"]
#[doc = r" try adding parentheses: `(tup.0).1`"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" The message"]
#[doc = r""]
#[doc =
r" * should not end in any punctuation (a `:` is added automatically)"]
#[doc = r#" * should not be a question (avoid language like "did you mean")"#]
#[doc =
r#" * should not contain any phrases like "the following", "as shown", etc."#]
#[doc = r#" * may look like "to do xyz, use" or "to do xyz, use abc""#]
#[doc =
r" * may contain a name of a function, variable, or type, but not whole expressions"]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion()`]."]
pub fn span_suggestion(&mut self, sp: Span, msg: impl Into<DiagMessage>,
    suggestion: impl ToString, applicability: Applicability) -> &mut Self {
    self.span_suggestion_with_style(sp, msg, suggestion, applicability,
        SuggestionStyle::ShowCode);
    self
}
#[doc = r" Prints out a message with a suggested edit of the code."]
#[doc = r""]
#[doc =
r" In case of short messages and a simple suggestion, rustc displays it as a label:"]
#[doc = r""]
#[doc = r" ```text"]
#[doc = r" try adding parentheses: `(tup.0).1`"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" The message"]
#[doc = r""]
#[doc =
r" * should not end in any punctuation (a `:` is added automatically)"]
#[doc = r#" * should not be a question (avoid language like "did you mean")"#]
#[doc =
r#" * should not contain any phrases like "the following", "as shown", etc."#]
#[doc = r#" * may look like "to do xyz, use" or "to do xyz, use abc""#]
#[doc =
r" * may contain a name of a function, variable, or type, but not whole expressions"]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion()`]."]
pub fn with_span_suggestion(mut self, sp: Span, msg: impl Into<DiagMessage>,
    suggestion: impl ToString, applicability: Applicability) -> Self {
    self.span_suggestion(sp, msg, suggestion, applicability);
    self
}with_fn! { with_span_suggestion,
909    /// Prints out a message with a suggested edit of the code.
910    ///
911    /// In case of short messages and a simple suggestion, rustc displays it as a label:
912    ///
913    /// ```text
914    /// try adding parentheses: `(tup.0).1`
915    /// ```
916    ///
917    /// The message
918    ///
919    /// * should not end in any punctuation (a `:` is added automatically)
920    /// * should not be a question (avoid language like "did you mean")
921    /// * should not contain any phrases like "the following", "as shown", etc.
922    /// * may look like "to do xyz, use" or "to do xyz, use abc"
923    /// * may contain a name of a function, variable, or type, but not whole expressions
924    ///
925    /// See [`CodeSuggestion`] for more information.
926    pub fn span_suggestion(
927        &mut self,
928        sp: Span,
929        msg: impl Into<DiagMessage>,
930        suggestion: impl ToString,
931        applicability: Applicability,
932    ) -> &mut Self {
933        self.span_suggestion_with_style(
934            sp,
935            msg,
936            suggestion,
937            applicability,
938            SuggestionStyle::ShowCode,
939        );
940        self
941    } }
942
943    #[doc =
r" [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`]."]
#[doc = "See [`Diag::span_suggestion_with_style()`]."]
pub fn span_suggestion_with_style(&mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability, style: SuggestionStyle) -> &mut Self {
    if true {
        if !!(sp.is_empty() && suggestion.to_string().is_empty()) {
            {
                ::core::panicking::panic_fmt(format_args!("Span must not be empty and have no suggestion"));
            }
        };
    };
    self.push_suggestion(CodeSuggestion {
            substitutions: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [Substitution {
                                parts: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                        [SubstitutionPart {
                                                    snippet: suggestion.to_string(),
                                                    span: sp,
                                                }])),
                            }])),
            msg: msg.into(),
            style,
            applicability,
        });
    self
}
#[doc =
r" [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`]."]
#[doc = "See [`Diag::span_suggestion_with_style()`]."]
pub fn with_span_suggestion_with_style(mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability, style: SuggestionStyle) -> Self {
    self.span_suggestion_with_style(sp, msg, suggestion, applicability,
        style);
    self
}with_fn! { with_span_suggestion_with_style,
944    /// [`Diag::span_suggestion()`] but you can set the [`SuggestionStyle`].
945    pub fn span_suggestion_with_style(
946        &mut self,
947        sp: Span,
948        msg: impl Into<DiagMessage>,
949        suggestion: impl ToString,
950        applicability: Applicability,
951        style: SuggestionStyle,
952    ) -> &mut Self {
953        debug_assert!(
954            !(sp.is_empty() && suggestion.to_string().is_empty()),
955            "Span must not be empty and have no suggestion"
956        );
957        self.push_suggestion(CodeSuggestion {
958            substitutions: vec![Substitution {
959                parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
960            }],
961            msg: msg.into(),
962            style,
963            applicability,
964        });
965        self
966    } }
967
968    #[doc = r" Always show the suggested change."]
#[doc = "See [`Diag::span_suggestion_verbose()`]."]
pub fn span_suggestion_verbose(&mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability) -> &mut Self {
    self.span_suggestion_with_style(sp, msg, suggestion, applicability,
        SuggestionStyle::ShowAlways);
    self
}
#[doc = r" Always show the suggested change."]
#[doc = "See [`Diag::span_suggestion_verbose()`]."]
pub fn with_span_suggestion_verbose(mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability) -> Self {
    self.span_suggestion_verbose(sp, msg, suggestion, applicability);
    self
}with_fn! { with_span_suggestion_verbose,
969    /// Always show the suggested change.
970    pub fn span_suggestion_verbose(
971        &mut self,
972        sp: Span,
973        msg: impl Into<DiagMessage>,
974        suggestion: impl ToString,
975        applicability: Applicability,
976    ) -> &mut Self {
977        self.span_suggestion_with_style(
978            sp,
979            msg,
980            suggestion,
981            applicability,
982            SuggestionStyle::ShowAlways,
983        );
984        self
985    } }
986
987    #[doc = r" Prints out a message with multiple suggested edits of the code."]
#[doc = r" See also [`Diag::span_suggestion()`]."]
#[doc = "See [`Diag::span_suggestions()`]."]
pub fn span_suggestions(&mut self, sp: Span, msg: impl Into<DiagMessage>,
    suggestions: impl IntoIterator<Item = String>,
    applicability: Applicability) -> &mut Self {
    self.span_suggestions_with_style(sp, msg, suggestions, applicability,
        SuggestionStyle::ShowAlways)
}
#[doc = r" Prints out a message with multiple suggested edits of the code."]
#[doc = r" See also [`Diag::span_suggestion()`]."]
#[doc = "See [`Diag::span_suggestions()`]."]
pub fn with_span_suggestions(mut self, sp: Span, msg: impl Into<DiagMessage>,
    suggestions: impl IntoIterator<Item = String>,
    applicability: Applicability) -> Self {
    self.span_suggestions(sp, msg, suggestions, applicability);
    self
}with_fn! { with_span_suggestions,
988    /// Prints out a message with multiple suggested edits of the code.
989    /// See also [`Diag::span_suggestion()`].
990    pub fn span_suggestions(
991        &mut self,
992        sp: Span,
993        msg: impl Into<DiagMessage>,
994        suggestions: impl IntoIterator<Item = String>,
995        applicability: Applicability,
996    ) -> &mut Self {
997        self.span_suggestions_with_style(
998            sp,
999            msg,
1000            suggestions,
1001            applicability,
1002            SuggestionStyle::ShowAlways,
1003        )
1004    } }
1005
1006    pub fn span_suggestions_with_style(
1007        &mut self,
1008        sp: Span,
1009        msg: impl Into<DiagMessage>,
1010        suggestions: impl IntoIterator<Item = String>,
1011        applicability: Applicability,
1012        style: SuggestionStyle,
1013    ) -> &mut Self {
1014        let substitutions = suggestions
1015            .into_iter()
1016            .map(|snippet| {
1017                if true {
    if !!(sp.is_empty() && snippet.is_empty()) {
        {
            ::core::panicking::panic_fmt(format_args!("Span `{0:?}` must not be empty and have no suggestion",
                    sp));
        }
    };
};debug_assert!(
1018                    !(sp.is_empty() && snippet.is_empty()),
1019                    "Span `{sp:?}` must not be empty and have no suggestion"
1020                );
1021                Substitution { parts: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [SubstitutionPart { snippet, span: sp }]))vec![SubstitutionPart { snippet, span: sp }] }
1022            })
1023            .collect();
1024        self.push_suggestion(CodeSuggestion {
1025            substitutions,
1026            msg: msg.into(),
1027            style,
1028            applicability,
1029        });
1030        self
1031    }
1032
1033    /// Prints out a message with multiple suggested edits of the code, where each edit consists of
1034    /// multiple parts.
1035    /// See also [`Diag::multipart_suggestion()`].
1036    pub fn multipart_suggestions(
1037        &mut self,
1038        msg: impl Into<DiagMessage>,
1039        suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
1040        applicability: Applicability,
1041    ) -> &mut Self {
1042        let substitutions = suggestions
1043            .into_iter()
1044            .map(|sugg| {
1045                let mut parts = sugg
1046                    .into_iter()
1047                    .map(|(span, snippet)| SubstitutionPart { snippet, span })
1048                    .collect::<Vec<_>>();
1049
1050                parts.sort_unstable_by_key(|part| part.span);
1051
1052                if !!parts.is_empty() {
    ::core::panicking::panic("assertion failed: !parts.is_empty()")
};assert!(!parts.is_empty());
1053                if true {
    match (&parts.iter().find(|part|
                        part.span.is_empty() && part.snippet.is_empty()), &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("Span must not be empty and have no suggestion")));
            }
        }
    };
};debug_assert_eq!(
1054                    parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
1055                    None,
1056                    "Span must not be empty and have no suggestion",
1057                );
1058                if true {
    match (&parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
            &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("suggestion must not have overlapping parts")));
            }
        }
    };
};debug_assert_eq!(
1059                    parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
1060                    None,
1061                    "suggestion must not have overlapping parts",
1062                );
1063
1064                Substitution { parts }
1065            })
1066            .collect();
1067
1068        self.push_suggestion(CodeSuggestion {
1069            substitutions,
1070            msg: msg.into(),
1071            style: SuggestionStyle::ShowAlways,
1072            applicability,
1073        });
1074        self
1075    }
1076
1077    #[doc =
r" Prints out a message with a suggested edit of the code. If the suggestion is presented"]
#[doc = r" inline, it will only show the message and not the suggestion."]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion_short()`]."]
pub fn span_suggestion_short(&mut self, sp: Span, msg: impl Into<DiagMessage>,
    suggestion: impl ToString, applicability: Applicability) -> &mut Self {
    self.span_suggestion_with_style(sp, msg, suggestion, applicability,
        SuggestionStyle::HideCodeInline);
    self
}
#[doc =
r" Prints out a message with a suggested edit of the code. If the suggestion is presented"]
#[doc = r" inline, it will only show the message and not the suggestion."]
#[doc = r""]
#[doc = r" See [`CodeSuggestion`] for more information."]
#[doc = "See [`Diag::span_suggestion_short()`]."]
pub fn with_span_suggestion_short(mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability) -> Self {
    self.span_suggestion_short(sp, msg, suggestion, applicability);
    self
}with_fn! { with_span_suggestion_short,
1078    /// Prints out a message with a suggested edit of the code. If the suggestion is presented
1079    /// inline, it will only show the message and not the suggestion.
1080    ///
1081    /// See [`CodeSuggestion`] for more information.
1082    pub fn span_suggestion_short(
1083        &mut self,
1084        sp: Span,
1085        msg: impl Into<DiagMessage>,
1086        suggestion: impl ToString,
1087        applicability: Applicability,
1088    ) -> &mut Self {
1089        self.span_suggestion_with_style(
1090            sp,
1091            msg,
1092            suggestion,
1093            applicability,
1094            SuggestionStyle::HideCodeInline,
1095        );
1096        self
1097    } }
1098
1099    /// Prints out a message for a suggestion without showing the suggested code.
1100    ///
1101    /// This is intended to be used for suggestions that are obvious in what the changes need to
1102    /// be from the message, showing the span label inline would be visually unpleasant
1103    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
1104    /// improve understandability.
1105    pub fn span_suggestion_hidden(
1106        &mut self,
1107        sp: Span,
1108        msg: impl Into<DiagMessage>,
1109        suggestion: impl ToString,
1110        applicability: Applicability,
1111    ) -> &mut Self {
1112        self.span_suggestion_with_style(
1113            sp,
1114            msg,
1115            suggestion,
1116            applicability,
1117            SuggestionStyle::HideCodeAlways,
1118        );
1119        self
1120    }
1121
1122    #[doc =
r" Adds a suggestion to the JSON output that will not be shown in the CLI."]
#[doc = r""]
#[doc =
r" This is intended to be used for suggestions that are *very* obvious in what the changes"]
#[doc =
r" need to be from the message, but we still want other tools to be able to apply them."]
#[doc = "See [`Diag::tool_only_span_suggestion()`]."]
pub fn tool_only_span_suggestion(&mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability) -> &mut Self {
    self.span_suggestion_with_style(sp, msg, suggestion, applicability,
        SuggestionStyle::CompletelyHidden);
    self
}
#[doc =
r" Adds a suggestion to the JSON output that will not be shown in the CLI."]
#[doc = r""]
#[doc =
r" This is intended to be used for suggestions that are *very* obvious in what the changes"]
#[doc =
r" need to be from the message, but we still want other tools to be able to apply them."]
#[doc = "See [`Diag::tool_only_span_suggestion()`]."]
pub fn with_tool_only_span_suggestion(mut self, sp: Span,
    msg: impl Into<DiagMessage>, suggestion: impl ToString,
    applicability: Applicability) -> Self {
    self.tool_only_span_suggestion(sp, msg, suggestion, applicability);
    self
}with_fn! { with_tool_only_span_suggestion,
1123    /// Adds a suggestion to the JSON output that will not be shown in the CLI.
1124    ///
1125    /// This is intended to be used for suggestions that are *very* obvious in what the changes
1126    /// need to be from the message, but we still want other tools to be able to apply them.
1127    pub fn tool_only_span_suggestion(
1128        &mut self,
1129        sp: Span,
1130        msg: impl Into<DiagMessage>,
1131        suggestion: impl ToString,
1132        applicability: Applicability,
1133    ) -> &mut Self {
1134        self.span_suggestion_with_style(
1135            sp,
1136            msg,
1137            suggestion,
1138            applicability,
1139            SuggestionStyle::CompletelyHidden,
1140        );
1141        self
1142    } }
1143
1144    /// Add a subdiagnostic from a type that implements `Subdiagnostic` (see
1145    /// [rustc_macros::Subdiagnostic]). Performs eager formatting of any messages
1146    /// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of
1147    /// interpolated variables).
1148    pub fn subdiagnostic(&mut self, subdiagnostic: impl Subdiagnostic) -> &mut Self {
1149        subdiagnostic.add_to_diag(self);
1150        self
1151    }
1152
1153    #[doc = r" Add a span."]
#[doc = "See [`Diag::span()`]."]
pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
    self.span = sp.into();
    if let Some(span) = self.span.primary_span() { self.sort_span = span; }
    self
}
#[doc = r" Add a span."]
#[doc = "See [`Diag::span()`]."]
pub fn with_span(mut self, sp: impl Into<MultiSpan>) -> Self {
    self.span(sp);
    self
}with_fn! { with_span,
1154    /// Add a span.
1155    pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
1156        self.span = sp.into();
1157        if let Some(span) = self.span.primary_span() {
1158            self.sort_span = span;
1159        }
1160        self
1161    } }
1162
1163    pub fn is_lint(
1164        &mut self,
1165        name: String,
1166        has_future_breakage: bool,
1167        rust_version: Option<RustcVersion>,
1168    ) -> &mut Self {
1169        self.is_lint = Some(IsLint { name, has_future_breakage, rust_version });
1170        self
1171    }
1172
1173    #[doc = r" Add an error code."]
#[doc = "See [`Diag::code()`]."]
pub fn code(&mut self, code: ErrCode) -> &mut Self {
    self.code = Some(code);
    self
}
#[doc = r" Add an error code."]
#[doc = "See [`Diag::code()`]."]
pub fn with_code(mut self, code: ErrCode) -> Self { self.code(code); self }with_fn! { with_code,
1174    /// Add an error code.
1175    pub fn code(&mut self, code: ErrCode) -> &mut Self {
1176        self.code = Some(code);
1177        self
1178    } }
1179
1180    #[doc = r" Add an argument."]
#[doc = "See [`Diag::lint_id()`]."]
pub fn lint_id(&mut self, id: LintExpectationId) -> &mut Self {
    self.lint_id = Some(id);
    self
}
#[doc = r" Add an argument."]
#[doc = "See [`Diag::lint_id()`]."]
pub fn with_lint_id(mut self, id: LintExpectationId) -> Self {
    self.lint_id(id);
    self
}with_fn! { with_lint_id,
1181    /// Add an argument.
1182    pub fn lint_id(
1183        &mut self,
1184        id: LintExpectationId,
1185    ) -> &mut Self {
1186        self.lint_id = Some(id);
1187        self
1188    } }
1189
1190    #[doc = r" Add a primary message."]
#[doc = "See [`Diag::primary_message()`]."]
pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
    self.messages[0] = (msg.into(), Style::NoStyle);
    self
}
#[doc = r" Add a primary message."]
#[doc = "See [`Diag::primary_message()`]."]
pub fn with_primary_message(mut self, msg: impl Into<DiagMessage>) -> Self {
    self.primary_message(msg);
    self
}with_fn! { with_primary_message,
1191    /// Add a primary message.
1192    pub fn primary_message(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
1193        self.messages[0] = (msg.into(), Style::NoStyle);
1194        self
1195    } }
1196
1197    #[doc = r" Add an argument."]
#[doc = "See [`Diag::arg()`]."]
pub fn arg(&mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg)
    -> &mut Self {
    self.deref_mut().arg(name, arg);
    self
}
#[doc = r" Add an argument."]
#[doc = "See [`Diag::arg()`]."]
pub fn with_arg(mut self, name: impl Into<DiagArgName>, arg: impl IntoDiagArg)
    -> Self {
    self.arg(name, arg);
    self
}with_fn! { with_arg,
1198    /// Add an argument.
1199    pub fn arg(
1200        &mut self,
1201        name: impl Into<DiagArgName>,
1202        arg: impl IntoDiagArg,
1203    ) -> &mut Self {
1204        self.deref_mut().arg(name, arg);
1205        self
1206    } }
1207
1208    /// Convenience function for internal use, clients should use one of the
1209    /// public methods above.
1210    ///
1211    /// Used by `proc_macro_server` for implementing `server::Diagnostic`.
1212    pub fn sub(&mut self, level: Level, message: impl Into<DiagMessage>, span: MultiSpan) {
1213        self.deref_mut().sub(level, message, span);
1214    }
1215
1216    /// Convenience function for internal use, clients should use one of the
1217    /// public methods above.
1218    fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
1219        let messages = messages.into_iter().map(|m| (m.content.into(), m.style)).collect();
1220        let sub = Subdiag { level, messages, span };
1221        self.children.push(sub);
1222    }
1223
1224    /// Takes the diagnostic. For use by methods that consume the Diag: `emit`,
1225    /// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
1226    /// `self`.
1227    fn take_diag(&mut self) -> DiagInner {
1228        if let Some(path) = &self.long_ty_path {
1229            self.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the full name for the type has been written to \'{0}\'",
                path.display()))
    })format!(
1230                "the full name for the type has been written to '{}'",
1231                path.display()
1232            ));
1233            self.note("consider using `--verbose` to print the full type name to the console");
1234        }
1235        *self.diag.take().unwrap()
1236    }
1237
1238    /// This method allows us to access the path of the file where "long types" are written to.
1239    ///
1240    /// When calling `Diag::emit`, as part of that we will check if a `long_ty_path` has been set,
1241    /// and if it has been then we add a note mentioning the file where the "long types" were
1242    /// written to.
1243    ///
1244    /// When calling `tcx.short_string()` after a `Diag` is constructed, the preferred way of doing
1245    /// so is `tcx.short_string(ty, diag.long_ty_path())`. The diagnostic itself is the one that
1246    /// keeps the existence of a "long type" anywhere in the diagnostic, so the note telling the
1247    /// user where we wrote the file to is only printed once at most, *and* it makes it much harder
1248    /// to forget to set it.
1249    ///
1250    /// If the diagnostic hasn't been created before a "short ty string" is created, then you should
1251    /// ensure that this method is called to set it `*diag.long_ty_path() = path`.
1252    ///
1253    /// As a rule of thumb, if you see or add at least one `tcx.short_string()` call anywhere, in a
1254    /// scope, `diag.long_ty_path()` should be called once somewhere close by.
1255    pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1256        &mut self.long_ty_path
1257    }
1258
1259    pub fn with_long_ty_path(mut self, long_ty_path: Option<PathBuf>) -> Self {
1260        self.long_ty_path = long_ty_path;
1261        self
1262    }
1263
1264    /// Most `emit_producing_guarantee` functions use this as a starting point.
1265    fn emit_producing_nothing(mut self) {
1266        let diag = self.take_diag();
1267        self.dcx.emit_diagnostic(diag);
1268    }
1269
1270    /// `ErrorGuaranteed::emit_producing_guarantee` uses this.
1271    fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
1272        let diag = self.take_diag();
1273
1274        // The only error levels that produce `ErrorGuaranteed` are
1275        // `Error` and `DelayedBug`. But `DelayedBug` should never occur here
1276        // because delayed bugs have their level changed to `Bug` when they are
1277        // actually printed, so they produce an ICE.
1278        //
1279        // (Also, even though `level` isn't `pub`, the whole `DiagInner` could
1280        // be overwritten with a new one thanks to `DerefMut`. So this assert
1281        // protects against that, too.)
1282        if !#[allow(non_exhaustive_omitted_patterns)] match diag.level {
            Level::Error | Level::DelayedBug => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("invalid diagnostic level ({0:?})",
                diag.level));
    }
};assert!(
1283            matches!(diag.level, Level::Error | Level::DelayedBug),
1284            "invalid diagnostic level ({:?})",
1285            diag.level,
1286        );
1287
1288        let guar = self.dcx.emit_diagnostic(diag);
1289        guar.unwrap()
1290    }
1291
1292    /// Emit and consume the diagnostic.
1293    #[track_caller]
1294    pub fn emit(self) -> G::EmitResult {
1295        G::emit_producing_guarantee(self)
1296    }
1297
1298    /// Emit the diagnostic unless `delay` is true,
1299    /// in which case the emission will be delayed as a bug.
1300    ///
1301    /// See `emit` and `delay_as_bug` for details.
1302    #[track_caller]
1303    pub fn emit_unless_delay(mut self, delay: bool) -> G::EmitResult {
1304        if delay {
1305            self.downgrade_to_delayed_bug();
1306        }
1307        self.emit()
1308    }
1309
1310    /// Cancel and consume the diagnostic. (A diagnostic must either be emitted or
1311    /// cancelled or it will panic when dropped).
1312    pub fn cancel(mut self) {
1313        self.diag = None;
1314        drop(self);
1315    }
1316
1317    /// Cancels this diagnostic and returns its first message, if it exists.
1318    pub fn cancel_into_message(self) -> Option<String> {
1319        let s = self.diag.as_ref()?.messages.get(0)?.0.as_str().map(ToString::to_string);
1320        self.cancel();
1321        s
1322    }
1323
1324    /// See `DiagCtxt::stash_diagnostic` for details.
1325    pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
1326        let diag = self.take_diag();
1327        self.dcx.stash_diagnostic(span, key, diag)
1328    }
1329
1330    /// Delay emission of this diagnostic as a bug.
1331    ///
1332    /// This can be useful in contexts where an error indicates a bug but
1333    /// typically this only happens when other compilation errors have already
1334    /// happened. In those cases this can be used to defer emission of this
1335    /// diagnostic as a bug in the compiler only if no other errors have been
1336    /// emitted.
1337    ///
1338    /// In the meantime, though, callsites are required to deal with the "bug"
1339    /// locally in whichever way makes the most sense.
1340    #[track_caller]
1341    pub fn delay_as_bug(mut self) -> G::EmitResult {
1342        self.downgrade_to_delayed_bug();
1343        self.emit()
1344    }
1345}
1346
1347/// Destructor bomb: every `Diag` must be consumed (emitted, cancelled, etc.)
1348/// or we emit a bug.
1349impl<G: EmissionGuarantee> Drop for Diag<'_, G> {
1350    fn drop(&mut self) {
1351        match self.diag.take() {
1352            Some(diag) if !panicking() => {
1353                self.dcx.emit_diagnostic(DiagInner::new(
1354                    Level::Bug,
1355                    DiagMessage::from("the following error was constructed but not emitted"),
1356                ));
1357                self.dcx.emit_diagnostic(*diag);
1358                {
    ::core::panicking::panic_fmt(format_args!("error was constructed but not emitted"));
};panic!("error was constructed but not emitted");
1359            }
1360            _ => {}
1361        }
1362    }
1363}
1364
1365#[macro_export]
1366macro_rules! struct_span_code_err {
1367    ($dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
1368        $dcx.struct_span_err($span, format!($($message)*)).with_code($code)
1369    })
1370}