about summary refs log tree commit diff
path: root/src/test/compile-fail-fulldeps
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-01-19 15:49:23 -0800
committerAlex Crichton <alex@alexcrichton.com>2017-01-20 08:34:30 -0800
commitd4d276faafb07f13f15178540e337864af8f74db (patch)
tree2619f62b9c8d6ec8e9c0901a57109877313f5a99 /src/test/compile-fail-fulldeps
parent437d2b5e2840b220772c8d58a925c2b232bd0c2e (diff)
parent04ecee158c2c56f4a6d81ad17ac3547848ec1e4c (diff)
downloadrust-d4d276faafb07f13f15178540e337864af8f74db.tar.gz
rust-d4d276faafb07f13f15178540e337864af8f74db.zip
Rollup merge of #38842 - abonander:proc_macro_attribute, r=jseyfried
Implement `#[proc_macro_attribute]`

This implements `#[proc_macro_attribute]` as described in https://github.com/rust-lang/rfcs/pull/1566

The following major (hopefully non-breaking) changes are included:

* Refactor `proc_macro::TokenStream` to use `syntax::tokenstream::TokenStream`.
    * `proc_macro::tokenstream::TokenStream` no longer emits newlines between items, this can be trivially restored if desired
    * `proc_macro::TokenStream::from_str` does not try to parse an item anymore, moved to `impl MultiItemModifier for CustomDerive` with more informative error message

* Implement `#[proc_macro_attribute]`, which expects functions of the kind `fn(TokenStream, TokenStream) -> TokenStream`
    * Reactivated `#![feature(proc_macro)]` and gated `#[proc_macro_attribute]` under it
    * `#![feature(proc_macro)]` and `#![feature(custom_attribute)]` are mutually exclusive
    * adding `#![feature(proc_macro)]` makes the expansion pass assume that any attributes that are not built-in, or introduced by existing syntax extensions, are proc-macro attributes

* Fix `feature_gate::find_lang_feature_issue()` to not use `unwrap()`

    * This change wasn't necessary for this PR, but it helped debugging a problem where I was using the wrong feature string.

* Move "completed feature gate checking" pass to after "name resolution" pass

    * This was necessary for proper feature-gating of `#[proc_macro_attribute]` invocations when the `proc_macro` feature flag isn't set.

Prototype/Litmus Test: [Implementation](https://github.com/abonander/anterofit/blob/proc_macro/service-attr/src/lib.rs#L13) -- [Usage](https://github.com/abonander/anterofit/blob/proc_macro/service-attr/examples/post_service.rs#L35)
Diffstat (limited to 'src/test/compile-fail-fulldeps')
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/auxiliary/attr_proc_macro.rs23
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs3
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/feature-gate-proc_macro.rs24
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/macro-use-attr.rs22
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/proc-macro-custom-attr-mutex.rs24
5 files changed, 94 insertions, 2 deletions
diff --git a/src/test/compile-fail-fulldeps/proc-macro/auxiliary/attr_proc_macro.rs b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/attr_proc_macro.rs
new file mode 100644
index 00000000000..db0c19e96f8
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/auxiliary/attr_proc_macro.rs
@@ -0,0 +1,23 @@
+// 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)]
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_attribute]
+pub fn attr_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream {
+    input
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs b/src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs
index 0e4ac9fe1ea..a5359946c09 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs
@@ -16,8 +16,7 @@ extern crate derive_bad;
 #[derive(
     A
 )]
-//~^^ ERROR: custom derive attribute panicked
-//~| HELP: called `Result::unwrap()` on an `Err` value: LexError
+//~^^ ERROR: custom derive produced unparseable tokens
 struct A;
 
 fn main() {}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/feature-gate-proc_macro.rs b/src/test/compile-fail-fulldeps/proc-macro/feature-gate-proc_macro.rs
new file mode 100644
index 00000000000..7e32800e0f9
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/feature-gate-proc_macro.rs
@@ -0,0 +1,24 @@
+// 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:attr_proc_macro.rs
+// gate-test-proc_macro
+#![feature(use_extern_macros)]
+
+extern crate attr_proc_macro;
+use attr_proc_macro::attr_proc_macro;
+
+#[attr_proc_macro]
+//~^ ERROR: attribute procedural macros are experimental
+struct Foo;
+
+fn main() {
+    let _ = Foo;
+}
\ No newline at end of file
diff --git a/src/test/compile-fail-fulldeps/proc-macro/macro-use-attr.rs b/src/test/compile-fail-fulldeps/proc-macro/macro-use-attr.rs
new file mode 100644
index 00000000000..76253487b51
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/macro-use-attr.rs
@@ -0,0 +1,22 @@
+// 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:attr_proc_macro.rs
+#![feature(proc_macro)]
+
+#[macro_use] extern crate attr_proc_macro;
+
+#[attr_proc_macro]
+//~^ ERROR: attribute procedural macros cannot be imported with `#[macro_use]`
+struct Foo;
+
+fn main() {
+    let _ = Foo;
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/proc-macro-custom-attr-mutex.rs b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-custom-attr-mutex.rs
new file mode 100644
index 00000000000..288cab71ff4
--- /dev/null
+++ b/src/test/compile-fail-fulldeps/proc-macro/proc-macro-custom-attr-mutex.rs
@@ -0,0 +1,24 @@
+// 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:attr_proc_macro.rs
+
+#![feature(proc_macro, custom_attribute)]
+//~^ ERROR Cannot use `#![feature(proc_macro)]` and `#![feature(custom_attribute)] at the same time
+
+extern crate attr_proc_macro;
+use attr_proc_macro::attr_proc_macro;
+
+#[attr_proc_macro]
+fn foo() {}
+
+fn main() {
+    foo();
+}