Skip to main content

rustc_errors/
json.rs

1//! A JSON emitter for errors.
2//!
3//! This works by converting errors to a simplified structural format (see the
4//! structs at the start of the file) and then serializing them. These should
5//! contain as much information about the error as possible.
6//!
7//! The format of the JSON output should be considered *unstable*. For now the
8//! structs at the end of this file (Diagnostic*) specify the error format.
9
10// FIXME: spec the JSON output properly.
11
12use std::io::{self, Write};
13use std::path::{Path, PathBuf};
14use std::sync::{Arc, Mutex};
15use std::vec;
16
17use anstream::{AutoStream, ColorChoice};
18use derive_setters::Setters;
19use rustc_data_structures::sync::IntoDynSyncSend;
20use rustc_error_messages::DiagArgMap;
21use rustc_lint_defs::Applicability;
22use rustc_span::hygiene::ExpnData;
23use rustc_span::source_map::{FilePathMapping, SourceMap};
24use rustc_span::{FileName, RealFileName, Span};
25use serde::Serialize;
26
27use crate::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
28use crate::diagnostic::IsLint;
29use crate::emitter::{
30    ColorConfig, Destination, Emitter, HumanReadableErrorType, OutputTheme, TimingEvent,
31    should_show_source_code,
32};
33use crate::formatting::{format_diag_message, format_diag_messages};
34use crate::timings::{TimingRecord, TimingSection};
35use crate::{CodeSuggestion, MultiSpan, SpanLabel, Subdiag, Suggestions, TerminalUrl};
36
37#[cfg(test)]
38mod tests;
39
40#[derive(impl JsonEmitter {
    #[must_use]
    pub fn ui_testing(mut self, value: bool) -> Self {
        self.ui_testing = value;
        self
    }
    #[must_use]
    pub fn ignored_directories_in_source_blocks(mut self, value: Vec<String>)
        -> Self {
        self.ignored_directories_in_source_blocks = value;
        self
    }
    #[must_use]
    pub fn color_config(mut self, value: ColorConfig) -> Self {
        self.color_config = value;
        self
    }
    #[must_use]
    pub fn diagnostic_width(mut self, value: Option<usize>) -> Self {
        self.diagnostic_width = value;
        self
    }
    #[must_use]
    pub fn macro_backtrace(mut self, value: bool) -> Self {
        self.macro_backtrace = value;
        self
    }
    #[must_use]
    pub fn track_diagnostics(mut self, value: bool) -> Self {
        self.track_diagnostics = value;
        self
    }
    #[must_use]
    pub fn terminal_url(mut self, value: TerminalUrl) -> Self {
        self.terminal_url = value;
        self
    }
}Setters)]
41pub struct JsonEmitter {
42    #[setters(skip)]
43    dst: IntoDynSyncSend<Box<dyn Write + Send>>,
44    #[setters(skip)]
45    sm: Option<Arc<SourceMap>>,
46    #[setters(skip)]
47    pretty: bool,
48    ui_testing: bool,
49    ignored_directories_in_source_blocks: Vec<String>,
50    #[setters(skip)]
51    json_rendered: HumanReadableErrorType,
52    color_config: ColorConfig,
53    diagnostic_width: Option<usize>,
54    macro_backtrace: bool,
55    track_diagnostics: bool,
56    terminal_url: TerminalUrl,
57}
58
59impl JsonEmitter {
60    pub fn new(
61        dst: Box<dyn Write + Send>,
62        sm: Option<Arc<SourceMap>>,
63        pretty: bool,
64        json_rendered: HumanReadableErrorType,
65        color_config: ColorConfig,
66    ) -> JsonEmitter {
67        JsonEmitter {
68            dst: IntoDynSyncSend(dst),
69            sm,
70            pretty,
71            ui_testing: false,
72            ignored_directories_in_source_blocks: Vec::new(),
73            json_rendered,
74            color_config,
75            diagnostic_width: None,
76            macro_backtrace: false,
77            track_diagnostics: false,
78            terminal_url: TerminalUrl::No,
79        }
80    }
81
82    fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> {
83        if self.pretty {
84            serde_json::to_writer_pretty(&mut *self.dst, &val)?
85        } else {
86            serde_json::to_writer(&mut *self.dst, &val)?
87        };
88        self.dst.write_all(b"\n")?;
89        self.dst.flush()
90    }
91}
92
93#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for EmitTyped<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                match *self {
                    EmitTyped::Diagnostic(ref __field0) =>
                        _serde::__private228::ser::serialize_tagged_newtype(__serializer,
                            "EmitTyped", "Diagnostic", "$message_type", "diagnostic",
                            __field0),
                    EmitTyped::Artifact(ref __field0) =>
                        _serde::__private228::ser::serialize_tagged_newtype(__serializer,
                            "EmitTyped", "Artifact", "$message_type", "artifact",
                            __field0),
                    EmitTyped::SectionTiming(ref __field0) =>
                        _serde::__private228::ser::serialize_tagged_newtype(__serializer,
                            "EmitTyped", "SectionTiming", "$message_type",
                            "section_timing", __field0),
                    EmitTyped::FutureIncompat(ref __field0) =>
                        _serde::__private228::ser::serialize_tagged_newtype(__serializer,
                            "EmitTyped", "FutureIncompat", "$message_type",
                            "future_incompat", __field0),
                    EmitTyped::UnusedExtern(ref __field0) =>
                        _serde::__private228::ser::serialize_tagged_newtype(__serializer,
                            "EmitTyped", "UnusedExtern", "$message_type",
                            "unused_extern", __field0),
                }
            }
        }
    };Serialize)]
