Skip to content

Commit 91d8c3b

Browse files
committed
Make sure all fields are accounted for in encode_fields!
This will make sure the encoder will get updated if any new fields are added to Diagnostic.
1 parent 50572d6 commit 91d8c3b

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

compiler/rustc_errors/src/json.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,24 @@ struct Diagnostic {
188188
}
189189

190190
macro_rules! encode_fields {
191-
($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
191+
(
192+
$enc:expr, // encoder
193+
$idx:expr, // starting field index
194+
$struct:expr, // struct we're serializing
195+
$struct_name:ident, // struct name
196+
[ $($name:ident),+$(,)? ], // fields to encode
197+
[ $($ignore:ident),+$(,)? ] // fields we're skipping
198+
) => {
192199
{
200+
// Pattern match to make sure all fields are accounted for
201+
let $struct_name { $($name,)+ $($ignore: _,)+ } = $struct;
193202
let mut idx = $idx;
194203
$(
195-
$enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
204+
$enc.emit_struct_field(
205+
stringify!($name),
206+
idx,
207+
|enc| $name.encode(enc),
208+
)?;
196209
idx += 1;
197210
)+
198211
idx
@@ -206,9 +219,23 @@ impl<E: Encoder> Encodable<E> for Diagnostic {
206219
s.emit_struct("diagnostic", 7, |s| {
207220
let mut idx = 0;
208221

209-
idx = encode_fields!(s, self, idx, [message, code, level, spans, children, rendered]);
222+
idx = encode_fields!(
223+
s,
224+
idx,
225+
self,
226+
Self,
227+
[message, code, level, spans, children, rendered],
228+
[tool_metadata]
229+
);
210230
if self.tool_metadata.is_set() {
211-
idx = encode_fields!(s, self, idx, [tool_metadata]);
231+
idx = encode_fields!(
232+
s,
233+
idx,
234+
self,
235+
Self,
236+
[tool_metadata],
237+
[message, code, level, spans, children, rendered]
238+
);
212239
}
213240

214241
let _ = idx;

0 commit comments

Comments
 (0)