Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit 7a5651c

Browse files
bors[bot]japaric
andcommitted
Merge #127
127: bump the syn dependency r=therealprof a=japaric and switch to the recommended way to parse tokens: `parse_macro_input!`. This improves (?) error messages when the user applies one of our attributes to an item that's not a function. Consider ``` rust #[entry] static MAIN: () = (); ``` The error message changed from: ``` error: custom attribute panicked --> src/main.rs:10:1 | 10 | #[entry] | ^^^^^^^^ | = help: message: `#[entry]` must be applied to a function: ParseError(Some("failed to parse fn item: failed to parse")) ``` to: ``` error: expected `fn` --> src/main.rs:11:1 | 11 | static MAIN: () = (); | ^^^^^^ error: aborting due to previous error ``` --- Before landing this I'd like to hear more details about #125 to see if this helps Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 7094ee1 + a2bef20 commit 7a5651c

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

macros/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ version = "0.1.1"
1313
proc-macro = true
1414

1515
[dependencies]
16-
quote = "0.6.6"
17-
proc-macro2 = "0.4.15"
16+
quote = "0.6.8"
17+
proc-macro2 = "0.4.19"
1818

1919
[dependencies.syn]
2020
features = ["extra-traits", "full"]
21-
version = "0.14.8"
21+
version = "0.15.4"
2222

2323
[dependencies.rand]
2424
version = "0.5.5"

macros/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate rand;
66
extern crate quote;
77
extern crate core;
88
extern crate proc_macro2;
9+
#[macro_use]
910
extern crate syn;
1011

1112
use proc_macro2::Span;
@@ -77,7 +78,7 @@ use proc_macro::TokenStream;
7778
/// ```
7879
#[proc_macro_attribute]
7980
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
80-
let f: ItemFn = syn::parse(input).expect("`#[entry]` must be applied to a function");
81+
let f = parse_macro_input!(input as ItemFn);
8182

8283
// check the function signature
8384
assert!(
@@ -253,7 +254,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
253254
/// ```
254255
#[proc_macro_attribute]
255256
pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
256-
let f: ItemFn = syn::parse(input).expect("`#[exception]` must be applied to a function");
257+
let f = parse_macro_input!(input as ItemFn);
257258

258259
assert!(
259260
args.to_string() == "",
@@ -459,7 +460,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
459460
/// ```
460461
#[proc_macro_attribute]
461462
pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream {
462-
let f: ItemFn = syn::parse(input).expect("`#[pre_init]` must be applied to a function");
463+
let f = parse_macro_input!(input as ItemFn);
463464

464465
// check the function signature
465466
assert!(

0 commit comments

Comments
 (0)