1
Fork 0

improve diagnostics: break/continue wrong context

This commit is contained in:
Artem Varaksa 2019-08-21 13:17:59 +03:00
parent bea0372a1a
commit a8d7ea74a4
12 changed files with 73 additions and 59 deletions

View file

@ -131,7 +131,7 @@ be taken. Erroneous code example:
```compile_fail,E0268 ```compile_fail,E0268
fn some_func() { fn some_func() {
break; // error: `break` outside of loop break; // error: `break` outside of a loop
} }
``` ```

View file

@ -16,8 +16,8 @@ use errors::Applicability;
enum Context { enum Context {
Normal, Normal,
Loop(hir::LoopSource), Loop(hir::LoopSource),
Closure, Closure(Span),
AsyncClosure, AsyncClosure(Span),
LabeledBlock, LabeledBlock,
AnonConst, AnonConst,
} }
@ -58,11 +58,11 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprKind::Loop(ref b, _, source) => { hir::ExprKind::Loop(ref b, _, source) => {
self.with_context(Loop(source), |v| v.visit_block(&b)); self.with_context(Loop(source), |v| v.visit_block(&b));
} }
hir::ExprKind::Closure(_, ref function_decl, b, _, movability) => { hir::ExprKind::Closure(_, ref function_decl, b, span, movability) => {
let cx = if let Some(GeneratorMovability::Static) = movability { let cx = if let Some(GeneratorMovability::Static) = movability {
AsyncClosure AsyncClosure(span)
} else { } else {
Closure Closure(span)
}; };
self.visit_fn_decl(&function_decl); self.visit_fn_decl(&function_decl);
self.with_context(cx, |v| v.visit_nested_body(b)); self.with_context(cx, |v| v.visit_nested_body(b));
@ -170,23 +170,22 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
} }
fn require_break_cx(&self, name: &str, span: Span) { fn require_break_cx(&self, name: &str, span: Span) {
let err_inside_of = |article, r#type, closure_span| {
struct_span_err!(self.sess, span, E0267, "`{}` inside of {} {}", name, article, r#type)
.span_label(span, format!("cannot `{}` inside of {} {}", name, article, r#type))
.span_label(closure_span, &format!("enclosing {}", r#type))
.emit();
};
match self.cx { match self.cx {
LabeledBlock | Loop(_) => {} LabeledBlock | Loop(_) => {},
Closure => { Closure(closure_span) => err_inside_of("a", "closure", closure_span),
struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name) AsyncClosure(closure_span) => err_inside_of("an", "`async` block", closure_span),
.span_label(span, "cannot break inside of a closure")
.emit();
}
AsyncClosure => {
struct_span_err!(self.sess, span, E0267, "`{}` inside of an async block", name)
.span_label(span, "cannot break inside of an async block")
.emit();
}
Normal | AnonConst => { Normal | AnonConst => {
struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name) struct_span_err!(self.sess, span, E0268, "`{}` outside of a loop", name)
.span_label(span, "cannot break outside of a loop") .span_label(span, format!("cannot `{}` outside of a loop", name))
.emit(); .emit();
} },
} }
} }

View file

@ -1,14 +1,14 @@
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/array-break-length.rs:3:17 --> $DIR/array-break-length.rs:3:17
| |
LL | |_: [_; break]| {} LL | |_: [_; break]| {}
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error[E0268]: `continue` outside of loop error[E0268]: `continue` outside of a loop
--> $DIR/array-break-length.rs:8:17 --> $DIR/array-break-length.rs:8:17
| |
LL | |_: [_; continue]| {} LL | |_: [_; continue]| {}
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `continue` outside of a loop
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/array-break-length.rs:3:9 --> $DIR/array-break-length.rs:3:9

View file

@ -1,14 +1,22 @@
error[E0267]: `break` inside of an async block error[E0267]: `break` inside of an async block
--> $DIR/async-block-control-flow-static-semantics.rs:33:9 --> $DIR/async-block-control-flow-static-semantics.rs:33:9
| |
LL | break 0u8; LL | async {
| ^^^^^^^^^ cannot break inside of an async block | ___________-
LL | | break 0u8;
| | ^^^^^^^^^ cannot `break` inside of an `async` block
LL | | };
| |_____- enclosing `async` block
error[E0267]: `break` inside of an async block error[E0267]: `break` inside of an async block
--> $DIR/async-block-control-flow-static-semantics.rs:40:13 --> $DIR/async-block-control-flow-static-semantics.rs:40:13
| |
LL | break 0u8; LL | async {
| ^^^^^^^^^ cannot break inside of an async block | _______________-
LL | | break 0u8;
| | ^^^^^^^^^ cannot `break` inside of an `async` block
LL | | };
| |_________- enclosing `async` block
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/async-block-control-flow-static-semantics.rs:13:43 --> $DIR/async-block-control-flow-static-semantics.rs:13:43

View file

@ -1,32 +1,37 @@
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/break-outside-loop.rs:10:15 --> $DIR/break-outside-loop.rs:10:15
| |
LL | let pth = break; LL | let pth = break;
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error[E0268]: `continue` outside of loop error[E0268]: `continue` outside of a loop
--> $DIR/break-outside-loop.rs:11:17 --> $DIR/break-outside-loop.rs:11:17
| |
LL | if cond() { continue } LL | if cond() { continue }
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `continue` outside of a loop
error[E0267]: `break` inside of a closure error[E0267]: `break` inside of a closure
--> $DIR/break-outside-loop.rs:17:25 --> $DIR/break-outside-loop.rs:17:25
| |
LL | foo(|| {
| -- enclosing closure
LL | if cond() { break } LL | if cond() { break }
| ^^^^^ cannot break inside of a closure | ^^^^^ cannot `break` inside of a closure
error[E0267]: `continue` inside of a closure error[E0267]: `continue` inside of a closure
--> $DIR/break-outside-loop.rs:18:25 --> $DIR/break-outside-loop.rs:18:25
| |
LL | foo(|| {
| -- enclosing closure
LL | if cond() { break }
LL | if cond() { continue } LL | if cond() { continue }
| ^^^^^^^^ cannot break inside of a closure | ^^^^^^^^ cannot `continue` inside of a closure
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/break-outside-loop.rs:24:25 --> $DIR/break-outside-loop.rs:24:25
| |
LL | let unconstrained = break; LL | let unconstrained = break;
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error: aborting due to 5 previous errors error: aborting due to 5 previous errors

View file

@ -1,20 +1,20 @@
error[E0268]: `continue` outside of loop error[E0268]: `continue` outside of a loop
--> $DIR/closure-array-break-length.rs:2:13 --> $DIR/closure-array-break-length.rs:2:13
| |
LL | |_: [_; continue]| {}; LL | |_: [_; continue]| {};
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `continue` outside of a loop
error[E0268]: `continue` outside of loop error[E0268]: `continue` outside of a loop
--> $DIR/closure-array-break-length.rs:4:19 --> $DIR/closure-array-break-length.rs:4:19
| |
LL | while |_: [_; continue]| {} {} LL | while |_: [_; continue]| {} {}
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `continue` outside of a loop
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/closure-array-break-length.rs:7:19 --> $DIR/closure-array-break-length.rs:7:19
| |
LL | while |_: [_; break]| {} {} LL | while |_: [_; break]| {} {}
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/closure-array-break-length.rs:4:11 --> $DIR/closure-array-break-length.rs:4:11

View file

@ -2,7 +2,9 @@ error[E0267]: `break` inside of a closure
--> $DIR/E0267.rs:2:18 --> $DIR/E0267.rs:2:18
| |
LL | let w = || { break; }; LL | let w = || { break; };
| ^^^^^ cannot break inside of a closure | -- ^^^^^ cannot `break` inside of a closure
| |
| enclosing closure
error: aborting due to previous error error: aborting due to previous error

View file

@ -1,8 +1,8 @@
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/E0268.rs:2:5 --> $DIR/E0268.rs:2:5
| |
LL | break; LL | break;
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error: aborting due to previous error error: aborting due to previous error

View file

@ -1,14 +1,14 @@
error[E0268]: `continue` outside of loop error[E0268]: `continue` outside of a loop
--> $DIR/issue-28105.rs:4:5 --> $DIR/issue-28105.rs:4:5
| |
LL | continue LL | continue
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `continue` outside of a loop
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-28105.rs:6:5 --> $DIR/issue-28105.rs:6:5
| |
LL | break LL | break
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -1,14 +1,14 @@
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-43162.rs:3:5 --> $DIR/issue-43162.rs:3:5
| |
LL | break true; LL | break true;
| ^^^^^^^^^^ cannot break outside of a loop | ^^^^^^^^^^ cannot `break` outside of a loop
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-43162.rs:7:5 --> $DIR/issue-43162.rs:7:5
| |
LL | break {}; LL | break {};
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `break` outside of a loop
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-43162.rs:1:13 --> $DIR/issue-43162.rs:1:13

View file

@ -4,17 +4,17 @@ error[E0426]: use of undeclared label `'L`
LL | |bool: [u8; break 'L]| 0; LL | |bool: [u8; break 'L]| 0;
| ^^ undeclared label `'L` | ^^ undeclared label `'L`
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-50576.rs:2:17 --> $DIR/issue-50576.rs:2:17
| |
LL | |bool: [u8; break 'L]| 0; LL | |bool: [u8; break 'L]| 0;
| ^^^^^^^^ cannot break outside of a loop | ^^^^^^^^ cannot `break` outside of a loop
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-50576.rs:5:16 --> $DIR/issue-50576.rs:5:16
| |
LL | Vec::<[u8; break]>::new(); LL | Vec::<[u8; break]>::new();
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View file

@ -1,8 +1,8 @@
error[E0268]: `break` outside of loop error[E0268]: `break` outside of a loop
--> $DIR/issue-50581.rs:2:14 --> $DIR/issue-50581.rs:2:14
| |
LL | |_: [u8; break]| (); LL | |_: [u8; break]| ();
| ^^^^^ cannot break outside of a loop | ^^^^^ cannot `break` outside of a loop
error: aborting due to previous error error: aborting due to previous error