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 @@ impl !Enterprise for Foo { }
```compile_fail,E0268
fn some_func() {
break; // error: `break` outside of loop
break; // error: `break` outside of a loop
}
```

View File

@ -16,8 +16,8 @@
enum Context {
Normal,
Loop(hir::LoopSource),
Closure,
AsyncClosure,
Closure(Span),
AsyncClosure(Span),
LabeledBlock,
AnonConst,
}
@ -58,11 +58,11 @@ fn visit_expr(&mut self, e: &'hir hir::Expr) {
hir::ExprKind::Loop(ref b, _, source) => {
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 {
AsyncClosure
AsyncClosure(span)
} else {
Closure
Closure(span)
};
self.visit_fn_decl(&function_decl);
self.with_context(cx, |v| v.visit_nested_body(b));
@ -170,23 +170,22 @@ fn with_context<F>(&mut self, cx: Context, f: F)
}
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 {
LabeledBlock | Loop(_) => {}
Closure => {
struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
.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();
}
LabeledBlock | Loop(_) => {},
Closure(closure_span) => err_inside_of("a", "closure", closure_span),
AsyncClosure(closure_span) => err_inside_of("an", "`async` block", closure_span),
Normal | AnonConst => {
struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
.span_label(span, "cannot break outside of a loop")
struct_span_err!(self.sess, span, E0268, "`{}` outside of a loop", name)
.span_label(span, format!("cannot `{}` outside of a loop", name))
.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
|
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
|
LL | |_: [_; continue]| {}
| ^^^^^^^^ cannot break outside of a loop
| ^^^^^^^^ cannot `continue` outside of a loop
error[E0308]: mismatched types
--> $DIR/array-break-length.rs:3:9

View File

@ -1,14 +1,22 @@
error[E0267]: `break` inside of an async block
--> $DIR/async-block-control-flow-static-semantics.rs:33:9
|
LL | break 0u8;
| ^^^^^^^^^ cannot break inside of an async block
LL | async {
| ___________-
LL | | break 0u8;
| | ^^^^^^^^^ cannot `break` inside of an `async` block
LL | | };
| |_____- enclosing `async` block
error[E0267]: `break` inside of an async block
--> $DIR/async-block-control-flow-static-semantics.rs:40:13
|
LL | break 0u8;
| ^^^^^^^^^ cannot break inside of an async block
LL | async {
| _______________-
LL | | break 0u8;
| | ^^^^^^^^^ cannot `break` inside of an `async` block
LL | | };
| |_________- enclosing `async` block
error[E0308]: mismatched types
--> $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
|
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
|
LL | if cond() { continue }
| ^^^^^^^^ cannot break outside of a loop
| ^^^^^^^^ cannot `continue` outside of a loop
error[E0267]: `break` inside of a closure
--> $DIR/break-outside-loop.rs:17:25
|
LL | foo(|| {
| -- enclosing closure
LL | if cond() { break }
| ^^^^^ cannot break inside of a closure
| ^^^^^ cannot `break` inside of a closure
error[E0267]: `continue` inside of a closure
--> $DIR/break-outside-loop.rs:18:25
|
LL | foo(|| {
| -- enclosing closure
LL | if cond() { break }
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
|
LL | let unconstrained = break;
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
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
|
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
|
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
|
LL | while |_: [_; break]| {} {}
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
error[E0308]: mismatched types
--> $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
|
LL | let w = || { break; };
| ^^^^^ cannot break inside of a closure
| -- ^^^^^ cannot `break` inside of a closure
| |
| enclosing closure
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
|
LL | break;
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
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
|
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
|
LL | break
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
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
|
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
|
LL | break {};
| ^^^^^^^^ cannot break outside of a loop
| ^^^^^^^^ cannot `break` outside of a loop
error[E0308]: mismatched types
--> $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;
| ^^ undeclared label `'L`
error[E0268]: `break` outside of loop
error[E0268]: `break` outside of a loop
--> $DIR/issue-50576.rs:2:17
|
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
|
LL | Vec::<[u8; break]>::new();
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
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
|
LL | |_: [u8; break]| ();
| ^^^^^ cannot break outside of a loop
| ^^^^^ cannot `break` outside of a loop
error: aborting due to previous error