]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/check_loop.rs
Use static strings
[rust.git] / src / librustc / middle / check_loop.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 use middle::ty;
13
14 use syntax::ast::*;
15 use syntax::visit;
16
17 pub struct Context {
18     in_loop: bool,
19     can_ret: bool
20 }
21
22 pub fn check_crate(tcx: ty::ctxt, crate: @crate) {
23     visit::visit_crate(crate,
24                        Context { in_loop: false, can_ret: true },
25                        visit::mk_vt(@visit::Visitor {
26         visit_item: |i, _cx, v| {
27             visit::visit_item(i, Context {
28                                     in_loop: false,
29                                     can_ret: true
30                                  }, v);
31         },
32         visit_expr: |e: @expr, cx: Context, v: visit::vt<Context>| {
33             match e.node {
34               expr_while(e, ref b) => {
35                 (v.visit_expr)(e, cx, v);
36                 (v.visit_block)(b, Context { in_loop: true,.. cx }, v);
37               }
38               expr_loop(ref b, _) => {
39                 (v.visit_block)(b, Context { in_loop: true,.. cx }, v);
40               }
41               expr_fn_block(_, ref b) => {
42                 (v.visit_block)(b, Context {
43                                          in_loop: false,
44                                          can_ret: false
45                                       }, v);
46               }
47               expr_loop_body(@expr {node: expr_fn_block(_, ref b), _}) => {
48                 let sigil = ty::ty_closure_sigil(ty::expr_ty(tcx, e));
49                 let blk = (sigil == BorrowedSigil);
50                 (v.visit_block)(b, Context {
51                                          in_loop: true,
52                                          can_ret: blk
53                                      }, v);
54               }
55               expr_break(_) => {
56                 if !cx.in_loop {
57                     tcx.sess.span_err(e.span, "`break` outside of loop");
58                 }
59               }
60               expr_again(_) => {
61                 if !cx.in_loop {
62                     tcx.sess.span_err(e.span, "`again` outside of loop");
63                 }
64               }
65               expr_ret(oe) => {
66                 if !cx.can_ret {
67                     tcx.sess.span_err(e.span, "`return` in block function");
68                 }
69                 visit::visit_expr_opt(oe, cx, v);
70               }
71               _ => visit::visit_expr(e, cx, v)
72             }
73         },
74         .. *visit::default_visitor()
75     }));
76 }