@@ -2,9 +2,9 @@ use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
22use quote:: { format_ident, quote, ToTokens } ;
33use std:: collections:: BTreeSet as Set ;
44use std:: iter:: FromIterator ;
5- use syn:: parse:: { Nothing , ParseStream } ;
5+ use syn:: parse:: ParseStream ;
66use syn:: {
7- braced, bracketed, parenthesized, token, Attribute , Error , Ident , Index , LitInt , LitStr ,
7+ braced, bracketed, parenthesized, token, Attribute , Error , Ident , Index , LitInt , LitStr , Meta ,
88 Result , Token ,
99} ;
1010
@@ -54,24 +54,27 @@ pub fn get(input: &[Attribute]) -> Result<Attrs> {
5454 } ;
5555
5656 for attr in input {
57- if attr. path . is_ident ( "error" ) {
57+ if attr. path ( ) . is_ident ( "error" ) {
5858 parse_error_attribute ( & mut attrs, attr) ?;
59- } else if attr. path . is_ident ( "source" ) {
59+ } else if attr. path ( ) . is_ident ( "source" ) {
6060 require_empty_attribute ( attr) ?;
6161 if attrs. source . is_some ( ) {
6262 return Err ( Error :: new_spanned ( attr, "duplicate #[source] attribute" ) ) ;
6363 }
6464 attrs. source = Some ( attr) ;
65- } else if attr. path . is_ident ( "backtrace" ) {
65+ } else if attr. path ( ) . is_ident ( "backtrace" ) {
6666 require_empty_attribute ( attr) ?;
6767 if attrs. backtrace . is_some ( ) {
6868 return Err ( Error :: new_spanned ( attr, "duplicate #[backtrace] attribute" ) ) ;
6969 }
7070 attrs. backtrace = Some ( attr) ;
71- } else if attr. path . is_ident ( "from" ) {
72- if !attr. tokens . is_empty ( ) {
73- // Assume this is meant for derive_more crate or something.
74- continue ;
71+ } else if attr. path ( ) . is_ident ( "from" ) {
72+ match attr. meta {
73+ Meta :: Path ( _) => { }
74+ Meta :: List ( _) | Meta :: NameValue ( _) => {
75+ // Assume this is meant for derive_more crate or something.
76+ continue ;
77+ }
7578 }
7679 if attrs. from . is_some ( ) {
7780 return Err ( Error :: new_spanned ( attr, "duplicate #[from] attribute" ) ) ;
@@ -166,21 +169,21 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
166169 let delimiter = parenthesized ! ( content in input) ;
167170 let nested = parse_token_expr ( & content, true ) ?;
168171 let mut group = Group :: new ( Delimiter :: Parenthesis , nested) ;
169- group. set_span ( delimiter. span ) ;
172+ group. set_span ( delimiter. span . join ( ) ) ;
170173 TokenTree :: Group ( group)
171174 } else if input. peek ( token:: Brace ) {
172175 let content;
173176 let delimiter = braced ! ( content in input) ;
174177 let nested = parse_token_expr ( & content, true ) ?;
175178 let mut group = Group :: new ( Delimiter :: Brace , nested) ;
176- group. set_span ( delimiter. span ) ;
179+ group. set_span ( delimiter. span . join ( ) ) ;
177180 TokenTree :: Group ( group)
178181 } else if input. peek ( token:: Bracket ) {
179182 let content;
180183 let delimiter = bracketed ! ( content in input) ;
181184 let nested = parse_token_expr ( & content, true ) ?;
182185 let mut group = Group :: new ( Delimiter :: Bracket , nested) ;
183- group. set_span ( delimiter. span ) ;
186+ group. set_span ( delimiter. span . join ( ) ) ;
184187 TokenTree :: Group ( group)
185188 } else {
186189 input. parse ( ) ?
@@ -191,8 +194,15 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
191194}
192195
193196fn require_empty_attribute ( attr : & Attribute ) -> Result < ( ) > {
194- syn:: parse2 :: < Nothing > ( attr. tokens . clone ( ) ) ?;
195- Ok ( ( ) )
197+ let error_span = match & attr. meta {
198+ Meta :: Path ( _) => return Ok ( ( ) ) ,
199+ Meta :: List ( meta) => meta. delimiter . span ( ) . open ( ) ,
200+ Meta :: NameValue ( meta) => meta. eq_token . span ,
201+ } ;
202+ Err ( Error :: new (
203+ error_span,
204+ "unexpected token in thiserror attribute" ,
205+ ) )
196206}
197207
198208impl ToTokens for Display < ' _ > {
0 commit comments