Add E0695 for unlabeled breaks

This commit is contained in:
est31 2018-05-12 09:52:20 +02:00
parent 5665980ad8
commit 63ef53f21d
4 changed files with 87 additions and 0 deletions

View File

@ -259,6 +259,43 @@ let result = loop { // ok!
i += 1;
};
```
"##,
E0695: r##"
A `break` statement without a label appeared inside a labeled block.
Example of erroneous code:
```compile_fail,E0695
# #![feature(label_break_value)]
loop {
'a: {
break;
}
}
```
Make sure to always label the `break`:
```
'l: loop {
'a: {
break 'l;
}
}
```
Or if you want to `break` the labeled block:
```
# #![feature(label_break_value)]
loop {
'a: {
break 'a;
}
break;
}
```
"##
}

View File

@ -109,6 +109,14 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
}
if self.cx == LabeledBlock {
if label.label.is_none() {
struct_span_err!(self.sess, e.span, E0695,
"unlabeled `break` inside of a labeled block")
.span_label(e.span,
"`break` statements that would diverge to or through \
a labeled block need to bear a label")
.emit();
}
return;
}

View File

@ -0,0 +1,27 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Simple unlabeled break should yield in an error
fn unlabeled_break_simple() {
'b: {
break; //~ ERROR unlabeled `break` inside of a labeled block
}
}
// Unlabeled break that would cross a labeled block should yield in an error
fn unlabeled_break_crossing() {
loop {
'b: {
break; //~ ERROR unlabeled `break` inside of a labeled block
}
}
}
pub fn main() {}

View File

@ -0,0 +1,15 @@
error[E0695]: unlabeled `break` inside of a labeled block
--> $DIR/label_break_value_unlabeled_break.rs:14:9
|
LL | break; //~ ERROR unlabeled `break` inside of a labeled block
| ^^^^^ `break` statements that would diverge to or through a labeled block need to bear a label
error[E0695]: unlabeled `break` inside of a labeled block
--> $DIR/label_break_value_unlabeled_break.rs:22:13
|
LL | break; //~ ERROR unlabeled `break` inside of a labeled block
| ^^^^^ `break` statements that would diverge to or through a labeled block need to bear a label
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0695`.