Skip to content

Commit 5fdbcc4

Browse files
committed
Improve error message for breaks in blocks
Before it was always stated that it was a "break outside of a loop" when you could very well be in a loop, but just in a block instead. Closes #3064
1 parent 0cc5e6c commit 5fdbcc4

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed

src/librustc/middle/check_loop.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,68 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
1211
use middle::ty;
1312

14-
use syntax::ast::*;
15-
use syntax::visit;
13+
use syntax::ast;
14+
use syntax::codemap::Span;
1615
use syntax::visit::Visitor;
16+
use syntax::visit;
1717

18-
#[deriving(Clone)]
19-
pub struct Context {
20-
in_loop: bool,
21-
can_ret: bool
18+
#[deriving(Clone, Eq)]
19+
enum Context {
20+
Normal, Loop, Closure
2221
}
2322

2423
struct CheckLoopVisitor {
2524
tcx: ty::ctxt,
2625
}
2726

28-
pub fn check_crate(tcx: ty::ctxt, crate: &Crate) {
29-
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx },
30-
crate,
31-
Context { in_loop: false, can_ret: true });
27+
pub fn check_crate(tcx: ty::ctxt, crate: &ast::Crate) {
28+
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, crate, Normal)
3229
}
3330

3431
impl Visitor<Context> for CheckLoopVisitor {
35-
fn visit_item(&mut self, i:@item, _cx:Context) {
36-
visit::walk_item(self, i, Context {
37-
in_loop: false,
38-
can_ret: true
39-
});
32+
fn visit_item(&mut self, i: @ast::item, _cx: Context) {
33+
visit::walk_item(self, i, Normal);
4034
}
4135

42-
fn visit_expr(&mut self, e:@Expr, cx:Context) {
43-
44-
match e.node {
45-
ExprWhile(e, ref b) => {
36+
fn visit_expr(&mut self, e: @ast::Expr, cx:Context) {
37+
match e.node {
38+
ast::ExprWhile(e, ref b) => {
4639
self.visit_expr(e, cx);
47-
self.visit_block(b, Context { in_loop: true,.. cx });
48-
}
49-
ExprLoop(ref b, _) => {
50-
self.visit_block(b, Context { in_loop: true,.. cx });
51-
}
52-
ExprFnBlock(_, ref b) | ExprProc(_, ref b) => {
53-
self.visit_block(b, Context { in_loop: false, can_ret: false });
54-
}
55-
ExprBreak(_) => {
56-
if !cx.in_loop {
57-
self.tcx.sess.span_err(e.span, "`break` outside of loop");
58-
}
59-
}
60-
ExprAgain(_) => {
61-
if !cx.in_loop {
62-
self.tcx.sess.span_err(e.span, "`loop` outside of loop");
63-
}
64-
}
65-
ExprRet(oe) => {
66-
if !cx.can_ret {
67-
self.tcx.sess.span_err(e.span, "`return` in block function");
40+
self.visit_block(b, Loop);
41+
}
42+
ast::ExprLoop(ref b, _) => {
43+
self.visit_block(b, Loop);
44+
}
45+
ast::ExprFnBlock(_, ref b) | ast::ExprProc(_, ref b) => {
46+
self.visit_block(b, Closure);
47+
}
48+
ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
49+
ast::ExprAgain(_) => self.require_loop("continue", cx, e.span),
50+
ast::ExprRet(oe) => {
51+
if cx == Closure {
52+
self.tcx.sess.span_err(e.span, "`return` in a closure");
6853
}
6954
visit::walk_expr_opt(self, oe, cx);
70-
}
71-
_ => visit::walk_expr(self, e, cx)
7255
}
56+
_ => visit::walk_expr(self, e, cx)
57+
}
58+
}
59+
}
7360

61+
impl CheckLoopVisitor {
62+
fn require_loop(&self, name: &str, cx: Context, span: Span) {
63+
match cx {
64+
Loop => {}
65+
Closure => {
66+
self.tcx.sess.span_err(span, format!("`{}` inside of a closure",
67+
name));
68+
}
69+
Normal => {
70+
self.tcx.sess.span_err(span, format!("`{}` outside of loop",
71+
name));
72+
}
73+
}
7474
}
7575
}

src/test/compile-fail/break-outside-loop.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:`break` outside of loop
12-
1311
struct Foo {
1412
t: ~str
1513
}
1614

15+
fn cond() -> bool { true }
16+
17+
fn foo(_: ||) {}
18+
1719
fn main() {
18-
let pth = break;
20+
let pth = break; //~ ERROR: `break` outside of loop
21+
if cond() { continue } //~ ERROR: `continue` outside of loop
1922

20-
let rs: Foo = Foo{t: pth};
23+
while cond() {
24+
if cond() { break }
25+
if cond() { continue }
26+
do foo {
27+
if cond() { break } //~ ERROR: `break` inside of a closure
28+
if cond() { continue } //~ ERROR: `continue` inside of a closure
29+
}
30+
}
2131

32+
let rs: Foo = Foo{t: pth};
2233
}

src/test/compile-fail/return-in-block-function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
fn main() {
1212
let _x = || {
13-
return //~ ERROR: `return` in block function
13+
return //~ ERROR: `return` in a closure
1414
};
1515
}

0 commit comments

Comments
 (0)