1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use std::collections::BTreeMap;
use std::fmt;

use rustc_hir as hir;
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Destination, Node};
use rustc_middle::hir::nested_filter;
use rustc_middle::query::Providers;
use rustc_middle::span_bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::{BytePos, Span};
use Context::*;

use crate::errors::{
    BreakInsideClosure, BreakInsideCoroutine, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
    OutsideLoopSuggestion, UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
};

#[derive(Clone, Copy, Debug, PartialEq)]
enum Context {
    Normal,
    Fn,
    Loop(hir::LoopSource),
    Closure(Span),
    Coroutine { coroutine_span: Span, kind: hir::CoroutineDesugaring, source: hir::CoroutineSource },
    UnlabeledBlock(Span),
    UnlabeledIfBlock(Span),
    LabeledBlock,
    Constant,
}

#[derive(Clone)]
struct BlockInfo {
    name: String,
    spans: Vec<Span>,
    suggs: Vec<Span>,
}

#[derive(PartialEq)]
enum BreakContextKind {
    Break,
    Continue,
}

impl fmt::Display for BreakContextKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BreakContextKind::Break => "break",
            BreakContextKind::Continue => "continue",
        }
        .fmt(f)
    }
}

#[derive(Clone)]
struct CheckLoopVisitor<'a, 'tcx> {
    sess: &'a Session,
    tcx: TyCtxt<'tcx>,
    // Keep track of a stack of contexts, so that suggestions
    // are not made for contexts where it would be incorrect,
    // such as adding a label for an `if`.
    // e.g. `if 'foo: {}` would be incorrect.
    cx_stack: Vec<Context>,
    block_breaks: BTreeMap<Span, BlockInfo>,
}

fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
    let mut check = CheckLoopVisitor {
        sess: tcx.sess,
        tcx,
        cx_stack: vec![Normal],
        block_breaks: Default::default(),
    };
    tcx.hir().visit_item_likes_in_module(module_def_id, &mut check);
    check.report_outside_loop_error();
}

pub(crate) fn provide(providers: &mut Providers) {
    *providers = Providers { check_mod_loops, ..*providers };
}

impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
    type NestedFilter = nested_filter::OnlyBodies;

    fn nested_visit_map(&mut self) -> Self::Map {
        self.tcx.hir()
    }

    fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
        self.with_context(Constant, |v| intravisit::walk_anon_const(v, c));
    }

    fn visit_inline_const(&mut self, c: &'hir hir::ConstBlock) {
        self.with_context(Constant, |v| intravisit::walk_inline_const(v, c));
    }

    fn visit_fn(
        &mut self,
        fk: hir::intravisit::FnKind<'hir>,
        fd: &'hir hir::FnDecl<'hir>,
        b: hir::BodyId,
        _: Span,
        id: LocalDefId,
    ) {
        self.with_context(Fn, |v| intravisit::walk_fn(v, fk, fd, b, id));
    }

    fn visit_trait_item(&mut self, trait_item: &'hir hir::TraitItem<'hir>) {
        self.with_context(Fn, |v| intravisit::walk_trait_item(v, trait_item));
    }

    fn visit_impl_item(&mut self, impl_item: &'hir hir::ImplItem<'hir>) {
        self.with_context(Fn, |v| intravisit::walk_impl_item(v, impl_item));
    }

    fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
        match e.kind {
            hir::ExprKind::If(cond, then, else_opt) => {
                self.visit_expr(cond);

                let get_block = |ck_loop: &CheckLoopVisitor<'a, 'hir>,
                                 expr: &hir::Expr<'hir>|
                 -> Option<&hir::Block<'hir>> {
                    if let hir::ExprKind::Block(b, None) = expr.kind
                        && matches!(
                            ck_loop.cx_stack.last(),
                            Some(&Normal)
                                | Some(&Constant)
                                | Some(&UnlabeledBlock(_))
                                | Some(&UnlabeledIfBlock(_))
                        )
                    {
                        Some(b)
                    } else {
                        None
                    }
                };

                if let Some(b) = get_block(self, then) {
                    self.with_context(UnlabeledIfBlock(b.span.shrink_to_lo()), |v| {
                        v.visit_block(b)
                    });
                } else {
                    self.visit_expr(then);
                }

                if let Some(else_expr) = else_opt {
                    if let Some(b) = get_block(self, else_expr) {
                        self.with_context(UnlabeledIfBlock(b.span.shrink_to_lo()), |v| {
                            v.visit_block(b)
                        });
                    } else {
                        self.visit_expr(else_expr);
                    }
                }
            }
            hir::ExprKind::Loop(ref b, _, source, _) => {
                self.with_context(Loop(source), |v| v.visit_block(b));
            }
            hir::ExprKind::Closure(&hir::Closure {
                ref fn_decl, body, fn_decl_span, kind, ..
            }) => {
                let cx = match kind {
                    hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
                        Coroutine { coroutine_span: fn_decl_span, kind, source }
                    }
                    _ => Closure(fn_decl_span),
                };
                self.visit_fn_decl(fn_decl);
                self.with_context(cx, |v| v.visit_nested_body(body));
            }
            hir::ExprKind::Block(ref b, Some(_label)) => {
                self.with_context(LabeledBlock, |v| v.visit_block(b));
            }
            hir::ExprKind::Block(ref b, None) if matches!(self.cx_stack.last(), Some(&Fn)) => {
                self.with_context(Normal, |v| v.visit_block(b));
            }
            hir::ExprKind::Block(ref b, None)
                if matches!(
                    self.cx_stack.last(),
                    Some(&Normal) | Some(&Constant) | Some(&UnlabeledBlock(_))
                ) =>
            {
                self.with_context(UnlabeledBlock(b.span.shrink_to_lo()), |v| v.visit_block(b));
            }
            hir::ExprKind::Break(break_label, ref opt_expr) => {
                if let Some(e) = opt_expr {
                    self.visit_expr(e);
                }

                if self.require_label_in_labeled_block(e.span, &break_label, "break") {
                    // If we emitted an error about an unlabeled break in a labeled
                    // block, we don't need any further checking for this break any more
                    return;
                }

                let loop_id = match break_label.target_id {
                    Ok(loop_id) => Some(loop_id),
                    Err(hir::LoopIdError::OutsideLoopScope) => None,
                    Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
                        self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
                            span: e.span,
                            cf_type: "break",
                        });
                        None
                    }
                    Err(hir::LoopIdError::UnresolvedLabel) => None,
                };

                if let Some(Node::Block(_)) = loop_id.map(|id| self.tcx.hir_node(id)) {
                    return;
                }

                if let Some(break_expr) = opt_expr {
                    let (head, loop_label, loop_kind) = if let Some(loop_id) = loop_id {
                        match self.tcx.hir().expect_expr(loop_id).kind {
                            hir::ExprKind::Loop(_, label, source, sp) => {
                                (Some(sp), label, Some(source))
                            }
                            ref r => {
                                span_bug!(e.span, "break label resolved to a non-loop: {:?}", r)
                            }
                        }
                    } else {
                        (None, None, None)
                    };
                    match loop_kind {
                        None | Some(hir::LoopSource::Loop) => (),
                        Some(kind) => {
                            let suggestion = format!(
                                "break{}",
                                break_label
                                    .label
                                    .map_or_else(String::new, |l| format!(" {}", l.ident))
                            );
                            self.sess.dcx().emit_err(BreakNonLoop {
                                span: e.span,
                                head,
                                kind: kind.name(),
                                suggestion,
                                loop_label,
                                break_label: break_label.label,
                                break_expr_kind: &break_expr.kind,
                                break_expr_span: break_expr.span,
                            });
                        }
                    }
                }

                let sp_lo = e.span.with_lo(e.span.lo() + BytePos("break".len() as u32));
                let label_sp = match break_label.label {
                    Some(label) => sp_lo.with_hi(label.ident.span.hi()),
                    None => sp_lo.shrink_to_lo(),
                };
                self.require_break_cx(
                    BreakContextKind::Break,
                    e.span,
                    label_sp,
                    self.cx_stack.len() - 1,
                );
            }
            hir::ExprKind::Continue(destination) => {
                self.require_label_in_labeled_block(e.span, &destination, "continue");

                match destination.target_id {
                    Ok(loop_id) => {
                        if let Node::Block(block) = self.tcx.hir_node(loop_id) {
                            self.sess.dcx().emit_err(ContinueLabeledBlock {
                                span: e.span,
                                block_span: block.span,
                            });
                        }
                    }
                    Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
                        self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
                            span: e.span,
                            cf_type: "continue",
                        });
                    }
                    Err(_) => {}
                }
                self.require_break_cx(
                    BreakContextKind::Continue,
                    e.span,
                    e.span,
                    self.cx_stack.len() - 1,
                )
            }
            _ => intravisit::walk_expr(self, e),
        }
    }
}

impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
    fn with_context<F>(&mut self, cx: Context, f: F)
    where
        F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>),
    {
        self.cx_stack.push(cx);
        f(self);
        self.cx_stack.pop();
    }

    fn require_break_cx(
        &mut self,
        br_cx_kind: BreakContextKind,
        span: Span,
        break_span: Span,
        cx_pos: usize,
    ) {
        match self.cx_stack[cx_pos] {
            LabeledBlock | Loop(_) => {}
            Closure(closure_span) => {
                self.sess.dcx().emit_err(BreakInsideClosure {
                    span,
                    closure_span,
                    name: &br_cx_kind.to_string(),
                });
            }
            Coroutine { coroutine_span, kind, source } => {
                let kind = match kind {
                    hir::CoroutineDesugaring::Async => "async",
                    hir::CoroutineDesugaring::Gen => "gen",
                    hir::CoroutineDesugaring::AsyncGen => "async gen",
                };
                let source = match source {
                    hir::CoroutineSource::Block => "block",
                    hir::CoroutineSource::Closure => "closure",
                    hir::CoroutineSource::Fn => "function",
                };
                self.sess.dcx().emit_err(BreakInsideCoroutine {
                    span,
                    coroutine_span,
                    name: &br_cx_kind.to_string(),
                    kind,
                    source,
                });
            }
            UnlabeledBlock(block_span)
                if br_cx_kind == BreakContextKind::Break && block_span.eq_ctxt(break_span) =>
            {
                let block = self.block_breaks.entry(block_span).or_insert_with(|| BlockInfo {
                    name: br_cx_kind.to_string(),
                    spans: vec![],
                    suggs: vec![],
                });
                block.spans.push(span);
                block.suggs.push(break_span);
            }
            UnlabeledIfBlock(_) if br_cx_kind == BreakContextKind::Break => {
                self.require_break_cx(br_cx_kind, span, break_span, cx_pos - 1);
            }
            Normal | Constant | Fn | UnlabeledBlock(_) | UnlabeledIfBlock(_) => {
                self.sess.dcx().emit_err(OutsideLoop {
                    spans: vec![span],
                    name: &br_cx_kind.to_string(),
                    is_break: br_cx_kind == BreakContextKind::Break,
                    suggestion: None,
                });
            }
        }
    }

    fn require_label_in_labeled_block(
        &mut self,
        span: Span,
        label: &Destination,
        cf_type: &str,
    ) -> bool {
        if !span.is_desugaring(DesugaringKind::QuestionMark)
            && self.cx_stack.last() == Some(&LabeledBlock)
            && label.label.is_none()
        {
            self.sess.dcx().emit_err(UnlabeledInLabeledBlock { span, cf_type });
            return true;
        }
        false
    }

    fn report_outside_loop_error(&mut self) {
        for (s, block) in &self.block_breaks {
            self.sess.dcx().emit_err(OutsideLoop {
                spans: block.spans.clone(),
                name: &block.name,
                is_break: true,
                suggestion: Some(OutsideLoopSuggestion {
                    block_span: *s,
                    break_spans: block.suggs.clone(),
                }),
            });
        }
    }
}