]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Allow `return` to return from a closure.
authorHuon Wilson <dbau.pp+github@gmail.com>
Tue, 17 Dec 2013 13:06:20 +0000 (00:06 +1100)
committerHuon Wilson <dbau.pp+github@gmail.com>
Wed, 18 Dec 2013 11:53:45 +0000 (22:53 +1100)
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.

src/librustc/middle/check_loop.rs
src/test/compile-fail/return-in-block-function.rs [deleted file]
src/test/run-pass/return-from-closure.rs [new file with mode: 0644]

index a08884857a6062206b7bc8e8050d6967348d5853..332e63288a119de9028fa74ad00c0cc2fbf6928a 100644 (file)
@@ -47,12 +47,6 @@ fn visit_expr(&mut self, e: @ast::Expr, cx:Context) {
             }
             ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
             ast::ExprAgain(_) => self.require_loop("continue", cx, e.span),
-            ast::ExprRet(oe) => {
-                if cx == Closure {
-                    self.tcx.sess.span_err(e.span, "`return` in a closure");
-                }
-                visit::walk_expr_opt(self, oe, cx);
-            }
             _ => visit::walk_expr(self, e, cx)
         }
     }
diff --git a/src/test/compile-fail/return-in-block-function.rs b/src/test/compile-fail/return-in-block-function.rs
deleted file mode 100644 (file)
index f231810..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright 2012 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.
-
-fn main() {
-    let _x = || {
-        return //~ ERROR: `return` in a closure
-    };
-}
diff --git a/src/test/run-pass/return-from-closure.rs b/src/test/run-pass/return-from-closure.rs
new file mode 100644 (file)
index 0000000..1756d74
--- /dev/null
@@ -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);
+}