@@ -21,8 +21,10 @@ use rustc::session::Session;
21
21
use syntax:: ast:: * ;
22
22
use syntax:: attr;
23
23
use syntax:: codemap:: Spanned ;
24
+ use syntax:: parse:: token;
24
25
use syntax:: visit:: { self , Visitor } ;
25
26
use syntax_pos:: Span ;
27
+ use syntax_pos:: symbol:: keywords;
26
28
use errors;
27
29
28
30
struct AstValidator < ' a > {
@@ -35,15 +37,15 @@ impl<'a> AstValidator<'a> {
35
37
}
36
38
37
39
fn check_lifetime ( & self , lifetime : & Lifetime ) {
38
- if !lifetime. ident . without_first_quote ( ) . is_valid ( ) &&
39
- !lifetime. ident . name . is_static_keyword ( ) {
40
+ let valid_names = [ keywords:: StaticLifetime . name ( ) , keywords:: Invalid . name ( ) ] ;
41
+ if !valid_names. contains ( & lifetime. ident . name ) &&
42
+ token:: Ident ( lifetime. ident . without_first_quote ( ) ) . is_reserved_ident ( ) {
40
43
self . err_handler ( ) . span_err ( lifetime. span , "lifetimes cannot use keyword names" ) ;
41
44
}
42
45
}
43
46
44
47
fn check_label ( & self , label : Ident , span : Span ) {
45
- if label. name . is_static_keyword ( ) || !label. without_first_quote ( ) . is_valid ( )
46
- || label. name == "'_" {
48
+ if token:: Ident ( label. without_first_quote ( ) ) . is_reserved_ident ( ) || label. name == "'_" {
47
49
self . err_handler ( ) . span_err ( span, & format ! ( "invalid label name `{}`" , label. name) ) ;
48
50
}
49
51
}
@@ -207,23 +209,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
207
209
visit:: walk_use_tree ( self , use_tree, id) ;
208
210
}
209
211
212
+ fn visit_lifetime ( & mut self , lifetime : & ' a Lifetime ) {
213
+ self . check_lifetime ( lifetime) ;
214
+ visit:: walk_lifetime ( self , lifetime) ;
215
+ }
216
+
210
217
fn visit_item ( & mut self , item : & ' a Item ) {
211
218
match item. node {
212
- ItemKind :: Impl ( .., ref generics , Some ( ..) , _, ref impl_items) => {
219
+ ItemKind :: Impl ( .., Some ( ..) , _, ref impl_items) => {
213
220
self . invalid_visibility ( & item. vis , item. span , None ) ;
214
221
for impl_item in impl_items {
215
222
self . invalid_visibility ( & impl_item. vis , impl_item. span , None ) ;
216
223
if let ImplItemKind :: Method ( ref sig, _) = impl_item. node {
217
224
self . check_trait_fn_not_const ( sig. constness ) ;
218
225
}
219
226
}
220
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
221
227
}
222
- ItemKind :: Impl ( .., ref generics , None , _, _) => {
228
+ ItemKind :: Impl ( .., None , _, _) => {
223
229
self . invalid_visibility ( & item. vis ,
224
230
item. span ,
225
231
Some ( "place qualifiers on individual impl items instead" ) ) ;
226
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
227
232
}
228
233
ItemKind :: AutoImpl ( ..) => {
229
234
self . invalid_visibility ( & item. vis , item. span , None ) ;
@@ -234,14 +239,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
234
239
Some ( "place qualifiers on individual foreign items \
235
240
instead") ) ;
236
241
}
237
- ItemKind :: Enum ( ref def, ref generics ) => {
242
+ ItemKind :: Enum ( ref def, _ ) => {
238
243
for variant in & def. variants {
239
244
self . invalid_non_exhaustive_attribute ( variant) ;
240
245
for field in variant. node . data . fields ( ) {
241
246
self . invalid_visibility ( & field. vis , field. span , None ) ;
242
247
}
243
248
}
244
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
245
249
}
246
250
ItemKind :: Trait ( is_auto, _, ref generics, ref bounds, ref trait_items) => {
247
251
if is_auto == IsAuto :: Yes {
@@ -278,7 +282,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
278
282
}
279
283
}
280
284
}
281
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
282
285
}
283
286
ItemKind :: Mod ( _) => {
284
287
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
@@ -289,7 +292,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
289
292
self . session . buffer_lint ( lint, item. id , item. span , msg) ;
290
293
}
291
294
}
292
- ItemKind :: Union ( ref vdata, ref generics ) => {
295
+ ItemKind :: Union ( ref vdata, _ ) => {
293
296
if !vdata. is_struct ( ) {
294
297
self . err_handler ( ) . span_err ( item. span ,
295
298
"tuple and unit unions are not permitted" ) ;
@@ -298,12 +301,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
298
301
self . err_handler ( ) . span_err ( item. span ,
299
302
"unions cannot have zero fields" ) ;
300
303
}
301
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
302
- }
303
- ItemKind :: Fn ( .., ref generics, _) |
304
- ItemKind :: Ty ( _, ref generics) |
305
- ItemKind :: Struct ( _, ref generics) => {
306
- generics. lifetimes . iter ( ) . for_each ( |l| self . check_lifetime ( & l. lifetime ) )
307
304
}
308
305
_ => { }
309
306
}
0 commit comments