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.
|
|
|
|
|
2014-04-04 18:05:31 -05:00
|
|
|
use driver::session::Session;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
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
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
2013-11-11 13:29:15 -06:00
|
|
|
enum Context {
|
|
|
|
Normal, Loop, Closure
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2012-03-26 05:54:06 -05:00
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
struct CheckLoopVisitor<'a> {
|
2014-04-04 18:05:31 -05:00
|
|
|
sess: &'a Session,
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 18:05:31 -05:00
|
|
|
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
|
|
|
|
visit::walk_crate(&mut CheckLoopVisitor { sess: sess }, krate, Normal)
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
impl<'a> Visitor<Context> for CheckLoopVisitor<'a> {
|
2014-01-09 07:05:33 -06:00
|
|
|
fn visit_item(&mut self, i: &ast::Item, _cx: Context) {
|
2013-11-11 13:29:15 -06:00
|
|
|
visit::walk_item(self, i, Normal);
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn visit_expr(&mut self, e: &ast::Expr, cx:Context) {
|
2013-11-11 13:29:15 -06:00
|
|
|
match e.node {
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprWhile(ref e, ref b) => {
|
|
|
|
self.visit_expr(&**e, cx);
|
|
|
|
self.visit_block(&**b, Loop);
|
2013-11-11 13:29:15 -06:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprLoop(ref b, _) => {
|
|
|
|
self.visit_block(&**b, Loop);
|
2013-11-11 13:29:15 -06:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprFnBlock(_, ref b) | ast::ExprProc(_, ref b) => {
|
|
|
|
self.visit_block(&**b, Closure);
|
2013-11-11 13:29:15 -06:00
|
|
|
}
|
|
|
|
ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
|
|
|
|
ast::ExprAgain(_) => self.require_loop("continue", cx, e.span),
|
|
|
|
_ => visit::walk_expr(self, e, cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 19:49:30 -05:00
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
impl<'a> CheckLoopVisitor<'a> {
|
2013-11-11 13:29:15 -06:00
|
|
|
fn require_loop(&self, name: &str, cx: Context, span: Span) {
|
|
|
|
match cx {
|
|
|
|
Loop => {}
|
|
|
|
Closure => {
|
2014-05-16 12:45:16 -05:00
|
|
|
self.sess.span_err(span,
|
|
|
|
format!("`{}` inside of a closure",
|
|
|
|
name).as_slice());
|
2013-11-11 13:29:15 -06:00
|
|
|
}
|
|
|
|
Normal => {
|
2014-05-16 12:45:16 -05:00
|
|
|
self.sess.span_err(span,
|
|
|
|
format!("`{}` outside of loop",
|
|
|
|
name).as_slice());
|
2013-11-11 13:29:15 -06:00
|
|
|
}
|
|
|
|
}
|
2013-08-12 19:49:30 -05:00
|
|
|
}
|
2012-07-13 10:24:07 -05:00
|
|
|
}
|