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