about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-05-16 06:58:52 -0700
committerbors <bors@rust-lang.org>2013-05-16 06:58:52 -0700
commit53196bb364426fb2b8db16f6748836779cd031c7 (patch)
treef41ac73b63fe90b628884b5f919fbf4841c3f3a0 /src
parentf19883223cbf72ec28b503cae71f823fc2229708 (diff)
parent47c9157d872fed7a7ef422efe7176f8a29b949d8 (diff)
downloadrust-53196bb364426fb2b8db16f6748836779cd031c7.tar.gz
rust-53196bb364426fb2b8db16f6748836779cd031c7.zip
auto merge of #6530 : huonw/rust/deriving-deepclone, r=bstrie
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/ext/deriving/clone.rs49
-rw-r--r--src/libsyntax/ext/deriving/mod.rs1
-rw-r--r--src/test/run-pass/deriving-clone-enum.rs7
-rw-r--r--src/test/run-pass/deriving-clone-generic-enum.rs17
-rw-r--r--src/test/run-pass/deriving-clone-generic-struct.rs6
-rw-r--r--src/test/run-pass/deriving-clone-generic-tuple-struct.rs16
-rw-r--r--src/test/run-pass/deriving-clone-struct.rs12
7 files changed, 92 insertions, 16 deletions
diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs
index e2d6685ec65..aceb60ebbd7 100644
--- a/src/libsyntax/ext/deriving/clone.rs
+++ b/src/libsyntax/ext/deriving/clone.rs
@@ -32,7 +32,7 @@ pub fn expand_deriving_clone(cx: @ext_ctxt,
                 args: ~[],
                 ret_ty: Self,
                 const_nonmatching: false,
-                combine_substructure: cs_clone
+                combine_substructure: |c, s, sub| cs_clone("Clone", c, s, sub)
             }
         ]
     };
@@ -42,8 +42,39 @@ pub fn expand_deriving_clone(cx: @ext_ctxt,
                             &trait_def)
 }
 
-fn cs_clone(cx: @ext_ctxt, span: span,
-            substr: &Substructure) -> @expr {
+pub fn expand_deriving_deep_clone(cx: @ext_ctxt,
+                                 span: span,
+                                 mitem: @meta_item,
+                                 in_items: ~[@item])
+    -> ~[@item] {
+    let trait_def = TraitDef {
+        path: Path::new(~[~"core", ~"clone", ~"DeepClone"]),
+        additional_bounds: ~[],
+        generics: LifetimeBounds::empty(),
+        methods: ~[
+            MethodDef {
+                name: ~"deep_clone",
+                generics: LifetimeBounds::empty(),
+                explicit_self: borrowed_explicit_self(),
+                args: ~[],
+                ret_ty: Self,
+                const_nonmatching: false,
+                // cs_clone uses the ident passed to it, i.e. it will
+                // call deep_clone (not clone) here.
+                combine_substructure: |c, s, sub| cs_clone("DeepClone", c, s, sub)
+            }
+        ]
+    };
+
+    expand_deriving_generic(cx, span,
+                            mitem, in_items,
+                            &trait_def)
+}
+
+fn cs_clone(
+    name: &str,
+    cx: @ext_ctxt, span: span,
+    substr: &Substructure) -> @expr {
     let clone_ident = substr.method_ident;
     let ctor_ident;
     let all_fields;
@@ -59,8 +90,12 @@ fn cs_clone(cx: @ext_ctxt, span: span,
             ctor_ident = ~[ variant.node.name ];
             all_fields = af;
         },
-        EnumNonMatching(*) => cx.span_bug(span, "Non-matching enum variants in `deriving(Clone)`"),
-        StaticEnum(*) | StaticStruct(*) => cx.span_bug(span, "Static method in `deriving(Clone)`")
+        EnumNonMatching(*) => cx.span_bug(span,
+                                          fmt!("Non-matching enum variants in `deriving(%s)`",
+                                               name)),
+        StaticEnum(*) | StaticStruct(*) => cx.span_bug(span,
+                                                       fmt!("Static method in `deriving(%s)`",
+                                                            name))
     }
 
     match *all_fields {
@@ -75,8 +110,8 @@ fn cs_clone(cx: @ext_ctxt, span: span,
                 let ident = match o_id {
                     Some(i) => i,
                     None => cx.span_bug(span,
-                                        ~"unnamed field in normal struct \
-                                          in `deriving(Clone)`")
+                                        fmt!("unnamed field in normal struct in `deriving(%s)`",
+                                             name))
                 };
                 build::Field { ident: ident, ex: subcall(self_f) }
             };
diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs
index 99785d55761..78cd5cdb423 100644
--- a/src/libsyntax/ext/deriving/mod.rs
+++ b/src/libsyntax/ext/deriving/mod.rs
@@ -84,6 +84,7 @@ pub fn expand_meta_deriving(cx: @ext_ctxt,
                                                                    titem, in_items)));
                         match *tname {
                             ~"Clone" => expand!(clone::expand_deriving_clone),
+                            ~"DeepClone" => expand!(clone::expand_deriving_deep_clone),
 
                             ~"IterBytes" => expand!(iter_bytes::expand_deriving_iter_bytes),
 
diff --git a/src/test/run-pass/deriving-clone-enum.rs b/src/test/run-pass/deriving-clone-enum.rs
index 969e1fb5dd6..9ce965aa49f 100644
--- a/src/test/run-pass/deriving-clone-enum.rs
+++ b/src/test/run-pass/deriving-clone-enum.rs
@@ -8,11 +8,14 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#[deriving(Clone)]
+#[deriving(Clone, DeepClone)]
 enum E {
     A,
     B(()),
     C
 }
 
-pub fn main() {}
+pub fn main() {
+    let _ = A.clone();
+    let _ = B(()).deep_clone();
+}
diff --git a/src/test/run-pass/deriving-clone-generic-enum.rs b/src/test/run-pass/deriving-clone-generic-enum.rs
index 23841017e93..78abbf504f3 100644
--- a/src/test/run-pass/deriving-clone-generic-enum.rs
+++ b/src/test/run-pass/deriving-clone-generic-enum.rs
@@ -1,8 +1,21 @@
-#[deriving(Clone)]
+// Copyright 2013 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.
+
+#[deriving(Clone, DeepClone)]
 enum E<T,U> {
     A(T),
     B(T,U),
     C
 }
 
-fn main() {}
+fn main() {
+    let _ = A::<int, int>(1i).clone();
+    let _ = B(1i, 1.234).deep_clone();
+}
diff --git a/src/test/run-pass/deriving-clone-generic-struct.rs b/src/test/run-pass/deriving-clone-generic-struct.rs
index 0a7a5a3aa75..fd300cbc8b7 100644
--- a/src/test/run-pass/deriving-clone-generic-struct.rs
+++ b/src/test/run-pass/deriving-clone-generic-struct.rs
@@ -8,11 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#[deriving(Clone)]
+#[deriving(Clone, DeepClone)]
 struct S<T> {
     foo: (),
     bar: (),
     baz: T,
 }
 
-pub fn main() {}
+pub fn main() {
+    let _ = S { foo: (), bar: (), baz: 1i }.clone().deep_clone();
+}
diff --git a/src/test/run-pass/deriving-clone-generic-tuple-struct.rs b/src/test/run-pass/deriving-clone-generic-tuple-struct.rs
index d6a69e8e6ac..c082a11eab8 100644
--- a/src/test/run-pass/deriving-clone-generic-tuple-struct.rs
+++ b/src/test/run-pass/deriving-clone-generic-tuple-struct.rs
@@ -1,4 +1,16 @@
-#[deriving(Clone)]
+// Copyright 2013 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.
+
+#[deriving(Clone, DeepClone)]
 struct S<T>(T, ());
 
-fn main() {}
+fn main() {
+    let _ = S(1i, ()).clone().deep_clone();
+}
diff --git a/src/test/run-pass/deriving-clone-struct.rs b/src/test/run-pass/deriving-clone-struct.rs
index 4dcbadbb3ef..d540b047af7 100644
--- a/src/test/run-pass/deriving-clone-struct.rs
+++ b/src/test/run-pass/deriving-clone-struct.rs
@@ -1,4 +1,14 @@
-#[deriving(Clone)]
+// Copyright 2013 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.
+
+#[deriving(Clone, DeepClone)]
 struct S {
     _int: int,
     _i8: i8,