Skip to content

Commit 7735f45

Browse files
committed
rustc: Verify #[proc_macro] is only a word
... and perform the same verification for #[proc_macro_attribute], currently neither of these attributes take any arguments. Closes #52273
1 parent 90bd83c commit 7735f45

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

src/libsyntax_ext/proc_macro_registrar.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ impl<'a> CollectProcMacros<'a> {
200200
}
201201

202202
fn collect_attr_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) {
203-
if let Some(_) = attr.meta_item_list() {
204-
self.handler.span_err(attr.span, "`#[proc_macro_attribute]` attribute
203+
if !attr.is_word() {
204+
self.handler.span_err(attr.span, "`#[proc_macro_attribute]` attribute \
205205
does not take any arguments");
206206
return;
207207
}
@@ -223,8 +223,8 @@ impl<'a> CollectProcMacros<'a> {
223223
}
224224

225225
fn collect_bang_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) {
226-
if let Some(_) = attr.meta_item_list() {
227-
self.handler.span_err(attr.span, "`#[proc_macro]` attribute
226+
if !attr.is_word() {
227+
self.handler.span_err(attr.span, "`#[proc_macro]` attribute \
228228
does not take any arguments");
229229
return;
230230
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
#![feature(proc_macro)]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro = "test"] //~ ERROR: does not take any arguments
21+
pub fn a(a: TokenStream) -> TokenStream { a }
22+
23+
#[proc_macro()] //~ ERROR: does not take any arguments
24+
pub fn c(a: TokenStream) -> TokenStream { a }
25+
26+
#[proc_macro(x)] //~ ERROR: does not take any arguments
27+
pub fn d(a: TokenStream) -> TokenStream { a }
28+
29+
#[proc_macro_attribute = "test"] //~ ERROR: does not take any arguments
30+
pub fn e(_: TokenStream, a: TokenStream) -> TokenStream { a }
31+
32+
#[proc_macro_attribute()] //~ ERROR: does not take any arguments
33+
pub fn g(_: TokenStream, a: TokenStream) -> TokenStream { a }
34+
35+
#[proc_macro_attribute(x)] //~ ERROR: does not take any arguments
36+
pub fn h(_: TokenStream, a: TokenStream) -> TokenStream { a }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: `#[proc_macro]` attribute does not take any arguments
2+
--> $DIR/invalid-attributes.rs:20:1
3+
|
4+
LL | #[proc_macro = "test"] //~ ERROR: does not take any arguments
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: `#[proc_macro]` attribute does not take any arguments
8+
--> $DIR/invalid-attributes.rs:23:1
9+
|
10+
LL | #[proc_macro()] //~ ERROR: does not take any arguments
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: `#[proc_macro]` attribute does not take any arguments
14+
--> $DIR/invalid-attributes.rs:26:1
15+
|
16+
LL | #[proc_macro(x)] //~ ERROR: does not take any arguments
17+
| ^^^^^^^^^^^^^^^^
18+
19+
error: `#[proc_macro_attribute]` attribute does not take any arguments
20+
--> $DIR/invalid-attributes.rs:29:1
21+
|
22+
LL | #[proc_macro_attribute = "test"] //~ ERROR: does not take any arguments
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: `#[proc_macro_attribute]` attribute does not take any arguments
26+
--> $DIR/invalid-attributes.rs:32:1
27+
|
28+
LL | #[proc_macro_attribute()] //~ ERROR: does not take any arguments
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: `#[proc_macro_attribute]` attribute does not take any arguments
32+
--> $DIR/invalid-attributes.rs:35:1
33+
|
34+
LL | #[proc_macro_attribute(x)] //~ ERROR: does not take any arguments
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+

0 commit comments

Comments
 (0)