about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-05-03 08:06:49 -0700
committerbors <bors@rust-lang.org>2014-05-03 08:06:49 -0700
commit757f106bcc771e10073bfcfa5331ad2bd5f1fe21 (patch)
tree682524424989f2a93dd11bb655b58a88bbc0950b /src/libsyntax
parent529b19f37bfcb39bf8f8709497c10c9fbd8dc5d5 (diff)
parentc39271e99cadcc580583bb29bce6e8475db15b0a (diff)
downloadrust-757f106bcc771e10073bfcfa5331ad2bd5f1fe21.tar.gz
rust-757f106bcc771e10073bfcfa5331ad2bd5f1fe21.zip
auto merge of #13868 : FlaPer87/rust/opt-in-phase1, r=alexcrichton
This is a first patch towards an opt-in built-in trait world. This patch removes the restriction on built-in traits and allows such traits to be derived.

[RFC#3]

cc #13231

@nikomatsakis r?
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/deriving/bounds.rs46
-rw-r--r--src/libsyntax/ext/deriving/mod.rs5
2 files changed, 51 insertions, 0 deletions
diff --git a/src/libsyntax/ext/deriving/bounds.rs b/src/libsyntax/ext/deriving/bounds.rs
new file mode 100644
index 00000000000..b5b2667f892
--- /dev/null
+++ b/src/libsyntax/ext/deriving/bounds.rs
@@ -0,0 +1,46 @@
+// Copyright 2014 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.
+
+use ast::{MetaItem, MetaWord, Item};
+use codemap::Span;
+use ext::base::ExtCtxt;
+use ext::deriving::generic::*;
+
+pub fn expand_deriving_bound(cx: &mut ExtCtxt,
+                             span: Span,
+                             mitem: @MetaItem,
+                             item: @Item,
+                             push: |@Item|) {
+
+    let name = match mitem.node {
+        MetaWord(ref tname) => {
+            match tname.get() {
+                "Copy" => "Copy",
+                "Send" => "Send",
+                "Share" => "Share",
+                ref tname => cx.span_bug(span,
+                                         format!("expected built-in trait name but found {}",
+                                                 *tname))
+            }
+        },
+        _ => return cx.span_err(span, "unexpected value in deriving, expected a trait")
+    };
+
+    let trait_def = TraitDef {
+        span: span,
+        attributes: Vec::new(),
+        path: Path::new(vec!("std", "kinds", name)),
+        additional_bounds: Vec::new(),
+        generics: LifetimeBounds::empty(),
+        methods: vec!()
+    };
+
+    trait_def.expand(cx, mitem, item, push)
+}
diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs
index 5a980cb9de9..1187e83308b 100644
--- a/src/libsyntax/ext/deriving/mod.rs
+++ b/src/libsyntax/ext/deriving/mod.rs
@@ -22,6 +22,7 @@ use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
 use ext::base::ExtCtxt;
 use codemap::Span;
 
+pub mod bounds;
 pub mod clone;
 pub mod encodable;
 pub mod decodable;
@@ -90,6 +91,10 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
 
                             "FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
 
+                            "Send" => expand!(bounds::expand_deriving_bound),
+                            "Share" => expand!(bounds::expand_deriving_bound),
+                            "Copy" => expand!(bounds::expand_deriving_bound),
+
                             ref tname => {
                                 cx.span_err(titem.span, format!("unknown \
                                     `deriving` trait: `{}`", *tname));