Skip to content

Commit

Permalink
Allow built-in traits to be derived
Browse files Browse the repository at this point in the history
[RFC #3]

cc #13231
  • Loading branch information
flaper87 committed May 1, 2014
1 parent a51be8e commit c39271e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/libsyntax/ext/deriving/bounds.rs
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions src/libsyntax/ext/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/deriving-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

//NOTE: Remove in the next snapshot
#[cfg(not(stage0))]
#[deriving(Share(Bad),Send,Copy)]
//~^ ERROR unexpected value in deriving, expected a trait
struct Test;

pub fn main() {}
16 changes: 16 additions & 0 deletions src/test/run-pass/deriving-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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.

//NOTE: Remove in the next snapshot
#[cfg(not(stage0))]
#[deriving(Share,Send,Copy)]
struct Test;

pub fn main() {}

29 comments on commit c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = 527a7a03

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = 702c49c2

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = 9cce8c63

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = 632432a4

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = aa41f598

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = a1be66ac

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at flaper87@c39271e

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging FlaPer87/rust/opt-in-phase1 = c39271e into auto

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlaPer87/rust/opt-in-phase1 = c39271e merged ok, testing candidate = 757f106

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

@bors
Copy link
Contributor

@bors bors commented on c39271e May 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 757f106

Please sign in to comment.