@@ -17,47 +17,71 @@ use util::ppaux;
1717
1818use syntax:: ast:: * ;
1919use syntax:: codemap;
20- use syntax:: { oldvisit, ast_util, ast_map} ;
20+ use syntax:: { ast_util, ast_map} ;
21+ use syntax:: visit:: Visitor ;
22+ use syntax:: visit;
23+
24+ struct CheckCrateVisitor {
25+ sess : Session ,
26+ ast_map : ast_map:: map ,
27+ def_map : resolve:: DefMap ,
28+ method_map : typeck:: method_map ,
29+ tcx : ty:: ctxt ,
30+ }
31+
32+ impl Visitor < bool > for CheckCrateVisitor {
33+ fn visit_item ( & mut self , i: @item, env : bool ) {
34+ check_item ( self , self . sess , self . ast_map , self . def_map , i, env) ;
35+ }
36+ fn visit_pat ( & mut self , p: @pat, env : bool ) {
37+ check_pat ( self , p, env) ;
38+ }
39+ fn visit_expr ( & mut self , ex: @expr, env : bool ) {
40+ check_expr ( self , self . sess , self . def_map , self . method_map ,
41+ self . tcx , ex, env) ;
42+ }
43+ }
2144
2245pub fn check_crate ( sess : Session ,
2346 crate : & Crate ,
2447 ast_map : ast_map:: map ,
2548 def_map : resolve:: DefMap ,
2649 method_map : typeck:: method_map ,
2750 tcx : ty:: ctxt ) {
28- oldvisit:: visit_crate ( crate , ( false , oldvisit:: mk_vt ( @oldvisit:: Visitor {
29- visit_item : |a, b| check_item ( sess, ast_map, def_map, a, b) ,
30- visit_pat : check_pat,
31- visit_expr : |a, b|
32- check_expr ( sess, def_map, method_map, tcx, a, b) ,
33- .. * oldvisit:: default_visitor ( )
34- } ) ) ) ;
51+ let mut v = CheckCrateVisitor {
52+ sess : sess,
53+ ast_map : ast_map,
54+ def_map : def_map,
55+ method_map : method_map,
56+ tcx : tcx,
57+ } ;
58+ visit:: walk_crate ( & mut v, crate , false ) ;
3559 sess. abort_if_errors ( ) ;
3660}
3761
38- pub fn check_item ( sess : Session ,
62+ pub fn check_item ( v : & mut CheckCrateVisitor ,
63+ sess : Session ,
3964 ast_map : ast_map:: map ,
4065 def_map : resolve:: DefMap ,
4166 it: @item,
42- ( _is_const , v ) : ( bool ,
43- oldvisit:: vt < bool > ) ) {
67+ _is_const : bool ) {
4468 match it. node {
4569 item_static( _, _, ex) => {
46- ( v. visit_expr ) ( ex, ( true , v ) ) ;
70+ v. visit_expr ( ex, true ) ;
4771 check_item_recursion ( sess, ast_map, def_map, it) ;
4872 }
4973 item_enum( ref enum_definition, _) => {
5074 for var in ( * enum_definition) . variants . iter ( ) {
5175 for ex in var. node . disr_expr . iter ( ) {
52- ( v. visit_expr ) ( * ex, ( true , v ) ) ;
76+ v. visit_expr ( * ex, true ) ;
5377 }
5478 }
5579 }
56- _ => oldvisit :: visit_item ( it , ( false , v ) )
80+ _ => visit :: walk_item ( v , it , false )
5781 }
5882}
5983
60- pub fn check_pat( p : @pat, ( _is_const , v ) : ( bool , oldvisit :: vt < bool > ) ) {
84+ pub fn check_pat( v : & mut CheckCrateVisitor , p: @pat, _is_const : bool ) {
6185 fn is_str ( e: @expr) -> bool {
6286 match e. node {
6387 expr_vstore(
@@ -72,22 +96,22 @@ pub fn check_pat(p: @pat, (_is_const, v): (bool, oldvisit::vt<bool>)) {
7296 }
7397 match p. node {
7498 // Let through plain ~-string literals here
75- pat_lit( a) => if !is_str ( a) { ( v. visit_expr ) ( a, ( true , v ) ) ; } ,
99+ pat_lit( a) => if !is_str ( a) { v. visit_expr ( a, true ) ; } ,
76100 pat_range( a, b) => {
77- if !is_str ( a) { ( v. visit_expr ) ( a, ( true , v ) ) ; }
78- if !is_str ( b) { ( v. visit_expr ) ( b, ( true , v ) ) ; }
101+ if !is_str ( a) { v. visit_expr ( a, true ) ; }
102+ if !is_str ( b) { v. visit_expr ( b, true ) ; }
79103 }
80- _ => oldvisit :: visit_pat ( p , ( false , v ) )
104+ _ => visit :: walk_pat ( v , p , false )
81105 }
82106}
83107
84- pub fn check_expr ( sess : Session ,
108+ pub fn check_expr ( v : & mut CheckCrateVisitor ,
109+ sess : Session ,
85110 def_map : resolve:: DefMap ,
86111 method_map : typeck:: method_map ,
87112 tcx : ty:: ctxt ,
88113 e: @expr,
89- ( is_const , v ) : ( bool ,
90- oldvisit:: vt < bool > ) ) {
114+ is_const : bool ) {
91115 if is_const {
92116 match e. node {
93117 expr_unary( _, deref, _) => { }
@@ -152,8 +176,8 @@ pub fn check_expr(sess: Session,
152176 }
153177 }
154178 }
155- expr_paren( e) => { check_expr ( sess, def_map, method_map,
156- tcx, e, ( is_const, v ) ) ; }
179+ expr_paren( e) => { check_expr ( v , sess, def_map, method_map,
180+ tcx, e, is_const) ; }
157181 expr_vstore( _, expr_vstore_slice) |
158182 expr_vec( _, m_imm) |
159183 expr_addr_of( m_imm, _) |
@@ -192,7 +216,7 @@ pub fn check_expr(sess: Session,
192216 }
193217 _ => ( )
194218 }
195- oldvisit :: visit_expr ( e , ( is_const , v ) ) ;
219+ visit :: walk_expr ( v , e , is_const ) ;
196220}
197221
198222#[ deriving( Clone ) ]
@@ -204,6 +228,8 @@ struct env {
204228 idstack : @mut ~[ NodeId ]
205229}
206230
231+ struct CheckItemRecursionVisitor ;
232+
207233// Make sure a const item doesn't recursively refer to itself
208234// FIXME: Should use the dependency graph when it's available (#1356)
209235pub fn check_item_recursion ( sess : Session ,
@@ -218,36 +244,34 @@ pub fn check_item_recursion(sess: Session,
218244 idstack : @mut ~[ ]
219245 } ;
220246
221- let visitor = oldvisit:: mk_vt ( @oldvisit:: Visitor {
222- visit_item : visit_item,
223- visit_expr : visit_expr,
224- .. * oldvisit:: default_visitor ( )
225- } ) ;
226- ( visitor. visit_item ) ( it, ( env, visitor) ) ;
247+ let mut visitor = CheckItemRecursionVisitor ;
248+ visitor. visit_item ( it, env) ;
249+ }
227250
228- fn visit_item ( it : @item, ( env , v ) : ( env , oldvisit:: vt < env > ) ) {
251+ impl Visitor < env > for CheckItemRecursionVisitor {
252+ fn visit_item ( & mut self , it: @item, env : env ) {
229253 if env. idstack . iter ( ) . any ( |x| x == & ( it. id ) ) {
230254 env. sess . span_fatal ( env. root_it . span , "recursive constant" ) ;
231255 }
232256 env. idstack . push ( it. id ) ;
233- oldvisit :: visit_item ( it , ( env , v ) ) ;
257+ visit :: walk_item ( self , it , env ) ;
234258 env. idstack . pop ( ) ;
235259 }
236260
237- fn visit_expr ( e : @expr, ( env , v ) : ( env , oldvisit :: vt < env > ) ) {
261+ fn visit_expr ( & mut self , e: @expr, env : env ) {
238262 match e. node {
239263 expr_path( * ) => match env. def_map . find ( & e. id ) {
240264 Some ( & def_static( def_id, _) ) if ast_util:: is_local ( def_id) =>
241265 match env. ast_map . get_copy ( & def_id. node ) {
242266 ast_map:: node_item( it, _) => {
243- ( v . visit_item ) ( it, ( env, v ) ) ;
267+ self . visit_item ( it, env) ;
244268 }
245269 _ => fail ! ( "const not bound to an item" )
246270 } ,
247271 _ => ( )
248272 } ,
249273 _ => ( )
250274 }
251- oldvisit :: visit_expr ( e , ( env , v ) ) ;
275+ visit :: walk_expr ( self , e , env ) ;
252276 }
253277}
0 commit comments