Skip to content

Commit f8c2598

Browse files
committed
Add E0696 for continue pointing to a labeled block
1 parent 50f063c commit f8c2598

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/librustc_passes/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,5 @@ register_diagnostics! {
308308
E0642, // patterns aren't allowed in methods without bodies
309309
E0666, // nested `impl Trait` is illegal
310310
E0667, // `impl Trait` in projections
311+
E0696, // `continue` pointing to a labeled block
311312
}

src/librustc_passes/loops.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,22 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
152152
hir::ExprAgain(label) => {
153153
self.require_label_in_labeled_block(e.span, &label, "continue");
154154

155-
if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
156-
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
155+
match label.target_id {
156+
Ok(loop_id) => {
157+
if let hir::map::NodeBlock(block) = self.hir_map.find(loop_id).unwrap() {
158+
struct_span_err!(self.sess, e.span, E0696,
159+
"`continue` pointing to a labeled block")
160+
.span_label(e.span,
161+
"labeled blocks cannot be `continue`'d")
162+
.span_note(block.span,
163+
"labeled block the continue points to")
164+
.emit();
165+
}
166+
}
167+
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
168+
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
169+
}
170+
_ => {}
157171
}
158172
self.require_break_cx("continue", e.span)
159173
},
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Simple continue pointing to an unlabeled break should yield in an error
12+
fn continue_simple() {
13+
'b: {
14+
continue; //~ ERROR unlabeled `continue` inside of a labeled block
15+
}
16+
}
17+
18+
// Labeled continue pointing to an unlabeled break should yield in an error
19+
fn continue_labeled() {
20+
'b: {
21+
continue 'b; //~ ERROR `continue` pointing to a labeled block
22+
}
23+
}
24+
25+
// Simple continue that would cross a labeled block should yield in an error
26+
fn continue_crossing() {
27+
loop {
28+
'b: {
29+
continue; //~ ERROR unlabeled `continue` inside of a labeled block
30+
}
31+
}
32+
}
33+
34+
pub fn main() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0695]: unlabeled `continue` inside of a labeled block
2+
--> $DIR/label_break_value_continue.rs:14:9
3+
|
4+
LL | continue; //~ ERROR unlabeled `continue` inside of a labeled block
5+
| ^^^^^^^^ `continue` statements that would diverge to or through a labeled block need to bear a label
6+
7+
error[E0696]: `continue` pointing to a labeled block
8+
--> $DIR/label_break_value_continue.rs:21:9
9+
|
10+
LL | continue 'b; //~ ERROR `continue` pointing to a labeled block
11+
| ^^^^^^^^^^^ labeled blocks cannot be `continue`'d
12+
|
13+
note: labeled block the continue points to
14+
--> $DIR/label_break_value_continue.rs:20:5
15+
|
16+
LL | / 'b: {
17+
LL | | continue 'b; //~ ERROR `continue` pointing to a labeled block
18+
LL | | }
19+
| |_____^
20+
21+
error[E0695]: unlabeled `continue` inside of a labeled block
22+
--> $DIR/label_break_value_continue.rs:29:13
23+
|
24+
LL | continue; //~ ERROR unlabeled `continue` inside of a labeled block
25+
| ^^^^^^^^ `continue` statements that would diverge to or through a labeled block need to bear a label
26+
27+
error: aborting due to 3 previous errors
28+
29+
Some errors occurred: E0695, E0696.
30+
For more information about an error, try `rustc --explain E0695`.

0 commit comments

Comments
 (0)