]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/loops.rs
4719f00b4d25929068af9789f80ee8693dfcbf02
[rust.git] / src / librustc_passes / loops.rs
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.
4 //
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.
10 use self::Context::*;
11
12 use rustc::session::Session;
13
14 use rustc::hir::map::Map;
15 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
16 use rustc::hir::{self, Destination};
17 use syntax::ast;
18 use syntax_pos::Span;
19
20 #[derive(Clone, Copy, PartialEq)]
21 enum LoopKind {
22     Loop(hir::LoopSource),
23     WhileLoop,
24 }
25
26 impl LoopKind {
27     fn name(self) -> &'static str {
28         match self {
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",
33         }
34     }
35 }
36
37 #[derive(Clone, Copy, PartialEq)]
38 enum Context {
39     Normal,
40     Loop(LoopKind),
41     Closure,
42     LabeledBlock,
43 }
44
45 #[derive(Copy, Clone)]
46 struct CheckLoopVisitor<'a, 'hir: 'a> {
47     sess: &'a Session,
48     hir_map: &'a Map<'hir>,
49     cx: Context,
50 }
51
52 pub fn check_crate(sess: &Session, map: &Map) {
53     let krate = map.krate();
54     krate.visit_all_item_likes(&mut CheckLoopVisitor {
55         sess,
56         hir_map: map,
57         cx: Normal,
58     }.as_deep_visitor());
59 }
60
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)
64     }
65
66     fn visit_item(&mut self, i: &'hir hir::Item) {
67         self.with_context(Normal, |v| intravisit::walk_item(v, i));
68     }
69
70     fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
71         self.with_context(Normal, |v| intravisit::walk_impl_item(v, i));
72     }
73
74     fn visit_expr(&mut self, e: &'hir hir::Expr) {
75         match e.node {
76             hir::ExprWhile(ref e, ref b, _) => {
77                 self.with_context(Loop(LoopKind::WhileLoop), |v| {
78                     v.visit_expr(&e);
79                     v.visit_block(&b);
80                 });
81             }
82             hir::ExprLoop(ref b, _, source) => {
83                 self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
84             }
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));
88             }
89             hir::ExprBlock(ref b, Some(_label)) => {
90                 self.with_context(LabeledBlock, |v| v.visit_block(&b));
91             }
92             hir::ExprBreak(label, ref opt_expr) => {
93                 opt_expr.as_ref().map(|e| self.visit_expr(e));
94
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
98                     return;
99                 }
100
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");
106                         ast::DUMMY_NODE_ID
107                     },
108                     Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
109                 };
110
111                 if loop_id != ast::DUMMY_NODE_ID {
112                     match self.hir_map.find(loop_id).unwrap() {
113                         hir::map::NodeBlock(_) => return,
114                         _=> (),
115                     }
116                 }
117
118                 if opt_expr.is_some() {
119                     let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
120                         None
121                     } else {
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),
127                         })
128                     };
129                     match loop_kind {
130                         None |
131                         Some(LoopKind::Loop(hir::LoopSource::Loop)) => (),
132                         Some(kind) => {
133                             struct_span_err!(self.sess, e.span, E0571,
134                                              "`break` with value from a `{}` loop",
135                                              kind.name())
136                                 .span_label(e.span,
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",
142                                                           kind.name()),
143                                                  "break".to_string())
144                                 .emit();
145                         }
146                     }
147                 }
148
149                 self.require_break_cx("break", e.span);
150             }
151             hir::ExprAgain(label) => {
152                 self.require_label_in_labeled_block(e.span, &label, "continue");
153
154                 match label.target_id {
155                     Ok(loop_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")
159                                 .span_label(e.span,
160                                             "labeled blocks cannot be `continue`'d")
161                                 .span_note(block.span,
162                                             "labeled block the continue points to")
163                                 .emit();
164                         }
165                     }
166                     Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
167                         self.emit_unlabled_cf_in_while_condition(e.span, "continue");
168                     }
169                     _ => {}
170                 }
171                 self.require_break_cx("continue", e.span)
172             },
173             _ => intravisit::walk_expr(self, e),
174         }
175     }
176 }
177
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>)
181     {
182         let old_cx = self.cx;
183         self.cx = cx;
184         f(self);
185         self.cx = old_cx;
186     }
187
188     fn require_break_cx(&self, name: &str, span: Span) {
189         match self.cx {
190             LabeledBlock |
191             Loop(_) => {}
192             Closure => {
193                 struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
194                 .span_label(span, "cannot break inside of a closure")
195                 .emit();
196             }
197             Normal => {
198                 struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
199                 .span_label(span, "cannot break outside of a loop")
200                 .emit();
201             }
202         }
203     }
204
205     fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str)
206         -> bool
207     {
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)
212                     .span_label(span,
213                                 format!("`{}` statements that would diverge to or through \
214                                 a labeled block need to bear a label", cf_type))
215                     .emit();
216                 return true;
217             }
218         }
219         return false;
220     }
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")
224             .span_label(span,
225                         format!("unlabeled `{}` in the condition of a `while` loop", cf_type))
226             .emit();
227     }
228 }