8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // Sanity check AST before lowering it to HIR
11
+ // Validate AST before lowering it to HIR
12
12
//
13
13
// This pass is supposed to catch things that fit into AST data structures,
14
14
// but not permitted by the language. It runs after expansion when AST is frozen,
@@ -24,11 +24,11 @@ use syntax::errors;
24
24
use syntax:: parse:: token:: { self , keywords} ;
25
25
use syntax:: visit:: { self , Visitor } ;
26
26
27
- struct SanityChecker < ' a > {
27
+ struct AstValidator < ' a > {
28
28
session : & ' a Session ,
29
29
}
30
30
31
- impl < ' a > SanityChecker < ' a > {
31
+ impl < ' a > AstValidator < ' a > {
32
32
fn err_handler ( & self ) -> & errors:: Handler {
33
33
& self . session . parse_sess . span_diagnostic
34
34
}
@@ -55,9 +55,21 @@ impl<'a> SanityChecker<'a> {
55
55
err. emit ( ) ;
56
56
}
57
57
}
58
+
59
+ fn check_path ( & self , path : & Path , id : NodeId ) {
60
+ if path. global && path. segments . len ( ) > 0 {
61
+ let ident = path. segments [ 0 ] . identifier ;
62
+ if token:: Ident ( ident) . is_path_segment_keyword ( ) {
63
+ self . session . add_lint (
64
+ lint:: builtin:: SUPER_OR_SELF_IN_GLOBAL_PATH , id, path. span ,
65
+ format ! ( "global paths cannot start with `{}`" , ident)
66
+ ) ;
67
+ }
68
+ }
69
+ }
58
70
}
59
71
60
- impl < ' a , ' v > Visitor < ' v > for SanityChecker < ' a > {
72
+ impl < ' a , ' v > Visitor < ' v > for AstValidator < ' a > {
61
73
fn visit_lifetime ( & mut self , lt : & Lifetime ) {
62
74
if lt. name . as_str ( ) == "'_" {
63
75
self . session . add_lint (
@@ -85,19 +97,17 @@ impl<'a, 'v> Visitor<'v> for SanityChecker<'a> {
85
97
}
86
98
87
99
fn visit_path ( & mut self , path : & Path , id : NodeId ) {
88
- if path. global && path. segments . len ( ) > 0 {
89
- let ident = path. segments [ 0 ] . identifier ;
90
- if token:: Ident ( ident) . is_path_segment_keyword ( ) {
91
- self . session . add_lint (
92
- lint:: builtin:: SUPER_OR_SELF_IN_GLOBAL_PATH , id, path. span ,
93
- format ! ( "global paths cannot start with `{}`" , ident)
94
- ) ;
95
- }
96
- }
100
+ self . check_path ( path, id) ;
97
101
98
102
visit:: walk_path ( self , path)
99
103
}
100
104
105
+ fn visit_path_list_item ( & mut self , prefix : & Path , item : & PathListItem ) {
106
+ self . check_path ( prefix, item. node . id ( ) ) ;
107
+
108
+ visit:: walk_path_list_item ( self , prefix, item)
109
+ }
110
+
101
111
fn visit_item ( & mut self , item : & Item ) {
102
112
match item. node {
103
113
ItemKind :: Use ( ref view_path) => {
@@ -169,5 +179,5 @@ impl<'a, 'v> Visitor<'v> for SanityChecker<'a> {
169
179
}
170
180
171
181
pub fn check_crate ( session : & Session , krate : & Crate ) {
172
- visit:: walk_crate ( & mut SanityChecker { session : session } , krate)
182
+ visit:: walk_crate ( & mut AstValidator { session : session } , krate)
173
183
}
0 commit comments