94#[serde(tag = "$message_type", rename_all = "snake_case")]
95enum EmitTyped<'a> {
96    Diagnostic(Diagnostic),
97    Artifact(ArtifactNotification<'a>),
98    SectionTiming(SectionTimestamp<'a>),
99    FutureIncompat(FutureIncompatReport<'a>),
100    UnusedExtern(UnusedExterns<'a>),
101}
102
103impl Emitter for JsonEmitter {
104    fn emit_diagnostic(&mut self, diag: crate::DiagInner) {
105        let data = Diagnostic::from_errors_diagnostic(diag, self);
106        let result = self.emit(EmitTyped::Diagnostic(data));
107        if let Err(e) = result {
108            {
    ::core::panicking::panic_fmt(format_args!("failed to print diagnostics: {0:?}",
            e));
};panic!("failed to print diagnostics: {e:?}");
109        }
110    }
111
112    fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
113        let data = ArtifactNotification { artifact: path, emit: artifact_type };
114        let result = self.emit(EmitTyped::Artifact(data));
115        if let Err(e) = result {
116            {
    ::core::panicking::panic_fmt(format_args!("failed to print notification: {0:?}",
            e));
};panic!("failed to print notification: {e:?}");
117        }
118    }
119
120    fn emit_timing_section(&mut self, record: TimingRecord, event: TimingEvent) {
121        let event = match event {
122            TimingEvent::Start => "start",
123            TimingEvent::End => "end",
124        };
125        let name = match record.section {
126            TimingSection::Linking => "link",
127            TimingSection::Codegen => "codegen",
128        };
129        let data = SectionTimestamp { name, event, timestamp: record.timestamp };
130        let result = self.emit(EmitTyped::SectionTiming(data));
131        if let Err(e) = result {
132            {
    ::core::panicking::panic_fmt(format_args!("failed to print timing section: {0:?}",
            e));
};panic!("failed to print timing section: {e:?}");
133        }
134    }
135
136    fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>) {
137        let data: Vec<FutureBreakageItem<'_>> = diags
138            .into_iter()
139            .map(|mut diag| {
140                // Allowed or expected lints don't normally (by definition) emit a lint
141                // but future incompat lints are special and are emitted anyway.
142                //
143                // So to avoid ICEs and confused users we "upgrade" the lint level for
144                // those `FutureBreakageItem` to warn.
145                if #[allow(non_exhaustive_omitted_patterns)] match diag.level {
    crate::Level::Allow | crate::Level::Expect => true,
    _ => false,
}matches!(diag.level, crate::Level::Allow | crate::Level::Expect) {
146                    diag.level = crate::Level::Warning;
147                }
148                FutureBreakageItem {
149                    diagnostic: EmitTyped::Diagnostic(Diagnostic::from_errors_diagnostic(
150                        diag, self,
151                    )),
152                }
153            })
154            .collect();
155        let report = FutureIncompatReport { future_incompat_report: data };
156        let result = self.emit(EmitTyped::FutureIncompat(report));
157        if let Err(e) = result {
158            {
    ::core::panicking::panic_fmt(format_args!("failed to print future breakage report: {0:?}",
            e));
};panic!("failed to print future breakage report: {e:?}");
159        }
160    }
161
162    fn emit_unused_externs(&mut self, lint_level: rustc_lint_defs::Level, unused_externs: &[&str]) {
163        let lint_level = lint_level.as_str();
164        let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
165        let result = self.emit(EmitTyped::UnusedExtern(data));
166        if let Err(e) = result {
167            {
    ::core::panicking::panic_fmt(format_args!("failed to print unused externs: {0:?}",
            e));
};panic!("failed to print unused externs: {e:?}");
168        }
169    }
170
171    fn source_map(&self) -> Option<&SourceMap> {
172        self.sm.as_deref()
173    }
174
175    fn should_show_explain(&self) -> bool {
176        !self.json_rendered.short()
177    }
178}
179
180// The following data types are provided just for serialisation.
181
182#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for Diagnostic {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "Diagnostic", false as usize + 1 + 1 + 1 + 1 + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "message", &self.message)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "code", &self.code)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "level", &self.level)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "spans", &self.spans)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "children", &self.children)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "rendered", &self.rendered)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
183struct Diagnostic {
184    /// The primary error message.
185    message: String,
186    code: Option<DiagnosticCode>,
187    /// "error: internal compiler error", "error", "warning", "note", "help".
188    level: &'static str,
189    spans: Vec<DiagnosticSpan>,
190    /// Associated diagnostic messages.
191    children: Vec<Diagnostic>,
192    /// The message as rustc would render it.
193    rendered: Option<String>,
194}
195
196#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for DiagnosticSpan {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "DiagnosticSpan",
                            false as usize + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
                                    1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "file_name", &self.file_name)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "byte_start", &self.byte_start)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "byte_end", &self.byte_end)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "line_start", &self.line_start)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "line_end", &self.line_end)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "column_start", &self.column_start)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "column_end", &self.column_end)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "is_primary", &self.is_primary)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "text", &self.text)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "label", &self.label)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "suggested_replacement", &self.suggested_replacement)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "suggestion_applicability",
                        &self.suggestion_applicability)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "expansion", &self.expansion)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
