2015-12-31 01:43:42 +00:00
|
|
|
//! A JSON emitter for errors.
|
2015-12-31 05:47:14 +00:00
|
|
|
//!
|
|
|
|
//! This works by converting errors to a simplified structural format (see the
|
2017-08-15 19:45:21 +00:00
|
|
|
//! structs at the start of the file) and then serializing them. These should
|
2015-12-31 05:47:14 +00:00
|
|
|
//! contain as much information about the error as possible.
|
|
|
|
//!
|
|
|
|
//! The format of the JSON output should be considered *unstable*. For now the
|
|
|
|
//! structs at the end of this file (Diagnostic*) specify the error format.
|
|
|
|
|
2019-01-11 16:32:31 +00:00
|
|
|
// FIXME: spec the JSON output properly.
|
2015-12-31 05:47:14 +00:00
|
|
|
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::source_map::{FilePathMapping, SourceMap};
|
2023-07-26 13:58:50 +00:00
|
|
|
use termcolor::{ColorSpec, WriteColor};
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2023-09-15 13:32:34 +00:00
|
|
|
use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
|
2019-11-14 22:24:44 +00:00
|
|
|
use crate::registry::Registry;
|
2022-10-03 13:02:49 +00:00
|
|
|
use crate::translation::{to_fluent_args, Translate};
|
2022-04-12 08:34:40 +00:00
|
|
|
use crate::{
|
2024-01-13 02:11:56 +00:00
|
|
|
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
|
|
|
|
SubDiagnostic, TerminalUrl,
|
2022-04-12 08:34:40 +00:00
|
|
|
};
|
2021-07-11 20:08:58 +00:00
|
|
|
use rustc_lint_defs::Applicability;
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2020-10-31 02:14:32 +00:00
|
|
|
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
|
2022-03-26 07:27:43 +00:00
|
|
|
use rustc_error_messages::FluentArgs;
|
2020-01-20 23:27:14 +00:00
|
|
|
use rustc_span::hygiene::ExpnData;
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::Span;
|
2023-01-08 22:35:43 +00:00
|
|
|
use std::error::Report;
|
2015-12-31 05:47:14 +00:00
|
|
|
use std::io::{self, Write};
|
2019-05-02 02:06:33 +00:00
|
|
|
use std::path::Path;
|
2017-11-17 07:55:22 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2016-04-16 01:23:50 +00:00
|
|
|
use std::vec;
|
2015-12-31 05:47:14 +00:00
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
use serde::Serialize;
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2019-10-03 00:55:31 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2015-12-31 01:43:42 +00:00
|
|
|
pub struct JsonEmitter {
|
2020-10-31 02:14:32 +00:00
|
|
|
dst: IntoDynSyncSend<Box<dyn Write + Send>>,
|
2015-12-31 05:47:14 +00:00
|
|
|
registry: Option<Registry>,
|
2019-11-15 13:32:31 +00:00
|
|
|
sm: Lrc<SourceMap>,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 08:34:40 +00:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty: bool,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: bool,
|
2023-09-15 13:32:34 +00:00
|
|
|
ignored_directories_in_source_blocks: Vec<String>,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width: Option<usize>,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace: bool,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics: bool,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url: TerminalUrl,
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonEmitter {
|
2019-03-12 12:06:43 +00:00
|
|
|
pub fn stderr(
|
|
|
|
registry: Option<Registry>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 08:34:40 +00:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2019-03-12 12:06:43 +00:00
|
|
|
pretty: bool,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width: Option<usize>,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace: bool,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics: bool,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url: TerminalUrl,
|
2019-03-12 12:06:43 +00:00
|
|
|
) -> JsonEmitter {
|
2016-09-26 22:45:50 +00:00
|
|
|
JsonEmitter {
|
2020-10-31 02:14:32 +00:00
|
|
|
dst: IntoDynSyncSend(Box::new(io::BufWriter::new(io::stderr()))),
|
2017-08-07 05:54:09 +00:00
|
|
|
registry,
|
2018-10-29 20:26:13 +00:00
|
|
|
sm: source_map,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle,
|
2022-03-26 07:27:43 +00:00
|
|
|
fallback_bundle,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: false,
|
2023-09-15 13:32:34 +00:00
|
|
|
ignored_directories_in_source_blocks: Vec::new(),
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url,
|
2016-09-26 22:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 13:57:11 +00:00
|
|
|
pub fn basic(
|
|
|
|
pretty: bool,
|
|
|
|
json_rendered: HumanReadableErrorType,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 08:34:40 +00:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width: Option<usize>,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace: bool,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics: bool,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url: TerminalUrl,
|
2019-09-07 13:57:11 +00:00
|
|
|
) -> JsonEmitter {
|
2017-04-24 17:01:19 +00:00
|
|
|
let file_path_mapping = FilePathMapping::empty();
|
2018-08-18 10:13:35 +00:00
|
|
|
JsonEmitter::stderr(
|
|
|
|
None,
|
|
|
|
Lrc::new(SourceMap::new(file_path_mapping)),
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle,
|
2022-03-26 07:27:43 +00:00
|
|
|
fallback_bundle,
|
2019-09-07 13:57:11 +00:00
|
|
|
pretty,
|
|
|
|
json_rendered,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url,
|
2019-09-07 13:57:11 +00:00
|
|
|
)
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 12:06:43 +00:00
|
|
|
pub fn new(
|
|
|
|
dst: Box<dyn Write + Send>,
|
|
|
|
registry: Option<Registry>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 08:34:40 +00:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2019-03-12 12:06:43 +00:00
|
|
|
pretty: bool,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width: Option<usize>,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace: bool,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics: bool,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url: TerminalUrl,
|
2019-03-12 12:06:43 +00:00
|
|
|
) -> JsonEmitter {
|
2015-12-31 01:43:42 +00:00
|
|
|
JsonEmitter {
|
2020-10-31 02:14:32 +00:00
|
|
|
dst: IntoDynSyncSend(dst),
|
2017-08-07 05:54:09 +00:00
|
|
|
registry,
|
2018-10-29 20:26:13 +00:00
|
|
|
sm: source_map,
|
2022-03-28 08:36:20 +00:00
|
|
|
fluent_bundle,
|
2022-03-26 07:27:43 +00:00
|
|
|
fallback_bundle,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: false,
|
2023-09-15 13:32:34 +00:00
|
|
|
ignored_directories_in_source_blocks: Vec::new(),
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered,
|
2022-07-06 10:57:41 +00:00
|
|
|
diagnostic_width,
|
2019-12-15 15:12:30 +00:00
|
|
|
macro_backtrace,
|
2022-10-18 22:08:20 +00:00
|
|
|
track_diagnostics,
|
2023-02-09 10:16:00 +00:00
|
|
|
terminal_url,
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 00:18:53 +00:00
|
|
|
|
|
|
|
pub fn ui_testing(self, ui_testing: bool) -> Self {
|
|
|
|
Self { ui_testing, ..self }
|
|
|
|
}
|
2023-09-15 13:32:34 +00:00
|
|
|
|
|
|
|
pub fn ignored_directories_in_source_blocks(self, value: Vec<String>) -> Self {
|
|
|
|
Self { ignored_directories_in_source_blocks: value, ..self }
|
|
|
|
}
|
2023-09-09 01:47:59 +00:00
|
|
|
|
|
|
|
fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> {
|
|
|
|
if self.pretty {
|
2023-09-09 18:30:23 +00:00
|
|
|
serde_json::to_writer_pretty(&mut *self.dst, &val)?
|
2023-09-09 01:47:59 +00:00
|
|
|
} else {
|
2023-09-09 18:30:23 +00:00
|
|
|
serde_json::to_writer(&mut *self.dst, &val)?
|
|
|
|
};
|
|
|
|
self.dst.write_all(b"\n")?;
|
|
|
|
self.dst.flush()
|
2023-09-09 01:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
2023-10-13 22:50:01 +00:00
|
|
|
#[serde(tag = "$message_type", rename_all = "snake_case")]
|
2023-09-09 01:47:59 +00:00
|
|
|
enum EmitTyped<'a> {
|
|
|
|
Diagnostic(Diagnostic),
|
|
|
|
Artifact(ArtifactNotification<'a>),
|
2023-09-09 17:02:51 +00:00
|
|
|
FutureIncompat(FutureIncompatReport<'a>),
|
2023-09-09 01:47:59 +00:00
|
|
|
UnusedExtern(UnusedExterns<'a, 'a, 'a>),
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:30:47 +00:00
|
|
|
impl Translate for JsonEmitter {
|
|
|
|
fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
|
|
|
|
self.fluent_bundle.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fallback_fluent_bundle(&self) -> &FluentBundle {
|
2022-11-29 11:01:17 +00:00
|
|
|
&self.fallback_bundle
|
2022-08-10 16:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 01:43:42 +00:00
|
|
|
impl Emitter for JsonEmitter {
|
2019-11-14 22:24:44 +00:00
|
|
|
fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
|
2019-10-15 06:12:55 +00:00
|
|
|
let data = Diagnostic::from_errors_diagnostic(diag, self);
|
2023-09-09 01:47:59 +00:00
|
|
|
let result = self.emit(EmitTyped::Diagnostic(data));
|
2017-11-03 12:38:26 +00:00
|
|
|
if let Err(e) = result {
|
2023-07-25 20:00:13 +00:00
|
|
|
panic!("failed to print diagnostics: {e:?}");
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 22:26:08 +00:00
|
|
|
|
2019-05-21 19:38:46 +00:00
|
|
|
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
|
|
|
|
let data = ArtifactNotification { artifact: path, emit: artifact_type };
|
2023-09-09 01:47:59 +00:00
|
|
|
let result = self.emit(EmitTyped::Artifact(data));
|
2019-04-14 22:26:08 +00:00
|
|
|
if let Err(e) = result {
|
2023-07-25 20:00:13 +00:00
|
|
|
panic!("failed to print notification: {e:?}");
|
2019-04-14 22:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 23:53:33 +00:00
|
|
|
|
2021-07-11 20:08:58 +00:00
|
|
|
fn emit_future_breakage_report(&mut self, diags: Vec<crate::Diagnostic>) {
|
2023-09-09 17:02:51 +00:00
|
|
|
let data: Vec<FutureBreakageItem<'_>> = diags
|
2020-08-13 19:41:52 +00:00
|
|
|
.into_iter()
|
2021-07-11 20:08:58 +00:00
|
|
|
.map(|mut diag| {
|
2020-08-13 19:41:52 +00:00
|
|
|
if diag.level == crate::Level::Allow {
|
2024-01-09 01:28:45 +00:00
|
|
|
diag.level = crate::Level::Warning;
|
2020-08-13 19:41:52 +00:00
|
|
|
}
|
2023-09-09 17:02:51 +00:00
|
|
|
FutureBreakageItem {
|
|
|
|
diagnostic: EmitTyped::Diagnostic(Diagnostic::from_errors_diagnostic(
|
|
|
|
&diag, self,
|
|
|
|
)),
|
|
|
|
}
|
2020-08-13 19:41:52 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2020-10-18 19:28:23 +00:00
|
|
|
let report = FutureIncompatReport { future_incompat_report: data };
|
2023-09-09 01:47:59 +00:00
|
|
|
let result = self.emit(EmitTyped::FutureIncompat(report));
|
2020-08-13 19:41:52 +00:00
|
|
|
if let Err(e) = result {
|
2023-07-25 20:00:13 +00:00
|
|
|
panic!("failed to print future breakage report: {e:?}");
|
2020-08-13 19:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 08:24:49 +00:00
|
|
|
fn emit_unused_externs(&mut self, lint_level: rustc_lint_defs::Level, unused_externs: &[&str]) {
|
|
|
|
let lint_level = lint_level.as_str();
|
2020-08-09 23:57:35 +00:00
|
|
|
let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
|
2023-09-09 01:47:59 +00:00
|
|
|
let result = self.emit(EmitTyped::UnusedExtern(data));
|
2020-06-30 16:58:15 +00:00
|
|
|
if let Err(e) = result {
|
2023-07-25 20:00:13 +00:00
|
|
|
panic!("failed to print unused externs: {e:?}");
|
2020-06-30 16:58:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:32:31 +00:00
|
|
|
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
|
2019-10-14 04:48:39 +00:00
|
|
|
Some(&self.sm)
|
|
|
|
}
|
|
|
|
|
2019-09-24 23:53:33 +00:00
|
|
|
fn should_show_explain(&self) -> bool {
|
2020-10-27 01:02:48 +00:00
|
|
|
!matches!(self.json_rendered, HumanReadableErrorType::Short(_))
|
2019-09-24 23:53:33 +00:00
|
|
|
}
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following data types are provided just for serialisation.
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2017-01-11 21:55:41 +00:00
|
|
|
struct Diagnostic {
|
2015-12-31 05:47:14 +00:00
|
|
|
/// The primary error message.
|
2017-01-11 21:55:41 +00:00
|
|
|
message: String,
|
2015-12-31 05:47:14 +00:00
|
|
|
code: Option<DiagnosticCode>,
|
|
|
|
/// "error: internal compiler error", "error", "warning", "note", "help".
|
|
|
|
level: &'static str,
|
2015-12-13 12:12:47 +00:00
|
|
|
spans: Vec<DiagnosticSpan>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// Associated diagnostic messages.
|
2017-01-11 21:55:41 +00:00
|
|
|
children: Vec<Diagnostic>,
|
2017-11-17 07:55:22 +00:00
|
|
|
/// The message as rustc would render it.
|
2016-04-16 01:23:50 +00:00
|
|
|
rendered: Option<String>,
|
2021-01-18 22:10:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2015-12-31 05:47:14 +00:00
|
|
|
struct DiagnosticSpan {
|
|
|
|
file_name: String,
|
|
|
|
byte_start: u32,
|
|
|
|
byte_end: u32,
|
|
|
|
/// 1-based.
|
|
|
|
line_start: usize,
|
|
|
|
line_end: usize,
|
|
|
|
/// 1-based, character offset.
|
|
|
|
column_start: usize,
|
|
|
|
column_end: usize,
|
2016-04-20 18:57:20 +00:00
|
|
|
/// Is this a "primary" span -- meaning the point, or one of the points,
|
|
|
|
/// where the error occurred?
|
|
|
|
is_primary: bool,
|
2016-03-24 02:32:42 +00:00
|
|
|
/// Source text from the start of line_start to the end of line_end.
|
|
|
|
text: Vec<DiagnosticSpanLine>,
|
2016-04-20 18:57:20 +00:00
|
|
|
/// Label that should be placed at this location (if any)
|
|
|
|
label: Option<String>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// If we are suggesting a replacement, this will contain text
|
2017-11-03 11:37:11 +00:00
|
|
|
/// that should be sliced in atop this span.
|
2016-04-16 01:23:50 +00:00
|
|
|
suggested_replacement: Option<String>,
|
2018-01-18 11:47:46 +00:00
|
|
|
/// If the suggestion is approximate
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion_applicability: Option<Applicability>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// Macro invocations that created the code at this span, if any.
|
|
|
|
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
|
2016-03-24 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2016-03-24 02:32:42 +00:00
|
|
|
struct DiagnosticSpanLine {
|
|
|
|
text: String,
|
2016-04-16 01:23:50 +00:00
|
|
|
|
2016-03-24 02:32:42 +00:00
|
|
|
/// 1-based, character offset in self.text.
|
|
|
|
highlight_start: usize,
|
2016-04-16 01:23:50 +00:00
|
|
|
|
2016-03-24 02:32:42 +00:00
|
|
|
highlight_end: usize,
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2016-04-16 01:23:50 +00:00
|
|
|
struct DiagnosticSpanMacroExpansion {
|
|
|
|
/// span where macro was applied to generate this code; note that
|
|
|
|
/// this may itself derive from a macro (if
|
|
|
|
/// `span.expansion.is_some()`)
|
|
|
|
span: DiagnosticSpan,
|
|
|
|
|
|
|
|
/// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
|
|
|
|
macro_decl_name: String,
|
|
|
|
|
|
|
|
/// span where macro was defined (if known)
|
2019-06-30 00:05:52 +00:00
|
|
|
def_site_span: DiagnosticSpan,
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2015-12-31 05:47:14 +00:00
|
|
|
struct DiagnosticCode {
|
2024-01-13 02:11:56 +00:00
|
|
|
/// The error code (e.g. "E1234"), if the diagnostic has one. Or the lint
|
|
|
|
/// name, if it's a lint without an error code.
|
2015-12-31 05:47:14 +00:00
|
|
|
code: String,
|
|
|
|
/// An explanation for the code.
|
|
|
|
explanation: Option<&'static str>,
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2019-05-02 02:06:33 +00:00
|
|
|
struct ArtifactNotification<'a> {
|
|
|
|
/// The path of the artifact.
|
|
|
|
artifact: &'a Path,
|
2019-05-21 19:38:46 +00:00
|
|
|
/// What kind of artifact we're emitting.
|
|
|
|
emit: &'a str,
|
2019-04-14 22:26:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2023-09-09 17:02:51 +00:00
|
|
|
struct FutureBreakageItem<'a> {
|
2023-11-21 00:02:04 +00:00
|
|
|
// Always EmitTyped::Diagnostic, but we want to make sure it gets serialized
|
|
|
|
// with "$message_type".
|
2023-09-09 17:02:51 +00:00
|
|
|
diagnostic: EmitTyped<'a>,
|
2020-08-13 19:41:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2023-09-09 17:02:51 +00:00
|
|
|
struct FutureIncompatReport<'a> {
|
|
|
|
future_incompat_report: Vec<FutureBreakageItem<'a>>,
|
2020-10-18 19:28:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 02:07:13 +00:00
|
|
|
// NOTE: Keep this in sync with the equivalent structs in rustdoc's
|
|
|
|
// doctest component (as well as cargo).
|
|
|
|
// We could unify this struct the one in rustdoc but they have different
|
|
|
|
// ownership semantics, so doing so would create wasteful allocations.
|
2021-06-03 19:14:15 +00:00
|
|
|
#[derive(Serialize)]
|
2020-08-09 23:57:35 +00:00
|
|
|
struct UnusedExterns<'a, 'b, 'c> {
|
|
|
|
/// The severity level of the unused dependencies lint
|
|
|
|
lint_level: &'a str,
|
2020-06-30 16:58:15 +00:00
|
|
|
/// List of unused externs by their names.
|
2020-08-09 23:57:35 +00:00
|
|
|
unused_extern_names: &'b [&'c str],
|
2020-06-30 16:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 21:55:41 +00:00
|
|
|
impl Diagnostic {
|
2019-11-14 22:24:44 +00:00
|
|
|
fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
|
2022-10-03 13:02:49 +00:00
|
|
|
let args = to_fluent_args(diag.args());
|
2022-03-26 07:27:43 +00:00
|
|
|
let sugg = diag.suggestions.iter().flatten().map(|sugg| {
|
2023-01-08 22:35:43 +00:00
|
|
|
let translated_message =
|
|
|
|
je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();
|
2022-03-26 07:27:43 +00:00
|
|
|
Diagnostic {
|
|
|
|
message: translated_message.to_string(),
|
|
|
|
code: None,
|
|
|
|
level: "help",
|
|
|
|
spans: DiagnosticSpan::from_suggestion(sugg, &args, je),
|
|
|
|
children: vec![],
|
|
|
|
rendered: None,
|
|
|
|
}
|
2017-03-24 16:31:41 +00:00
|
|
|
});
|
2017-11-17 07:55:22 +00:00
|
|
|
|
|
|
|
// generate regular command line output and store it in the json
|
|
|
|
|
|
|
|
// A threadsafe buffer for writing.
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
struct BufWriter(Arc<Mutex<Vec<u8>>>);
|
|
|
|
|
|
|
|
impl Write for BufWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.lock().unwrap().write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.0.lock().unwrap().flush()
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 13:58:50 +00:00
|
|
|
impl WriteColor for BufWriter {
|
|
|
|
fn supports_color(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_color(&mut self, _spec: &ColorSpec) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 07:55:22 +00:00
|
|
|
let buf = BufWriter::default();
|
|
|
|
let output = buf.clone();
|
2019-09-07 13:57:11 +00:00
|
|
|
je.json_rendered
|
2023-07-25 13:25:38 +00:00
|
|
|
.new_emitter(Box::new(buf), je.fallback_bundle.clone())
|
|
|
|
.sm(Some(je.sm.clone()))
|
|
|
|
.fluent_bundle(je.fluent_bundle.clone())
|
|
|
|
.diagnostic_width(je.diagnostic_width)
|
|
|
|
.macro_backtrace(je.macro_backtrace)
|
|
|
|
.track_diagnostics(je.track_diagnostics)
|
|
|
|
.terminal_url(je.terminal_url)
|
2019-10-15 06:12:55 +00:00
|
|
|
.ui_testing(je.ui_testing)
|
2023-09-15 13:32:34 +00:00
|
|
|
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
|
2019-10-15 06:12:55 +00:00
|
|
|
.emit_diagnostic(diag);
|
2017-11-17 07:55:22 +00:00
|
|
|
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
|
|
|
|
let output = String::from_utf8(output).unwrap();
|
|
|
|
|
2023-12-20 06:12:17 +00:00
|
|
|
let translated_message = je.translate_messages(&diag.messages, &args);
|
2024-01-13 02:11:56 +00:00
|
|
|
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-13 23:57:07 +00:00
|
|
|
let code = if let Some(code) = diag.code {
|
2024-01-13 02:11:56 +00:00
|
|
|
Some(DiagnosticCode {
|
|
|
|
code: code.to_string(),
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-13 23:57:07 +00:00
|
|
|
explanation: je.registry.as_ref().unwrap().try_find_description(code).ok(),
|
2024-01-13 02:11:56 +00:00
|
|
|
})
|
|
|
|
} else if let Some(IsLint { name, .. }) = &diag.is_lint {
|
|
|
|
Some(DiagnosticCode { code: name.to_string(), explanation: None })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2015-12-31 05:47:14 +00:00
|
|
|
Diagnostic {
|
2022-03-26 07:27:43 +00:00
|
|
|
message: translated_message.to_string(),
|
2024-01-13 02:11:56 +00:00
|
|
|
code,
|
2019-10-15 06:12:55 +00:00
|
|
|
level: diag.level.to_str(),
|
2022-03-26 07:27:43 +00:00
|
|
|
spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
|
2019-10-15 06:12:55 +00:00
|
|
|
children: diag
|
|
|
|
.children
|
|
|
|
.iter()
|
2022-03-26 07:27:43 +00:00
|
|
|
.map(|c| Diagnostic::from_sub_diagnostic(c, &args, je))
|
2017-05-09 08:04:24 +00:00
|
|
|
.chain(sugg)
|
|
|
|
.collect(),
|
2017-11-20 08:40:55 +00:00
|
|
|
rendered: Some(output),
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:27:43 +00:00
|
|
|
fn from_sub_diagnostic(
|
|
|
|
diag: &SubDiagnostic,
|
|
|
|
args: &FluentArgs<'_>,
|
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> Diagnostic {
|
2023-12-20 06:12:17 +00:00
|
|
|
let translated_message = je.translate_messages(&diag.messages, args);
|
2015-12-31 05:47:14 +00:00
|
|
|
Diagnostic {
|
2022-03-26 07:27:43 +00:00
|
|
|
message: translated_message.to_string(),
|
2015-12-31 05:47:14 +00:00
|
|
|
code: None,
|
2019-10-15 06:12:55 +00:00
|
|
|
level: diag.level.to_str(),
|
2023-12-20 23:30:17 +00:00
|
|
|
spans: DiagnosticSpan::from_multispan(&diag.span, args, je),
|
2015-12-31 05:47:14 +00:00
|
|
|
children: vec![],
|
2017-05-09 08:04:24 +00:00
|
|
|
rendered: None,
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpan {
|
2016-04-20 18:57:20 +00:00
|
|
|
fn from_span_label(
|
|
|
|
span: SpanLabel,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2022-03-26 07:27:43 +00:00
|
|
|
args: &FluentArgs<'_>,
|
2016-04-20 18:57:20 +00:00
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> DiagnosticSpan {
|
2022-03-24 02:03:04 +00:00
|
|
|
Self::from_span_etc(
|
|
|
|
span.span,
|
|
|
|
span.is_primary,
|
2023-01-08 22:35:43 +00:00
|
|
|
span.label
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| je.translate_message(m, args).unwrap())
|
|
|
|
.map(|m| m.to_string()),
|
2022-03-24 02:03:04 +00:00
|
|
|
suggestion,
|
|
|
|
je,
|
|
|
|
)
|
2016-04-20 18:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_span_etc(
|
|
|
|
span: Span,
|
|
|
|
is_primary: bool,
|
|
|
|
label: Option<String>,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2016-04-20 18:57:20 +00:00
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> DiagnosticSpan {
|
2016-04-16 01:23:50 +00:00
|
|
|
// obtain the full backtrace from the `macro_backtrace`
|
|
|
|
// helper; in some ways, it'd be better to expand the
|
|
|
|
// backtrace ourselves, but the `macro_backtrace` helper makes
|
|
|
|
// some decision, such as dropping some frames, and I don't
|
|
|
|
// want to duplicate that logic here.
|
2020-01-20 23:46:53 +00:00
|
|
|
let backtrace = span.macro_backtrace();
|
2016-04-20 18:57:20 +00:00
|
|
|
DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:57:20 +00:00
|
|
|
fn from_span_full(
|
|
|
|
span: Span,
|
|
|
|
is_primary: bool,
|
|
|
|
label: Option<String>,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2020-01-20 23:46:53 +00:00
|
|
|
mut backtrace: impl Iterator<Item = ExpnData>,
|
2016-04-20 18:57:20 +00:00
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> DiagnosticSpan {
|
2018-10-29 20:26:13 +00:00
|
|
|
let start = je.sm.lookup_char_pos(span.lo());
|
|
|
|
let end = je.sm.lookup_char_pos(span.hi());
|
2016-04-20 18:57:20 +00:00
|
|
|
let backtrace_step = backtrace.next().map(|bt| {
|
|
|
|
let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
|
2022-02-24 16:16:45 +00:00
|
|
|
let def_site_span = Self::from_span_full(
|
|
|
|
je.sm.guess_head_span(bt.def_site),
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
[].into_iter(),
|
|
|
|
je,
|
|
|
|
);
|
2016-04-20 18:57:20 +00:00
|
|
|
Box::new(DiagnosticSpanMacroExpansion {
|
|
|
|
span: call_site,
|
2020-01-20 23:27:14 +00:00
|
|
|
macro_decl_name: bt.kind.descr(),
|
2017-08-07 05:54:09 +00:00
|
|
|
def_site_span,
|
2016-04-20 18:57:20 +00:00
|
|
|
})
|
|
|
|
});
|
2018-01-19 07:34:14 +00:00
|
|
|
|
2016-04-16 01:23:50 +00:00
|
|
|
DiagnosticSpan {
|
2021-08-26 10:46:01 +00:00
|
|
|
file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
|
2019-10-03 00:55:31 +00:00
|
|
|
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
|
|
|
|
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
|
2016-04-16 01:23:50 +00:00
|
|
|
line_start: start.line,
|
|
|
|
line_end: end.line,
|
|
|
|
column_start: start.col.0 + 1,
|
|
|
|
column_end: end.col.0 + 1,
|
2017-08-07 05:54:09 +00:00
|
|
|
is_primary,
|
2016-04-16 01:23:50 +00:00
|
|
|
text: DiagnosticSpanLine::from_span(span, je),
|
2018-01-18 11:47:46 +00:00
|
|
|
suggested_replacement: suggestion.map(|x| x.0.clone()),
|
2018-05-06 20:30:06 +00:00
|
|
|
suggestion_applicability: suggestion.map(|x| x.1),
|
2016-04-16 01:23:50 +00:00
|
|
|
expansion: backtrace_step,
|
2017-08-07 05:54:09 +00:00
|
|
|
label,
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:27:43 +00:00
|
|
|
fn from_multispan(
|
|
|
|
msp: &MultiSpan,
|
|
|
|
args: &FluentArgs<'_>,
|
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> Vec<DiagnosticSpan> {
|
2016-04-20 18:57:20 +00:00
|
|
|
msp.span_labels()
|
|
|
|
.into_iter()
|
2022-03-26 07:27:43 +00:00
|
|
|
.map(|span_str| Self::from_span_label(span_str, None, args, je))
|
2016-04-20 18:57:20 +00:00
|
|
|
.collect()
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:27:43 +00:00
|
|
|
fn from_suggestion(
|
|
|
|
suggestion: &CodeSuggestion,
|
|
|
|
args: &FluentArgs<'_>,
|
|
|
|
je: &JsonEmitter,
|
|
|
|
) -> Vec<DiagnosticSpan> {
|
2017-11-03 15:17:33 +00:00
|
|
|
suggestion
|
|
|
|
.substitutions
|
2017-05-09 08:04:24 +00:00
|
|
|
.iter()
|
2017-05-11 13:26:22 +00:00
|
|
|
.flat_map(|substitution| {
|
2018-01-18 11:47:46 +00:00
|
|
|
substitution.parts.iter().map(move |suggestion_inner| {
|
2017-05-09 08:04:24 +00:00
|
|
|
let span_label =
|
2018-01-18 11:47:46 +00:00
|
|
|
SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
|
2017-05-09 08:04:24 +00:00
|
|
|
DiagnosticSpan::from_span_label(
|
|
|
|
span_label,
|
2018-01-18 11:47:46 +00:00
|
|
|
Some((&suggestion_inner.snippet, suggestion.applicability)),
|
2022-03-26 07:27:43 +00:00
|
|
|
args,
|
2017-05-09 08:04:24 +00:00
|
|
|
je,
|
|
|
|
)
|
|
|
|
})
|
2016-04-20 18:57:20 +00:00
|
|
|
})
|
|
|
|
.collect()
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2016-04-03 22:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpanLine {
|
2018-08-18 10:13:56 +00:00
|
|
|
fn line_from_source_file(
|
2020-02-22 14:07:05 +00:00
|
|
|
sf: &rustc_span::SourceFile,
|
2016-04-03 22:32:37 +00:00
|
|
|
index: usize,
|
|
|
|
h_start: usize,
|
|
|
|
h_end: usize,
|
|
|
|
) -> DiagnosticSpanLine {
|
|
|
|
DiagnosticSpanLine {
|
2021-03-26 19:32:37 +00:00
|
|
|
text: sf.get_line(index).map_or_else(String::new, |l| l.into_owned()),
|
2016-04-03 22:32:37 +00:00
|
|
|
highlight_start: h_start,
|
|
|
|
highlight_end: h_end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates a list of DiagnosticSpanLines from span - each line with any part
|
2016-04-03 22:32:37 +00:00
|
|
|
/// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
|
|
|
|
/// `span` within the line.
|
2016-04-16 01:23:50 +00:00
|
|
|
fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
|
2018-10-29 20:26:13 +00:00
|
|
|
je.sm
|
|
|
|
.span_to_lines(span)
|
2019-04-10 23:40:12 +00:00
|
|
|
.map(|lines| {
|
2020-03-05 15:31:11 +00:00
|
|
|
// We can't get any lines if the source is unavailable.
|
2023-09-15 13:32:34 +00:00
|
|
|
if !should_show_source_code(
|
|
|
|
&je.ignored_directories_in_source_blocks,
|
|
|
|
&je.sm,
|
|
|
|
&lines.file,
|
|
|
|
) {
|
2020-03-05 15:31:11 +00:00
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
2020-02-22 14:07:05 +00:00
|
|
|
let sf = &*lines.file;
|
2019-04-10 23:40:12 +00:00
|
|
|
lines
|
|
|
|
.lines
|
|
|
|
.iter()
|
|
|
|
.map(|line| {
|
|
|
|
DiagnosticSpanLine::line_from_source_file(
|
2020-02-22 14:07:05 +00:00
|
|
|
sf,
|
2019-04-10 23:40:12 +00:00
|
|
|
line.line_index,
|
|
|
|
line.start_col.0 + 1,
|
|
|
|
line.end_col.0 + 1,
|
|
|
|
)
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
2019-04-10 23:40:12 +00:00
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| vec![])
|
2016-04-03 22:32:37 +00:00
|
|
|
}
|
2016-03-24 02:32:42 +00:00
|
|
|
}
|