From 50f063c75b4d05976a1c132afd213c75913c7605 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 12 May 2018 11:09:36 +0200 Subject: [PATCH] Extend error E0695 to unlabeled continue statements --- src/librustc_passes/loops.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/librustc_passes/loops.rs b/src/librustc_passes/loops.rs index 2d240a7cf2f..bd43cd71e41 100644 --- a/src/librustc_passes/loops.rs +++ b/src/librustc_passes/loops.rs @@ -13,7 +13,7 @@ use rustc::hir::map::Map; use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap}; -use rustc::hir; +use rustc::hir::{self, Destination}; use syntax::ast; use syntax_pos::Span; @@ -91,6 +91,8 @@ fn visit_expr(&mut self, e: &'hir hir::Expr) { self.with_context(LabeledBlock, |v| v.visit_block(&b)); } hir::ExprBreak(label, ref opt_expr) => { + self.require_label_in_labeled_block(e.span, &label, "break"); + let loop_id = match label.target_id.into() { Ok(loop_id) => loop_id, Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID, @@ -109,14 +111,6 @@ fn visit_expr(&mut self, e: &'hir hir::Expr) { } if self.cx == LabeledBlock { - if label.label.is_none() { - struct_span_err!(self.sess, e.span, E0695, - "unlabeled `break` inside of a labeled block") - .span_label(e.span, - "`break` statements that would diverge to or through \ - a labeled block need to bear a label") - .emit(); - } return; } @@ -156,6 +150,8 @@ fn visit_expr(&mut self, e: &'hir hir::Expr) { self.require_break_cx("break", e.span); } hir::ExprAgain(label) => { + self.require_label_in_labeled_block(e.span, &label, "continue"); + if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id { self.emit_unlabled_cf_in_while_condition(e.span, "continue"); } @@ -193,6 +189,18 @@ fn require_break_cx(&self, name: &str, span: Span) { } } + fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str) { + if self.cx == LabeledBlock { + if label.label.is_none() { + struct_span_err!(self.sess, span, E0695, + "unlabeled `{}` inside of a labeled block", cf_type) + .span_label(span, + format!("`{}` statements that would diverge to or through \ + a labeled block need to bear a label", cf_type)) + .emit(); + } + } + } fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) { struct_span_err!(self.sess, span, E0590, "`break` or `continue` with no label in the condition of a `while` loop") -- 2.44.0