197struct DiagnosticSpan {
198    file_name: String,
199    byte_start: u32,
200    byte_end: u32,
201    /// 1-based.
202    line_start: usize,
203    line_end: usize,
204    /// 1-based, character offset.
205    column_start: usize,
206    column_end: usize,
207    /// Is this a "primary" span -- meaning the point, or one of the points,
208    /// where the error occurred?
209    is_primary: bool,
210    /// Source text from the start of line_start to the end of line_end.
211    text: Vec<DiagnosticSpanLine>,
212    /// Label that should be placed at this location (if any)
213    label: Option<String>,
214    /// If we are suggesting a replacement, this will contain text
215    /// that should be sliced in atop this span.
216    suggested_replacement: Option<String>,
217    /// If the suggestion is approximate
218    suggestion_applicability: Option<Applicability>,
219    /// Macro invocations that created the code at this span, if any.
220    expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
221}
222
223#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for DiagnosticSpanLine {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "DiagnosticSpanLine", false as usize + 1 + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "text", &self.text)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "highlight_start", &self.highlight_start)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "highlight_end", &self.highlight_end)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
224struct DiagnosticSpanLine {
225    text: String,
226
227    /// 1-based, character offset in self.text.
228    highlight_start: usize,
229
230    highlight_end: usize,
231}
232
233#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for DiagnosticSpanMacroExpansion {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "DiagnosticSpanMacroExpansion",
                            false as usize + 1 + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "span", &self.span)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "macro_decl_name", &self.macro_decl_name)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "def_site_span", &self.def_site_span)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
