Skip to content

Commit cea4f53

Browse files
committed
Add type field to json diagnostic outputs
Currently the json-formatted outputs have no way to unambiguously determine which kind of message is being output. A consumer can look for specific fields in the json object (eg "message"), but there's no guarantee that in future some other kind of output will have a field of the same name. This PR adds a `"type"` field to add json outputs which can be used to unambiguously determine which kind of output it is. The mapping is: diagnostic: regular compiler diagnostics artifact: artifact notifications future_breakage: Future breakage report unused_extern: Unused crate warnings/errors This matches the "internally tagged" representation for serde enums.
1 parent b0b8c52 commit cea4f53

14 files changed

+96
-96
lines changed

compiler/rustc_errors/src/json.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ impl JsonEmitter {
138138
pub fn ui_testing(self, ui_testing: bool) -> Self {
139139
Self { ui_testing, ..self }
140140
}
141+
142+
fn emit_typed(
143+
&mut self,
144+
ty: &str,
145+
val: impl Serialize,
146+
) -> io::Result<()> {
147+
let mut v = serde_json::to_value(val).unwrap();
148+
149+
// only attempt to type objects
150+
if let Some(obj) = v.as_object_mut() {
151+
obj.insert("type".to_string(), ty.into());
152+
}
153+
154+
if self.pretty {
155+
writeln!(self.dst, "{}", serde_json::to_string_pretty(&v).unwrap())
156+
} else {
157+
writeln!(self.dst, "{}", serde_json::to_string(&v).unwrap())
158+
}
159+
.and_then(|_| self.dst.flush())
160+
}
141161
}
142162

