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};
16 use rustc::hir::{self, Destination};
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)]
45 #[derive(Copy, Clone)]
46 struct CheckLoopVisitor<'a, 'hir: 'a> {
48 hir_map: &'a Map<'hir>,
52 pub fn check_crate(sess: &Session, map: &Map) {
53 let krate = map.krate();
54 krate.visit_all_item_likes(&mut CheckLoopVisitor {
61 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
62 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
63 NestedVisitorMap::OnlyBodies(&self.hir_map)
66 fn visit_item(&mut self, i: &'hir hir::Item) {
67 self.with_context(Normal, |v| intravisit::walk_item(v, i));
70 fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
71 self.with_context(Normal, |v| intravisit::walk_impl_item(v, i));
74 fn visit_expr(&mut self, e: &'hir hir::Expr) {
76 hir::ExprWhile(ref e, ref b, _) => {
77 self.with_context(Loop(LoopKind::WhileLoop), |v| {
82 hir::ExprLoop(ref b, _, source) => {
83 self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
85 hir::ExprClosure(_, ref function_decl, b, _, _) => {
86 self.visit_fn_decl(&function_decl);
87 self.with_context(Closure, |v| v.visit_nested_body(b));
89 hir::ExprBlock(ref b, Some(_label)) => {
90 self.with_context(LabeledBlock, |v| v.visit_block(&b));
92 hir::ExprBreak(label, ref opt_expr) => {
93 opt_expr.as_ref().map(|e| self.visit_expr(e));
95 if self.require_label_in_labeled_block(e.span, &label, "break") {
96 // If we emitted an error about an unlabeled break in a labeled
97 // block, we don't need any further checking for this break any more
101 let loop_id = match label.target_id.into() {
102 Ok(loop_id) => loop_id,
103 Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
104 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
105 self.emit_unlabled_cf_in_while_condition(e.span, "break");
108 Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
111 if loop_id != ast::DUMMY_NODE_ID {
112 match self.hir_map.find(loop_id).unwrap() {
113 hir::map::NodeBlock(_) => return,
118 if opt_expr.is_some() {
119 let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
122 Some(match self.hir_map.expect_expr(loop_id).node {
123 hir::ExprWhile(..) => LoopKind::WhileLoop,
124 hir::ExprLoop(_, _, source) => LoopKind::Loop(source),
125 ref r => span_bug!(e.span,
126 "break label resolved to a non-loop: {:?}", r),
131 Some(LoopKind::Loop(hir::LoopSource::Loop)) => (),
133 struct_span_err!(self.sess, e.span, E0571,
134 "`break` with value from a `{}` loop",
137 "can only break with a value inside \
138 `loop` or breakable block")
139 .span_suggestion(e.span,
140 &format!("instead, use `break` on its own \
141 without a value inside this `{}` loop",
149 self.require_break_cx("break", e.span);
151 hir::ExprAgain(label) => {
152 self.require_label_in_labeled_block(e.span, &label, "continue");
154 match label.target_id {
156 if let hir::map::NodeBlock(block) = self.hir_map.find(loop_id).unwrap() {
157 struct_span_err!(self.sess, e.span, E0696,
158 "`continue` pointing to a labeled block")
160 "labeled blocks cannot be `continue`'d")
161 .span_note(block.span,
162 "labeled block the continue points to")
166 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
167 self.emit_unlabled_cf_in_while_condition(e.span, "continue");
171 self.require_break_cx("continue", e.span)
173 _ => intravisit::walk_expr(self, e),
178 impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
179 fn with_context<F>(&mut self, cx: Context, f: F)
180 where F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>)
182 let old_cx = self.cx;
188 fn require_break_cx(&self, name: &str, span: Span) {
193 struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
194 .span_label(span, "cannot break inside of a closure")
198 struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
199 .span_label(span, "cannot break outside of a loop")
205 fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str)
208 if self.cx == LabeledBlock {
209 if label.label.is_none() {
210 struct_span_err!(self.sess, span, E0695,
211 "unlabeled `{}` inside of a labeled block", cf_type)
213 format!("`{}` statements that would diverge to or through \
214 a labeled block need to bear a label", cf_type))
221 fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
222 struct_span_err!(self.sess, span, E0590,
223 "`break` or `continue` with no label in the condition of a `while` loop")
225 format!("unlabeled `{}` in the condition of a `while` loop", cf_type))