4 use rustc_hir::def_id::LocalDefId;
5 use rustc_hir::intravisit::{self, Visitor};
6 use rustc_hir::{Destination, Movability, Node};
7 use rustc_middle::hir::map::Map;
8 use rustc_middle::hir::nested_filter;
9 use rustc_middle::ty::query::Providers;
10 use rustc_middle::ty::TyCtxt;
11 use rustc_session::Session;
12 use rustc_span::hygiene::DesugaringKind;
16 BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
17 UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
20 #[derive(Clone, Copy, Debug, PartialEq)]
23 Loop(hir::LoopSource),
30 #[derive(Copy, Clone)]
31 struct CheckLoopVisitor<'a, 'hir> {
37 fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
38 tcx.hir().visit_item_likes_in_module(
40 &mut CheckLoopVisitor { sess: &tcx.sess, hir_map: tcx.hir(), cx: Normal },
44 pub(crate) fn provide(providers: &mut Providers) {
45 *providers = Providers { check_mod_loops, ..*providers };
48 impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
49 type NestedFilter = nested_filter::OnlyBodies;
51 fn nested_visit_map(&mut self) -> Self::Map {
55 fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
56 self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
59 fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
61 hir::ExprKind::Loop(ref b, _, source, _) => {
62 self.with_context(Loop(source), |v| v.visit_block(&b));
64 hir::ExprKind::Closure(&hir::Closure {
71 let cx = if let Some(Movability::Static) = movability {
72 AsyncClosure(fn_decl_span)
76 self.visit_fn_decl(&fn_decl);
77 self.with_context(cx, |v| v.visit_nested_body(body));
79 hir::ExprKind::Block(ref b, Some(_label)) => {
80 self.with_context(LabeledBlock, |v| v.visit_block(&b));
82 hir::ExprKind::Break(break_label, ref opt_expr) => {
83 if let Some(e) = opt_expr {
87 if self.require_label_in_labeled_block(e.span, &break_label, "break") {
88 // If we emitted an error about an unlabeled break in a labeled
89 // block, we don't need any further checking for this break any more
93 let loop_id = match break_label.target_id {
94 Ok(loop_id) => Some(loop_id),
95 Err(hir::LoopIdError::OutsideLoopScope) => None,
96 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
97 self.sess.emit_err(UnlabeledCfInWhileCondition {
103 Err(hir::LoopIdError::UnresolvedLabel) => None,
106 if let Some(Node::Block(_)) = loop_id.and_then(|id| self.hir_map.find(id)) {
110 if let Some(break_expr) = opt_expr {
111 let (head, loop_label, loop_kind) = if let Some(loop_id) = loop_id {
112 match self.hir_map.expect_expr(loop_id).kind {
113 hir::ExprKind::Loop(_, label, source, sp) => {
114 (Some(sp), label, Some(source))
117 span_bug!(e.span, "break label resolved to a non-loop: {:?}", r)
124 None | Some(hir::LoopSource::Loop) => (),
126 let suggestion = format!(
130 .map_or_else(String::new, |l| format!(" {}", l.ident))
132 self.sess.emit_err(BreakNonLoop {
138 break_label: break_label.label,
139 break_expr_kind: &break_expr.kind,
140 break_expr_span: break_expr.span,
146 self.require_break_cx("break", e.span);
148 hir::ExprKind::Continue(destination) => {
149 self.require_label_in_labeled_block(e.span, &destination, "continue");
151 match destination.target_id {
153 if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
154 self.sess.emit_err(ContinueLabeledBlock {
156 block_span: block.span,
160 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
161 self.sess.emit_err(UnlabeledCfInWhileCondition {
168 self.require_break_cx("continue", e.span)
170 _ => intravisit::walk_expr(self, e),
175 impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
176 fn with_context<F>(&mut self, cx: Context, f: F)
178 F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>),
180 let old_cx = self.cx;
186 fn require_break_cx(&self, name: &str, span: Span) {
188 LabeledBlock | Loop(_) => {}
189 Closure(closure_span) => {
190 self.sess.emit_err(BreakInsideClosure { span, closure_span, name });
192 AsyncClosure(closure_span) => {
193 self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
195 Normal | AnonConst => {
196 self.sess.emit_err(OutsideLoop { span, name, is_break: name == "break" });
201 fn require_label_in_labeled_block(
207 if !span.is_desugaring(DesugaringKind::QuestionMark)
208 && self.cx == LabeledBlock
209 && label.label.is_none()
211 self.sess.emit_err(UnlabeledInLabeledBlock { span, cf_type });