Skip to content

Commit 2fd2210

Browse files
committed
prevent other encode methods from breaking derive(RustcEncodable)
1 parent 28c9fda commit 2fd2210

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

src/librustc_incremental/persist/data.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
//! The data that we will serialize and deserialize.
1212
1313
use rustc::dep_graph::DepNode;
14-
use rustc_serialize::{Decoder as RustcDecoder,
15-
Encodable as RustcEncodable, Encoder as RustcEncoder};
14+
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};
1615

1716
use super::directory::DefPathIndex;
1817

@@ -32,4 +31,3 @@ pub struct SerializedHash {
3231
/// the hash itself, computed by `calculate_item_hash`
3332
pub hash: u64,
3433
}
35-

src/librustc_incremental/persist/directory.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use rustc::hir::map::DefPath;
1818
use rustc::hir::def_id::DefId;
1919
use rustc::ty;
2020
use rustc::util::nodemap::DefIdMap;
21-
use rustc_serialize::{Decoder as RustcDecoder,
22-
Encodable as RustcEncodable, Encoder as RustcEncoder};
21+
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};
2322
use std::fmt::{self, Debug};
2423

2524
/// Index into the DefIdDirectory

src/libsyntax_ext/deriving/encodable.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
162162
attributes: Vec::new(),
163163
is_unsafe: false,
164164
combine_substructure: combine_substructure(Box::new(|a, b, c| {
165-
encodable_substructure(a, b, c)
165+
encodable_substructure(a, b, c, krate)
166166
})),
167167
}
168168
),
@@ -173,12 +173,14 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
173173
}
174174

175175
fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
176-
substr: &Substructure) -> P<Expr> {
176+
substr: &Substructure, krate: &'static str) -> P<Expr> {
177177
let encoder = substr.nonself_args[0].clone();
178178
// throw an underscore in front to suppress unused variable warnings
179179
let blkarg = cx.ident_of("_e");
180180
let blkencoder = cx.expr_ident(trait_span, blkarg);
181-
let encode = cx.ident_of("encode");
181+
let fn_path = cx.expr_path(cx.path_global(trait_span, vec![cx.ident_of(krate),
182+
cx.ident_of("Encodable"),
183+
cx.ident_of("encode")]));
182184

183185
return match *substr.fields {
184186
Struct(_, ref fields) => {
@@ -196,8 +198,8 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
196198
token::intern_and_get_ident(&format!("_field{}", i))
197199
}
198200
};
199-
let enc = cx.expr_method_call(span, self_.clone(),
200-
encode, vec!(blkencoder.clone()));
201+
let self_ref = cx.expr_addr_of(span, self_.clone());
202+
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]);
201203
let lambda = cx.lambda_expr_1(span, enc, blkarg);
202204
let call = cx.expr_method_call(span, blkencoder.clone(),
203205
emit_struct_field,
@@ -245,8 +247,9 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
245247
if !fields.is_empty() {
246248
let last = fields.len() - 1;
247249
for (i, &FieldInfo { ref self_, span, .. }) in fields.iter().enumerate() {
248-
let enc = cx.expr_method_call(span, self_.clone(),
249-
encode, vec!(blkencoder.clone()));
250+
let self_ref = cx.expr_addr_of(span, self_.clone());
251+
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref,
252+
blkencoder.clone()]);
250253
let lambda = cx.lambda_expr_1(span, enc, blkarg);
251254
let call = cx.expr_method_call(span, blkencoder.clone(),
252255
emit_variant_arg,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2016 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+
#![feature(rustc_private)]
12+
13+
#[allow(dead_code)]
14+
15+
extern crate serialize as rustc_serialize;
16+
17+
#[derive(RustcDecodable, RustcEncodable,Debug)]
18+
struct A {
19+
a: String,
20+
}
21+
22+
trait Trait {
23+
fn encode(&self);
24+
}
25+
26+
impl<T> Trait for T {
27+
fn encode(&self) {
28+
unimplemented!()
29+
}
30+
}
31+
32+
fn main() {}

0 commit comments

Comments
 (0)