prevent other encode methods from breaking derive(RustcEncodable)

This commit is contained in:
Oliver Schneider 2016-04-12 15:41:46 +02:00
parent 28c9fdafc0
commit 2fd2210b88
4 changed files with 44 additions and 12 deletions

View file

@ -11,8 +11,7 @@
//! The data that we will serialize and deserialize.
use rustc::dep_graph::DepNode;
use rustc_serialize::{Decoder as RustcDecoder,
Encodable as RustcEncodable, Encoder as RustcEncoder};
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};
use super::directory::DefPathIndex;
@ -32,4 +31,3 @@ pub struct SerializedHash {
/// the hash itself, computed by `calculate_item_hash`
pub hash: u64,
}

View file

@ -18,8 +18,7 @@ use rustc::hir::map::DefPath;
use rustc::hir::def_id::DefId;
use rustc::ty;
use rustc::util::nodemap::DefIdMap;
use rustc_serialize::{Decoder as RustcDecoder,
Encodable as RustcEncodable, Encoder as RustcEncoder};
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};
use std::fmt::{self, Debug};
/// Index into the DefIdDirectory

View file

@ -162,7 +162,7 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
attributes: Vec::new(),
is_unsafe: false,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
encodable_substructure(a, b, c)
encodable_substructure(a, b, c, krate)
})),
}
),
@ -173,12 +173,14 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
}
fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
substr: &Substructure) -> P<Expr> {
substr: &Substructure, krate: &'static str) -> P<Expr> {
let encoder = substr.nonself_args[0].clone();
// throw an underscore in front to suppress unused variable warnings
let blkarg = cx.ident_of("_e");
let blkencoder = cx.expr_ident(trait_span, blkarg);
let encode = cx.ident_of("encode");
let fn_path = cx.expr_path(cx.path_global(trait_span, vec![cx.ident_of(krate),
cx.ident_of("Encodable"),
cx.ident_of("encode")]));
return match *substr.fields {
Struct(_, ref fields) => {
@ -196,8 +198,8 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
token::intern_and_get_ident(&format!("_field{}", i))
}
};
let enc = cx.expr_method_call(span, self_.clone(),
encode, vec!(blkencoder.clone()));
let self_ref = cx.expr_addr_of(span, self_.clone());
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]);
let lambda = cx.lambda_expr_1(span, enc, blkarg);
let call = cx.expr_method_call(span, blkencoder.clone(),
emit_struct_field,
@ -245,8 +247,9 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
if !fields.is_empty() {
let last = fields.len() - 1;
for (i, &FieldInfo { ref self_, span, .. }) in fields.iter().enumerate() {
let enc = cx.expr_method_call(span, self_.clone(),
encode, vec!(blkencoder.clone()));
let self_ref = cx.expr_addr_of(span, self_.clone());
let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref,
blkencoder.clone()]);
let lambda = cx.lambda_expr_1(span, enc, blkarg);
let call = cx.expr_method_call(span, blkencoder.clone(),
emit_variant_arg,

View file

@ -0,0 +1,32 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(rustc_private)]
#[allow(dead_code)]
extern crate serialize as rustc_serialize;
#[derive(RustcDecodable, RustcEncodable,Debug)]
struct A {
a: String,
}
trait Trait {
fn encode(&self);
}
impl<T> Trait for T {
fn encode(&self) {
unimplemented!()
}
}
fn main() {}