diff options
| author | bjorn3 <bjorn3@users.noreply.github.com> | 2021-06-03 21:14:15 +0200 |
|---|---|---|
| committer | bjorn3 <bjorn3@users.noreply.github.com> | 2022-06-03 16:46:19 +0000 |
| commit | 62a4f91a5a7c8c89e3eb681e6218d637ef419181 (patch) | |
| tree | 4487a55c87bc85ded62f1ef497c3566549dd6036 /compiler/rustc_errors/src | |
| parent | fc1df4ff170ec137ecab7d7aa7f4dc894bb17449 (diff) | |
| download | rust-62a4f91a5a7c8c89e3eb681e6218d637ef419181.tar.gz rust-62a4f91a5a7c8c89e3eb681e6218d637ef419181.zip | |
Use serde_json for json error messages
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/json.rs | 36 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json/tests.rs | 26 |
2 files changed, 30 insertions, 32 deletions
diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 6ff52182d6b..fff35ac6ac8 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -28,7 +28,7 @@ use std::path::Path; use std::sync::{Arc, Mutex}; use std::vec; -use rustc_serialize::json::{as_json, as_pretty_json}; +use serde::Serialize; #[cfg(test)] mod tests; @@ -126,9 +126,9 @@ impl Emitter for JsonEmitter { fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) { let data = Diagnostic::from_errors_diagnostic(diag, self); let result = if self.pretty { - writeln!(&mut self.dst, "{}", as_pretty_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap()) } else { - writeln!(&mut self.dst, "{}", as_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap()) } .and_then(|_| self.dst.flush()); if let Err(e) = result { @@ -139,9 +139,9 @@ impl Emitter for JsonEmitter { fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) { let data = ArtifactNotification { artifact: path, emit: artifact_type }; let result = if self.pretty { - writeln!(&mut self.dst, "{}", as_pretty_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap()) } else { - writeln!(&mut self.dst, "{}", as_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap()) } .and_then(|_| self.dst.flush()); if let Err(e) = result { @@ -161,9 +161,9 @@ impl Emitter for JsonEmitter { .collect(); let report = FutureIncompatReport { future_incompat_report: data }; let result = if self.pretty { - writeln!(&mut self.dst, "{}", as_pretty_json(&report)) + writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&report).unwrap()) } else { - writeln!(&mut self.dst, "{}", as_json(&report)) + writeln!(&mut self.dst, "{}", serde_json::to_string(&report).unwrap()) } .and_then(|_| self.dst.flush()); if let Err(e) = result { @@ -175,9 +175,9 @@ impl Emitter for JsonEmitter { let lint_level = lint_level.as_str(); let data = UnusedExterns { lint_level, unused_extern_names: unused_externs }; let result = if self.pretty { - writeln!(&mut self.dst, "{}", as_pretty_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap()) } else { - writeln!(&mut self.dst, "{}", as_json(&data)) + writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap()) } .and_then(|_| self.dst.flush()); if let Err(e) = result { @@ -204,7 +204,7 @@ impl Emitter for JsonEmitter { // The following data types are provided just for serialisation. -#[derive(Encodable)] +#[derive(Serialize)] struct Diagnostic { /// The primary error message. message: String, @@ -218,7 +218,7 @@ struct Diagnostic { rendered: Option<String>, } -#[derive(Encodable)] +#[derive(Serialize)] struct DiagnosticSpan { file_name: String, byte_start: u32, @@ -245,7 +245,7 @@ struct DiagnosticSpan { expansion: Option<Box<DiagnosticSpanMacroExpansion>>, } -#[derive(Encodable)] +#[derive(Serialize)] struct DiagnosticSpanLine { text: String, @@ -255,7 +255,7 @@ struct DiagnosticSpanLine { highlight_end: usize, } -#[derive(Encodable)] +#[derive(Serialize)] struct DiagnosticSpanMacroExpansion { /// span where macro was applied to generate this code; note that /// this may itself derive from a macro (if @@ -269,7 +269,7 @@ struct DiagnosticSpanMacroExpansion { def_site_span: DiagnosticSpan, } -#[derive(Encodable)] +#[derive(Serialize)] struct DiagnosticCode { /// The code itself. code: String, @@ -277,7 +277,7 @@ struct DiagnosticCode { explanation: Option<&'static str>, } -#[derive(Encodable)] +#[derive(Serialize)] struct ArtifactNotification<'a> { /// The path of the artifact. artifact: &'a Path, @@ -285,12 +285,12 @@ struct ArtifactNotification<'a> { emit: &'a str, } -#[derive(Encodable)] +#[derive(Serialize)] struct FutureBreakageItem { diagnostic: Diagnostic, } -#[derive(Encodable)] +#[derive(Serialize)] struct FutureIncompatReport { future_incompat_report: Vec<FutureBreakageItem>, } @@ -299,7 +299,7 @@ struct FutureIncompatReport { // 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. -#[derive(Encodable)] +#[derive(Serialize)] struct UnusedExterns<'a, 'b, 'c> { /// The severity level of the unused dependencies lint lint_level: &'a str, diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs index 7eb6a4975fe..d940d14e1db 100644 --- a/compiler/rustc_errors/src/json/tests.rs +++ b/compiler/rustc_errors/src/json/tests.rs @@ -5,12 +5,18 @@ use rustc_span::source_map::{FilePathMapping, SourceMap}; use crate::emitter::{ColorConfig, HumanReadableErrorType}; use crate::Handler; -use rustc_serialize::json; use rustc_span::{BytePos, Span}; use std::str; -#[derive(Debug, PartialEq, Eq)] +use serde::Deserialize; + +#[derive(Deserialize, Debug, PartialEq, Eq)] +struct TestData { + spans: Vec<SpanTestData>, +} + +#[derive(Deserialize, Debug, PartialEq, Eq)] struct SpanTestData { pub byte_start: u32, pub byte_end: u32, @@ -61,19 +67,11 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { let bytes = output.lock().unwrap(); let actual_output = str::from_utf8(&bytes).unwrap(); - let actual_output = json::from_str(&actual_output).unwrap(); - let spans = actual_output["spans"].as_array().unwrap(); + let actual_output: TestData = serde_json::from_str(actual_output).unwrap(); + let spans = actual_output.spans; assert_eq!(spans.len(), 1); - let obj = &spans[0]; - let actual_output = SpanTestData { - byte_start: obj["byte_start"].as_u64().unwrap() as u32, - byte_end: obj["byte_end"].as_u64().unwrap() as u32, - line_start: obj["line_start"].as_u64().unwrap() as u32, - line_end: obj["line_end"].as_u64().unwrap() as u32, - column_start: obj["column_start"].as_u64().unwrap() as u32, - column_end: obj["column_end"].as_u64().unwrap() as u32, - }; - assert_eq!(expected_output, actual_output); + + assert_eq!(expected_output, spans[0]) }) } |
