Auto merge of #23137 - kmcallister:derive-sugar, r=sfackler
This is a hack, but I don't think we can do much better as long as `derive` is running at the syntax expansion phase.
If the `custom_derive` feature gate is enabled, this works with user-defined traits and syntax extensions. Without the gate, you can't use e.g. `#[derive_Clone]` directly, so this does not change the stable language.
To make this effective, we now check gated attributes both before and after macro expansion. This uncovered a number of tests that were missing feature gates.
This PR also cleans up the deriving code somewhat, and forbids some previously-meaningless attribute syntax. For this reason it's technically a
[breaking-change]
r? @sfackler
This commit is contained in:
commit
668c647408
24 changed files with 468 additions and 205 deletions
74
src/test/auxiliary/custom_derive_plugin.rs
Normal file
74
src/test/auxiliary/custom_derive_plugin.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
// force-host
|
||||
|
||||
#![feature(plugin_registrar)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate syntax;
|
||||
extern crate rustc;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::{Decorator, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ext::deriving::generic::{cs_fold, TraitDef, MethodDef, combine_substructure};
|
||||
use syntax::ext::deriving::generic::ty::{Literal, LifetimeBounds, Path, borrowed_explicit_self};
|
||||
use syntax::parse::token;
|
||||
use syntax::ptr::P;
|
||||
use rustc::plugin::Registry;
|
||||
|
||||
#[plugin_registrar]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
reg.register_syntax_extension(
|
||||
token::intern("derive_TotalSum"),
|
||||
Decorator(box expand));
|
||||
}
|
||||
|
||||
fn expand(cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
mitem: &ast::MetaItem,
|
||||
item: &ast::Item,
|
||||
push: &mut FnMut(P<ast::Item>)) {
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: vec![],
|
||||
path: Path::new(vec!["TotalSum"]),
|
||||
additional_bounds: vec![],
|
||||
generics: LifetimeBounds::empty(),
|
||||
associated_types: vec![],
|
||||
methods: vec![
|
||||
MethodDef {
|
||||
name: "total_sum",
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![],
|
||||
ret_ty: Literal(Path::new_local("isize")),
|
||||
attributes: vec![],
|
||||
combine_substructure: combine_substructure(box |cx, span, substr| {
|
||||
let zero = cx.expr_int(span, 0);
|
||||
cs_fold(false,
|
||||
|cx, span, subexpr, field, _| {
|
||||
cx.expr_binary(span, ast::BiAdd, subexpr,
|
||||
cx.expr_method_call(span, field,
|
||||
token::str_to_ident("total_sum"), vec![]))
|
||||
},
|
||||
zero,
|
||||
box |cx, span, _, _| { cx.span_bug(span, "wtf??"); },
|
||||
cx, span, substr)
|
||||
}),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
trait_def.expand(cx, mitem, item, |i| push(i))
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(custom_attribute)]
|
||||
|
||||
#[phase(blah)]
|
||||
//~^ ERROR #[phase] is deprecated
|
||||
extern crate foo;
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#[derive(Copy(Bad))]
|
||||
//~^ ERROR unexpected value in deriving, expected a trait
|
||||
#[derive(Send)]
|
||||
//~^ ERROR this unsafe trait should be implemented explicitly
|
||||
struct Test;
|
||||
|
||||
#[derive(Sync)]
|
||||
//~^ ERROR Sync is an unsafe trait and it should be implemented explicitly
|
||||
//~^ ERROR this unsafe trait should be implemented explicitly
|
||||
struct Test1;
|
||||
|
||||
pub fn main() {}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#[derive(Eqr)] //~ ERROR unknown `derive` trait: `Eqr`
|
||||
#[derive(Eqr)]
|
||||
//~^ ERROR `#[derive]` for custom traits is not stable enough for use and is subject to change
|
||||
struct Foo;
|
||||
|
||||
pub fn main() {}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#[lang="foo"] //~ ERROR language items are subject to change
|
||||
trait Foo {}
|
||||
|
||||
extern "rust-intrinsic" { //~ ERROR intrinsics are subject to change
|
||||
fn bar();
|
||||
}
|
||||
|
|
@ -20,4 +17,3 @@ extern "rust-intrinsic" fn baz() { //~ ERROR intrinsics are subject to change
|
|||
|
||||
fn main() {
|
||||
}
|
||||
|
||||
15
src/test/compile-fail/feature-gate-lang-items.rs
Normal file
15
src/test/compile-fail/feature-gate-lang-items.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// 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.
|
||||
|
||||
#[lang="foo"] //~ ERROR language items are subject to change
|
||||
trait Foo {}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -11,5 +11,4 @@
|
|||
extern {
|
||||
#[linkage = "extern_weak"] static foo: isize;
|
||||
//~^ ERROR: the `linkage` attribute is experimental and not portable
|
||||
//~^^ ERROR: the `linkage` attribute is experimental and not portable
|
||||
}
|
||||
|
|
|
|||
25
src/test/compile-fail/malformed-derive-entry.rs
Normal file
25
src/test/compile-fail/malformed-derive-entry.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#[derive(Copy(Bad))]
|
||||
//~^ ERROR malformed `derive` entry
|
||||
struct Test1;
|
||||
|
||||
#[derive(Copy="bad")]
|
||||
//~^ ERROR malformed `derive` entry
|
||||
struct Test2;
|
||||
|
||||
#[derive()]
|
||||
//~^ WARNING empty trait list
|
||||
struct Test3;
|
||||
|
||||
#[derive]
|
||||
//~^ WARNING empty trait list
|
||||
struct Test4;
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(plugin)]
|
||||
#![plugin] //~ ERROR malformed plugin attribute
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(plugin)]
|
||||
#![plugin="bleh"] //~ ERROR malformed plugin attribute
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(plugin)]
|
||||
#![plugin(foo="bleh")] //~ ERROR malformed plugin attribute
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(plugin)]
|
||||
|
||||
#[plugin] //~ ERROR #[plugin] on `extern crate` is deprecated
|
||||
//~^ HELP use a crate attribute instead, i.e. #![plugin(std)]
|
||||
extern crate std;
|
||||
|
|
|
|||
18
src/test/compile-fail/reserved-attr-on-macro.rs
Normal file
18
src/test/compile-fail/reserved-attr-on-macro.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#[rustc_attribute_should_be_reserved] //~ ERROR attributes with the prefix `rustc_` are reserved
|
||||
macro_rules! foo {
|
||||
() => (());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
foo!();
|
||||
}
|
||||
15
src/test/compile-fail/single-derive-attr.rs
Normal file
15
src/test/compile-fail/single-derive-attr.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#[derive_Clone]
|
||||
//~^ ERROR attributes of the form `#[derive_*]` are reserved
|
||||
struct Test;
|
||||
|
||||
pub fn main() {}
|
||||
59
src/test/run-pass-fulldeps/derive-totalsum.rs
Normal file
59
src/test/run-pass-fulldeps/derive-totalsum.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
// aux-build:custom_derive_plugin.rs
|
||||
// ignore-stage1
|
||||
|
||||
#![feature(plugin, custom_derive)]
|
||||
#![plugin(custom_derive_plugin)]
|
||||
|
||||
trait TotalSum {
|
||||
fn total_sum(&self) -> isize;
|
||||
}
|
||||
|
||||
impl TotalSum for isize {
|
||||
fn total_sum(&self) -> isize {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
struct Seven;
|
||||
|
||||
impl TotalSum for Seven {
|
||||
fn total_sum(&self) -> isize {
|
||||
7
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(TotalSum)]
|
||||
struct Foo {
|
||||
seven: Seven,
|
||||
bar: Bar,
|
||||
baz: isize,
|
||||
}
|
||||
|
||||
#[derive(TotalSum)]
|
||||
struct Bar {
|
||||
quux: isize,
|
||||
bleh: isize,
|
||||
}
|
||||
|
||||
|
||||
pub fn main() {
|
||||
let v = Foo {
|
||||
seven: Seven,
|
||||
bar: Bar {
|
||||
quux: 9,
|
||||
bleh: 3,
|
||||
},
|
||||
baz: 80,
|
||||
};
|
||||
assert_eq!(v.total_sum(), 99);
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
// aux-build:macro_crate_test.rs
|
||||
// ignore-stage1
|
||||
|
||||
#![feature(plugin)]
|
||||
#![feature(plugin, custom_attribute)]
|
||||
#![plugin(macro_crate_test)]
|
||||
|
||||
#[macro_use] #[no_link]
|
||||
|
|
|
|||
15
src/test/run-pass/deprecated-derive.rs
Normal file
15
src/test/run-pass/deprecated-derive.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#[derive(Show)]
|
||||
//~^ WARNING derive(Show) is deprecated
|
||||
struct Test1;
|
||||
|
||||
fn main() { }
|
||||
18
src/test/run-pass/single-derive-attr-with-gate.rs
Normal file
18
src/test/run-pass/single-derive-attr-with-gate.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#![feature(custom_derive)]
|
||||
|
||||
#[derive_Clone]
|
||||
struct Test;
|
||||
|
||||
pub fn main() {
|
||||
Test.clone();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue