]> git.lizzy.rs Git - rust.git/commitdiff
Add error when proc_macro_derive is used not on functions
authorest31 <MTest31@outlook.com>
Fri, 4 Nov 2016 21:37:51 +0000 (22:37 +0100)
committerest31 <MTest31@outlook.com>
Fri, 4 Nov 2016 22:52:20 +0000 (23:52 +0100)
Fixes #37590

src/libsyntax_ext/proc_macro_registrar.rs
src/test/compile-fail-fulldeps/proc-macro/illegal-proc-macro-derive-use.rs [new file with mode: 0644]
src/test/compile-fail-fulldeps/proc-macro/require-rustc-macro-crate-type.rs [deleted file]

index f49a5f0e0706ed11084c9261be45ec2d45489082..a8accd63dcf0a1252e223abb675f34153d5f3ca3 100644 (file)
@@ -105,6 +105,17 @@ fn visit_item(&mut self, item: &ast::Item) {
         match item.node {
             ast::ItemKind::Fn(..) => {}
             _ => {
+                // Check for invalid use of proc_macro_derive
+                let attr = item.attrs.iter()
+                    .filter(|a| a.check_name("proc_macro_derive"))
+                    .next();
+                if let Some(attr) = attr {
+                    self.handler.span_err(attr.span(),
+                                          "the `#[proc_macro_derive]` \
+                                          attribute may only be used \
+                                          on bare functions");
+                    return;
+                }
                 self.check_not_pub_in_root(&item.vis, item.span);
                 return visit::walk_item(self, item)
             }
diff --git a/src/test/compile-fail-fulldeps/proc-macro/illegal-proc-macro-derive-use.rs b/src/test/compile-fail-fulldeps/proc-macro/illegal-proc-macro-derive-use.rs
new file mode 100644 (file)
index 0000000..405994b
--- /dev/null
@@ -0,0 +1,27 @@
+// 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.
+
+#![feature(proc_macro)]
+
+extern crate proc_macro;
+
+#[proc_macro_derive(Foo)]
+//~^ ERROR: only usable with crates of the `proc-macro` crate type
+pub fn foo(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    a
+}
+
+// Issue #37590
+#[proc_macro_derive(Foo)]
+//~^ ERROR: the `#[proc_macro_derive]` attribute may only be used on bare functions
+pub struct Foo {
+}
+
+fn main() {}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/require-rustc-macro-crate-type.rs b/src/test/compile-fail-fulldeps/proc-macro/require-rustc-macro-crate-type.rs
deleted file mode 100644 (file)
index 44397cd..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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.
-
-#![feature(proc_macro)]
-
-extern crate proc_macro;
-
-#[proc_macro_derive(Foo)]
-//~^ ERROR: only usable with crates of the `proc-macro` crate type
-pub fn foo(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
-    a
-}
-
-fn main() {}