1 // Copyright 2012-2014 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.
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.
12 use rustc::session::Session;
14 use rustc::hir::map::Map;
15 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
20 #[derive(Clone, Copy, PartialEq)]
22 Loop(hir::LoopSource),
27 fn name(self) -> &'static str {
29 LoopKind::Loop(hir::LoopSource::Loop) => "loop",
30 LoopKind::Loop(hir::LoopSource::WhileLet) => "while let",
31 LoopKind::Loop(hir::LoopSource::ForLoop) => "for",
32 LoopKind::WhileLoop => "while",
37 #[derive(Clone, Copy, PartialEq)]
44 #[derive(Copy, Clone)]
45 struct CheckLoopVisitor<'a, 'hir: 'a> {
47 hir_map: &'a Map<'hir>,
51 pub fn check_crate(sess: &Session, map: &Map) {
52 let krate = map.krate();
53 krate.visit_all_item_likes(&mut CheckLoopVisitor {
60 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
61 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
62 NestedVisitorMap::OnlyBodies(&self.hir_map)
65 fn visit_item(&mut self, i: &'hir hir::Item) {
66 self.with_context(Normal, |v| intravisit::walk_item(v, i));
69 fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
70 self.with_context(Normal, |v| intravisit::walk_impl_item(v, i));
73 fn visit_expr(&mut self, e: &'hir hir::Expr) {
75 hir::ExprWhile(ref e, ref b, _) => {
76 self.with_context(Loop(LoopKind::WhileLoop), |v| {
81 hir::ExprLoop(ref b, _, source) => {
82 self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
84 hir::ExprClosure(.., b, _, _) => {
85 self.with_context(Closure, |v| v.visit_nested_body(b));
87 hir::ExprBreak(label, ref opt_expr) => {
88 let loop_id = match label.target_id {
89 hir::ScopeTarget::Block(_) => return,
90 hir::ScopeTarget::Loop(loop_res) => {
91 match loop_res.into() {
92 Ok(loop_id) => loop_id,
93 Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
94 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
95 self.emit_unlabled_cf_in_while_condition(e.span, "break");
98 Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
103 if opt_expr.is_some() {
104 let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
107 Some(match self.hir_map.expect_expr(loop_id).node {
108 hir::ExprWhile(..) => LoopKind::WhileLoop,
109 hir::ExprLoop(_, _, source) => LoopKind::Loop(source),
110 ref r => span_bug!(e.span,
111 "break label resolved to a non-loop: {:?}", r),
115 None | Some(LoopKind::Loop(hir::LoopSource::Loop)) => (),
117 struct_span_err!(self.sess, e.span, E0571,
118 "`break` with value from a `{}` loop",
121 "can only break with a value inside `loop`")
127 self.require_loop("break", e.span);
129 hir::ExprAgain(label) => {
130 if let hir::ScopeTarget::Loop(
131 hir::LoopIdResult::Err(
132 hir::LoopIdError::UnlabeledCfInWhileCondition)) = label.target_id {
133 self.emit_unlabled_cf_in_while_condition(e.span, "continue");
135 self.require_loop("continue", e.span)
137 _ => intravisit::walk_expr(self, e),
142 impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
143 fn with_context<F>(&mut self, cx: Context, f: F)
144 where F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>)
146 let old_cx = self.cx;
152 fn require_loop(&self, name: &str, span: Span) {
156 struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
157 .span_label(span, "cannot break inside of a closure")
161 struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
162 .span_label(span, "cannot break outside of a loop")
168 fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
169 struct_span_err!(self.sess, span, E0590,
170 "`break` or `continue` with no label in the condition of a `while` loop")
172 format!("unlabeled `{}` in the condition of a `while` loop", cf_type))