about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2016-11-09 20:51:18 +0200
committerGitHub <noreply@github.com>2016-11-09 20:51:18 +0200
commit5ebd7c50a0b9236af4389b00c97b00a59d9fa747 (patch)
tree7ae11942986307fad0aa452b5ee4a9db0cb89e9f /src/test
parent3d2ffa06ea4e04dbda99d7038e9afd04c040b472 (diff)
parent134ef4f7933b87efdc04eac3e8d9a530d56d9cfe (diff)
downloadrust-5ebd7c50a0b9236af4389b00c97b00a59d9fa747.tar.gz
rust-5ebd7c50a0b9236af4389b00c97b00a59d9fa747.zip
Rollup merge of #37614 - keeperofdakeys:proc_macro, r=jseyfried
macros 1.1: Allow proc_macro functions to declare attributes to be mark as used

This PR allows proc macro functions to declare attribute names that should be marked as used when attached to the deriving item. There are a few questions for this PR.

- Currently this uses a separate attribute named `#[proc_macro_attributes(..)]`, is this the best choice?
- In order to make this work, the `check_attribute` function had to be modified to not error on attributes marked as used. This is a pretty large change in semantics, is there a better way to do this?
- I've got a few clones where I don't know if I need them (like turning `item` into a `TokenStream`), can these be avoided?
- Is switching to `MultiItemDecorator` the right thing here?

Also fixes https://github.com/rust-lang/rust/issues/37563.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/attribute.rs22
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/auxiliary/derive-b.rs25
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable-2.rs2
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable.rs2
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/item-error.rs26
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs26
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/add-impl.rs5
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/append-impl.rs11
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-a.rs2
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-b.rs29
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-same-struct.rs2
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/expand-with-a-macro.rs2
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/derive-b.rs32
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/derive-same-struct.rs2
14 files changed, 169 insertions, 19 deletions
diff --git a/src/test/compile-fail-fulldeps/proc-macro/attribute.rs b/src/test/compile-fail-fulldeps/proc-macro/attribute.rs
index d1b2aa330ed..e22339694f9 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/attribute.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/attribute.rs
@@ -33,8 +33,8 @@ pub fn foo3(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     input
 }
 
-#[proc_macro_derive(b, c)]
-//~^ ERROR: attribute must only have one argument
+#[proc_macro_derive(b, c, d)]
+//~^ ERROR: attribute must have either one or two arguments
 pub fn foo4(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     input
 }
