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