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