|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| - |
12 | 11 | use middle::ty;
|
13 | 12 |
|
14 |
| -use syntax::ast::*; |
15 |
| -use syntax::visit; |
| 13 | +use syntax::ast; |
| 14 | +use syntax::codemap::Span; |
16 | 15 | use syntax::visit::Visitor;
|
| 16 | +use syntax::visit; |
17 | 17 |
|
18 |
| -#[deriving(Clone)] |
19 |
| -pub struct Context { |
20 |
| - in_loop: bool, |
21 |
| - can_ret: bool |
| 18 | +#[deriving(Clone, Eq)] |
| 19 | +enum Context { |
| 20 | + Normal, Loop, Closure |
22 | 21 | }
|
23 | 22 |
|
24 | 23 | struct CheckLoopVisitor {
|
25 | 24 | tcx: ty::ctxt,
|
26 | 25 | }
|
27 | 26 |
|
28 |
| -pub fn check_crate(tcx: ty::ctxt, crate: &Crate) { |
29 |
| - visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, |
30 |
| - crate, |
31 |
| - Context { in_loop: false, can_ret: true }); |
| 27 | +pub fn check_crate(tcx: ty::ctxt, crate: &ast::Crate) { |
| 28 | + visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, crate, Normal) |
32 | 29 | }
|
33 | 30 |
|
34 | 31 | impl Visitor<Context> for CheckLoopVisitor {
|
35 |
| - fn visit_item(&mut self, i:@item, _cx:Context) { |
36 |
| - visit::walk_item(self, i, Context { |
37 |
| - in_loop: false, |
38 |
| - can_ret: true |
39 |
| - }); |
| 32 | + fn visit_item(&mut self, i: @ast::item, _cx: Context) { |
| 33 | + visit::walk_item(self, i, Normal); |
40 | 34 | }
|
41 | 35 |
|
42 |
| - fn visit_expr(&mut self, e:@Expr, cx:Context) { |
43 |
| - |
44 |
| - match e.node { |
45 |
| - ExprWhile(e, ref b) => { |
| 36 | + fn visit_expr(&mut self, e: @ast::Expr, cx:Context) { |
| 37 | + match e.node { |
| 38 | + ast::ExprWhile(e, ref b) => { |
46 | 39 | self.visit_expr(e, cx);
|
47 |
| - self.visit_block(b, Context { in_loop: true,.. cx }); |
48 |
| - } |
49 |
| - ExprLoop(ref b, _) => { |
50 |
| - self.visit_block(b, Context { in_loop: true,.. cx }); |
51 |
| - } |
52 |
| - ExprFnBlock(_, ref b) | ExprProc(_, ref b) => { |
53 |
| - self.visit_block(b, Context { in_loop: false, can_ret: false }); |
54 |
| - } |
55 |
| - ExprBreak(_) => { |
56 |
| - if !cx.in_loop { |
57 |
| - self.tcx.sess.span_err(e.span, "`break` outside of loop"); |
58 |
| - } |
59 |
| - } |
60 |
| - ExprAgain(_) => { |
61 |
| - if !cx.in_loop { |
62 |
| - self.tcx.sess.span_err(e.span, "`loop` outside of loop"); |
63 |
| - } |
64 |
| - } |
65 |
| - ExprRet(oe) => { |
66 |
| - if !cx.can_ret { |
67 |
| - self.tcx.sess.span_err(e.span, "`return` in block function"); |
| 40 | + self.visit_block(b, Loop); |
| 41 | + } |
| 42 | + ast::ExprLoop(ref b, _) => { |
| 43 | + self.visit_block(b, Loop); |
| 44 | + } |
| 45 | + ast::ExprFnBlock(_, ref b) | ast::ExprProc(_, ref b) => { |
| 46 | + self.visit_block(b, Closure); |
| 47 | + } |
| 48 | + ast::ExprBreak(_) => self.require_loop("break", cx, e.span), |
| 49 | + ast::ExprAgain(_) => self.require_loop("continue", cx, e.span), |
| 50 | + ast::ExprRet(oe) => { |
| 51 | + if cx == Closure { |
| 52 | + self.tcx.sess.span_err(e.span, "`return` in a closure"); |
68 | 53 | }
|
69 | 54 | visit::walk_expr_opt(self, oe, cx);
|
70 |
| - } |
71 |
| - _ => visit::walk_expr(self, e, cx) |
72 | 55 | }
|
| 56 | + _ => visit::walk_expr(self, e, cx) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
73 | 60 |
|
| 61 | +impl CheckLoopVisitor { |
| 62 | + fn require_loop(&self, name: &str, cx: Context, span: Span) { |
| 63 | + match cx { |
| 64 | + Loop => {} |
| 65 | + Closure => { |
| 66 | + self.tcx.sess.span_err(span, format!("`{}` inside of a closure", |
| 67 | + name)); |
| 68 | + } |
| 69 | + Normal => { |
| 70 | + self.tcx.sess.span_err(span, format!("`{}` outside of loop", |
| 71 | + name)); |
| 72 | + } |
| 73 | + } |
74 | 74 | }
|
75 | 75 | }
|
0 commit comments