From a51be8ecd81dd8f10cec1363cd14c3fdd7b19a1a Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Fri, 11 Apr 2014 23:48:02 +0200 Subject: [PATCH 1/2] Allow manual implementations of built-in traits [RFC #3] cc #13231 --- src/librustc/middle/typeck/collect.rs | 9 +-------- .../cant-implement-builtin-kinds.rs | 19 ------------------- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 src/test/compile-fail/cant-implement-builtin-kinds.rs diff --git a/src/librustc/middle/typeck/collect.rs b/src/librustc/middle/typeck/collect.rs index 9c49512d4dc0d..c87776ba8935e 100644 --- a/src/librustc/middle/typeck/collect.rs +++ b/src/librustc/middle/typeck/collect.rs @@ -631,14 +631,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) { parent_visibility); for trait_ref in opt_trait_ref.iter() { - let trait_ref = instantiate_trait_ref(ccx, trait_ref, selfty); - - // Prevent the builtin kind traits from being manually implemented. - if tcx.lang_items.to_builtin_kind(trait_ref.def_id).is_some() { - tcx.sess.span_err(it.span, - "cannot provide an explicit implementation \ - for a builtin kind"); - } + instantiate_trait_ref(ccx, trait_ref, selfty); } }, ast::ItemTrait(ref generics, _, _, ref trait_methods) => { diff --git a/src/test/compile-fail/cant-implement-builtin-kinds.rs b/src/test/compile-fail/cant-implement-builtin-kinds.rs deleted file mode 100644 index 6bedac6d12d6c..0000000000000 --- a/src/test/compile-fail/cant-implement-builtin-kinds.rs +++ /dev/null @@ -1,19 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// See issue #8517 for why this should be illegal. - -struct X(T); - -impl Send for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind -impl Sized for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind -impl Share for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind - -fn main() { } From c39271e99cadcc580583bb29bce6e8475db15b0a Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Wed, 30 Apr 2014 11:26:50 +0200 Subject: [PATCH 2/2] Allow built-in traits to be derived [RFC #3] cc #13231 --- src/libsyntax/ext/deriving/bounds.rs | 46 ++++++++++++++++++++++++ src/libsyntax/ext/deriving/mod.rs | 5 +++ src/test/compile-fail/deriving-bounds.rs | 17 +++++++++ src/test/run-pass/deriving-bounds.rs | 16 +++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/libsyntax/ext/deriving/bounds.rs create mode 100644 src/test/compile-fail/deriving-bounds.rs create mode 100644 src/test/run-pass/deriving-bounds.rs diff --git a/src/libsyntax/ext/deriving/bounds.rs b/src/libsyntax/ext/deriving/bounds.rs new file mode 100644 index 0000000000000..b5b2667f892de --- /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 or the MIT license +// , 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 5a980cb9de90b..1187e83308b84 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)); diff --git a/src/test/compile-fail/deriving-bounds.rs b/src/test/compile-fail/deriving-bounds.rs new file mode 100644 index 0000000000000..9d9f5527366d2 --- /dev/null +++ b/src/test/compile-fail/deriving-bounds.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/run-pass/deriving-bounds.rs b/src/test/run-pass/deriving-bounds.rs new file mode 100644 index 0000000000000..a395158470259 --- /dev/null +++ b/src/test/run-pass/deriving-bounds.rs @@ -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 or the MIT license +// , 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() {}