2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-07 14:16:52 -08:00
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::ty;
|
|
|
|
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::visit;
|
2012-03-26 12:54:06 +02:00
|
|
|
|
2013-02-19 02:40:42 -05:00
|
|
|
pub struct Context {
|
|
|
|
in_loop: bool,
|
|
|
|
can_ret: bool
|
|
|
|
}
|
2012-03-26 12:54:06 +02:00
|
|
|
|
2013-06-27 15:04:22 +02:00
|
|
|
pub fn check_crate(tcx: ty::ctxt, crate: &crate) {
|
2013-04-17 12:15:37 -04:00
|
|
|
visit::visit_crate(crate,
|
2013-06-12 04:55:16 +02:00
|
|
|
(Context { in_loop: false, can_ret: true },
|
2013-01-08 14:00:45 -08:00
|
|
|
visit::mk_vt(@visit::Visitor {
|
2013-06-12 04:55:16 +02:00
|
|
|
visit_item: |i, (_cx, v)| {
|
|
|
|
visit::visit_item(i, (Context {
|
2013-02-19 02:40:42 -05:00
|
|
|
in_loop: false,
|
|
|
|
can_ret: true
|
2013-06-12 04:55:16 +02:00
|
|
|
}, v));
|
2012-03-26 12:54:06 +02:00
|
|
|
},
|
2013-06-12 04:55:16 +02:00
|
|
|
visit_expr: |e: @expr, (cx, v): (Context, visit::vt<Context>)| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2012-12-04 10:50:00 -08:00
|
|
|
expr_while(e, ref b) => {
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_expr)(e, (cx, v));
|
|
|
|
(v.visit_block)(b, (Context { in_loop: true,.. cx }, v));
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2012-12-04 10:50:00 -08:00
|
|
|
expr_loop(ref b, _) => {
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_block)(b, (Context { in_loop: true,.. cx }, v));
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2013-01-10 10:59:58 -08:00
|
|
|
expr_fn_block(_, ref b) => {
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_block)(b, (Context {
|
2013-02-19 02:40:42 -05:00
|
|
|
in_loop: false,
|
|
|
|
can_ret: false
|
2013-06-12 04:55:16 +02:00
|
|
|
}, v));
|
2012-03-26 16:09:27 +02:00
|
|
|
}
|
2013-01-10 10:59:58 -08:00
|
|
|
expr_loop_body(@expr {node: expr_fn_block(_, ref b), _}) => {
|
2013-01-31 17:12:29 -08:00
|
|
|
let sigil = ty::ty_closure_sigil(ty::expr_ty(tcx, e));
|
|
|
|
let blk = (sigil == BorrowedSigil);
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_block)(b, (Context {
|
2013-02-19 02:40:42 -05:00
|
|
|
in_loop: true,
|
|
|
|
can_ret: blk
|
2013-06-12 04:55:16 +02:00
|
|
|
}, v));
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2012-08-14 19:20:56 -07:00
|
|
|
expr_break(_) => {
|
2012-03-26 12:54:06 +02:00
|
|
|
if !cx.in_loop {
|
2013-05-01 01:47:52 +09:00
|
|
|
tcx.sess.span_err(e.span, "`break` outside of loop");
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 19:20:56 -07:00
|
|
|
expr_again(_) => {
|
2012-03-26 12:54:06 +02:00
|
|
|
if !cx.in_loop {
|
2013-05-01 01:47:52 +09:00
|
|
|
tcx.sess.span_err(e.span, "`again` outside of loop");
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_ret(oe) => {
|
2012-03-26 12:54:06 +02:00
|
|
|
if !cx.can_ret {
|
2013-05-01 01:47:52 +09:00
|
|
|
tcx.sess.span_err(e.span, "`return` in block function");
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
visit::visit_expr_opt(oe, (cx, v));
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
_ => visit::visit_expr(e, (cx, v))
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2012-09-04 13:29:32 -07:00
|
|
|
},
|
|
|
|
.. *visit::default_visitor()
|
2013-06-12 04:55:16 +02:00
|
|
|
})));
|
2012-07-14 01:24:07 +10:00
|
|
|
}
|