Skip to content

Commit 540f95d

Browse files
committed
Add internal-only rustc_serialize_exclude_null attribute for making the field only exist in the json if the flag is passed
1 parent a53bdc6 commit 540f95d

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

src/libsyntax/feature_gate.rs

+5
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,11 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
788788
is just used for rustc unit tests \
789789
and will never be stable",
790790
cfg_fn!(rustc_attrs))),
791+
("rustc_serialize_exclude_null", Normal, Gated(Stability::Unstable,
792+
"rustc_attrs",
793+
"the `#[rustc_serialize_exclude_null]` attribute \
794+
is an internal-only feature",
795+
cfg_fn!(rustc_attrs))),
791796
("rustc_synthetic", Whitelisted, Gated(Stability::Unstable,
792797
"rustc_attrs",
793798
"this attribute \

src/libsyntax/json.rs

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct Diagnostic {
108108
}
109109

110110
#[derive(RustcEncodable)]
111+
#[allow(unused_attributes)]
111112
struct DiagnosticSpan {
112113
file_name: String,
113114
byte_start: u32,
@@ -129,6 +130,7 @@ struct DiagnosticSpan {
129130
/// that should be sliced in atop this span.
130131
suggested_replacement: Option<String>,
131132
/// If the suggestion is approximate
133+
#[rustc_serialize_exclude_null]
132134
suggestion_approximate: Option<bool>,
133135
/// Macro invocations that created the code at this span, if any.
134136
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(match_default_bindings)]
2626
#![feature(i128_type)]
2727
#![feature(const_atomic_usize_new)]
28+
#![feature(rustc_attrs)]
2829

2930
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
3031
#[allow(unused_extern_crates)]

src/libsyntax_ext/deriving/encodable.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn encodable_substructure(cx: &mut ExtCtxt,
190190
Struct(_, ref fields) => {
191191
let emit_struct_field = cx.ident_of("emit_struct_field");
192192
let mut stmts = Vec::new();
193-
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
193+
for (i, &FieldInfo { name, ref self_, span, attrs, .. }) in fields.iter().enumerate() {
194194
let name = match name {
195195
Some(id) => id.name,
196196
None => Symbol::intern(&format!("_field{}", i)),
@@ -212,7 +212,19 @@ fn encodable_substructure(cx: &mut ExtCtxt,
212212
} else {
213213
cx.expr(span, ExprKind::Ret(Some(call)))
214214
};
215-
stmts.push(cx.stmt_expr(call));
215+
216+
// This exists for https://github.com/rust-lang/rust/pull/47540
217+
//
218+
// If we decide to stabilize that flag this can be removed
219+
let expr = if attrs.iter().any(|a| a.check_name("rustc_serialize_exclude_null")) {
220+
let is_some = cx.ident_of("is_some");
221+
let condition = cx.expr_method_call(span, self_.clone(), is_some, vec![]);
222+
cx.expr_if(span, condition, call, None)
223+
} else {
224+
call
225+
};
226+
let stmt = cx.stmt_expr(expr);
227+
stmts.push(stmt);
216228
}
217229

218230
// unit structs have no fields and need to return Ok()

0 commit comments

Comments
 (0)