-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Allow
return
to return from a closure.
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.
- Loading branch information
Showing
3 changed files
with
41 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// just to make sure that `return` is only returning from the closure, | ||
// not the surrounding function. | ||
static mut calls: uint = 0; | ||
|
||
fn surrounding() { | ||
let return_works = |n: int| { | ||
unsafe { calls += 1 } | ||
|
||
if n >= 0 { return; } | ||
fail!() | ||
}; | ||
|
||
return_works(10); | ||
return_works(20); | ||
|
||
|
||
let return_works_proc = proc(n: int) { | ||
unsafe { calls += 1 } | ||
|
||
if n >= 0 { return; } | ||
fail!() | ||
}; | ||
|
||
return_works_proc(10); | ||
} | ||
|
||
pub fn main() { | ||
surrounding(); | ||
|
||
assert_eq!(unsafe {calls}, 3); | ||
} |
6876916
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from alexcrichton
at huonw@6876916
6876916
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging huonw/rust/return-from-closures = 6876916 into auto
6876916
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huonw/rust/return-from-closures = 6876916 merged ok, testing candidate = c335734
6876916
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/3055
success: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-c/builds/898
success: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-t/builds/900
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/3061
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/2163
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/2164
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/3072
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/2161
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/2166
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/3074
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/2162
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/2165
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android/builds/2241
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/3060
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/2162
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/2166
success: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/2836
6876916
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = c335734