2012-09-04 11:54:36 -07:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::visit;
|
2012-03-26 12:54:06 +02:00
|
|
|
|
|
|
|
type ctx = {in_loop: bool, can_ret: bool};
|
|
|
|
|
2012-03-26 16:09:27 +02:00
|
|
|
fn check_crate(tcx: ty::ctxt, crate: @crate) {
|
2012-03-26 12:54:06 +02:00
|
|
|
visit::visit_crate(*crate, {in_loop: false,can_ret: true}, visit::mk_vt(@{
|
2012-06-30 16:19:07 -07:00
|
|
|
visit_item: |i, _cx, v| {
|
2012-03-26 12:54:06 +02:00
|
|
|
visit::visit_item(i, {in_loop: false, can_ret: true}, v);
|
|
|
|
},
|
2012-06-30 16:19:07 -07:00
|
|
|
visit_expr: |e: @expr, cx: ctx, v: visit::vt<ctx>| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_while(e, b) => {
|
2012-03-26 12:54:06 +02:00
|
|
|
v.visit_expr(e, cx, v);
|
2012-09-04 13:29:32 -07:00
|
|
|
v.visit_block(b, {in_loop: true,.. cx}, v);
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2012-08-14 19:20:56 -07:00
|
|
|
expr_loop(b, _) => {
|
2012-09-04 13:29:32 -07:00
|
|
|
v.visit_block(b, {in_loop: true,.. cx}, v);
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_fn(_, _, _, _) => {
|
2012-03-26 12:54:06 +02:00
|
|
|
visit::visit_expr(e, {in_loop: false, can_ret: true}, v);
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_fn_block(_, b, _) => {
|
2012-03-26 16:09:27 +02:00
|
|
|
v.visit_block(b, {in_loop: false, can_ret: false}, v);
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_loop_body(@{node: expr_fn_block(_, b, _), _}) => {
|
2012-08-10 18:15:08 -07:00
|
|
|
let blk = ty::is_blockish(ty::ty_fn_proto(ty::expr_ty(tcx,
|
|
|
|
e)));
|
2012-03-27 12:33:13 +02:00
|
|
|
v.visit_block(b, {in_loop: true, can_ret: blk}, 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 {
|
2012-07-13 22:57:48 -07: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 {
|
2012-07-13 22:57:48 -07: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 {
|
2012-08-10 12:20:46 -07:00
|
|
|
tcx.sess.span_err(e.span, ~"`return` in block function");
|
2012-03-26 12:54:06 +02:00
|
|
|
}
|
|
|
|
visit::visit_expr_opt(oe, cx, v);
|
|
|
|
}
|
2012-08-03 19:59:04 -07: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()
|
2012-03-26 12:54:06 +02:00
|
|
|
}));
|
2012-07-14 01:24:07 +10:00
|
|
|
}
|