3 use rustc::session::Session;
5 use rustc::ty::query::Providers;
7 use rustc::hir::def_id::DefId;
8 use rustc::hir::map::Map;
9 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
10 use rustc::hir::{self, Node, Destination};
11 use syntax::struct_span_err;
13 use errors::Applicability;
15 #[derive(Clone, Copy, Debug, PartialEq)]
17 Loop(hir::LoopSource),
22 fn name(self) -> &'static str {
24 LoopKind::Loop(hir::LoopSource::Loop) => "loop",
25 LoopKind::Loop(hir::LoopSource::WhileLet) => "while let",
26 LoopKind::Loop(hir::LoopSource::ForLoop) => "for",
27 LoopKind::WhileLoop => "while",
32 #[derive(Clone, Copy, Debug, PartialEq)]
41 #[derive(Copy, Clone)]
42 struct CheckLoopVisitor<'a, 'hir: 'a> {
44 hir_map: &'a Map<'hir>,
48 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
49 for &module in tcx.hir().krate().modules.keys() {
50 tcx.ensure().check_mod_loops(tcx.hir().local_def_id(module));
54 fn check_mod_loops<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
55 tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckLoopVisitor {
62 pub(crate) fn provide(providers: &mut Providers<'_>) {
63 *providers = Providers {
69 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
70 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
71 NestedVisitorMap::OnlyBodies(&self.hir_map)
74 fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
75 self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
78 fn visit_expr(&mut self, e: &'hir hir::Expr) {
80 hir::ExprKind::While(ref e, ref b, _) => {
81 self.with_context(Loop(LoopKind::WhileLoop), |v| {
86 hir::ExprKind::Loop(ref b, _, source) => {
87 self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
89 hir::ExprKind::Closure(_, ref function_decl, b, _, _) => {
90 self.visit_fn_decl(&function_decl);
91 self.with_context(Closure, |v| v.visit_nested_body(b));
93 hir::ExprKind::Block(ref b, Some(_label)) => {
94 self.with_context(LabeledBlock, |v| v.visit_block(&b));
96 hir::ExprKind::Break(label, ref opt_expr) => {
97 opt_expr.as_ref().map(|e| self.visit_expr(e));
99 if self.require_label_in_labeled_block(e.span, &label, "break") {
100 // If we emitted an error about an unlabeled break in a labeled
101 // block, we don't need any further checking for this break any more
105 let loop_id = match label.target_id.into() {
106 Ok(loop_id) => loop_id,
107 Err(hir::LoopIdError::OutsideLoopScope) => hir::DUMMY_HIR_ID,
108 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
109 self.emit_unlabled_cf_in_while_condition(e.span, "break");
112 Err(hir::LoopIdError::UnresolvedLabel) => hir::DUMMY_HIR_ID,
115 if loop_id != hir::DUMMY_HIR_ID {
116 if let Node::Block(_) = self.hir_map.find_by_hir_id(loop_id).unwrap() {
121 if opt_expr.is_some() {
122 let loop_kind = if loop_id == hir::DUMMY_HIR_ID {
125 Some(match self.hir_map.expect_expr_by_hir_id(loop_id).node {
126 hir::ExprKind::While(..) => LoopKind::WhileLoop,
127 hir::ExprKind::Loop(_, _, source) => LoopKind::Loop(source),
128 ref r => span_bug!(e.span,
129 "break label resolved to a non-loop: {:?}", r),
134 Some(LoopKind::Loop(hir::LoopSource::Loop)) => (),
136 struct_span_err!(self.sess, e.span, E0571,
137 "`break` with value from a `{}` loop",
140 "can only break with a value inside \
141 `loop` or breakable block")
145 "instead, use `break` on its own \
146 without a value inside this `{}` loop",
150 Applicability::MaybeIncorrect,
157 self.require_break_cx("break", e.span);
159 hir::ExprKind::Continue(destination) => {
160 self.require_label_in_labeled_block(e.span, &destination, "continue");
162 match destination.target_id {
164 if let Node::Block(block) = self.hir_map.find_by_hir_id(loop_id).unwrap() {
165 struct_span_err!(self.sess, e.span, E0696,
166 "`continue` pointing to a labeled block")
168 "labeled blocks cannot be `continue`'d")
169 .span_note(block.span,
170 "labeled block the continue points to")
174 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
175 self.emit_unlabled_cf_in_while_condition(e.span, "continue");
179 self.require_break_cx("continue", e.span)
181 _ => intravisit::walk_expr(self, e),
186 impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
187 fn with_context<F>(&mut self, cx: Context, f: F)
188 where F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>)
190 let old_cx = self.cx;
196 fn require_break_cx(&self, name: &str, span: Span) {
198 LabeledBlock | Loop(_) => {}
200 struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
201 .span_label(span, "cannot break inside of a closure")
204 Normal | AnonConst => {
205 struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
206 .span_label(span, "cannot break outside of a loop")
212 fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str)
215 if self.cx == LabeledBlock {
216 if label.label.is_none() {
217 struct_span_err!(self.sess, span, E0695,
218 "unlabeled `{}` inside of a labeled block", cf_type)
220 format!("`{}` statements that would diverge to or through \
221 a labeled block need to bear a label", cf_type))
228 fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
229 struct_span_err!(self.sess, span, E0590,
230 "`break` or `continue` with no label in the condition of a `while` loop")
232 format!("unlabeled `{}` in the condition of a `while` loop", cf_type))