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),
28 fn name(self) -> &'static str {
30 LoopKind::Loop(hir::LoopSource::Loop) => "loop",
31 LoopKind::Loop(hir::LoopSource::WhileLet) => "while let",
32 LoopKind::Loop(hir::LoopSource::ForLoop) => "for",
33 LoopKind::WhileLoop => "while",
34 LoopKind::Block => "block",
39 #[derive(Clone, Copy, PartialEq)]
47 #[derive(Copy, Clone)]
48 struct CheckLoopVisitor<'a, 'hir: 'a> {
50 hir_map: &'a Map<'hir>,
54 pub fn check_crate(sess: &Session, map: &Map) {
55 let krate = map.krate();
56 krate.visit_all_item_likes(&mut CheckLoopVisitor {
63 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
64 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
65 NestedVisitorMap::OnlyBodies(&self.hir_map)
68 fn visit_item(&mut self, i: &'hir hir::Item) {
69 self.with_context(Normal, |v| intravisit::walk_item(v, i));
72 fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
73 self.with_context(Normal, |v| intravisit::walk_impl_item(v, i));
76 fn visit_expr(&mut self, e: &'hir hir::Expr) {
78 hir::ExprWhile(ref e, ref b, _) => {
79 self.with_context(Loop(LoopKind::WhileLoop), |v| {
84 hir::ExprLoop(ref b, _, source) => {
85 self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
87 hir::ExprClosure(.., b, _, _) => {
88 self.with_context(Closure, |v| v.visit_nested_body(b));
90 hir::ExprBlock(ref b, Some(_label)) => {
91 self.with_context(LabeledBlock, |v| v.visit_block(&b));
93 hir::ExprBreak(label, ref opt_expr) => {
94 let loop_id = match label.target_id.into() {
95 Ok(loop_id) => loop_id,
96 Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
97 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
98 self.emit_unlabled_cf_in_while_condition(e.span, "break");
101 Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
104 if loop_id != ast::DUMMY_NODE_ID {
105 match self.hir_map.find(loop_id).unwrap() {
106 hir::map::NodeBlock(_) => return,
111 if self.cx == LabeledBlock {
112 if label.label.is_none() {
113 struct_span_err!(self.sess, e.span, E0695,
114 "unlabeled `break` inside of a labeled block")
116 "`break` statements that would diverge to or through \
117 a labeled block need to bear a label")
123 if opt_expr.is_some() {
124 let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
127 Some(match self.hir_map.expect_expr(loop_id).node {
128 hir::ExprWhile(..) => LoopKind::WhileLoop,
129 hir::ExprLoop(_, _, source) => LoopKind::Loop(source),
130 hir::ExprBlock(..) => LoopKind::Block,
131 ref r => span_bug!(e.span,
132 "break label resolved to a non-loop: {:?}", r),
137 Some(LoopKind::Loop(hir::LoopSource::Loop)) |
138 Some(LoopKind::Block) => (),
140 struct_span_err!(self.sess, e.span, E0571,
141 "`break` with value from a `{}` loop",
144 "can only break with a value inside \
145 `loop` or breakable block")
146 .span_suggestion(e.span,
147 &format!("instead, use `break` on its own \
148 without a value inside this `{}` loop",
156 self.require_break_cx("break", e.span);
158 hir::ExprAgain(label) => {
159 if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
160 self.emit_unlabled_cf_in_while_condition(e.span, "continue");
162 self.require_break_cx("continue", e.span)
164 _ => intravisit::walk_expr(self, e),
169 impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
170 fn with_context<F>(&mut self, cx: Context, f: F)
171 where F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>)
173 let old_cx = self.cx;
179 fn require_break_cx(&self, name: &str, span: Span) {
184 struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
185 .span_label(span, "cannot break inside of a closure")
189 struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
190 .span_label(span, "cannot break outside of a loop")
196 fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
197 struct_span_err!(self.sess, span, E0590,
198 "`break` or `continue` with no label in the condition of a `while` loop")
200 format!("unlabeled `{}` in the condition of a `while` loop", cf_type))