]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_errors/src/json.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / compiler / rustc_errors / src / 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
12 use rustc_span::source_map::{FilePathMapping, SourceMap};
13
14 use crate::emitter::{Emitter, HumanReadableErrorType};
15 use crate::registry::Registry;
16 use crate::translation::{to_fluent_args, Translate};
17 use crate::DiagnosticId;
18 use crate::{
19     CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
20 };
21 use rustc_lint_defs::Applicability;
22
23 use rustc_data_structures::sync::Lrc;
24 use rustc_error_messages::FluentArgs;
25 use rustc_span::hygiene::ExpnData;
26 use rustc_span::Span;
27 use std::error::Report;
28 use std::io::{self, Write};
29 use std::path::Path;
30 use std::sync::{Arc, Mutex};
31 use std::vec;
32
33 use serde::Serialize;
34
35 #[cfg(test)]
36 mod tests;
37
38 pub struct JsonEmitter {
39     dst: Box<dyn Write + Send>,
40     registry: Option<Registry>,
41     sm: Lrc<SourceMap>,
42     fluent_bundle: Option<Lrc<FluentBundle>>,
43     fallback_bundle: LazyFallbackBundle,
44     pretty: bool,
45     ui_testing: bool,
46     json_rendered: HumanReadableErrorType,
47     diagnostic_width: Option<usize>,
48     macro_backtrace: bool,
49     track_diagnostics: bool,
50 }
51
52 impl JsonEmitter {
53     pub fn stderr(
54         registry: Option<Registry>,
55         source_map: Lrc<SourceMap>,
56         fluent_bundle: Option<Lrc<FluentBundle>>,
57         fallback_bundle: LazyFallbackBundle,
58         pretty: bool,
59         json_rendered: HumanReadableErrorType,
60         diagnostic_width: Option<usize>,
61         macro_backtrace: bool,
62         track_diagnostics: bool,
63     ) -> JsonEmitter {
64         JsonEmitter {
65             dst: Box::new(io::BufWriter::new(io::stderr())),
66             registry,
67             sm: source_map,
68             fluent_bundle,
69             fallback_bundle,
70             pretty,
71             ui_testing: false,
72             json_rendered,
73             diagnostic_width,
74             macro_backtrace,
75             track_diagnostics,
76         }
77     }
78
79     pub fn basic(
80         pretty: bool,
81         json_rendered: HumanReadableErrorType,
82         fluent_bundle: Option<Lrc<FluentBundle>>,
83         fallback_bundle: LazyFallbackBundle,
84         diagnostic_width: Option<usize>,
85         macro_backtrace: bool,
86         track_diagnostics: bool,
87     ) -> JsonEmitter {
88         let file_path_mapping = FilePathMapping::empty();
89         JsonEmitter::stderr(
90             None,
91             Lrc::new(SourceMap::new(file_path_mapping)),
92             fluent_bundle,
93             fallback_bundle,
94             pretty,
95             json_rendered,
96             diagnostic_width,
97             macro_backtrace,
98             track_diagnostics,
99         )
100     }
101
102     pub fn new(
103         dst: Box<dyn Write + Send>,
104         registry: Option<Registry>,
105         source_map: Lrc<SourceMap>,
106         fluent_bundle: Option<Lrc<FluentBundle>>,
107         fallback_bundle: LazyFallbackBundle,
108         pretty: bool,
109         json_rendered: HumanReadableErrorType,
110         diagnostic_width: Option<usize>,
111         macro_backtrace: bool,
112         track_diagnostics: bool,
113     ) -> JsonEmitter {
114         JsonEmitter {
115             dst,
116             registry,
117             sm: source_map,
118             fluent_bundle,
119             fallback_bundle,
120             pretty,
121             ui_testing: false,
122             json_rendered,
123             diagnostic_width,
124             macro_backtrace,
125             track_diagnostics,
126         }
127     }
128
129     pub fn ui_testing(self, ui_testing: bool) -> Self {
130         Self { ui_testing, ..self }
131     }
132 }
133
134 impl Translate for JsonEmitter {
135     fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
136         self.fluent_bundle.as_ref()
137     }
138
139     fn fallback_fluent_bundle(&self) -> &FluentBundle {
140         &self.fallback_bundle
141     }
142 }
143
144 impl Emitter for JsonEmitter {
145     fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
146         let data = Diagnostic::from_errors_diagnostic(diag, self);
147         let result = if self.pretty {
148             writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
149         } else {
150             writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
151         }
152         .and_then(|_| self.dst.flush());
153         if let Err(e) = result {
154             panic!("failed to print diagnostics: {:?}", e);
155         }
156     }
157
158     fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
159         let data = ArtifactNotification { artifact: path, emit: artifact_type };
160         let result = if self.pretty {
161             writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
162         } else {
163             writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
164         }
165         .and_then(|_| self.dst.flush());
166         if let Err(e) = result {
167             panic!("failed to print notification: {:?}", e);
168         }
169     }
170
171     fn emit_future_breakage_report(&mut self, diags: Vec<crate::Diagnostic>) {
172         let data: Vec<FutureBreakageItem> = diags
173             .into_iter()
174             .map(|mut diag| {
175                 if diag.level == crate::Level::Allow {
176                     diag.level = crate::Level::Warning(None);
177                 }
178                 FutureBreakageItem { diagnostic: Diagnostic::from_errors_diagnostic(&diag, self) }
179             })
180             .collect();
181         let report = FutureIncompatReport { future_incompat_report: data };
182         let result = if self.pretty {
183             writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&report).unwrap())
184         } else {
185             writeln!(&mut self.dst, "{}", serde_json::to_string(&report).unwrap())
186         }
187         .and_then(|_| self.dst.flush());
188         if let Err(e) = result {
189             panic!("failed to print future breakage report: {:?}", e);
190         }
191     }
192
193     fn emit_unused_externs(&mut self, lint_level: rustc_lint_defs::Level, unused_externs: &[&str]) {
194         let lint_level = lint_level.as_str();
195         let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
196         let result = if self.pretty {
197             writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
198         } else {
199             writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
200         }
201         .and_then(|_| self.dst.flush());
202         if let Err(e) = result {
203             panic!("failed to print unused externs: {:?}", e);
204         }
205     }
206
207     fn source_map(&self) -> Option<&Lrc<SourceMap>> {
208         Some(&self.sm)
209     }
210
211     fn should_show_explain(&self) -> bool {
212         !matches!(self.json_rendered, HumanReadableErrorType::Short(_))
213     }
214 }
215
216 // The following data types are provided just for serialisation.
217
218 #[derive(Serialize)]
219 struct Diagnostic {
220     /// The primary error message.
221     message: String,
222     code: Option<DiagnosticCode>,
223     /// "error: internal compiler error", "error", "warning", "note", "help".
224     level: &'static str,
225     spans: Vec<DiagnosticSpan>,
226     /// Associated diagnostic messages.
227     children: Vec<Diagnostic>,
228     /// The message as rustc would render it.
229     rendered: Option<String>,
230 }
231
232 #[derive(Serialize)]
233 struct DiagnosticSpan {
234     file_name: String,
235     byte_start: u32,
236     byte_end: u32,
237     /// 1-based.
238     line_start: usize,
239     line_end: usize,
240     /// 1-based, character offset.
241     column_start: usize,
242     column_end: usize,
243     /// Is this a "primary" span -- meaning the point, or one of the points,
244     /// where the error occurred?
245     is_primary: bool,
246     /// Source text from the start of line_start to the end of line_end.
247     text: Vec<DiagnosticSpanLine>,
248     /// Label that should be placed at this location (if any)
249     label: Option<String>,
250     /// If we are suggesting a replacement, this will contain text
251     /// that should be sliced in atop this span.
252     suggested_replacement: Option<String>,
253     /// If the suggestion is approximate
254     suggestion_applicability: Option<Applicability>,
255     /// Macro invocations that created the code at this span, if any.
256     expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
257 }
258
259 #[derive(Serialize)]
260 struct DiagnosticSpanLine {
261     text: String,
262
263     /// 1-based, character offset in self.text.
264     highlight_start: usize,
265
266     highlight_end: usize,
267 }
268
269 #[derive(Serialize)]
270 struct DiagnosticSpanMacroExpansion {
271     /// span where macro was applied to generate this code; note that
272     /// this may itself derive from a macro (if
273     /// `span.expansion.is_some()`)
274     span: DiagnosticSpan,
275
276     /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
277     macro_decl_name: String,
278
279     /// span where macro was defined (if known)
280     def_site_span: DiagnosticSpan,
281 }
282
283 #[derive(Serialize)]
284 struct DiagnosticCode {
285     /// The code itself.
286     code: String,
287     /// An explanation for the code.
288     explanation: Option<&'static str>,
289 }
290
291 #[derive(Serialize)]
292 struct ArtifactNotification<'a> {
293     /// The path of the artifact.
294     artifact: &'a Path,
295     /// What kind of artifact we're emitting.
296     emit: &'a str,
297 }
298
299 #[derive(Serialize)]
300 struct FutureBreakageItem {
301     diagnostic: Diagnostic,
302 }
303
304 #[derive(Serialize)]
305 struct FutureIncompatReport {
306     future_incompat_report: Vec<FutureBreakageItem>,
307 }
308
309 // NOTE: Keep this in sync with the equivalent structs in rustdoc's
310 // doctest component (as well as cargo).
311 // We could unify this struct the one in rustdoc but they have different
312 // ownership semantics, so doing so would create wasteful allocations.
313 #[derive(Serialize)]
314 struct UnusedExterns<'a, 'b, 'c> {
315     /// The severity level of the unused dependencies lint
316     lint_level: &'a str,
317     /// List of unused externs by their names.
318     unused_extern_names: &'b [&'c str],
319 }
320
321 impl Diagnostic {
322     fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
323         let args = to_fluent_args(diag.args());
324         let sugg = diag.suggestions.iter().flatten().map(|sugg| {
325             let translated_message =
326                 je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();
327             Diagnostic {
328                 message: translated_message.to_string(),
329                 code: None,
330                 level: "help",
331                 spans: DiagnosticSpan::from_suggestion(sugg, &args, je),
332                 children: vec![],
333                 rendered: None,
334             }
335         });
336
337         // generate regular command line output and store it in the json
338
339         // A threadsafe buffer for writing.
340         #[derive(Default, Clone)]
341         struct BufWriter(Arc<Mutex<Vec<u8>>>);
342
343         impl Write for BufWriter {
344             fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
345                 self.0.lock().unwrap().write(buf)
346             }
347             fn flush(&mut self) -> io::Result<()> {
348                 self.0.lock().unwrap().flush()
349             }
350         }
351         let buf = BufWriter::default();
352         let output = buf.clone();
353         je.json_rendered
354             .new_emitter(
355                 Box::new(buf),
356                 Some(je.sm.clone()),
357                 je.fluent_bundle.clone(),
358                 je.fallback_bundle.clone(),
359                 false,
360                 je.diagnostic_width,
361                 je.macro_backtrace,
362                 je.track_diagnostics,
363             )
364             .ui_testing(je.ui_testing)
365             .emit_diagnostic(diag);
366         let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
367         let output = String::from_utf8(output).unwrap();
368
369         let translated_message = je.translate_messages(&diag.message, &args);
370         Diagnostic {
371             message: translated_message.to_string(),
372             code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
373             level: diag.level.to_str(),
374             spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
375             children: diag
376                 .children
377                 .iter()
378                 .map(|c| Diagnostic::from_sub_diagnostic(c, &args, je))
379                 .chain(sugg)
380                 .collect(),
381             rendered: Some(output),
382         }
383     }
384
385     fn from_sub_diagnostic(
386         diag: &SubDiagnostic,
387         args: &FluentArgs<'_>,
388         je: &JsonEmitter,
389     ) -> Diagnostic {
390         let translated_message = je.translate_messages(&diag.message, args);
391         Diagnostic {
392             message: translated_message.to_string(),
393             code: None,
394             level: diag.level.to_str(),
395             spans: diag
396                 .render_span
397                 .as_ref()
398                 .map(|sp| DiagnosticSpan::from_multispan(sp, args, je))
399                 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, args, je)),
400             children: vec![],
401             rendered: None,
402         }
403     }
404 }
405
406 impl DiagnosticSpan {
407     fn from_span_label(
408         span: SpanLabel,
409         suggestion: Option<(&String, Applicability)>,
410         args: &FluentArgs<'_>,
411         je: &JsonEmitter,
412     ) -> DiagnosticSpan {
413         Self::from_span_etc(
414             span.span,
415             span.is_primary,
416             span.label
417                 .as_ref()
418                 .map(|m| je.translate_message(m, args).unwrap())
419                 .map(|m| m.to_string()),
420             suggestion,
421             je,
422         )
423     }
424
425     fn from_span_etc(
426         span: Span,
427         is_primary: bool,
428         label: Option<String>,
429         suggestion: Option<(&String, Applicability)>,
430         je: &JsonEmitter,
431     ) -> DiagnosticSpan {
432         // obtain the full backtrace from the `macro_backtrace`
433         // helper; in some ways, it'd be better to expand the
434         // backtrace ourselves, but the `macro_backtrace` helper makes
435         // some decision, such as dropping some frames, and I don't
436         // want to duplicate that logic here.
437         let backtrace = span.macro_backtrace();
438         DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
439     }
440
441     fn from_span_full(
442         span: Span,
443         is_primary: bool,
444         label: Option<String>,
445         suggestion: Option<(&String, Applicability)>,
446         mut backtrace: impl Iterator<Item = ExpnData>,
447         je: &JsonEmitter,
448     ) -> DiagnosticSpan {
449         let start = je.sm.lookup_char_pos(span.lo());
450         let end = je.sm.lookup_char_pos(span.hi());
451         let backtrace_step = backtrace.next().map(|bt| {
452             let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
453             let def_site_span = Self::from_span_full(
454                 je.sm.guess_head_span(bt.def_site),
455                 false,
456                 None,
457                 None,
458                 [].into_iter(),
459                 je,
460             );
461             Box::new(DiagnosticSpanMacroExpansion {
462                 span: call_site,
463                 macro_decl_name: bt.kind.descr(),
464                 def_site_span,
465             })
466         });
467
468         DiagnosticSpan {
469             file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
470             byte_start: start.file.original_relative_byte_pos(span.lo()).0,
471             byte_end: start.file.original_relative_byte_pos(span.hi()).0,
472             line_start: start.line,
473             line_end: end.line,
474             column_start: start.col.0 + 1,
475             column_end: end.col.0 + 1,
476             is_primary,
477             text: DiagnosticSpanLine::from_span(span, je),
478             suggested_replacement: suggestion.map(|x| x.0.clone()),
479             suggestion_applicability: suggestion.map(|x| x.1),
480             expansion: backtrace_step,
481             label,
482         }
483     }
484
485     fn from_multispan(
486         msp: &MultiSpan,
487         args: &FluentArgs<'_>,
488         je: &JsonEmitter,
489     ) -> Vec<DiagnosticSpan> {
490         msp.span_labels()
491             .into_iter()
492             .map(|span_str| Self::from_span_label(span_str, None, args, je))
493             .collect()
494     }
495
496     fn from_suggestion(
497         suggestion: &CodeSuggestion,
498         args: &FluentArgs<'_>,
499         je: &JsonEmitter,
500     ) -> Vec<DiagnosticSpan> {
501         suggestion
502             .substitutions
503             .iter()
504             .flat_map(|substitution| {
505                 substitution.parts.iter().map(move |suggestion_inner| {
506                     let span_label =
507                         SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
508                     DiagnosticSpan::from_span_label(
509                         span_label,
510                         Some((&suggestion_inner.snippet, suggestion.applicability)),
511                         args,
512                         je,
513                     )
514                 })
515             })
516             .collect()
517     }
518 }
519
520 impl DiagnosticSpanLine {
521     fn line_from_source_file(
522         sf: &rustc_span::SourceFile,
523         index: usize,
524         h_start: usize,
525         h_end: usize,
526     ) -> DiagnosticSpanLine {
527         DiagnosticSpanLine {
528             text: sf.get_line(index).map_or_else(String::new, |l| l.into_owned()),
529             highlight_start: h_start,
530             highlight_end: h_end,
531         }
532     }
533
534     /// Creates a list of DiagnosticSpanLines from span - each line with any part
535     /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
536     /// `span` within the line.
537     fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
538         je.sm
539             .span_to_lines(span)
540             .map(|lines| {
541                 // We can't get any lines if the source is unavailable.
542                 if !je.sm.ensure_source_file_source_present(lines.file.clone()) {
543                     return vec![];
544                 }
545
546                 let sf = &*lines.file;
547                 lines
548                     .lines
549                     .iter()
550                     .map(|line| {
551                         DiagnosticSpanLine::line_from_source_file(
552                             sf,
553                             line.line_index,
554                             line.start_col.0 + 1,
555                             line.end_col.0 + 1,
556                         )
557                     })
558                     .collect()
559             })
560             .unwrap_or_else(|_| vec![])
561     }
562 }
563
564 impl DiagnosticCode {
565     fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
566         s.map(|s| {
567             let s = match s {
568                 DiagnosticId::Error(s) => s,
569                 DiagnosticId::Lint { name, .. } => name,
570             };
571             let je_result =
572                 je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
573
574             DiagnosticCode { code: s, explanation: je_result.unwrap_or(None) }
575         })
576     }
577 }