Skip to content

Commit c335734

Browse files
committed
auto merge of #11024 : huonw/rust/return-from-closures, r=alexcrichton
With the old `for` gone, this behaviour is no longer conflicting with that use of `return` in closures, and this allows shortcircuiting in a closure.
2 parents 5cea7db + 6876916 commit c335734

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

src/librustc/middle/check_loop.rs

-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ impl Visitor<Context> for CheckLoopVisitor {
4747
}
4848
ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
4949
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");
53-
}
54-
visit::walk_expr_opt(self, oe, cx);
55-
}
5650
_ => visit::walk_expr(self, e, cx)
5751
}
5852
}

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

-15
This file was deleted.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// just to make sure that `return` is only returning from the closure,
12+
// not the surrounding function.
13+
static mut calls: uint = 0;
14+
15+
fn surrounding() {
16+
let return_works = |n: int| {
17+
unsafe { calls += 1 }
18+
19+
if n >= 0 { return; }
20+
fail!()
21+
};
22+
23+
return_works(10);
24+
return_works(20);
25+
26+
27+
let return_works_proc = proc(n: int) {
28+
unsafe { calls += 1 }
29+
30+
if n >= 0 { return; }
31+
fail!()
32+
};
33+
34+
return_works_proc(10);
35+
}
36+
37+
pub fn main() {
38+
surrounding();
39+
40+
assert_eq!(unsafe {calls}, 3);
41+
}

0 commit comments

Comments
 (0)