|
12 | 12 | use middle::ty;
|
13 | 13 |
|
14 | 14 | use syntax::ast::*;
|
15 |
| -use syntax::oldvisit; |
| 15 | +use syntax::visit; |
| 16 | +use syntax::visit::Visitor; |
16 | 17 |
|
17 | 18 | #[deriving(Clone)]
|
18 | 19 | pub struct Context {
|
19 | 20 | in_loop: bool,
|
20 | 21 | can_ret: bool
|
21 | 22 | }
|
22 | 23 |
|
| 24 | +struct CheckLoopVisitor { |
| 25 | + tcx: ty::ctxt, |
| 26 | +} |
| 27 | + |
23 | 28 | pub fn check_crate(tcx: ty::ctxt, crate: &Crate) {
|
24 |
| - oldvisit::visit_crate(crate, |
25 |
| - (Context { in_loop: false, can_ret: true }, |
26 |
| - oldvisit::mk_vt(@oldvisit::Visitor { |
27 |
| - visit_item: |i, (_cx, v)| { |
28 |
| - oldvisit::visit_item(i, (Context { |
| 29 | + visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, |
| 30 | + crate, |
| 31 | + Context { in_loop: false, can_ret: true }); |
| 32 | +} |
| 33 | + |
| 34 | +impl Visitor<Context> for CheckLoopVisitor { |
| 35 | + fn visit_item(&mut self, i:@item, _cx:Context) { |
| 36 | + visit::walk_item(self, i, Context { |
29 | 37 | in_loop: false,
|
30 | 38 | can_ret: true
|
31 |
| - }, v)); |
32 |
| - }, |
33 |
| - visit_expr: |e: @expr, (cx, v): (Context, oldvisit::vt<Context>)| { |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + fn visit_expr(&mut self, e:@expr, cx:Context) { |
| 43 | + |
34 | 44 | match e.node {
|
35 | 45 | expr_while(e, ref b) => {
|
36 |
| - (v.visit_expr)(e, (cx, v)); |
37 |
| - (v.visit_block)(b, (Context { in_loop: true,.. cx }, v)); |
| 46 | + self.visit_expr(e, cx); |
| 47 | + self.visit_block(b, Context { in_loop: true,.. cx }); |
38 | 48 | }
|
39 | 49 | expr_loop(ref b, _) => {
|
40 |
| - (v.visit_block)(b, (Context { in_loop: true,.. cx }, v)); |
| 50 | + self.visit_block(b, Context { in_loop: true,.. cx }); |
41 | 51 | }
|
42 | 52 | expr_fn_block(_, ref b) => {
|
43 |
| - (v.visit_block)(b, (Context { |
44 |
| - in_loop: false, |
45 |
| - can_ret: false |
46 |
| - }, v)); |
| 53 | + self.visit_block(b, Context { in_loop: false, can_ret: false }); |
47 | 54 | }
|
48 | 55 | expr_break(_) => {
|
49 | 56 | if !cx.in_loop {
|
50 |
| - tcx.sess.span_err(e.span, "`break` outside of loop"); |
| 57 | + self.tcx.sess.span_err(e.span, "`break` outside of loop"); |
51 | 58 | }
|
52 | 59 | }
|
53 | 60 | expr_again(_) => {
|
54 | 61 | if !cx.in_loop {
|
55 |
| - tcx.sess.span_err(e.span, "`loop` outside of loop"); |
| 62 | + self.tcx.sess.span_err(e.span, "`loop` outside of loop"); |
56 | 63 | }
|
57 | 64 | }
|
58 | 65 | expr_ret(oe) => {
|
59 | 66 | if !cx.can_ret {
|
60 |
| - tcx.sess.span_err(e.span, "`return` in block function"); |
| 67 | + self.tcx.sess.span_err(e.span, "`return` in block function"); |
61 | 68 | }
|
62 |
| - oldvisit::visit_expr_opt(oe, (cx, v)); |
| 69 | + visit::walk_expr_opt(self, oe, cx); |
63 | 70 | }
|
64 |
| - _ => oldvisit::visit_expr(e, (cx, v)) |
| 71 | + _ => visit::walk_expr(self, e, cx) |
65 | 72 | }
|
66 |
| - }, |
67 |
| - .. *oldvisit::default_visitor() |
68 |
| - }))); |
| 73 | + |
| 74 | + } |
69 | 75 | }
|
0 commit comments