about summary refs log tree commit diff
path: root/src/libsyntax_ext
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-04-14 08:23:11 -0700
committerbors <bors@rust-lang.org>2016-04-14 08:23:11 -0700
commit073a09fd63c9b4ec3bb4709986a2517ca4c3cdf1 (patch)
tree23087cf0681e16d3512f3c9d731e0440547e91c0 /src/libsyntax_ext
parentfbf8a8ce5e1d69d687b74dcc6c2068204164ed2f (diff)
parent2fd2210b880498829bf1556aee47f3753887ecfa (diff)
downloadrust-073a09fd63c9b4ec3bb4709986a2517ca4c3cdf1.tar.gz
rust-073a09fd63c9b4ec3bb4709986a2517ca4c3cdf1.zip
Auto merge of #32908 - oli-obk:hygienic_derive_encodable, r=alexcrichton
prevent other `encode` methods from breaking `derive(RustcEncodable)`

fixes https://github.com/rust-lang-nursery/rustc-serialize/issues/151
Diffstat (limited to 'src/libsyntax_ext')
-rw-r--r--src/libsyntax_ext/deriving/encodable.rs17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/libsyntax_ext/deriving/encodable.rs b/src/libsyntax_ext/deriving/encodable.rs
index a05bd7869b2..6e47f2dd08f 100644
--- a/src/libsyntax_ext/deriving/encodable.rs
+++ b/src/libsyntax_ext/deriving/encodable.rs
@@ -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,