2019-02-08 11:40:49 +00:00
|
|
|
use Context::*;
|
2012-12-04 00:48:01 +00:00
|
|
|
|
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;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-01-18 20:53:53 +00:00
|
|
|
use rustc_session::Session;
|
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::{
|
|
|
|
BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
|
2023-10-03 00:20:59 +00:00
|
|
|
OutsideLoopSuggestion, UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
|
2022-09-26 04:52:19 +00:00
|
|
|
};
|
|
|
|
|
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),
|
|
|
|
AsyncClosure(Span),
|
2023-10-03 00:20:59 +00:00
|
|
|
UnlabeledBlock(Span),
|
2018-04-25 17:28:04 +00:00
|
|
|
LabeledBlock,
|
2023-02-25 19:53:37 +00:00
|
|
|
Constant,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
2012-03-26 10:54:06 +00:00
|
|
|
|
2015-03-30 13:38:44 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2023-12-01 13:28:34 +00:00
|
|
|
struct CheckLoopVisitor<'a, 'tcx> {
|
2014-04-04 23:05:31 +00:00
|
|
|
sess: &'a Session,
|
2023-12-01 13:28:34 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-07-21 01:31:14 +00:00
|
|
|
cx: Context,
|
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) {
|
2022-07-03 13:28:57 +00:00
|
|
|
tcx.hir().visit_item_likes_in_module(
|
2019-01-11 03:58:46 +00:00
|
|
|
module_def_id,
|
2023-12-01 13:28:34 +00:00
|
|
|
&mut CheckLoopVisitor { sess: tcx.sess, tcx, cx: Normal },
|
2016-11-02 22:22:59 +00:00
|
|
|
);
|
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 };
|
|
|
|
}
|
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, '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) {
|
2023-02-25 19:53:37 +00:00
|
|
|
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));
|
2018-07-01 16:24:07 +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 {
|
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
|
|
|
// FIXME(coroutines): This doesn't handle coroutines correctly
|
|
|
|
let cx = match kind {
|
2023-12-25 16:56:12 +00:00
|
|
|
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Async,
|
|
|
|
hir::CoroutineSource::Block,
|
|
|
|
)) => AsyncClosure(fn_decl_span),
|
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
|
|
|
}
|
2023-10-03 00:20:59 +00:00
|
|
|
hir::ExprKind::Block(ref b, None) if matches!(self.cx, Fn) => {
|
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
|
|
|
}
|
|
|
|
hir::ExprKind::Block(ref b, None)
|
|
|
|
if matches!(self.cx, Normal | Constant | UnlabeledBlock(_)) =>
|
|
|
|
{
|
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) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.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
|
|
|
);
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.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(),
|
|
|
|
};
|
|
|
|
self.require_break_cx("break", e.span, label_sp);
|
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) {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.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) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.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
|
|
|
}
|
2023-10-03 00:20:59 +00:00
|
|
|
self.require_break_cx("continue", e.span, e.span)
|
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
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
impl<'a, 'hir> CheckLoopVisitor<'a, '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
|
|
|
|
F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>),
|
2014-12-09 01:26:43 +00:00
|
|
|
{
|
2014-09-12 10:10:30 +00:00
|
|
|
let old_cx = self.cx;
|
|
|
|
self.cx = cx;
|
|
|
|
f(self);
|
|
|
|
self.cx = old_cx;
|
|
|
|
}
|
|
|
|
|
2023-10-03 00:20:59 +00:00
|
|
|
fn require_break_cx(&self, name: &str, span: Span, break_span: Span) {
|
|
|
|
let is_break = name == "break";
|
2019-08-21 10:17:59 +00:00
|
|
|
match self.cx {
|
|
|
|
LabeledBlock | Loop(_) => {}
|
2022-09-26 04:52:19 +00:00
|
|
|
Closure(closure_span) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.dcx().emit_err(BreakInsideClosure { span, closure_span, name });
|
2022-09-26 04:52:19 +00:00
|
|
|
}
|
|
|
|
AsyncClosure(closure_span) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.dcx().emit_err(BreakInsideAsyncBlock { span, closure_span, name });
|
2022-09-26 04:52:19 +00:00
|
|
|
}
|
2023-10-16 08:17:52 +00:00
|
|
|
UnlabeledBlock(block_span) if is_break && block_span.eq_ctxt(break_span) => {
|
2023-10-03 00:20:59 +00:00
|
|
|
let suggestion = Some(OutsideLoopSuggestion { block_span, break_span });
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.dcx().emit_err(OutsideLoop { span, name, is_break, suggestion });
|
2023-10-03 00:20:59 +00:00
|
|
|
}
|
|
|
|
Normal | Constant | Fn | UnlabeledBlock(_) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.dcx().emit_err(OutsideLoop { span, name, is_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(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
label: &Destination,
|
|
|
|
cf_type: &str,
|
|
|
|
) -> bool {
|
2022-09-26 04:52:19 +00:00
|
|
|
if !span.is_desugaring(DesugaringKind::QuestionMark)
|
|
|
|
&& self.cx == LabeledBlock
|
|
|
|
&& label.label.is_none()
|
|
|
|
{
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.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
|
|
|
}
|
2012-07-13 15:24:07 +00:00
|
|
|
}
|