Skip to content

Commit 50572d6

Browse files
committed
Implement Encoder for Diagnostic manually
...so we can skip serializing `tool_metadata` if it hasn't been set. This makes the output a bit cleaner, and avoiding having to update a bunch of unrelated tests.
1 parent 82ccb65 commit 50572d6

12 files changed

+84
-52
lines changed

compiler/rustc_errors/src/json.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::sync::{Arc, Mutex};
2727
use std::vec;
2828

2929
use rustc_serialize::json::{as_json, as_pretty_json};
30+
use rustc_serialize::{Encodable, Encoder};
3031

3132
#[cfg(test)]
3233
mod tests;
@@ -169,7 +170,8 @@ impl Emitter for JsonEmitter {
169170

170171
// The following data types are provided just for serialisation.
171172

172-
#[derive(Encodable)]
173+
// NOTE: this has a manual implementation of Encodable which needs to be updated in
174+
// parallel.
173175
struct Diagnostic {
174176
/// The primary error message.
175177
message: String,
@@ -185,6 +187,36 @@ struct Diagnostic {
185187
tool_metadata: ToolMetadata,
186188
}
187189

190+
macro_rules! encode_fields {
191+
($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
192+
{
193+
let mut idx = $idx;
194+
$(
195+
$enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
196+
idx += 1;
197+
)+
198+
idx
199+
}
200+
};
201+
}
202+
203+
// Special-case encoder to skip tool_metadata if not set
204+
impl<E: Encoder> Encodable<E> for Diagnostic {
205+
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
206+
s.emit_struct("diagnostic", 7, |s| {
207+
let mut idx = 0;
208+
209+
idx = encode_fields!(s, self, idx, [message, code, level, spans, children, rendered]);
210+
if self.tool_metadata.is_set() {
211+
idx = encode_fields!(s, self, idx, [tool_metadata]);
212+
}
213+
214+
let _ = idx;
215+
Ok(())
216+
})
217+
}
218+
}
219+
188220
#[derive(Encodable)]
189221
struct DiagnosticSpan {
190222
file_name: String,

compiler/rustc_errors/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ impl ToolMetadata {
8383
fn new(json: Json) -> Self {
8484
ToolMetadata(Some(json))
8585
}
86+
87+
fn is_set(&self) -> bool {
88+
self.0.is_some()
89+
}
8690
}
8791

8892
impl Hash for ToolMetadata {

src/test/ui/json-bom-plus-crlf-multifile.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ This error occurs when an expression was used in a place where the compiler
2424
expected an expression of a different type. It can occur in several cases, the
2525
most common being when calling a function and passing an argument which has a
2626
different type than the matching type in the function declaration.
27-
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":621,"byte_end":622,"line_start":17,"line_end":17,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":22,"highlight_end":23}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":612,"byte_end":618,"line_start":17,"line_end":17,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":621,"byte_end":622,"line_start":17,"line_end":17,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":22,"highlight_end":23}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null,"tool_metadata":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:17:22: error[E0308]: mismatched types
28-
","tool_metadata":null}
27+
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":621,"byte_end":622,"line_start":17,"line_end":17,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":22,"highlight_end":23}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":612,"byte_end":618,"line_start":17,"line_end":17,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":621,"byte_end":622,"line_start":17,"line_end":17,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":22,"highlight_end":23}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:17:22: error[E0308]: mismatched types
28+
"}
2929
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
3030

3131
Erroneous code examples:
@@ -52,8 +52,8 @@ This error occurs when an expression was used in a place where the compiler
5252
expected an expression of a different type. It can occur in several cases, the
5353
most common being when calling a function and passing an argument which has a
5454
different type than the matching type in the function declaration.
55-
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":681,"byte_end":682,"line_start":19,"line_end":19,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1","highlight_start":22,"highlight_end":23}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":672,"byte_end":678,"line_start":19,"line_end":19,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String = 1","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":681,"byte_end":682,"line_start":19,"line_end":19,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1","highlight_start":22,"highlight_end":23}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null,"tool_metadata":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:19:22: error[E0308]: mismatched types
56-
","tool_metadata":null}
55+
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":681,"byte_end":682,"line_start":19,"line_end":19,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1","highlight_start":22,"highlight_end":23}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":672,"byte_end":678,"line_start":19,"line_end":19,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String = 1","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":681,"byte_end":682,"line_start":19,"line_end":19,"column_start":22,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1","highlight_start":22,"highlight_end":23}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:19:22: error[E0308]: mismatched types
56+
"}
5757
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
5858

5959
Erroneous code examples:
@@ -80,8 +80,8 @@ This error occurs when an expression was used in a place where the compiler
8080
expected an expression of a different type. It can occur in several cases, the
8181
most common being when calling a function and passing an argument which has a
8282
different type than the matching type in the function declaration.
83-
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":745,"byte_end":746,"line_start":23,"line_end":23,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"1; // Error after the newline.","highlight_start":1,"highlight_end":2}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":735,"byte_end":741,"line_start":22,"line_end":22,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String =","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":745,"byte_end":746,"line_start":23,"line_end":23,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"1; // Error after the newline.","highlight_start":1,"highlight_end":2}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null,"tool_metadata":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:23:1: error[E0308]: mismatched types
84-
","tool_metadata":null}
83+
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":745,"byte_end":746,"line_start":23,"line_end":23,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"1; // Error after the newline.","highlight_start":1,"highlight_end":2}],"label":"expected struct `String`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":735,"byte_end":741,"line_start":22,"line_end":22,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String =","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"try using a conversion method","code":null,"level":"help","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":745,"byte_end":746,"line_start":23,"line_end":23,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"1; // Error after the newline.","highlight_start":1,"highlight_end":2}],"label":null,"suggested_replacement":"1.to_string()","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:23:1: error[E0308]: mismatched types
84+
"}
8585
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
8686

8787
Erroneous code examples:
@@ -109,6 +109,6 @@ expected an expression of a different type. It can occur in several cases, the
109109
most common being when calling a function and passing an argument which has a
110110
different type than the matching type in the function declaration.
111111
"},"level":"error","spans":[{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":801,"byte_end":809,"line_start":25,"line_end":26,"column_start":22,"column_end":6,"is_primary":true,"text":[{"text":" let s : String = (","highlight_start":22,"highlight_end":23},{"text":" ); // Error spanning the newline.","highlight_start":1,"highlight_end":6}],"label":"expected struct `String`, found `()`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","byte_start":792,"byte_end":798,"line_start":25,"line_end":25,"column_start":13,"column_end":19,"is_primary":false,"text":[{"text":" let s : String = (","highlight_start":13,"highlight_end":19}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:25:22: error[E0308]: mismatched types
112-
","tool_metadata":null}
112+
"}
113113
{"message":"aborting due to 4 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 4 previous errors
114-
","tool_metadata":null}
114+
"}

0 commit comments

Comments
 (0)