Skip to content

Commit

Permalink
Make sure all fields are accounted for in encode_fields!
Browse files Browse the repository at this point in the history
This will make sure the encoder will get updated if any new fields are
added to Diagnostic.
  • Loading branch information
jsgf committed Feb 7, 2021
1 parent 50572d6 commit 91d8c3b
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,24 @@ struct Diagnostic {
}

macro_rules! encode_fields {
($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
(
$enc:expr, // encoder
$idx:expr, // starting field index
$struct:expr, // struct we're serializing
$struct_name:ident, // struct name
[ $($name:ident),+$(,)? ], // fields to encode
[ $($ignore:ident),+$(,)? ] // fields we're skipping
) => {
{
// Pattern match to make sure all fields are accounted for
let $struct_name { $($name,)+ $($ignore: _,)+ } = $struct;
let mut idx = $idx;
$(
$enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
$enc.emit_struct_field(
stringify!($name),
idx,
|enc| $name.encode(enc),
)?;
idx += 1;
)+
idx
Expand All @@ -206,9 +219,23 @@ impl<E: Encoder> Encodable<E> for Diagnostic {
s.emit_struct("diagnostic", 7, |s| {
let mut idx = 0;

idx = encode_fields!(s, self, idx, [message, code, level, spans, children, rendered]);
idx = encode_fields!(
s,
idx,
self,
Self,
[message, code, level, spans, children, rendered],
[tool_metadata]
);
if self.tool_metadata.is_set() {
idx = encode_fields!(s, self, idx, [tool_metadata]);
idx = encode_fields!(
s,
idx,
self,
Self,
[tool_metadata],
[message, code, level, spans, children, rendered]
);
}

let _ = idx;
Expand Down

0 comments on commit 91d8c3b

Please sign in to comment.