Skip to content

Commit 62a4f91

Browse files
committed
Use serde_json for json error messages
1 parent fc1df4f commit 62a4f91

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,8 @@ dependencies = [
38103810
"rustc_macros",
38113811
"rustc_serialize",
38123812
"rustc_span",
3813+
"serde",
3814+
"serde_json",
38133815
"termcolor",
38143816
"termize",
38153817
"tracing",
@@ -4025,6 +4027,7 @@ dependencies = [
40254027
"rustc_serialize",
40264028
"rustc_span",
40274029
"rustc_target",
4030+
"serde",
40284031
]
40294032

40304033
[[package]]

compiler/rustc_errors/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ atty = "0.2"
1919
termcolor = "1.0"
2020
annotate-snippets = "0.8.0"
2121
termize = "0.1.1"
22+
serde = { version = "1.0.125", features = ["derive"] }
23+
serde_json = "1.0.59"
2224

2325
[target.'cfg(windows)'.dependencies]
2426
winapi = { version = "0.3", features = ["handleapi", "synchapi", "winbase"] }

compiler/rustc_errors/src/json.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::path::Path;
2828
use std::sync::{Arc, Mutex};
2929
use std::vec;
3030

31-
use rustc_serialize::json::{as_json, as_pretty_json};
31+
use serde::Serialize;
3232

3333
#[cfg(test)]
3434
mod tests;
@@ -126,9 +126,9 @@ impl Emitter for JsonEmitter {
126126
fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
127127
let data = Diagnostic::from_errors_diagnostic(diag, self);
128128
let result = if self.pretty {
129-
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
129+
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
130130
} else {
131-
writeln!(&mut self.dst, "{}", as_json(&data))
131+
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
132132
}
133133
.and_then(|_| self.dst.flush());
134134
if let Err(e) = result {
@@ -139,9 +139,9 @@ impl Emitter for JsonEmitter {
139139
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
140140
let data = ArtifactNotification { artifact: path, emit: artifact_type };
141141
let result = if self.pretty {
142-
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
142+
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
143143
} else {
144-
writeln!(&mut self.dst, "{}", as_json(&data))
144+
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
145145
}
146146
.and_then(|_| self.dst.flush());
147147
if let Err(e) = result {
@@ -161,9 +161,9 @@ impl Emitter for JsonEmitter {
161161
.collect();
162162
let report = FutureIncompatReport { future_incompat_report: data };
163163
let result = if self.pretty {
164-
writeln!(&mut self.dst, "{}", as_pretty_json(&report))
164+
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&report).unwrap())
165165
} else {
166-
writeln!(&mut self.dst, "{}", as_json(&report))
166+
writeln!(&mut self.dst, "{}", serde_json::to_string(&report).unwrap())
167167
}
168168
.and_then(|_| self.dst.flush());
169169
if let Err(e) = result {
@@ -175,9 +175,9 @@ impl Emitter for JsonEmitter {
175175
let lint_level = lint_level.as_str();
176176
let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
177177
let result = if self.pretty {
178-
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
178+
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
179179
} else {
180-
writeln!(&mut self.dst, "{}", as_json(&data))
180+
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
181181
}
182182
.and_then(|_| self.dst.flush());
183183
if let Err(e) = result {
@@ -204,7 +204,7 @@ impl Emitter for JsonEmitter {
204204

205205
// The following data types are provided just for serialisation.
206206

207-
#[derive(Encodable)]
207+
#[derive(Serialize)]
208208
struct Diagnostic {
209209
/// The primary error message.
210210
message: String,
@@ -218,7 +218,7 @@ struct Diagnostic {
218218
rendered: Option<String>,
219219
}
220220

221-
#[derive(Encodable)]
221+
#[derive(Serialize)]
222222
struct DiagnosticSpan {
223223
file_name: String,
224224
byte_start: u32,
@@ -245,7 +245,7 @@ struct DiagnosticSpan {
245245
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
246246
}
247247

248-
#[derive(Encodable)]
248+
#[derive(Serialize)]
249249
struct DiagnosticSpanLine {
250250
text: String,
251251

@@ -255,7 +255,7 @@ struct DiagnosticSpanLine {
255255
highlight_end: usize,
256256
}
257257

258-
#[derive(Encodable)]
258+
#[derive(Serialize)]
259259
struct DiagnosticSpanMacroExpansion {
260260
/// span where macro was applied to generate this code; note that
261261
/// this may itself derive from a macro (if
@@ -269,28 +269,28 @@ struct DiagnosticSpanMacroExpansion {
269269
def_site_span: DiagnosticSpan,
270270
}
271271

272-
#[derive(Encodable)]
272+
#[derive(Serialize)]
273273
struct DiagnosticCode {
274274
/// The code itself.
275275
code: String,
276276
/// An explanation for the code.
277277
explanation: Option<&'static str>,
278278
}
279279

280-
#[derive(Encodable)]
280+
#[derive(Serialize)]
281281
struct ArtifactNotification<'a> {
282282
/// The path of the artifact.
283283
artifact: &'a Path,
284284
/// What kind of artifact we're emitting.
285285
emit: &'a str,
286286
}
287287

288-
#[derive(Encodable)]
288+
#[derive(Serialize)]
289289
struct FutureBreakageItem {
290290
diagnostic: Diagnostic,
291291
}
292292

293-
#[derive(Encodable)]
293+
#[derive(Serialize)]
294294
struct FutureIncompatReport {
295295
future_incompat_report: Vec<FutureBreakageItem>,
296296
}
@@ -299,7 +299,7 @@ struct FutureIncompatReport {
299299
// doctest component (as well as cargo).
300300
// We could unify this struct the one in rustdoc but they have different
301301
// ownership semantics, so doing so would create wasteful allocations.
302-
#[derive(Encodable)]
302+
#[derive(Serialize)]
303303
struct UnusedExterns<'a, 'b, 'c> {
304304
/// The severity level of the unused dependencies lint
305305
lint_level: &'a str,

compiler/rustc_errors/src/json/tests.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
55

66
use crate::emitter::{ColorConfig, HumanReadableErrorType};
77
use crate::Handler;
8-
use rustc_serialize::json;
98
use rustc_span::{BytePos, Span};
109

1110
use std::str;
1211

13-
#[derive(Debug, PartialEq, Eq)]
12+
use serde::Deserialize;
13+
14+
#[derive(Deserialize, Debug, PartialEq, Eq)]
15+
struct TestData {
16+
spans: Vec<SpanTestData>,
17+
}
18+
19+
#[derive(Deserialize, Debug, PartialEq, Eq)]
1420
struct SpanTestData {
1521
pub byte_start: u32,
1622
pub byte_end: u32,
@@ -61,19 +67,11 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
6167

6268
let bytes = output.lock().unwrap();
6369
let actual_output = str::from_utf8(&bytes).unwrap();
64-
let actual_output = json::from_str(&actual_output).unwrap();
65-
let spans = actual_output["spans"].as_array().unwrap();
70+
let actual_output: TestData = serde_json::from_str(actual_output).unwrap();
71+
let spans = actual_output.spans;
6672
assert_eq!(spans.len(), 1);
67-
let obj = &spans[0];
68-
let actual_output = SpanTestData {
69-
byte_start: obj["byte_start"].as_u64().unwrap() as u32,
70-
byte_end: obj["byte_end"].as_u64().unwrap() as u32,
71-
line_start: obj["line_start"].as_u64().unwrap() as u32,
72-
line_end: obj["line_end"].as_u64().unwrap() as u32,
73-
column_start: obj["column_start"].as_u64().unwrap() as u32,
74-
column_end: obj["column_end"].as_u64().unwrap() as u32,
75-
};
76-
assert_eq!(expected_output, actual_output);
73+
74+
assert_eq!(expected_output, spans[0])
7775
})
7876
}
7977

compiler/rustc_lint_defs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7+
serde = { version = "1.0.125", features = ["derive"] }
78
rustc_ast = { path = "../rustc_ast" }
89
rustc_data_structures = { path = "../rustc_data_structures" }
910
rustc_error_messages = { path = "../rustc_error_messages" }

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use rustc_span::edition::Edition;
1414
use rustc_span::{sym, symbol::Ident, Span, Symbol};
1515
use rustc_target::spec::abi::Abi;
1616

17+
use serde::{Deserialize, Serialize};
18+
1719
pub mod builtin;
1820

1921
#[macro_export]
@@ -34,7 +36,7 @@ macro_rules! pluralize {
3436
/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
3537
/// to determine whether it should be automatically applied or if the user should be consulted
3638
/// before applying the suggestion.
37-
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
39+
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable, Serialize, Deserialize)]
3840
pub enum Applicability {
3941
/// The suggestion is definitely what the user intended, or maintains the exact meaning of the code.
4042
/// This suggestion should be automatically applied.

0 commit comments

Comments
 (0)