about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/json.rs34
-rw-r--r--compiler/rustc_errors/src/lib.rs4
2 files changed, 37 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs
index 89e844879f7..f26c439add9 100644
--- a/compiler/rustc_errors/src/json.rs
+++ b/compiler/rustc_errors/src/json.rs
@@ -27,6 +27,7 @@ use std::sync::{Arc, Mutex};
 use std::vec;
 
 use rustc_serialize::json::{as_json, as_pretty_json};
+use rustc_serialize::{Encodable, Encoder};
 
 #[cfg(test)]
 mod tests;
@@ -169,7 +170,8 @@ impl Emitter for JsonEmitter {
 
 // The following data types are provided just for serialisation.
 
-#[derive(Encodable)]
+// NOTE: this has a manual implementation of Encodable which needs to be updated in
+// parallel.
 struct Diagnostic {
     /// The primary error message.
     message: String,
@@ -185,6 +187,36 @@ struct Diagnostic {
     tool_metadata: ToolMetadata,
 }
 
+macro_rules! encode_fields {
+    ($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
+        {
+            let mut idx = $idx;
+            $(
+                $enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
+                idx += 1;
+            )+
+            idx
+        }
+    };
+}
+
+// Special-case encoder to skip tool_metadata if not set
+impl<E: Encoder> Encodable<E> for Diagnostic {
+    fn encode(&self, s: &mut E) -> Result<(), E::Error> {
+        s.emit_struct("diagnostic", 7, |s| {
+            let mut idx = 0;
+
+            idx = encode_fields!(s, self, idx, [message, code, level, spans, children, rendered]);
+            if self.tool_metadata.is_set() {
+                idx = encode_fields!(s, self, idx, [tool_metadata]);
+            }
+
+            let _ = idx;
+            Ok(())
+        })
+    }
+}
+
 #[derive(Encodable)]
 struct DiagnosticSpan {
     file_name: String,
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 73ddf507723..9800ed9bfa9 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -83,6 +83,10 @@ impl ToolMetadata {
     fn new(json: Json) -> Self {
         ToolMetadata(Some(json))
     }
+
+    fn is_set(&self) -> bool {
+        self.0.is_some()
+    }
 }
 
 impl Hash for ToolMetadata {