Skip to content

Commit 78d1442

Browse files
committed
extended information for E0571 break with value in non-loop loop
1 parent 3f977ba commit 78d1442

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/librustc_passes/diagnostics.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,45 @@ To fix this, add a label specifying which loop is being broken out of:
223223
```
224224
'foo: while break 'foo {}
225225
```
226+
"##,
227+
228+
E0571: r##"
229+
A `break` statement with an argument appeared in a non-`loop` loop.
230+
231+
Example of erroneous code:
232+
233+
```compile_fail,E0571
234+
# let mut i = 1;
235+
# fn satisfied(n: usize) -> bool { n % 23 == 0 }
236+
let result = while true {
237+
if satisfied(i) {
238+
break 2*i; // error: `break` with value from a `while` loop
239+
}
240+
i += 1;
241+
};
242+
```
243+
244+
The `break` statement can take an argument (which will be the value of the loop
245+
expression if the `break` statement is executed) in `loop` loops, but not
246+
`for`, `while`, or `while let` loops.
247+
248+
Make sure `break value;` statements only occur in `loop` loops:
249+
250+
```
251+
# let mut i = 1;
252+
# fn satisfied(n: usize) -> bool { n % 23 == 0 }
253+
let result = loop { // ok!
254+
if satisfied(i) {
255+
break 2*i;
256+
}
257+
i += 1;
258+
};
259+
```
226260
"##
227261
}
228262

229263
register_diagnostics! {
230264
E0226, // only a single explicit lifetime bound is permitted
231265
E0472, // asm! is unsupported on this target
232266
E0561, // patterns aren't allowed in function pointer types
233-
E0571, // `break` with a value in a non-`loop`-loop
234267
}

0 commit comments

Comments
 (0)