Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent other encode methods from breaking derive(RustcEncodable) #32908

Merged
merged 1 commit into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/librustc_incremental/persist/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,4 +31,3 @@ pub struct SerializedHash {
/// the hash itself, computed by `calculate_item_hash`
pub hash: u64,
}

3 changes: 1 addition & 2 deletions src/librustc_incremental/persist/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/libsyntax_ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
}
),
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions src/test/run-pass-fulldeps/rustc_encodable_hygiene.rs
Original file line number Diff line number Diff line change
@@ -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() {}