3 use rustc::session::Session;
5 use errors::Applicability;
6 use rustc::hir::def_id::DefId;
7 use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
8 use rustc::hir::map::Map;
9 use rustc::hir::{self, Destination, Movability, Node};
10 use rustc::ty::query::Providers;
11 use rustc::ty::TyCtxt;
13 use syntax::struct_span_err;
15 use rustc_error_codes::*;
17 #[derive(Clone, Copy, Debug, PartialEq)]
20 Loop(hir::LoopSource),
27 #[derive(Copy, Clone)]
28 struct CheckLoopVisitor<'a, 'hir> {
30 hir_map: &'a Map<'hir>,
34 fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: DefId) {
35 tcx.hir().visit_item_likes_in_module(
37 &mut CheckLoopVisitor { sess: &tcx.sess, hir_map: &tcx.hir(), cx: Normal }
42 pub(crate) fn provide(providers: &mut Providers<'_>) {
43 *providers = Providers { check_mod_loops, ..*providers };
46 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
47 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
48 NestedVisitorMap::OnlyBodies(&self.hir_map)
51 fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
52 self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
55 fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
57 hir::ExprKind::Loop(ref b, _, source) => {
58 self.with_context(Loop(source), |v| v.visit_block(&b));
60 hir::ExprKind::Closure(_, ref function_decl, b, span, movability) => {
61 let cx = if let Some(Movability::Static) = movability {
66 self.visit_fn_decl(&function_decl);
67 self.with_context(cx, |v| v.visit_nested_body(b));
69 hir::ExprKind::Block(ref b, Some(_label)) => {
70 self.with_context(LabeledBlock, |v| v.visit_block(&b));
72 hir::ExprKind::Break(label, ref opt_expr) => {
73 opt_expr.as_ref().map(|e| self.visit_expr(e));
75 if self.require_label_in_labeled_block(e.span, &label, "break") {
76 // If we emitted an error about an unlabeled break in a labeled
77 // block, we don't need any further checking for this break any more
81 let loop_id = match label.target_id.into() {
82 Ok(loop_id) => loop_id,
83 Err(hir::LoopIdError::OutsideLoopScope) => hir::DUMMY_HIR_ID,
84 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
85 self.emit_unlabled_cf_in_while_condition(e.span, "break");
88 Err(hir::LoopIdError::UnresolvedLabel) => hir::DUMMY_HIR_ID,
91 if loop_id != hir::DUMMY_HIR_ID {
92 if let Node::Block(_) = self.hir_map.find(loop_id).unwrap() {
97 if opt_expr.is_some() {
98 let loop_kind = if loop_id == hir::DUMMY_HIR_ID {
101 Some(match self.hir_map.expect_expr(loop_id).kind {
102 hir::ExprKind::Loop(_, _, source) => source,
104 span_bug!(e.span, "break label resolved to a non-loop: {:?}", r)
109 None | Some(hir::LoopSource::Loop) => (),
115 "`break` with value from a `{}` loop",
120 "can only break with a value inside \
121 `loop` or breakable block",
126 "instead, use `break` on its own \
127 without a value inside this `{}` loop",
131 Applicability::MaybeIncorrect,
138 self.require_break_cx("break", e.span);
140 hir::ExprKind::Continue(destination) => {
141 self.require_label_in_labeled_block(e.span, &destination, "continue");
143 match destination.target_id {
145 if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
150 "`continue` pointing to a labeled block"
152 .span_label(e.span, "labeled blocks cannot be `continue`'d")
153 .span_label(block.span, "labeled block the `continue` points to")
157 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
158 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)
172 F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>),
174 let old_cx = self.cx;
180 fn require_break_cx(&self, name: &str, span: Span) {
181 let err_inside_of = |article, ty, closure_span| {
182 struct_span_err!(self.sess, span, E0267, "`{}` inside of {} {}", name, article, ty)
183 .span_label(span, format!("cannot `{}` inside of {} {}", name, article, ty))
184 .span_label(closure_span, &format!("enclosing {}", ty))
189 LabeledBlock | Loop(_) => {}
190 Closure(closure_span) => err_inside_of("a", "closure", closure_span),
191 AsyncClosure(closure_span) => err_inside_of("an", "`async` block", closure_span),
192 Normal | AnonConst => {
193 struct_span_err!(self.sess, span, E0268, "`{}` outside of a loop", name)
194 .span_label(span, format!("cannot `{}` outside of a loop", name))
200 fn require_label_in_labeled_block(
206 if self.cx == LabeledBlock {
207 if label.label.is_none() {
212 "unlabeled `{}` inside of a labeled block",
218 "`{}` statements that would diverge to or through \
219 a labeled block need to bear a label",
229 fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
234 "`break` or `continue` with no label in the condition of a `while` loop"
236 .span_label(span, format!("unlabeled `{}` in the condition of a `while` loop", cf_type))