143163
impl Translate for JsonEmitter {
@@ -153,25 +173,15 @@ impl Translate for JsonEmitter {
153173
impl Emitter for JsonEmitter {
154174
fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
155175
let data = Diagnostic::from_errors_diagnostic(diag, self);
156-
let result = if self.pretty {
157-
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
158-
} else {
159-
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
160-
}
161-
.and_then(|_| self.dst.flush());
176+
let result = self.emit_typed("diagnostic", data);
162177
if let Err(e) = result {
163178
panic!("failed to print diagnostics: {e:?}");
164179
}
165180
}
166181

167182
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
168183
let data = ArtifactNotification { artifact: path, emit: artifact_type };
169-
let result = if self.pretty {
170-
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
171-
} else {
172-
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
173-
}
174-
.and_then(|_| self.dst.flush());
184+
let result = self.emit_typed("artifact", data);
175185
if let Err(e) = result {
176186
panic!("failed to print notification: {e:?}");
177187
}
@@ -188,12 +198,7 @@ impl Emitter for JsonEmitter {
188198
})
189199
.collect();
190200
let report = FutureIncompatReport { future_incompat_report: data };
191-
let result = if self.pretty {
192-
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&report).unwrap())
193-
} else {
194-
writeln!(&mut self.dst, "{}", serde_json::to_string(&report).unwrap())
195-
}
196-
.and_then(|_| self.dst.flush());
201+
let result = self.emit_typed("future_breakage", report);
197202
if let Err(e) = result {
198203
panic!("failed to print future breakage report: {e:?}");
199204
}
@@ -202,12 +207,7 @@ impl Emitter for JsonEmitter {
202207
fn emit_unused_externs(&mut self, lint_level: rustc_lint_defs::Level, unused_externs: &[&str]) {
203208
let lint_level = lint_level.as_str();
204209
let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
205-
let result = if self.pretty {
206-
writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
207-
} else {
208-
writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
209-
}
210-
.and_then(|_| self.dst.flush());
210+
let result = self.emit_typed("unused_extern", data);
211211
if let Err(e) = result {
212212
panic!("failed to print unused externs: {e:?}");
213213
}

tests/ui/diagnostic-width/flag-json.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
1+
{"children":[],"code":{"code":"E0308","explanation":"Expected type did not match the received type.
22

33
Erroneous code examples:
44

@@ -24,17 +24,17 @@ 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/flag-json.rs","byte_start":243,"byte_end":245,"line_start":7,"line_end":7,"column_start":17,"column_end":19,"is_primary":true,"text":[{"text":" let _: () = 42;","highlight_start":17,"highlight_end":19}],"label":"expected `()`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/flag-json.rs","byte_start":238,"byte_end":240,"line_start":7,"line_end":7,"column_start":12,"column_end":14,"is_primary":false,"text":[{"text":" let _: () = 42;","highlight_start":12,"highlight_end":14}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0308]: mismatched types
27+
"},"level":"error","message":"mismatched types","rendered":"error[E0308]: mismatched types
2828
--> $DIR/flag-json.rs:7:17
2929
|
3030
LL | ..._: () = 42;
3131
| -- ^^ expected `()`, found integer
3232
| |
3333
| expected due to this
3434

35-
"}
36-
{"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to previous error
35+
","spans":[{"byte_end":245,"byte_start":243,"column_end":19,"column_start":17,"expansion":null,"file_name":"$DIR/flag-json.rs","is_primary":true,"label":"expected `()`, found integer","line_end":7,"line_start":7,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":19,"highlight_start":17,"text":" let _: () = 42;"}]},{"byte_end":240,"byte_start":238,"column_end":14,"column_start":12,"expansion":null,"file_name":"$DIR/flag-json.rs","is_primary":false,"label":"expected due to this","line_end":7,"line_start":7,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":14,"highlight_start":12,"text":" let _: () = 42;"}]}],"type":"diagnostic"}
36+
{"children":[],"code":null,"level":"error","message":"aborting due to previous error","rendered":"error: aborting due to previous error
3737

38-
"}
39-
{"message":"For more information about this error, try `rustc --explain E0308`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0308`.
40-
"}
38+
","spans":[],"type":"diagnostic"}
39+
{"children":[],"code":null,"level":"failure-note","message":"For more information about this error, try `rustc --explain E0308`.","rendered":"For more information about this error, try `rustc --explain E0308`.
40+
","spans":[],"type":"diagnostic"}

tests/ui/json/json-bom-plus-crlf-multifile.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
1+
{"children":[{"children":[],"code":null,"level":"help","message":"try using a conversion method","rendered":null,"spans":[{"byte_end":622,"byte_start":622,"column_end":23,"column_start":23,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":null,"line_end":17,"line_start":17,"suggested_replacement":".to_string()","suggestion_applicability":"MaybeIncorrect","text":[{"highlight_end":23,"highlight_start":23,"text":" let s : String = 1; // Error in the middle of line."}]}]}],"code":{"code":"E0308","explanation":"Expected type did not match the received type.
22

33
Erroneous code examples:
44

@@ -24,9 +24,9 @@ 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 `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":622,"byte_end":622,"line_start":17,"line_end":17,"column_start":23,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1; // Error in the middle of line.","highlight_start":23,"highlight_end":23}],"label":null,"suggested_replacement":".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-
"}
29-
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
27+
"},"level":"error","message":"mismatched types","rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:17:22: error[E0308]: mismatched types
28+
","spans":[{"byte_end":622,"byte_start":621,"column_end":23,"column_start":22,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":"expected `String`, found integer","line_end":17,"line_start":17,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":23,"highlight_start":22,"text":" let s : String = 1; // Error in the middle of line."}]},{"byte_end":618,"byte_start":612,"column_end":19,"column_start":13,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":false,"label":"expected due to this","line_end":17,"line_start":17,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":19,"highlight_start":13,"text":" let s : String = 1; // Error in the middle of line."}]}],"type":"diagnostic"}
29+
{"children":[{"children":[],"code":null,"level":"help","message":"try using a conversion method","rendered":null,"spans":[{"byte_end":682,"byte_start":682,"column_end":23,"column_start":23,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":null,"line_end":19,"line_start":19,"suggested_replacement":".to_string()","suggestion_applicability":"MaybeIncorrect","text":[{"highlight_end":23,"highlight_start":23,"text":" let s : String = 1"}]}]}],"code":{"code":"E0308","explanation":"Expected type did not match the received type.
3030

3131
Erroneous code examples:
3232

@@ -52,9 +52,9 @@ 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 `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":682,"byte_end":682,"line_start":19,"line_end":19,"column_start":23,"column_end":23,"is_primary":true,"text":[{"text":" let s : String = 1","highlight_start":23,"highlight_end":23}],"label":null,"suggested_replacement":".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-
"}
57-
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
55+
"},"level":"error","message":"mismatched types","rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:19:22: error[E0308]: mismatched types
56+
","spans":[{"byte_end":682,"byte_start":681,"column_end":23,"column_start":22,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":"expected `String`, found integer","line_end":19,"line_start":19,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":23,"highlight_start":22,"text":" let s : String = 1"}]},{"byte_end":678,"byte_start":672,"column_end":19,"column_start":13,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":false,"label":"expected due to this","line_end":19,"line_start":19,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":19,"highlight_start":13,"text":" let s : String = 1"}]}],"type":"diagnostic"}
57+
{"children":[{"children":[],"code":null,"level":"help","message":"try using a conversion method","rendered":null,"spans":[{"byte_end":746,"byte_start":746,"column_end":2,"column_start":2,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":null,"line_end":23,"line_start":23,"suggested_replacement":".to_string()","suggestion_applicability":"MaybeIncorrect","text":[{"highlight_end":2,"highlight_start":2,"text":"1; // Error after the newline."}]}]}],"code":{"code":"E0308","explanation":"Expected type did not match the received type.
5858

5959
Erroneous code examples:
6060

@@ -80,9 +80,9 @@ 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 `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":746,"byte_end":746,"line_start":23,"line_end":23,"column_start":2,"column_end":2,"is_primary":true,"text":[{"text":"1; // Error after the newline.","highlight_start":2,"highlight_end":2}],"label":null,"suggested_replacement":".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-
"}
85-
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
83+
"},"level":"error","message":"mismatched types","rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:23:1: error[E0308]: mismatched types
84+
","spans":[{"byte_end":746,"byte_start":745,"column_end":2,"column_start":1,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":"expected `String`, found integer","line_end":23,"line_start":23,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":2,"highlight_start":1,"text":"1; // Error after the newline."}]},{"byte_end":741,"byte_start":735,"column_end":19,"column_start":13,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":false,"label":"expected due to this","line_end":22,"line_start":22,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":19,"highlight_start":13,"text":" let s : String ="}]}],"type":"diagnostic"}
85+
{"children":[],"code":{"code":"E0308","explanation":"Expected type did not match the received type.
8686

8787
Erroneous code examples:
8888

@@ -108,7 +108,7 @@ This error occurs when an expression was used in a place where the compiler
108108
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.
111-
"},"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 `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-
"}
113-
{"message":"aborting due to 4 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 4 previous errors
114-
"}
111+
"},"level":"error","message":"mismatched types","rendered":"$DIR/json-bom-plus-crlf-multifile-aux.rs:25:22: error[E0308]: mismatched types
112+
","spans":[{"byte_end":809,"byte_start":801,"column_end":6,"column_start":22,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":true,"label":"expected `String`, found `()`","line_end":26,"line_start":25,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":23,"highlight_start":22,"text":" let s : String = ("},{"highlight_end":6,"highlight_start":1,"text":" ); // Error spanning the newline."}]},{"byte_end":798,"byte_start":792,"column_end":19,"column_start":13,"expansion":null,"file_name":"$DIR/json-bom-plus-crlf-multifile-aux.rs","is_primary":false,"label":"expected due to this","line_end":25,"line_start":25,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":19,"highlight_start":13,"text":" let s : String = ("}]}],"type":"diagnostic"}
113+
{"children":[],"code":null,"level":"error","message":"aborting due to 4 previous errors","rendered":"error: aborting due to 4 previous errors
114+
","spans":[],"type":"diagnostic"}

0 commit comments

Comments
 (0)