@@ -44,3 +44,21 @@ pub fn foo4(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
 pub fn foo5(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     input
 }
+
+#[proc_macro_derive(f, attributes(g = "h"))]
+//~^ ERROR: must only be one word
+pub fn foo6(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    input
+}
+
+#[proc_macro_derive(i, attributes(j(k)))]
+//~^ ERROR: must only be one word
+pub fn foo7(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    input
+}
+
+#[proc_macro_derive(l, attributes(m), n)]
+//~^ ERROR: attribute must have either one or two arguments
+pub fn foo8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    input
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/auxiliary/derive-b.rs b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/derive-b.rs
new file mode 100644
index 00000000000..70b778b1030
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/derive-b.rs
@@ -0,0 +1,25 @@
+// Copyright 2016 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
+// no-prefer-dynamic
+
+#![feature(proc_macro)]
+#![feature(proc_macro_lib)]
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_derive(B, attributes(B))]
+pub fn derive_b(input: TokenStream) -> TokenStream {
+    "".parse().unwrap()
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable-2.rs b/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable-2.rs
index 23dcbe03b5f..4f4ed90f8fc 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable-2.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable-2.rs
@@ -17,8 +17,8 @@
 extern crate derive_unstable_2;
 
 #[derive(Unstable)]
-struct A;
 //~^ ERROR: reserved for internal compiler
+struct A;
 
 fn main() {
     foo();
diff --git a/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable.rs b/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable.rs
index fb86f6f1b65..84ac776a765 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/expand-to-unstable.rs
@@ -17,8 +17,8 @@
 extern crate derive_unstable;
 
 #[derive(Unstable)]
-struct A;
 //~^ ERROR: use of unstable library feature
+struct A;
 
 fn main() {
     unsafe { foo(); }
diff --git a/src/test/compile-fail-fulldeps/proc-macro/item-error.rs b/src/test/compile-fail-fulldeps/proc-macro/item-error.rs
new file mode 100644
index 00000000000..2a68accf91f
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/item-error.rs
@@ -0,0 +1,26 @@
+// Copyright 2016 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:derive-b.rs
+
+#![feature(proc_macro)]
+#![allow(warnings)]
+
+#[macro_use]
+extern crate derive_b;
+
+#[derive(B)]
+struct A {
+    a: &u64
+//~^ ERROR: missing lifetime specifier
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs
new file mode 100644
index 00000000000..651a277d4ab
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs
@@ -0,0 +1,26 @@
+// Copyright 2016 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:derive-b.rs
+
+#![feature(proc_macro)]
+#![allow(warnings)]
+
+#[macro_use]
+extern crate derive_b;
+
+#[derive(B)]
+#[B]
+#[C] //~ ERROR: The attribute `C` is currently unknown to the compiler
+#[B(D)]
+#[B(E = "foo")]
+struct B;
+
+fn main() {}
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/add-impl.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/add-impl.rs
index 99586b0bb49..1d34049db24 100644
--- a/src/test/run-pass-fulldeps/proc-macro/auxiliary/add-impl.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/add-impl.rs
@@ -21,13 +21,12 @@ use proc_macro::TokenStream;
 #[proc_macro_derive(AddImpl)]
 // #[cfg(proc_macro)]
 pub fn derive(input: TokenStream) -> TokenStream {
-    (input.to_string() + "
-        impl B {
+    "impl B {
             fn foo(&self) {}
         }
 
         fn foo() {}
 
         mod bar { pub fn foo() {} }
-    ").parse().unwrap()
+    ".parse().unwrap()
 }
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/append-impl.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/append-impl.rs
index 27c3d643ca4..7260bc4a5e7 100644
--- a/src/test/run-pass-fulldeps/proc-macro/auxiliary/append-impl.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/append-impl.rs
@@ -21,11 +21,8 @@ use proc_macro::TokenStream;
 
 #[proc_macro_derive(Append)]
 pub fn derive_a(input: TokenStream) -> TokenStream {
-    let mut input = input.to_string();
-    input.push_str("
-        impl Append for A {
-            fn foo(&self) {}
-        }
-    ");
-    input.parse().unwrap()
+    "impl Append for A {
+         fn foo(&self) {}
+     }
+    ".parse().unwrap()
 }
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-a.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-a.rs
index c2de173568b..eaada554227 100644
--- a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-a.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-a.rs
@@ -23,5 +23,5 @@ pub fn derive(input: TokenStream) -> TokenStream {
     let input = input.to_string();
     assert!(input.contains("struct A;"));
     assert!(input.contains("#[derive(Debug, PartialEq, Eq, Copy, Clone)]"));
-    "#[derive(Debug, PartialEq, Eq, Copy, Clone)] struct A;".parse().unwrap()
+    "".parse().unwrap()
 }
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-b.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-b.rs
new file mode 100644
index 00000000000..a02b798c802
--- /dev/null
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-b.rs
@@ -0,0 +1,29 @@
+// Copyright 2016 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.
+
+// no-prefer-dynamic
+
+#![crate_type = "proc-macro"]
+#![feature(proc_macro)]
+#![feature(proc_macro_lib)]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_derive(B, attributes(B, C))]
+pub fn derive(input: TokenStream) -> TokenStream {
+    let input = input.to_string();
+    assert!(input.contains("#[B]"));
+    assert!(input.contains("struct B {"));
+    assert!(input.contains("#[C]"));
+    assert!(input.contains("#[derive(Debug, PartialEq, Eq, Copy, Clone)]"));
+    "".parse().unwrap()
+}
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-same-struct.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-same-struct.rs
index bd283ca57eb..bc8a0d57591 100644
--- a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-same-struct.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-same-struct.rs
@@ -21,7 +21,7 @@ use proc_macro::TokenStream;
 #[proc_macro_derive(AToB)]
 pub fn derive1(input: TokenStream) -> TokenStream {
     println!("input1: {:?}", input.to_string());
-    assert_eq!(input.to_string(), "#[derive(BToC)]\nstruct A;\n");
+    assert_eq!(input.to_string(), "struct A;\n");
     "#[derive(BToC)] struct B;".parse().unwrap()
 }
 
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/expand-with-a-macro.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/expand-with-a-macro.rs
index 155b125690c..50eaf035962 100644
--- a/src/test/run-pass-fulldeps/proc-macro/auxiliary/expand-with-a-macro.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/expand-with-a-macro.rs
@@ -24,8 +24,6 @@ pub fn derive(input: TokenStream) -> TokenStream {
     let input = input.to_string();
     assert!(input.contains("struct A;"));
     r#"
-        struct A;
-
         impl A {
             fn a(&self) {
                 panic!("hello");
diff --git a/src/test/run-pass-fulldeps/proc-macro/derive-b.rs b/src/test/run-pass-fulldeps/proc-macro/derive-b.rs
new file mode 100644
index 00000000000..f5bb93f0124
--- /dev/null
+++ b/src/test/run-pass-fulldeps/proc-macro/derive-b.rs
@@ -0,0 +1,32 @@
+// Copyright 2016 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:derive-b.rs
+// ignore-stage1
+
+#![feature(proc_macro)]
+
+#[macro_use]
+extern crate derive_b;
+
+#[derive(Debug, PartialEq, B, Eq, Copy, Clone)]
+#[B]
+struct B {
+    #[C]
+    a: u64
+}
+
+fn main() {
+    B { a: 3 };
+    assert_eq!(B { a: 3 }, B { a: 3 });
+    let b = B { a: 3 };
+    let _d = b;
+    let _e = b;
+}
diff --git a/src/test/run-pass-fulldeps/proc-macro/derive-same-struct.rs b/src/test/run-pass-fulldeps/proc-macro/derive-same-struct.rs
index b3edc8f1c31..608f86bca57 100644
--- a/src/test/run-pass-fulldeps/proc-macro/derive-same-struct.rs
+++ b/src/test/run-pass-fulldeps/proc-macro/derive-same-struct.rs
@@ -15,7 +15,7 @@
 #[macro_use]
 extern crate derive_same_struct;
 
-#[derive(AToB, BToC)]
+#[derive(AToB)]
 struct A;
 
 fn main() {