diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-05-16 14:15:37 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-05-16 22:55:08 +1000 |
| commit | cd2eb4701fc16e7acd6934759be043b1f7e5586d (patch) | |
| tree | 056658f88fad7d623ef7843e51992dc8c258050d /src/libsyntax/ext | |
| parent | 2329651770d026ff422ad40dabe576965f46c852 (diff) | |
| download | rust-cd2eb4701fc16e7acd6934759be043b1f7e5586d.tar.gz rust-cd2eb4701fc16e7acd6934759be043b1f7e5586d.zip | |
syntax: implement #[deriving(DeepClone)]. Fixes #6514.
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/deriving/clone.rs | 47 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/mod.rs | 1 |
2 files changed, 42 insertions, 6 deletions
diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index e2d6685ec65..935566b33d8 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, + ~"Non-matching enum variants in `deriving(" + + name + ")`"), + StaticEnum(*) | StaticStruct(*) => cx.span_bug(span, + ~"Static method in `deriving(" + + name + ")`") } match *all_fields { @@ -76,7 +111,7 @@ fn cs_clone(cx: @ext_ctxt, span: span, Some(i) => i, None => cx.span_bug(span, ~"unnamed field in normal struct \ - in `deriving(Clone)`") + in `deriving(" + 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), |