234struct DiagnosticSpanMacroExpansion {
235    /// span where macro was applied to generate this code; note that
236    /// this may itself derive from a macro (if
237    /// `span.expansion.is_some()`)
238    span: DiagnosticSpan,
239
240    /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
241    macro_decl_name: String,
242
243    /// span where macro was defined (if known)
244    def_site_span: DiagnosticSpan,
245}
246
247#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl _serde::Serialize for DiagnosticCode {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "DiagnosticCode", false as usize + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "code", &self.code)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "explanation", &self.explanation)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
248struct DiagnosticCode {
249    /// The error code (e.g. "E1234"), if the diagnostic has one. Or the lint
250    /// name, if it's a lint without an error code.
251    code: String,
252    /// An explanation for the code.
253    explanation: Option<&'static str>,
254}
255
256#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for ArtifactNotification<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "ArtifactNotification", false as usize + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "artifact", &self.artifact)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "emit", &self.emit)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
257struct ArtifactNotification<'a> {
258    /// The path of the artifact.
259    artifact: &'a Path,
260    /// What kind of artifact we're emitting.
261    emit: &'a str,
262}
263
264#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for SectionTimestamp<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "SectionTimestamp", false as usize + 1 + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "name", &self.name)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "event", &self.event)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "timestamp", &self.timestamp)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
265struct SectionTimestamp<'a> {
266    /// Name of the section
267    name: &'a str,
268    /// Start/end of the section
269    event: &'a str,
270    /// Opaque timestamp.
271    timestamp: u128,
272}
273
274#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for FutureBreakageItem<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "FutureBreakageItem", false as usize + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "diagnostic", &self.diagnostic)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
275struct FutureBreakageItem<'a> {
276    // Always EmitTyped::Diagnostic, but we want to make sure it gets serialized
277    // with "$message_type".
278    diagnostic: EmitTyped<'a>,
279}
280
281#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for FutureIncompatReport<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "FutureIncompatReport", false as usize + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "future_incompat_report", &self.future_incompat_report)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
282struct FutureIncompatReport<'a> {
283    future_incompat_report: Vec<FutureBreakageItem<'a>>,
284}
285
286// NOTE: Keep this in sync with the equivalent structs in rustdoc's
287// doctest component (as well as cargo).
288// We could unify this struct the one in rustdoc but they have different
289// ownership semantics, so doing so would create wasteful allocations.
290#[derive(#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
    {
        #[allow(unused_extern_crates, clippy :: useless_attribute)]
        extern crate serde as _serde;
        ;
        #[automatically_derived]
        impl<'a> _serde::Serialize for UnusedExterns<'a> {
            fn serialize<__S>(&self, __serializer: __S)
                -> _serde::__private228::Result<__S::Ok, __S::Error> where
                __S: _serde::Serializer {
                let mut __serde_state =
                    _serde::Serializer::serialize_struct(__serializer,
                            "UnusedExterns", false as usize + 1 + 1)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "lint_level", &self.lint_level)?;
                _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
                        "unused_extern_names", &self.unused_extern_names)?;
                _serde::ser::SerializeStruct::end(__serde_state)
            }
        }
    };Serialize)]
