2024-04-08 08:19:09 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::fmt;
|
2012-12-04 00:48:01 +00:00
|
|
|
|
2019-02-08 11:40:49 +00:00
|
|
|
use Context::*;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2023-10-03 00:20:59 +00:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2023-12-22 21:29:12 +00:00
|
|
|
use rustc_hir::{Destination, Node};
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2024-04-29 03:56:41 +00:00
|
|
|
use rustc_middle::span_bug;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-05-25 15:15:26 +00:00
|
|
|
use rustc_span::hygiene::DesugaringKind;
|
2023-10-03 00:20:59 +00:00
|
|
|
use rustc_span::{BytePos, Span};
|
2012-03-26 10:54:06 +00:00
|
|
|
|
2022-09-26 04:52:19 +00:00
|
|
|
use crate::errors::{
|
2024-05-05 22:14:23 +00:00
|
|
|
BreakInsideClosure, BreakInsideCoroutine, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
|
2023-10-03 00:20:59 +00:00
|
|
|
OutsideLoopSuggestion, UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
|
2022-09-26 04:52:19 +00:00
|
|
|
};
|
|
|
|
|
2024-08-06 18:10:00 +00:00
|
|
|
/// The context in which a block is encountered.
|
2018-07-01 22:40:03 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2013-11-11 19:29:15 +00:00
|
|
|
enum Context {
|
2016-07-21 01:31:14 +00:00
|
|
|
Normal,
|
2023-10-03 00:20:59 +00:00
|
|
|
Fn,
|
2019-06-19 15:21:28 +00:00
|
|
|
Loop(hir::LoopSource),
|
2019-08-21 10:17:59 +00:00
|
|
|
Closure(Span),
|
2024-08-06 18:10:00 +00:00
|
|
|
Coroutine {
|
|
|
|
coroutine_span: Span,
|
|
|
|
kind: hir::CoroutineDesugaring,
|
|
|
|
source: hir::CoroutineSource,
|
|
|
|
},
|
2023-10-03 00:20:59 +00:00
|
|
|
UnlabeledBlock(Span),
|
2024-04-08 08:19:09 +00:00
|
|
|
UnlabeledIfBlock(Span),
|
2018-04-25 17:28:04 +00:00
|
|
|
LabeledBlock,
|
2024-08-06 18:10:00 +00:00
|
|
|
/// E.g. The labeled block inside `['_'; 'block: { break 'block 1 + 2; }]`.
|
|
|
|
AnonConst,
|
|
|
|
/// E.g. `const { ... }`.
|
|
|
|
ConstBlock,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
2012-03-26 10:54:06 +00:00
|
|
|
|
2024-04-08 08:19:09 +00:00
|
|
|
#[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)]
|
2024-09-12 00:39:01 +00:00
|
|
|
struct CheckLoopVisitor<'tcx> {
|
2023-12-01 13:28:34 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-04-08 08:19:09 +00:00
|
|
|
// 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>,
|
2013-08-13 00:49:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:53:51 +00:00
|
|
|
fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
|
2024-09-13 07:48:32 +00:00
|
|
|
let mut check =
|
|
|
|
CheckLoopVisitor { tcx, cx_stack: vec![Normal], block_breaks: Default::default() };
|
2024-04-08 08:19:09 +00:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut check);
|
|
|
|
check.report_outside_loop_error();
|
2013-08-13 00:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2018-06-06 20:13:52 +00:00
|
|
|
*providers = Providers { check_mod_loops, ..*providers };
|
|
|
|
}
|
|
|
|
|
2024-09-12 00:39:01 +00:00
|
|
|
impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
|
2021-11-03 23:03:12 +00:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-01-07 16:25:33 +00:00
|
|
|
|
2021-11-03 23:03:12 +00:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
2023-12-01 13:28:34 +00:00
|
|
|
self.tcx.hir()
|
2016-10-29 13:01:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 16:24:07 +00:00
|
|
|
fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
|
2024-08-06 18:10:00 +00:00
|
|
|
self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
|
2023-02-25 19:53:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 09:11:58 +00:00
|
|
|
fn visit_inline_const(&mut self, c: &'hir hir::ConstBlock) {
|
2024-08-06 18:10:00 +00:00
|
|
|
self.with_context(ConstBlock, |v| intravisit::walk_inline_const(v, c));
|
2024-06-03 09:11:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 00:20:59 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-11-30 14:08:22 +00:00
|
|
|
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
|
2019-09-26 13:39:48 +00:00
|
|
|
match e.kind {
|
2024-04-08 08:19:09 +00:00
|
|
|
hir::ExprKind::If(cond, then, else_opt) => {
|
|
|
|
self.visit_expr(cond);
|
|
|
|
|
2024-09-12 00:39:01 +00:00
|
|
|
let get_block = |ck_loop: &CheckLoopVisitor<'hir>,
|
2024-04-08 08:19:09 +00:00
|
|
|
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)
|
2024-08-06 18:10:00 +00:00
|
|
|
| Some(&AnonConst)
|
2024-04-08 08:19:09 +00:00
|
|
|
| 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 01:15:08 +00:00
|
|
|
hir::ExprKind::Loop(ref b, _, source, _) => {
|
2023-11-21 19:07:32 +00:00
|
|
|
self.with_context(Loop(source), |v| v.visit_block(b));
|
2014-07-22 03:54:28 +00:00
|
|
|
}
|
2022-07-11 19:39:53 +00:00
|
|
|
hir::ExprKind::Closure(&hir::Closure {
|
2023-12-22 21:29:12 +00:00
|
|
|
ref fn_decl, body, fn_decl_span, kind, ..
|
2022-07-11 19:39:53 +00:00
|
|
|
}) => {
|
2023-12-22 21:29:12 +00:00
|
|
|
let cx = match kind {
|
2024-05-05 22:14:23 +00:00
|
|
|
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
|
|
|
|
Coroutine { coroutine_span: fn_decl_span, kind, source }
|
|
|
|
}
|
2023-12-22 21:29:12 +00:00
|
|
|
_ => Closure(fn_decl_span),
|
2019-08-17 11:17:02 +00:00
|
|
|
};
|
2023-11-21 19:07:32 +00:00
|
|
|
self.visit_fn_decl(fn_decl);
|
2022-06-11 19:25:25 +00:00
|
|
|
self.with_context(cx, |v| v.visit_nested_body(body));
|
2013-11-11 19:29:15 +00:00
|
|
|
}
|
2018-07-11 12:05:29 +00:00
|
|
|
hir::ExprKind::Block(ref b, Some(_label)) => {
|
2023-11-21 19:07:32 +00:00
|
|
|
self.with_context(LabeledBlock, |v| v.visit_block(b));
|
2018-04-25 17:28:04 +00:00
|
|
|
}
|
2024-08-06 18:10:00 +00:00
|
|
|
hir::ExprKind::Block(ref b, None)
|
|
|
|
if matches!(self.cx_stack.last(), Some(&Fn) | Some(&ConstBlock)) =>
|
|
|
|
{
|
2023-11-21 19:07:32 +00:00
|
|
|
self.with_context(Normal, |v| v.visit_block(b));
|
2023-10-03 00:20:59 +00:00
|
|
|
}
|
2024-08-06 18:10:00 +00:00
|
|
|
hir::ExprKind::Block(
|
|
|
|
ref b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. },
|
|
|
|
None,
|
|
|
|
) if matches!(
|
|
|
|
self.cx_stack.last(),
|
|
|
|
Some(&Normal) | Some(&AnonConst) | Some(&UnlabeledBlock(_))
|
|
|
|
) =>
|
2023-10-03 00:20:59 +00:00
|
|
|
{
|
2023-11-21 19:07:32 +00:00
|
|
|
self.with_context(UnlabeledBlock(b.span.shrink_to_lo()), |v| v.visit_block(b));
|
2023-10-03 00:20:59 +00:00
|
|
|
}
|
2021-01-22 02:13:05 +00:00
|
|
|
hir::ExprKind::Break(break_label, ref opt_expr) => {
|
2020-04-24 20:58:41 +00:00
|
|
|
if let Some(e) = opt_expr {
|
|
|
|
self.visit_expr(e);
|
|
|
|
}
|
2018-05-17 11:12:05 +00:00
|
|
|
|
2021-01-22 02:13:05 +00:00
|
|
|
if self.require_label_in_labeled_block(e.span, &break_label, "break") {
|
2018-05-16 07:18:26 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2018-05-12 09:09:36 +00:00
|
|
|
|
2021-01-22 02:13:05 +00:00
|
|
|
let loop_id = match break_label.target_id {
|
2020-04-12 18:20:07 +00:00
|
|
|
Ok(loop_id) => Some(loop_id),
|
|
|
|
Err(hir::LoopIdError::OutsideLoopScope) => None,
|
2018-05-11 13:00:09 +00:00
|
|
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(UnlabeledCfInWhileCondition {
|
2022-09-26 04:52:19 +00:00
|
|
|
span: e.span,
|
|
|
|
cf_type: "break",
|
|
|
|
});
|
2020-04-12 18:20:07 +00:00
|
|
|
None
|
2018-05-11 13:00:09 +00:00
|
|
|
}
|
2020-04-12 18:20:07 +00:00
|
|
|
Err(hir::LoopIdError::UnresolvedLabel) => None,
|
2017-02-16 07:28:59 +00:00
|
|
|
};
|
2018-04-25 17:28:04 +00:00
|
|
|
|
2024-01-21 18:13:15 +00:00
|
|
|
if let Some(Node::Block(_)) = loop_id.map(|id| self.tcx.hir_node(id)) {
|
2021-01-21 01:25:27 +00:00
|
|
|
return;
|
2018-05-11 13:00:09 +00:00
|
|
|
}
|
2017-02-16 07:28:59 +00:00
|
|
|
|
2021-01-21 01:25:27 +00:00
|
|
|
if let Some(break_expr) = opt_expr {
|
2021-01-22 02:13:05 +00:00
|
|
|
let (head, loop_label, loop_kind) = if let Some(loop_id) = loop_id {
|
2023-12-01 13:28:34 +00:00
|
|
|
match self.tcx.hir().expect_expr(loop_id).kind {
|
2021-01-21 01:25:27 +00:00
|
|
|
hir::ExprKind::Loop(_, label, source, sp) => {
|
|
|
|
(Some(sp), label, Some(source))
|
|
|
|
}
|
2017-02-15 22:52:27 +00:00
|
|
|
ref r => {
|
|
|
|
span_bug!(e.span, "break label resolved to a non-loop: {:?}", r)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2021-01-21 01:25:27 +00:00
|
|
|
}
|
2020-04-12 18:20:07 +00:00
|
|
|
} else {
|
2021-01-21 01:25:27 +00:00
|
|
|
(None, None, None)
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
};
|
|
|
|
match loop_kind {
|
2019-06-19 15:21:28 +00:00
|
|
|
None | Some(hir::LoopSource::Loop) => (),
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
Some(kind) => {
|
2022-09-26 04:52:19 +00:00
|
|
|
let suggestion = format!(
|
|
|
|
"break{}",
|
|
|
|
break_label
|
|
|
|
.label
|
|
|
|
.map_or_else(String::new, |l| format!(" {}", l.ident))
|
2021-01-21 01:25:27 +00:00
|
|
|
);
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(BreakNonLoop {
|
2022-09-26 04:52:19 +00:00
|
|
|
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,
|
|
|
|
});
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 07:28:59 +00:00
|
|
|
|
2023-10-03 00:20:59 +00:00
|
|
|
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(),
|
|
|
|
};
|
2024-04-08 08:19:09 +00:00
|
|
|
self.require_break_cx(
|
|
|
|
BreakContextKind::Break,
|
|
|
|
e.span,
|
|
|
|
label_sp,
|
|
|
|
self.cx_stack.len() - 1,
|
|
|
|
);
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
}
|
2018-09-07 13:10:16 +00:00
|
|
|
hir::ExprKind::Continue(destination) => {
|
|
|
|
self.require_label_in_labeled_block(e.span, &destination, "continue");
|
2018-05-12 09:09:36 +00:00
|
|
|
|
2018-09-07 13:10:16 +00:00
|
|
|
match destination.target_id {
|
2018-05-12 08:18:21 +00:00
|
|
|
Ok(loop_id) => {
|
2024-01-21 18:13:15 +00:00
|
|
|
if let Node::Block(block) = self.tcx.hir_node(loop_id) {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(ContinueLabeledBlock {
|
2022-09-26 04:52:19 +00:00
|
|
|
span: e.span,
|
|
|
|
block_span: block.span,
|
|
|
|
});
|
2018-05-12 08:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(UnlabeledCfInWhileCondition {
|
2022-09-26 04:52:19 +00:00
|
|
|
span: e.span,
|
|
|
|
cf_type: "continue",
|
|
|
|
});
|
2018-05-12 08:18:21 +00:00
|
|
|
}
|
2018-09-07 13:10:16 +00:00
|
|
|
Err(_) => {}
|
2017-02-16 07:28:59 +00:00
|
|
|
}
|
2024-04-08 08:19:09 +00:00
|
|
|
self.require_break_cx(
|
|
|
|
BreakContextKind::Continue,
|
|
|
|
e.span,
|
|
|
|
e.span,
|
|
|
|
self.cx_stack.len() - 1,
|
|
|
|
)
|
2017-02-16 07:28:59 +00:00
|
|
|
}
|
2016-07-21 01:31:14 +00:00
|
|
|
_ => intravisit::walk_expr(self, e),
|
2013-11-11 19:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 00:49:30 +00:00
|
|
|
|
2024-09-12 00:39:01 +00:00
|
|
|
impl<'hir> CheckLoopVisitor<'hir> {
|
2016-07-21 01:31:14 +00:00
|
|
|
fn with_context<F>(&mut self, cx: Context, f: F)
|
2017-01-26 01:21:50 +00:00
|
|
|
where
|
2024-09-12 00:39:01 +00:00
|
|
|
F: FnOnce(&mut CheckLoopVisitor<'hir>),
|
2014-12-09 01:26:43 +00:00
|
|
|
{
|
2024-04-08 08:19:09 +00:00
|
|
|
self.cx_stack.push(cx);
|
2014-09-12 10:10:30 +00:00
|
|
|
f(self);
|
2024-04-08 08:19:09 +00:00
|
|
|
self.cx_stack.pop();
|
2014-09-12 10:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 08:19:09 +00:00
|
|
|
fn require_break_cx(
|
|
|
|
&mut self,
|
|
|
|
br_cx_kind: BreakContextKind,
|
|
|
|
span: Span,
|
|
|
|
break_span: Span,
|
|
|
|
cx_pos: usize,
|
|
|
|
) {
|
|
|
|
match self.cx_stack[cx_pos] {
|
2019-08-21 10:17:59 +00:00
|
|
|
LabeledBlock | Loop(_) => {}
|
2022-09-26 04:52:19 +00:00
|
|
|
Closure(closure_span) => {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(BreakInsideClosure {
|
2024-04-08 08:19:09 +00:00
|
|
|
span,
|
|
|
|
closure_span,
|
|
|
|
name: &br_cx_kind.to_string(),
|
|
|
|
});
|
2022-09-26 04:52:19 +00:00
|
|
|
}
|
2024-05-05 22:14:23 +00:00
|
|
|
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",
|
|
|
|
};
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(BreakInsideCoroutine {
|
2024-05-05 22:14:23 +00:00
|
|
|
span,
|
|
|
|
coroutine_span,
|
2024-04-08 08:19:09 +00:00
|
|
|
name: &br_cx_kind.to_string(),
|
2024-05-05 22:14:23 +00:00
|
|
|
kind,
|
|
|
|
source,
|
|
|
|
});
|
2022-09-26 04:52:19 +00:00
|
|
|
}
|
2024-04-08 08:19:09 +00:00
|
|
|
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);
|
2023-10-03 00:20:59 +00:00
|
|
|
}
|
2024-08-06 18:10:00 +00:00
|
|
|
Normal | AnonConst | Fn | UnlabeledBlock(_) | UnlabeledIfBlock(_) | ConstBlock => {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(OutsideLoop {
|
2024-04-08 08:19:09 +00:00
|
|
|
spans: vec![span],
|
|
|
|
name: &br_cx_kind.to_string(),
|
|
|
|
is_break: br_cx_kind == BreakContextKind::Break,
|
|
|
|
suggestion: None,
|
|
|
|
});
|
2019-08-21 10:17:59 +00:00
|
|
|
}
|
2013-11-11 19:29:15 +00:00
|
|
|
}
|
2013-08-13 00:49:30 +00:00
|
|
|
}
|
2017-02-16 07:28:59 +00:00
|
|
|
|
2018-05-16 07:18:26 +00:00
|
|
|
fn require_label_in_labeled_block(
|
2024-08-06 18:10:00 +00:00
|
|
|
&self,
|
2018-05-16 07:18:26 +00:00
|
|
|
span: Span,
|
|
|
|
label: &Destination,
|
|
|
|
cf_type: &str,
|
|
|
|
) -> bool {
|
2022-09-26 04:52:19 +00:00
|
|
|
if !span.is_desugaring(DesugaringKind::QuestionMark)
|
2024-04-08 08:19:09 +00:00
|
|
|
&& self.cx_stack.last() == Some(&LabeledBlock)
|
2022-09-26 04:52:19 +00:00
|
|
|
&& label.label.is_none()
|
|
|
|
{
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(UnlabeledInLabeledBlock { span, cf_type });
|
2022-09-26 04:52:19 +00:00
|
|
|
return true;
|
2018-05-12 09:09:36 +00:00
|
|
|
}
|
2020-03-20 14:03:11 +00:00
|
|
|
false
|
2018-05-12 09:09:36 +00:00
|
|
|
}
|
2024-04-08 08:19:09 +00:00
|
|
|
|
2024-08-06 18:10:00 +00:00
|
|
|
fn report_outside_loop_error(&self) {
|
2024-04-08 08:19:09 +00:00
|
|
|
for (s, block) in &self.block_breaks {
|
2024-09-13 07:48:32 +00:00
|
|
|
self.tcx.dcx().emit_err(OutsideLoop {
|
2024-04-08 08:19:09 +00:00
|
|
|
spans: block.spans.clone(),
|
|
|
|
name: &block.name,
|
|
|
|
is_break: true,
|
|
|
|
suggestion: Some(OutsideLoopSuggestion {
|
|
|
|
block_span: *s,
|
|
|
|
break_spans: block.suggs.clone(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2012-07-13 15:24:07 +00:00
|
|
|
}
|