Skip to content

Commit 46dad76

Browse files
committed
auto merge of #14596 : Sawyer47/rust/encodable-fix, r=alexcrichton
Closes #14021
2 parents b981add + 1dc13e4 commit 46dad76

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/libsyntax/ext/deriving/encodable.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
175175
stmts.push(cx.stmt_expr(call));
176176
}
177177

178+
// unit structs have no fields and need to return Ok()
179+
if stmts.is_empty() {
180+
let ret_ok = cx.expr(trait_span,
181+
ExprRet(Some(cx.expr_ok(trait_span,
182+
cx.expr_lit(trait_span, LitNil)))));
183+
stmts.push(cx.stmt_expr(ret_ok));
184+
}
185+
178186
let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
179187
cx.expr_method_call(trait_span,
180188
encoder,

src/test/run-pass/issue-14021.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
extern crate serialize;
13+
14+
use serialize::{Encodable, Decodable};
15+
use serialize::json;
16+
17+
#[deriving(Encodable, Decodable, PartialEq, Show)]
18+
struct UnitLikeStruct;
19+
20+
pub fn main() {
21+
let obj = UnitLikeStruct;
22+
let json_str: String = json::Encoder::str_encode(&obj);
23+
24+
let json_object = json::from_str(json_str.as_slice());
25+
let mut decoder = json::Decoder::new(json_object.unwrap());
26+
let mut decoded_obj: UnitLikeStruct = Decodable::decode(&mut decoder).unwrap();
27+
28+
assert_eq!(obj, decoded_obj);
29+
}

0 commit comments

Comments
 (0)