]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_errors/src/json.rs
Rollup merge of #96142 - cjgillot:no-crate-def-index, 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::{
18     CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
19 };
20 use rustc_lint_defs::Applicability;
21
22 use rustc_data_structures::sync::Lrc;
23 use rustc_error_messages::FluentArgs;
24 use rustc_span::hygiene::ExpnData;
25 use rustc_span::Span;
26 use std::io::{self, Write};
27 use std::path::Path;
28 use std::sync::{Arc, Mutex};
29 use std::vec;
30
31 use rustc_serialize::json::{as_json, as_pretty_json};
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: LazyFallbackBundle,
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: LazyFallbackBundle,
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: LazyFallbackBundle,
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: LazyFallbackBundle,
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) -> &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 #[derive(Encodable)]
207 struct Diagnostic {
208     /// The primary error message.
209     message: String,
210     code: Option<DiagnosticCode>,
211     /// "error: internal compiler error", "error", "warning", "note", "help".
212     level: &'static str,
213     spans: Vec<DiagnosticSpan>,
214     /// Associated diagnostic messages.
215     children: Vec<Diagnostic>,
216     /// The message as rustc would render it.
217     rendered: Option<String>,
218 }
219
220 #[derive(Encodable)]
221 struct DiagnosticSpan {
222     file_name: String,
223     byte_start: u32,
224     byte_end: u32,
225     /// 1-based.
226     line_start: usize,
227     line_end: usize,
228     /// 1-based, character offset.
229     column_start: usize,
230     column_end: usize,
231     /// Is this a "primary" span -- meaning the point, or one of the points,
232     /// where the error occurred?
233     is_primary: bool,
234     /// Source text from the start of line_start to the end of line_end.
235     text: Vec<DiagnosticSpanLine>,
236     /// Label that should be placed at this location (if any)
237     label: Option<String>,
238     /// If we are suggesting a replacement, this will contain text
239     /// that should be sliced in atop this span.
240     suggested_replacement: Option<String>,
241     /// If the suggestion is approximate
242     suggestion_applicability: Option<Applicability>,
243     /// Macro invocations that created the code at this span, if any.
244     expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
245 }
246
247 #[derive(Encodable)]
248 struct DiagnosticSpanLine {
249     text: String,
250
251     /// 1-based, character offset in self.text.
252     highlight_start: usize,
253
254     highlight_end: usize,
255 }
256
257 #[derive(Encodable)]
258 struct DiagnosticSpanMacroExpansion {
259     /// span where macro was applied to generate this code; note that
260     /// this may itself derive from a macro (if
261     /// `span.expansion.is_some()`)
262     span: DiagnosticSpan,
263
264     /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
265     macro_decl_name: String,
266
267     /// span where macro was defined (if known)
268     def_site_span: DiagnosticSpan,
269 }
270
271 #[derive(Encodable)]
272 struct DiagnosticCode {
273     /// The code itself.
274     code: String,
275     /// An explanation for the code.
276     explanation: Option<&'static str>,
277 }
278
279 #[derive(Encodable)]
280 struct ArtifactNotification<'a> {
281     /// The path of the artifact.
282     artifact: &'a Path,
283     /// What kind of artifact we're emitting.
284     emit: &'a str,
285 }
286
287 #[derive(Encodable)]
288 struct FutureBreakageItem {
289     diagnostic: Diagnostic,
290 }
291
292 #[derive(Encodable)]
293 struct FutureIncompatReport {
294     future_incompat_report: Vec<FutureBreakageItem>,
295 }
296
297 // NOTE: Keep this in sync with the equivalent structs in rustdoc's
298 // doctest component (as well as cargo).
299 // We could unify this struct the one in rustdoc but they have different
300 // ownership semantics, so doing so would create wasteful allocations.
301 #[derive(Encodable)]
302 struct UnusedExterns<'a, 'b, 'c> {
303     /// The severity level of the unused dependencies lint
304     lint_level: &'a str,
305     /// List of unused externs by their names.
306     unused_extern_names: &'b [&'c str],
307 }
308
309 impl Diagnostic {
310     fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
311         let args = je.to_fluent_args(diag.args());
312         let sugg = diag.suggestions.iter().flatten().map(|sugg| {
313             let translated_message = je.translate_message(&sugg.msg, &args);
314             Diagnostic {
315                 message: translated_message.to_string(),
316                 code: None,
317                 level: "help",
318                 spans: DiagnosticSpan::from_suggestion(sugg, &args, je),
319                 children: vec![],
320                 rendered: None,
321             }
322         });
323
324         // generate regular command line output and store it in the json
325
326         // A threadsafe buffer for writing.
327         #[derive(Default, Clone)]
328         struct BufWriter(Arc<Mutex<Vec<u8>>>);
329
330         impl Write for BufWriter {
331             fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
332                 self.0.lock().unwrap().write(buf)
333             }
334             fn flush(&mut self) -> io::Result<()> {
335                 self.0.lock().unwrap().flush()
336             }
337         }
338         let buf = BufWriter::default();
339         let output = buf.clone();
340         je.json_rendered
341             .new_emitter(
342                 Box::new(buf),
343                 Some(je.sm.clone()),
344                 je.fluent_bundle.clone(),
345                 je.fallback_bundle.clone(),
346                 false,
347                 je.terminal_width,
348                 je.macro_backtrace,
349             )
350             .ui_testing(je.ui_testing)
351             .emit_diagnostic(diag);
352         let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
353         let output = String::from_utf8(output).unwrap();
354
355         let translated_message = je.translate_messages(&diag.message, &args);
356         Diagnostic {
357             message: translated_message.to_string(),
358             code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
359             level: diag.level.to_str(),
360             spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
361             children: diag
362                 .children
363                 .iter()
364                 .map(|c| Diagnostic::from_sub_diagnostic(c, &args, je))
365                 .chain(sugg)
366                 .collect(),
367             rendered: Some(output),
368         }
369     }
370
371     fn from_sub_diagnostic(
372         diag: &SubDiagnostic,
373         args: &FluentArgs<'_>,
374         je: &JsonEmitter,
375     ) -> Diagnostic {
376         let translated_message = je.translate_messages(&diag.message, args);
377         Diagnostic {
378             message: translated_message.to_string(),
379             code: None,
380             level: diag.level.to_str(),
381             spans: diag
382                 .render_span
383                 .as_ref()
384                 .map(|sp| DiagnosticSpan::from_multispan(sp, args, je))
385                 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, args, je)),
386             children: vec![],
387             rendered: None,
388         }
389     }
390 }
391
392 impl DiagnosticSpan {
393     fn from_span_label(
394         span: SpanLabel,
395         suggestion: Option<(&String, Applicability)>,
396         args: &FluentArgs<'_>,
397         je: &JsonEmitter,
398     ) -> DiagnosticSpan {
399         Self::from_span_etc(
400             span.span,
401             span.is_primary,
402             span.label.as_ref().map(|m| je.translate_message(m, args)).map(|m| m.to_string()),
403             suggestion,
404             je,
405         )
406     }
407
408     fn from_span_etc(
409         span: Span,
410         is_primary: bool,
411         label: Option<String>,
412         suggestion: Option<(&String, Applicability)>,
413         je: &JsonEmitter,
414     ) -> DiagnosticSpan {
415         // obtain the full backtrace from the `macro_backtrace`
416         // helper; in some ways, it'd be better to expand the
417         // backtrace ourselves, but the `macro_backtrace` helper makes
418         // some decision, such as dropping some frames, and I don't
419         // want to duplicate that logic here.
420         let backtrace = span.macro_backtrace();
421         DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
422     }
423
424     fn from_span_full(
425         span: Span,
426         is_primary: bool,
427         label: Option<String>,
428         suggestion: Option<(&String, Applicability)>,
429         mut backtrace: impl Iterator<Item = ExpnData>,
430         je: &JsonEmitter,
431     ) -> DiagnosticSpan {
432         let start = je.sm.lookup_char_pos(span.lo());
433         let end = je.sm.lookup_char_pos(span.hi());
434         let backtrace_step = backtrace.next().map(|bt| {
435             let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
436             let def_site_span = Self::from_span_full(
437                 je.sm.guess_head_span(bt.def_site),
438                 false,
439                 None,
440                 None,
441                 [].into_iter(),
442                 je,
443             );
444             Box::new(DiagnosticSpanMacroExpansion {
445                 span: call_site,
446                 macro_decl_name: bt.kind.descr(),
447                 def_site_span,
448             })
449         });
450
451         DiagnosticSpan {
452             file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
453             byte_start: start.file.original_relative_byte_pos(span.lo()).0,
454             byte_end: start.file.original_relative_byte_pos(span.hi()).0,
455             line_start: start.line,
456             line_end: end.line,
457             column_start: start.col.0 + 1,
458             column_end: end.col.0 + 1,
459             is_primary,
460             text: DiagnosticSpanLine::from_span(span, je),
461             suggested_replacement: suggestion.map(|x| x.0.clone()),
462             suggestion_applicability: suggestion.map(|x| x.1),
463             expansion: backtrace_step,
464             label,
465         }
466     }
467
468     fn from_multispan(
469         msp: &MultiSpan,
470         args: &FluentArgs<'_>,
471         je: &JsonEmitter,
472     ) -> Vec<DiagnosticSpan> {
473         msp.span_labels()
474             .into_iter()
475             .map(|span_str| Self::from_span_label(span_str, None, args, je))
476             .collect()
477     }
478
479     fn from_suggestion(
480         suggestion: &CodeSuggestion,
481         args: &FluentArgs<'_>,
482         je: &JsonEmitter,
483     ) -> Vec<DiagnosticSpan> {
484         suggestion
485             .substitutions
486             .iter()
487             .flat_map(|substitution| {
488                 substitution.parts.iter().map(move |suggestion_inner| {
489                     let span_label =
490                         SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
491                     DiagnosticSpan::from_span_label(
492                         span_label,
493                         Some((&suggestion_inner.snippet, suggestion.applicability)),
494                         args,
495                         je,
496                     )
497                 })
498             })
499             .collect()
500     }
501 }
502
503 impl DiagnosticSpanLine {
504     fn line_from_source_file(
505         sf: &rustc_span::SourceFile,
506         index: usize,
507         h_start: usize,
508         h_end: usize,
509     ) -> DiagnosticSpanLine {
510         DiagnosticSpanLine {
511             text: sf.get_line(index).map_or_else(String::new, |l| l.into_owned()),
512             highlight_start: h_start,
513             highlight_end: h_end,
514         }
515     }
516
517     /// Creates a list of DiagnosticSpanLines from span - each line with any part
518     /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
519     /// `span` within the line.
520     fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
521         je.sm
522             .span_to_lines(span)
523             .map(|lines| {
524                 // We can't get any lines if the source is unavailable.
525                 if !je.sm.ensure_source_file_source_present(lines.file.clone()) {
526                     return vec![];
527                 }
528
529                 let sf = &*lines.file;
530                 lines
531                     .lines
532                     .iter()
533                     .map(|line| {
534                         DiagnosticSpanLine::line_from_source_file(
535                             sf,
536                             line.line_index,
537                             line.start_col.0 + 1,
538                             line.end_col.0 + 1,
539                         )
540                     })
541                     .collect()
542             })
543             .unwrap_or_else(|_| vec![])
544     }
545 }
546
547 impl DiagnosticCode {
548     fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
549         s.map(|s| {
550             let s = match s {
551                 DiagnosticId::Error(s) => s,
552                 DiagnosticId::Lint { name, .. } => name,
553             };
554             let je_result =
555                 je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
556
557             DiagnosticCode { code: s, explanation: je_result.unwrap_or(None) }
558         })
559     }
560 }