1
1
use proc_macro2:: Span ;
2
2
use syn;
3
3
use syn:: spanned:: Spanned ;
4
+ use syn:: fold:: Fold ;
4
5
5
6
use diagnostic_shim:: * ;
6
7
7
8
pub struct MetaItem {
8
- // Due to https://github.com/rust-lang/rust/issues/47941
9
- // we can only ever get the span of the #, which is better than nothing
10
- pound_span : Span ,
11
9
meta : syn:: Meta ,
12
10
}
13
11
14
12
impl MetaItem {
15
13
pub fn with_name < ' a > ( attrs : & [ syn:: Attribute ] , name : & ' a str ) -> Option < Self > {
16
14
attrs
17
15
. iter ( )
18
- . filter_map ( |attr| attr. interpret_meta ( ) . map ( |m| ( attr. pound_token . 0 [ 0 ] , m) ) )
19
- . find ( |& ( _, ref m) | m. name ( ) == name)
20
- . map ( |( pound_span, meta) | Self { pound_span, meta } )
16
+ . filter_map ( |attr| {
17
+ attr. interpret_meta ( )
18
+ . map ( |m| FixSpan ( attr. pound_token . 0 [ 0 ] ) . fold_meta ( m) )
19
+ } )
20
+ . find ( |m| m. name ( ) == name)
21
+ . map ( |meta| Self { meta } )
21
22
}
22
23
23
24
pub fn nested_item < ' a > ( & self , name : & ' a str ) -> Result < Self , Diagnostic > {
@@ -76,10 +77,7 @@ impl MetaItem {
76
77
use syn:: Meta :: * ;
77
78
78
79
match self . meta {
79
- Word ( mut x) => {
80
- x. span = self . span_or_pound_token ( x. span ) ;
81
- Ok ( x)
82
- }
80
+ Word ( x) => Ok ( x) ,
83
81
_ => {
84
82
let meta = & self . meta ;
85
83
Err ( self . span ( ) . error ( format ! (
@@ -95,7 +93,7 @@ impl MetaItem {
95
93
use syn:: Meta :: * ;
96
94
97
95
match self . meta {
98
- List ( ref list) => Ok ( Nested ( list. nested . iter ( ) , self . pound_span ) ) ,
96
+ List ( ref list) => Ok ( Nested ( list. nested . iter ( ) ) ) ,
99
97
_ => Err ( self . span ( )
100
98
. error ( format ! ( "`{0}` must be in the form `{0}(...)`" , self . name( ) ) ) ) ,
101
99
}
@@ -131,33 +129,20 @@ impl MetaItem {
131
129
fn value_span ( & self ) -> Span {
132
130
use syn:: Meta :: * ;
133
131
134
- let s = match self . meta {
132
+ match self . meta {
135
133
Word ( ident) => ident. span ,
136
134
List ( ref meta) => meta. nested . span ( ) ,
137
135
NameValue ( ref meta) => meta. lit . span ( ) ,
138
- } ;
139
- self . span_or_pound_token ( s)
136
+ }
140
137
}
141
138
142
139
fn span ( & self ) -> Span {
143
- self . span_or_pound_token ( self . meta . span ( ) )
144
- }
145
-
146
- /// If the given span is affected by
147
- /// https://github.com/rust-lang/rust/issues/47941,
148
- /// returns the span of the pound token
149
- fn span_or_pound_token ( & self , span : Span ) -> Span {
150
- let bad_span_debug = "Span(Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 })" ;
151
- if format ! ( "{:?}" , span) == bad_span_debug {
152
- self . pound_span
153
- } else {
154
- span
155
- }
140
+ self . meta . span ( )
156
141
}
157
142
}
158
143
159
144
#[ cfg_attr( rustfmt, rustfmt_skip) ] // https://github.com/rust-lang-nursery/rustfmt/issues/2392
160
- pub struct Nested < ' a > ( syn:: punctuated:: Iter < ' a , syn:: NestedMeta , Token ! [ , ] > , Span ) ;
145
+ pub struct Nested < ' a > ( syn:: punctuated:: Iter < ' a , syn:: NestedMeta , Token ! [ , ] > ) ;
161
146
162
147
impl < ' a > Iterator for Nested < ' a > {
163
148
type Item = MetaItem ;
@@ -166,12 +151,25 @@ impl<'a> Iterator for Nested<'a> {
166
151
use syn:: NestedMeta :: * ;
167
152
168
153
match self . 0 . next ( ) {
169
- Some ( & Meta ( ref item) ) => Some ( MetaItem {
170
- pound_span : self . 1 ,
171
- meta : item. clone ( ) ,
172
- } ) ,
154
+ Some ( & Meta ( ref item) ) => Some ( MetaItem { meta : item. clone ( ) } ) ,
173
155
Some ( _) => self . next ( ) ,
174
156
None => None ,
175
157
}
176
158
}
177
159
}
160
+
161
+ /// If the given span is affected by
162
+ /// https://github.com/rust-lang/rust/issues/47941,
163
+ /// returns the span of the pound token
164
+ struct FixSpan ( Span ) ;
165
+
166
+ impl Fold for FixSpan {
167
+ fn fold_span ( & mut self , span : Span ) -> Span {
168
+ let bad_span_debug = "Span(Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 })" ;
169
+ if format ! ( "{:?}" , span) == bad_span_debug {
170
+ self . 0
171
+ } else {
172
+ span
173
+ }
174
+ }
175
+ }
0 commit comments