Skip to content

Commit 757f106

Browse files
committed
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?
2 parents 529b19f + c39271e commit 757f106

File tree

6 files changed

+85
-27
lines changed

6 files changed

+85
-27
lines changed

src/librustc/middle/typeck/collect.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
631631
parent_visibility);
632632

633633
for trait_ref in opt_trait_ref.iter() {
634-
let trait_ref = instantiate_trait_ref(ccx, trait_ref, selfty);
635-
636-
// Prevent the builtin kind traits from being manually implemented.
637-
if tcx.lang_items.to_builtin_kind(trait_ref.def_id).is_some() {
638-
tcx.sess.span_err(it.span,
639-
"cannot provide an explicit implementation \
640-
for a builtin kind");
641-
}
634+
instantiate_trait_ref(ccx, trait_ref, selfty);
642635
}
643636
},
644637
ast::ItemTrait(ref generics, _, _, ref trait_methods) => {

src/libsyntax/ext/deriving/bounds.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use ast::{MetaItem, MetaWord, Item};
12+
use codemap::Span;
13+
use ext::base::ExtCtxt;
14+
use ext::deriving::generic::*;
15+
16+
pub fn expand_deriving_bound(cx: &mut ExtCtxt,
17+
span: Span,
18+
mitem: @MetaItem,
19+
item: @Item,
20+
push: |@Item|) {
21+
22+
let name = match mitem.node {
23+
MetaWord(ref tname) => {
24+
match tname.get() {
25+
"Copy" => "Copy",
26+
"Send" => "Send",
27+
"Share" => "Share",
28+
ref tname => cx.span_bug(span,
29+
format!("expected built-in trait name but found {}",
30+
*tname))
31+
}
32+
},
33+
_ => return cx.span_err(span, "unexpected value in deriving, expected a trait")
34+
};
35+
36+
let trait_def = TraitDef {
37+
span: span,
38+
attributes: Vec::new(),
39+
path: Path::new(vec!("std", "kinds", name)),
40+
additional_bounds: Vec::new(),
41+
generics: LifetimeBounds::empty(),
42+
methods: vec!()
43+
};
44+
45+
trait_def.expand(cx, mitem, item, push)
46+
}

src/libsyntax/ext/deriving/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
2222
use ext::base::ExtCtxt;
2323
use codemap::Span;
2424

25+
pub mod bounds;
2526
pub mod clone;
2627
pub mod encodable;
2728
pub mod decodable;
@@ -90,6 +91,10 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
9091

9192
"FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
9293

94+
"Send" => expand!(bounds::expand_deriving_bound),
95+
"Share" => expand!(bounds::expand_deriving_bound),
96+
"Copy" => expand!(bounds::expand_deriving_bound),
97+
9398
ref tname => {
9499
cx.span_err(titem.span, format!("unknown \
95100
`deriving` trait: `{}`", *tname));

src/test/compile-fail/cant-implement-builtin-kinds.rs

-19
This file was deleted.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//NOTE: Remove in the next snapshot
12+
#[cfg(not(stage0))]
13+
#[deriving(Share(Bad),Send,Copy)]
14+
//~^ ERROR unexpected value in deriving, expected a trait
15+
struct Test;
16+
17+
pub fn main() {}

src/test/run-pass/deriving-bounds.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//NOTE: Remove in the next snapshot
12+
#[cfg(not(stage0))]
13+
#[deriving(Share,Send,Copy)]
14+
struct Test;
15+
16+
pub fn main() {}

0 commit comments

Comments
 (0)