mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 15:01:51 +00:00
Add E0695 for unlabeled breaks
This commit is contained in:
parent
5665980ad8
commit
63ef53f21d
@ -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;
|
||||
}
|
||||
```
|
||||
"##
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
27
src/test/ui/label_break_value_unlabeled_break.rs
Normal file
27
src/test/ui/label_break_value_unlabeled_break.rs
Normal 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() {}
|
15
src/test/ui/label_break_value_unlabeled_break.stderr
Normal file
15
src/test/ui/label_break_value_unlabeled_break.stderr
Normal 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`.
|
Loading…
Reference in New Issue
Block a user