Skip to content

Commit f3b8809

Browse files
committed
syntax_ext: Validate #[proc_macro_derive] input better
Tweak some error wording
1 parent 18d6c6e commit f3b8809

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
601601
res
602602
}
603603
ProcMacroDerive(..) | BuiltinDerive(..) => {
604-
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
604+
self.cx.span_err(attr.span, &format!("`{}` is a derive macro", attr.path));
605605
self.cx.trace_macros_diag();
606606
invoc.fragment_kind.dummy(attr.span)
607607
}
@@ -822,7 +822,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
822822
}
823823

824824
ProcMacroDerive(..) | BuiltinDerive(..) => {
825-
self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
825+
self.cx.span_err(path.span, &format!("`{}` is a derive macro", path));
826826
self.cx.trace_macros_diag();
827827
kind.dummy(span)
828828
}

src/libsyntax_ext/proc_macro_decls.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ impl<'a> CollectProcMacros<'a> {
128128
}
129129
};
130130

131+
if trait_ident.is_path_segment_keyword() {
132+
self.handler.span_err(trait_attr.span(),
133+
&format!("`{}` cannot be a name of derive macro", trait_ident));
134+
}
131135
if deriving::is_builtin_trait(trait_ident.name) {
132136
self.handler.span_err(trait_attr.span,
133-
"cannot override a built-in #[derive] mode");
137+
"cannot override a built-in derive macro");
134138
}
135139

136140
let attributes_attr = list.get(1);
@@ -140,16 +144,15 @@ impl<'a> CollectProcMacros<'a> {
140144
}
141145
attr.meta_item_list().unwrap_or_else(|| {
142146
self.handler.span_err(attr.span(),
143-
"attribute must be of form: \
144-
`attributes(foo, bar)`");
147+
"attribute must be of form: `attributes(foo, bar)`");
145148
&[]
146149
}).into_iter().filter_map(|attr| {
147150
let attr = match attr.meta_item() {
148151
Some(meta_item) => meta_item,
149152
_ => {
150153
self.handler.span_err(attr.span(), "not a meta item");
151154
return None;
152-
},
155+
}
153156
};
154157

155158
let ident = match attr.ident() {
@@ -159,6 +162,13 @@ impl<'a> CollectProcMacros<'a> {
159162
return None;
160163
}
161164
};
165+
if ident.is_path_segment_keyword() {
166+
self.handler.span_err(
167+
attr.span(),
168+
&format!("`{}` cannot be a name of derive helper attribute", ident),
169+
);
170+
}
171+
162172
Some(ident.name)
163173
}).collect()
164174
} else {

src/test/ui/proc-macro/attribute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ pub fn foo7(input: TokenStream) -> TokenStream { input }
3939
pub fn foo8(input: TokenStream) -> TokenStream { input }
4040

4141
#[proc_macro_derive(self)]
42-
//FIXME ERROR: `self` cannot be a name of derive macro
42+
//~^ ERROR: `self` cannot be a name of derive macro
4343
pub fn foo9(input: TokenStream) -> TokenStream { input }
4444

4545
#[proc_macro_derive(PartialEq)]
46-
//~^ ERROR: cannot override a built-in #[derive] mode
46+
//~^ ERROR: cannot override a built-in derive macro
4747
pub fn foo10(input: TokenStream) -> TokenStream { input }
4848

4949
#[proc_macro_derive(d11, a)]
@@ -72,5 +72,5 @@ pub fn foo15(input: TokenStream) -> TokenStream { input }
7272
pub fn foo16(input: TokenStream) -> TokenStream { input }
7373

7474
#[proc_macro_derive(d17, attributes(self))]
75-
//FIXME ERROR: `self` cannot be a name of derive helper attribute
75+
//~^ ERROR: `self` cannot be a name of derive helper attribute
7676
pub fn foo17(input: TokenStream) -> TokenStream { input }

src/test/ui/proc-macro/attribute.stderr

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ error: must only be one word
3434
LL | #[proc_macro_derive(d8(a))]
3535
| ^^^^^
3636

37-
error: cannot override a built-in #[derive] mode
37+
error: `self` cannot be a name of derive macro
38+
--> $DIR/attribute.rs:41:21
39+
|
40+
LL | #[proc_macro_derive(self)]
41+
| ^^^^
42+
43+
error: cannot override a built-in derive macro
3844
--> $DIR/attribute.rs:45:21
3945
|
4046
LL | #[proc_macro_derive(PartialEq)]
@@ -82,6 +88,12 @@ error: must only be one word
8288
LL | #[proc_macro_derive(d16, attributes(a(b)))]
8389
| ^^^^
8490

91+
error: `self` cannot be a name of derive helper attribute
92+
--> $DIR/attribute.rs:74:37
93+
|
94+
LL | #[proc_macro_derive(d17, attributes(self))]
95+
| ^^^^
96+
8597
error: attribute must be of the form `#[proc_macro_derive(TraitName, /*opt*/ attributes(name1, name2, ...))]`
8698
--> $DIR/attribute.rs:9:1
8799
|
@@ -94,5 +106,5 @@ error: attribute must be of the form `#[proc_macro_derive(TraitName, /*opt*/ att
94106
LL | #[proc_macro_derive = ""]
95107
| ^^^^^^^^^^^^^^^^^^^^^^^^^
96108

97-
error: aborting due to 16 previous errors
109+
error: aborting due to 18 previous errors
98110

0 commit comments

Comments
 (0)