2019-02-08 20:40:49 +09:00
|
|
|
use Context::*;
|
2012-12-03 16:48:01 -08:00
|
|
|
|
2016-01-21 10:52:37 +01:00
|
|
|
use rustc::session::Session;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
use rustc::ty::query::Providers;
|
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use rustc::hir::def_id::DefId;
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::map::Map;
|
2016-11-28 14:00:26 -05:00
|
|
|
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::{self, Node, Destination};
|
2019-02-08 20:40:49 +09:00
|
|
|
use syntax::struct_span_err;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2018-09-15 03:18:29 +03:00
|
|
|
use errors::Applicability;
|
2012-03-26 12:54:06 +02:00
|
|
|
|
2018-07-01 23:40:03 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
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 15:15:06 -07:00
|
|
|
enum LoopKind {
|
|
|
|
Loop(hir::LoopSource),
|
|
|
|
WhileLoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LoopKind {
|
|
|
|
fn name(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
LoopKind::Loop(hir::LoopSource::Loop) => "loop",
|
|
|
|
LoopKind::Loop(hir::LoopSource::WhileLet) => "while let",
|
|
|
|
LoopKind::Loop(hir::LoopSource::ForLoop) => "for",
|
|
|
|
LoopKind::WhileLoop => "while",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 23:40:03 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2013-11-11 11:29:15 -08:00
|
|
|
enum Context {
|
2016-07-21 07:01:14 +05:30
|
|
|
Normal,
|
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 15:15:06 -07:00
|
|
|
Loop(LoopKind),
|
2016-07-21 07:01:14 +05:30
|
|
|
Closure,
|
2018-04-25 19:28:04 +02:00
|
|
|
LabeledBlock,
|
2018-07-01 17:24:07 +01:00
|
|
|
AnonConst,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2012-03-26 12:54:06 +02:00
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2019-06-14 19:39:39 +03:00
|
|
|
struct CheckLoopVisitor<'a, 'hir> {
|
2014-04-05 10:05:31 +11:00
|
|
|
sess: &'a Session,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: &'a Map<'hir>,
|
2016-07-21 07:01:14 +05:30
|
|
|
cx: Context,
|
2013-08-13 02:49:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
fn check_mod_loops<'tcx>(tcx: TyCtxt<'tcx>, module_def_id: DefId) {
|
2019-01-11 04:58:46 +01:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckLoopVisitor {
|
2018-06-06 22:13:52 +02:00
|
|
|
sess: &tcx.sess,
|
|
|
|
hir_map: &tcx.hir(),
|
2016-07-21 07:01:14 +05:30
|
|
|
cx: Normal,
|
2016-11-02 18:22:59 -04:00
|
|
|
}.as_deep_visitor());
|
2013-08-13 02:49:30 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:40:49 +09:00
|
|
|
pub(crate) fn provide(providers: &mut Providers<'_>) {
|
2018-06-06 22:13:52 +02:00
|
|
|
*providers = Providers {
|
|
|
|
check_mod_loops,
|
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
|
2016-11-28 14:00:26 -05:00
|
|
|
NestedVisitorMap::OnlyBodies(&self.hir_map)
|
2016-10-29 15:01:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-01 17:24:07 +01:00
|
|
|
fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
|
|
|
|
self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn visit_expr(&mut self, e: &'hir hir::Expr) {
|
2013-11-11 11:29:15 -08:00
|
|
|
match e.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::While(ref e, ref b, _) => {
|
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 15:15:06 -07:00
|
|
|
self.with_context(Loop(LoopKind::WhileLoop), |v| {
|
|
|
|
v.visit_expr(&e);
|
|
|
|
v.visit_block(&b);
|
|
|
|
});
|
2013-11-11 11:29:15 -08:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Loop(ref b, _, source) => {
|
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 15:15:06 -07:00
|
|
|
self.with_context(Loop(LoopKind::Loop(source)), |v| v.visit_block(&b));
|
2014-07-21 20:54:28 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Closure(_, ref function_decl, b, _, _) => {
|
2018-05-17 23:50:27 +02:00
|
|
|
self.visit_fn_decl(&function_decl);
|
2016-12-21 12:32:59 +02:00
|
|
|
self.with_context(Closure, |v| v.visit_nested_body(b));
|
2013-11-11 11:29:15 -08:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Block(ref b, Some(_label)) => {
|
2018-04-25 19:28:04 +02:00
|
|
|
self.with_context(LabeledBlock, |v| v.visit_block(&b));
|
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Break(label, ref opt_expr) => {
|
2018-05-17 13:12:05 +02:00
|
|
|
opt_expr.as_ref().map(|e| self.visit_expr(e));
|
|
|
|
|
2018-05-16 09:18:26 +02:00
|
|
|
if self.require_label_in_labeled_block(e.span, &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;
|
|
|
|
}
|
2018-05-12 11:09:36 +02:00
|
|
|
|
2018-05-11 15:00:09 +02:00
|
|
|
let loop_id = match label.target_id.into() {
|
|
|
|
Ok(loop_id) => loop_id,
|
2019-03-07 12:43:27 +01:00
|
|
|
Err(hir::LoopIdError::OutsideLoopScope) => hir::DUMMY_HIR_ID,
|
2018-05-11 15:00:09 +02:00
|
|
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
|
|
|
self.emit_unlabled_cf_in_while_condition(e.span, "break");
|
2019-03-07 12:43:27 +01:00
|
|
|
hir::DUMMY_HIR_ID
|
2018-05-11 15:00:09 +02:00
|
|
|
},
|
2019-03-07 12:43:27 +01:00
|
|
|
Err(hir::LoopIdError::UnresolvedLabel) => hir::DUMMY_HIR_ID,
|
2017-02-15 23:28:59 -08:00
|
|
|
};
|
2018-04-25 19:28:04 +02:00
|
|
|
|
2019-03-07 12:43:27 +01:00
|
|
|
if loop_id != hir::DUMMY_HIR_ID {
|
|
|
|
if let Node::Block(_) = self.hir_map.find_by_hir_id(loop_id).unwrap() {
|
2018-09-07 15:10:16 +02:00
|
|
|
return
|
2018-05-11 15:00:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-15 23:28:59 -08:00
|
|
|
|
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 15:15:06 -07:00
|
|
|
if opt_expr.is_some() {
|
2019-03-07 12:43:27 +01:00
|
|
|
let loop_kind = if loop_id == hir::DUMMY_HIR_ID {
|
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 15:15:06 -07:00
|
|
|
None
|
2017-02-15 14:52:27 -08:00
|
|
|
} else {
|
2019-06-20 10:05:56 +02:00
|
|
|
Some(match self.hir_map.expect_expr(loop_id).node {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::While(..) => LoopKind::WhileLoop,
|
|
|
|
hir::ExprKind::Loop(_, _, source) => LoopKind::Loop(source),
|
2017-02-15 14:52:27 -08:00
|
|
|
ref r => span_bug!(e.span,
|
|
|
|
"break label resolved to a non-loop: {:?}", r),
|
|
|
|
})
|
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 15:15:06 -07:00
|
|
|
};
|
|
|
|
match loop_kind {
|
2018-04-25 19:28:04 +02:00
|
|
|
None |
|
2018-05-16 09:18:26 +02:00
|
|
|
Some(LoopKind::Loop(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 15:15:06 -07:00
|
|
|
Some(kind) => {
|
|
|
|
struct_span_err!(self.sess, e.span, E0571,
|
|
|
|
"`break` with value from a `{}` loop",
|
|
|
|
kind.name())
|
|
|
|
.span_label(e.span,
|
2018-04-25 19:28:04 +02:00
|
|
|
"can only break with a value inside \
|
|
|
|
`loop` or breakable block")
|
2019-01-25 16:03:27 -05:00
|
|
|
.span_suggestion(
|
2018-09-17 20:13:08 +03:00
|
|
|
e.span,
|
|
|
|
&format!(
|
|
|
|
"instead, use `break` on its own \
|
|
|
|
without a value inside this `{}` loop",
|
|
|
|
kind.name()
|
|
|
|
),
|
|
|
|
"break".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
2018-09-15 03:18:29 +03:00
|
|
|
)
|
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 15:15:06 -07:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 23:28:59 -08:00
|
|
|
|
2018-04-25 19:28:04 +02:00
|
|
|
self.require_break_cx("break", e.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 15:15:06 -07:00
|
|
|
}
|
2018-09-07 15:10:16 +02:00
|
|
|
hir::ExprKind::Continue(destination) => {
|
|
|
|
self.require_label_in_labeled_block(e.span, &destination, "continue");
|
2018-05-12 11:09:36 +02:00
|
|
|
|
2018-09-07 15:10:16 +02:00
|
|
|
match destination.target_id {
|
2018-05-12 10:18:21 +02:00
|
|
|
Ok(loop_id) => {
|
2019-03-07 12:43:27 +01:00
|
|
|
if let Node::Block(block) = self.hir_map.find_by_hir_id(loop_id).unwrap() {
|
2018-05-12 10:18:21 +02:00
|
|
|
struct_span_err!(self.sess, e.span, E0696,
|
|
|
|
"`continue` pointing to a labeled block")
|
|
|
|
.span_label(e.span,
|
|
|
|
"labeled blocks cannot be `continue`'d")
|
|
|
|
.span_note(block.span,
|
|
|
|
"labeled block the continue points to")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
|
|
|
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
|
|
|
|
}
|
2018-09-07 15:10:16 +02:00
|
|
|
Err(_) => {}
|
2017-02-15 23:28:59 -08:00
|
|
|
}
|
2018-04-25 19:28:04 +02:00
|
|
|
self.require_break_cx("continue", e.span)
|
2017-02-15 23:28:59 -08:00
|
|
|
},
|
2016-07-21 07:01:14 +05:30
|
|
|
_ => intravisit::walk_expr(self, e),
|
2013-11-11 11:29:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 02:49:30 +02:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
|
2016-07-21 07:01:14 +05:30
|
|
|
fn with_context<F>(&mut self, cx: Context, f: F)
|
2017-01-26 03:21:50 +02:00
|
|
|
where F: FnOnce(&mut CheckLoopVisitor<'a, 'hir>)
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2014-09-12 13:10:30 +03:00
|
|
|
let old_cx = self.cx;
|
|
|
|
self.cx = cx;
|
|
|
|
f(self);
|
|
|
|
self.cx = old_cx;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:28:04 +02:00
|
|
|
fn require_break_cx(&self, name: &str, span: Span) {
|
2014-09-12 13:10:30 +03:00
|
|
|
match self.cx {
|
2018-09-07 15:10:16 +02:00
|
|
|
LabeledBlock | Loop(_) => {}
|
2013-11-11 11:29:15 -08:00
|
|
|
Closure => {
|
2016-08-08 00:12:53 +05:30
|
|
|
struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(span, "cannot break inside of a closure")
|
2016-08-08 00:12:53 +05:30
|
|
|
.emit();
|
2013-11-11 11:29:15 -08:00
|
|
|
}
|
2018-07-01 17:24:07 +01:00
|
|
|
Normal | AnonConst => {
|
2016-08-08 00:12:53 +05:30
|
|
|
struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(span, "cannot break outside of a loop")
|
2016-08-08 00:12:53 +05:30
|
|
|
.emit();
|
2013-11-11 11:29:15 -08:00
|
|
|
}
|
|
|
|
}
|
2013-08-13 02:49:30 +02:00
|
|
|
}
|
2017-02-15 23:28:59 -08:00
|
|
|
|
2018-05-16 09:18:26 +02:00
|
|
|
fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str)
|
|
|
|
-> bool
|
|
|
|
{
|
2018-05-12 11:09:36 +02:00
|
|
|
if self.cx == LabeledBlock {
|
|
|
|
if label.label.is_none() {
|
|
|
|
struct_span_err!(self.sess, span, E0695,
|
|
|
|
"unlabeled `{}` inside of a labeled block", cf_type)
|
|
|
|
.span_label(span,
|
|
|
|
format!("`{}` statements that would diverge to or through \
|
|
|
|
a labeled block need to bear a label", cf_type))
|
|
|
|
.emit();
|
2018-05-16 09:18:26 +02:00
|
|
|
return true;
|
2018-05-12 11:09:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 09:18:26 +02:00
|
|
|
return false;
|
2018-05-12 11:09:36 +02:00
|
|
|
}
|
2017-02-15 23:28:59 -08:00
|
|
|
fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
|
2017-02-22 18:10:37 -08:00
|
|
|
struct_span_err!(self.sess, span, E0590,
|
2017-02-15 23:28:59 -08:00
|
|
|
"`break` or `continue` with no label in the condition of a `while` loop")
|
|
|
|
.span_label(span,
|
2017-05-04 14:17:23 +02:00
|
|
|
format!("unlabeled `{}` in the condition of a `while` loop", cf_type))
|
2017-02-15 23:28:59 -08:00
|
|
|
.emit();
|
|
|
|
}
|
2012-07-14 01:24:07 +10:00
|
|
|
}
|