291struct UnusedExterns<'a> {
292    /// The severity level of the unused dependencies lint
293    lint_level: &'a str,
294    /// List of unused externs by their names.
295    unused_extern_names: &'a [&'a str],
296}
297
298impl Diagnostic {
299    /// Converts from `rustc_errors::DiagInner` to `Diagnostic`.
300    fn from_errors_diagnostic(diag: crate::DiagInner, je: &JsonEmitter) -> Diagnostic {
301        let sugg_to_diag = |sugg: &CodeSuggestion| {
302            let formatted_message = format_diag_message(&sugg.msg, &diag.args);
303            Diagnostic {
304                message: formatted_message.to_string(),
305                code: None,
306                level: "help",
307                spans: DiagnosticSpan::from_suggestion(sugg, &diag.args, je),
308                children: ::alloc::vec::Vec::new()vec![],
309                rendered: None,
310            }
311        };
312        let sugg = match &diag.suggestions {
313            Suggestions::Enabled(suggestions) => suggestions.iter().map(sugg_to_diag),
314            Suggestions::Sealed(suggestions) => suggestions.iter().map(sugg_to_diag),
315            Suggestions::Disabled => [].iter().map(sugg_to_diag),
316        };
317
318        // generate regular command line output and store it in the json
319
320        // A threadsafe buffer for writing.
321        #[derive(#[automatically_derived]
impl ::core::clone::Clone for BufWriter {
    #[inline]
    fn clone(&self) -> BufWriter {
        BufWriter(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
322        struct BufWriter(Arc<Mutex<Vec<u8>>>);
323
324        impl Write for BufWriter {
325            fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
326                self.0.lock().unwrap().write(buf)
327            }
328            fn flush(&mut self) -> io::Result<()> {
329                self.0.lock().unwrap().flush()
330            }
331        }
332
333        let formatted_message = format_diag_messages(&diag.messages, &diag.args);
334
335        let code = if let Some(code) = diag.code {
336            Some(DiagnosticCode {
337                code: code.to_string(),
338                explanation: crate::codes::try_find_description(code).ok(),
339            })
340        } else if let Some(IsLint { name, .. }) = &diag.is_lint {
341            Some(DiagnosticCode { code: name.to_string(), explanation: None })
342        } else {
343            None
344        };
345        let level = diag.level.to_str();
346        let spans = DiagnosticSpan::from_multispan(&diag.span, &diag.args, je);
347        let mut children: Vec<Diagnostic> = diag
348            .children
349            .iter()
350            .map(|c| Diagnostic::from_sub_diagnostic(c, &diag.args, je))
351            .chain(sugg)
352            .collect();
353        if je.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() {
354            children.insert(
355                0,
356                Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &diag.args, je),
357            );
358        }
359        let buf = BufWriter(Arc::new(Mutex::new(Vec::new())));
360        let dst: Destination = AutoStream::new(
361            Box::new(buf.clone()),
362            match je.color_config.to_color_choice() {
363                ColorChoice::Auto => ColorChoice::Always,
364                choice => choice,
365            },
366        );
367        AnnotateSnippetEmitter::new(dst)
368            .short_message(je.json_rendered.short)
369            .sm(je.sm.clone())
370            .diagnostic_width(je.diagnostic_width)
371            .macro_backtrace(je.macro_backtrace)
372            .track_diagnostics(je.track_diagnostics)
373            .terminal_url(je.terminal_url)
374            .ui_testing(je.ui_testing)
375            .ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
376            .theme(if je.json_rendered.unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
377            .emit_diagnostic(diag);
378
379        let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();
380        let buf = String::from_utf8(buf).unwrap();
381
382        Diagnostic {
383            message: formatted_message.to_string(),
384            code,
385            level,
386            spans,
387            children,
388            rendered: Some(buf),
389        }
390    }
391
392    fn from_sub_diagnostic(subdiag: &Subdiag, args: &DiagArgMap, je: &JsonEmitter) -> Diagnostic {
393        let formatted_message = format_diag_messages(&subdiag.messages, args);
394        Diagnostic {
395            message: formatted_message.to_string(),
396            code: None,
397            level: subdiag.level.to_str(),
398            spans: DiagnosticSpan::from_multispan(&subdiag.span, args, je),
399            children: ::alloc::vec::Vec::new()vec![],
400            rendered: None,
401        }
402    }
403}
404
405impl DiagnosticSpan {
406    fn from_span_label(
407        span: SpanLabel,
408        suggestion: Option<(&String, Applicability)>,
409        args: &DiagArgMap,
410        je: &JsonEmitter,
411    ) -> DiagnosticSpan {
412        Self::from_span_etc(
413            span.span,
414            span.is_primary,
415            span.label.as_ref().map(|m| format_diag_message(m, args)).map(|m| m.to_string()),
416            suggestion,
417            je,
418        )
419    }
420
421    fn from_span_etc(
422        span: Span,
423        is_primary: bool,
424        label: Option<String>,
425        suggestion: Option<(&String, Applicability)>,
426        je: &JsonEmitter,
427    ) -> DiagnosticSpan {
428        // obtain the full backtrace from the `macro_backtrace`
429        // helper; in some ways, it'd be better to expand the
430        // backtrace ourselves, but the `macro_backtrace` helper makes
431        // some decision, such as dropping some frames, and I don't
432        // want to duplicate that logic here.
433        let backtrace = span.macro_backtrace();
434        DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
435    }
436
437    fn from_span_full(
438        mut span: Span,
439        is_primary: bool,
440        label: Option<String>,
441        suggestion: Option<(&String, Applicability)>,
442        mut backtrace: impl Iterator<Item = ExpnData>,
443        je: &JsonEmitter,
444    ) -> DiagnosticSpan {
445        let empty_source_map;
446        let sm = match &je.sm {
447            Some(s) => s,
448            None => {
449                span = rustc_span::DUMMY_SP;
450                empty_source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
451                empty_source_map.new_source_file(
452                    FileName::Real(
453                        empty_source_map
454                            .path_mapping()
455                            .to_real_filename(&RealFileName::empty(), PathBuf::from("empty.rs")),
456                    ),
457                    String::new(),
458                );
459                &empty_source_map
460            }
461        };
462        let start = sm.lookup_char_pos(span.lo());
463        // If this goes from the start of a line to the end and the replacement
464        // is an empty string, increase the length to include the newline so we don't
465        // leave an empty line
466        if start.col.0 == 0
467            && let Some((suggestion, _)) = suggestion
468            && suggestion.is_empty()
469            && let Ok(after) = sm.span_to_next_source(span)
470            && after.starts_with('\n')
471        {
472            span = span.with_hi(span.hi() + rustc_span::BytePos(1));
473        }
474        let end = sm.lookup_char_pos(span.hi());
475        let backtrace_step = backtrace.next().map(|bt| {
476            let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
477            let def_site_span = Self::from_span_full(
478                sm.guess_head_span(bt.def_site),
479                false,
480                None,
481                None,
482                [].into_iter(),
483                je,
484            );
485            Box::new(DiagnosticSpanMacroExpansion {
486                span: call_site,
487                macro_decl_name: bt.kind.descr(),
488                def_site_span,
489            })
490        });
491
492        DiagnosticSpan {
493            file_name: sm.filename_for_diagnostics(&start.file.name).to_string(),
494            byte_start: start.file.original_relative_byte_pos(span.lo()).0,
495            byte_end: start.file.original_relative_byte_pos(span.hi()).0,
496            line_start: start.line,
497            line_end: end.line,
498            column_start: start.col.0 + 1,
499            column_end: end.col.0 + 1,
500            is_primary,
501            text: DiagnosticSpanLine::from_span(span, je),
502            suggested_replacement: suggestion.map(|x| x.0.clone()),
503            suggestion_applicability: suggestion.map(|x| x.1),
504            expansion: backtrace_step,
505            label,
506        }
507    }
508
509    fn from_multispan(msp: &MultiSpan, args: &DiagArgMap, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
510        msp.span_labels()
511            .into_iter()
512            .map(|span_str| Self::from_span_label(span_str, None, args, je))
513            .collect()
514    }
515
516    fn from_suggestion(
517        suggestion: &CodeSuggestion,
518        args: &DiagArgMap,
519        je: &JsonEmitter,
520    ) -> Vec<DiagnosticSpan> {
521        suggestion
522            .substitutions
523            .iter()
524            .flat_map(|substitution| {
525                substitution.parts.iter().map(move |suggestion_inner| {
526                    let span_label =
527                        SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
528                    DiagnosticSpan::from_span_label(
529                        span_label,
530                        Some((&suggestion_inner.snippet, suggestion.applicability)),
531                        args,
532                        je,
533                    )
534                })
535            })
536            .collect()
537    }
538}
539
540impl DiagnosticSpanLine {
541    fn line_from_source_file(
542        sf: &rustc_span::SourceFile,
543        index: usize,
544        h_start: usize,
545        h_end: usize,
546    ) -> DiagnosticSpanLine {
547        DiagnosticSpanLine {
548            text: sf.get_line(index).map_or_else(String::new, |l| l.into_owned()),
549            highlight_start: h_start,
550            highlight_end: h_end,
551        }
552    }
553
554    /// Creates a list of DiagnosticSpanLines from span - each line with any part
555    /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
556    /// `span` within the line.
557    fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
558        je.sm
559            .as_ref()
560            .and_then(|sm| {
561                let lines = sm.span_to_lines(span).ok()?;
562                // We can't get any lines if the source is unavailable.
563                if !should_show_source_code(
564                    &je.ignored_directories_in_source_blocks,
565                    &sm,
566                    &lines.file,
567                ) {
568                    return None;
569                }
570
571                let sf = &*lines.file;
572                let span_lines = lines
573                    .lines
574                    .iter()
575                    .map(|line| {
576                        DiagnosticSpanLine::line_from_source_file(
577                            sf,
578                            line.line_index,
579                            line.start_col.0 + 1,
580                            line.end_col.0 + 1,
581                        )
582                    })
583                    .collect();
584                Some(span_lines)
585            })
586            .unwrap_or_default()
587    }
588}