Skip to content
Permalink
Browse files

syntax_ext: Validate `#[proc_macro_derive]` input better

Tweak some error wording
  • Loading branch information...
petrochenkov committed Mar 3, 2019
1 parent 8371caf commit 5cb5083909f19c107aec186a72b8b9104f8ca30d
@@ -601,7 +601,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
res
}
ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
self.cx.span_err(attr.span, &format!("`{}` is a derive macro", attr.path));
self.cx.trace_macros_diag();
invoc.fragment_kind.dummy(attr.span)
}
@@ -822,7 +822,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}

ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
self.cx.span_err(path.span, &format!("`{}` is a derive macro", path));
self.cx.trace_macros_diag();
kind.dummy(span)
}
@@ -128,9 +128,13 @@ impl<'a> CollectProcMacros<'a> {
}
};

if trait_ident.is_path_segment_keyword() {
self.handler.span_err(trait_attr.span(),
&format!("`{}` cannot be a name of derive macro", trait_ident));
}
if deriving::is_builtin_trait(trait_ident.name) {
self.handler.span_err(trait_attr.span,
"cannot override a built-in #[derive] mode");
"cannot override a built-in derive macro");
}

let attributes_attr = list.get(1);
@@ -140,16 +144,15 @@ impl<'a> CollectProcMacros<'a> {
}
attr.meta_item_list().unwrap_or_else(|| {
self.handler.span_err(attr.span(),
"attribute must be of form: \
`attributes(foo, bar)`");
"attribute must be of form: `attributes(foo, bar)`");
&[]
}).into_iter().filter_map(|attr| {
let attr = match attr.meta_item() {
Some(meta_item) => meta_item,
_ => {
self.handler.span_err(attr.span(), "not a meta item");
return None;
},
}
};

let ident = match attr.ident() {
@@ -159,6 +162,13 @@ impl<'a> CollectProcMacros<'a> {
return None;
}
};
if ident.is_path_segment_keyword() {
self.handler.span_err(
attr.span(),
&format!("`{}` cannot be a name of derive helper attribute", ident),
);
}

Some(ident.name)
}).collect()
} else {
@@ -39,11 +39,11 @@ pub fn foo7(input: TokenStream) -> TokenStream { input }
pub fn foo8(input: TokenStream) -> TokenStream { input }

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

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

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

#[proc_macro_derive(d17, attributes(self))]
//FIXME ERROR: `self` cannot be a name of derive helper attribute
//~^ ERROR: `self` cannot be a name of derive helper attribute
pub fn foo17(input: TokenStream) -> TokenStream { input }
@@ -34,7 +34,13 @@ error: must only be one word
LL | #[proc_macro_derive(d8(a))]
| ^^^^^

error: cannot override a built-in #[derive] mode
error: `self` cannot be a name of derive macro
--> $DIR/attribute.rs:41:21
|
LL | #[proc_macro_derive(self)]
| ^^^^

error: cannot override a built-in derive macro
--> $DIR/attribute.rs:45:21
|
LL | #[proc_macro_derive(PartialEq)]
@@ -82,6 +88,12 @@ error: must only be one word
LL | #[proc_macro_derive(d16, attributes(a(b)))]
| ^^^^

error: `self` cannot be a name of derive helper attribute
--> $DIR/attribute.rs:74:37
|
LL | #[proc_macro_derive(d17, attributes(self))]
| ^^^^

error: attribute must be of the form `#[proc_macro_derive(TraitName, /*opt*/ attributes(name1, name2, ...))]`
--> $DIR/attribute.rs:9:1
|
@@ -94,5 +106,5 @@ error: attribute must be of the form `#[proc_macro_derive(TraitName, /*opt*/ att
LL | #[proc_macro_derive = ""]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 16 previous errors
error: aborting due to 18 previous errors

0 comments on commit 5cb5083

Please sign in to comment.
You can’t perform that action at this time.