2012-12-03 18:48:01 -06: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.
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
|
|
|
|
2013-11-11 13:29:15 -06:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2013-08-12 19:49:30 -05:00
|
|
|
use syntax::visit::Visitor;
|
2013-11-11 13:29:15 -06:00
|
|
|
use syntax::visit;
|
2012-03-26 05:54:06 -05:00
|
|
|
|
2013-11-11 13:29:15 -06:00
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
enum Context {
|
|
|
|
Normal, Loop, Closure
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2012-03-26 05:54:06 -05:00
|
|
|
|
2013-08-12 19:49:30 -05:00
|
|
|
struct CheckLoopVisitor {
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
}
|
|
|
|
|
2013-11-11 13:29:15 -06:00
|
|
|
pub fn check_crate(tcx: ty::ctxt, crate: &ast::Crate) {
|
|
|
|
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, crate, Normal)
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<Context> for CheckLoopVisitor {
|
2013-11-11 13:29:15 -06:00
|
|
|
fn visit_item(&mut self, i: @ast::item, _cx: Context) {
|
|
|
|
visit::walk_item(self, i, Normal);
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 13:29:15 -06:00
|
|
|
fn visit_expr(&mut self, e: @ast::Expr, cx:Context) {
|
|
|
|
match e.node {
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprWhile(e, b) => {
|
2013-08-12 19:49:30 -05:00
|
|
|
self.visit_expr(e, cx);
|
2013-11-11 13:29:15 -06:00
|
|
|
self.visit_block(b, Loop);
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprLoop(b, _) => {
|
2013-11-11 13:29:15 -06:00
|
|
|
self.visit_block(b, Loop);
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprFnBlock(_, b) | ast::ExprProc(_, b) => {
|
2013-11-11 13:29:15 -06:00
|
|
|
self.visit_block(b, Closure);
|
|
|
|
}
|
|
|
|
ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
|
|
|
|
ast::ExprAgain(_) => self.require_loop("continue", cx, e.span),
|
|
|
|
ast::ExprRet(oe) => {
|
|
|
|
if cx == Closure {
|
|
|
|
self.tcx.sess.span_err(e.span, "`return` in a closure");
|
2012-03-26 05:54:06 -05:00
|
|
|
}
|
2013-08-12 19:49:30 -05:00
|
|
|
visit::walk_expr_opt(self, oe, cx);
|
2012-03-26 05:54:06 -05:00
|
|
|
}
|
2013-11-11 13:29:15 -06:00
|
|
|
_ => visit::walk_expr(self, e, cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 19:49:30 -05:00
|
|
|
|
2013-11-11 13:29:15 -06:00
|
|
|
impl CheckLoopVisitor {
|
|
|
|
fn require_loop(&self, name: &str, cx: Context, span: Span) {
|
|
|
|
match cx {
|
|
|
|
Loop => {}
|
|
|
|
Closure => {
|
|
|
|
self.tcx.sess.span_err(span, format!("`{}` inside of a closure",
|
|
|
|
name));
|
|
|
|
}
|
|
|
|
Normal => {
|
|
|
|
self.tcx.sess.span_err(span, format!("`{}` outside of loop",
|
|
|
|
name));
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
2012-07-13 10:24:07 -05:00
|
|
|
}
|