]> git.lizzy.rs Git - rust.git/commitdiff
Require braces when a closure has an explicit return type. This is a
authorNiko Matsakis <niko@alum.mit.edu>
Wed, 18 Mar 2015 13:22:38 +0000 (09:22 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Thu, 19 Mar 2015 00:07:27 +0000 (20:07 -0400)
[breaking-change]: instead of a closure like `|| -> i32 22`, prefer `||
-> i32 { 22 }`.

Fixes #23420.

src/libsyntax/parse/mod.rs
src/libsyntax/parse/parser.rs
src/libsyntax/print/pprust.rs
src/test/compile-fail/fn-trait-formatting.rs
src/test/compile-fail/liveness-issue-2163.rs
src/test/parse-fail/closure-return-syntax.rs [new file with mode: 0644]
src/test/run-pass/block-explicit-types.rs
src/test/run-pass/borrowck-move-by-capture-ok.rs
src/test/run-pass/issue-17816.rs

index 82ba873e54b95140ab7e0fb406729da21c68d393..968d2fd7e2a47c6e216845b527b97e82a8548a79 100644 (file)
@@ -404,7 +404,7 @@ fn esc(len: usize, lit: &str) -> Option<(char, isize)> {
         .map(|x| (x, len as isize))
     }
 
-    let unicode_escape = || -> Option<(char, isize)>
+    let unicode_escape = || -> Option<(char, isize)> {
         if lit.as_bytes()[2] == b'{' {
             let idx = lit.find('}').expect(msg2);
             let subslice = &lit[3..idx];
@@ -413,7 +413,8 @@ fn esc(len: usize, lit: &str) -> Option<(char, isize)> {
                 .map(|x| (x, subslice.chars().count() as isize + 4))
         } else {
             esc(6, lit)
-        };
+        }
+    };
 
     // Unicode escapes
     return match lit.as_bytes()[1] as char {
index bf2b2c0afe6c3825e0688dbd1852765cfe90fffc..d76355f9af00db6efb3039e0dfa47279dd4b838f 100644 (file)
@@ -2101,10 +2101,7 @@ pub fn parse_bottom_expr(&mut self) -> P<Expr> {
                 }
             },
             token::OpenDelim(token::Brace) => {
-                self.bump();
-                let blk = self.parse_block_tail(lo, DefaultBlock);
-                return self.mk_expr(blk.span.lo, blk.span.hi,
-                                    ExprBlock(blk));
+                return self.parse_block_expr(lo, DefaultBlock);
             },
             token::BinOp(token::Or) |  token::OrOr => {
                 return self.parse_lambda_expr(CaptureByRef);
@@ -3000,19 +2997,30 @@ pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
     {
         let lo = self.span.lo;
         let decl = self.parse_fn_block_decl();
-        let body = self.parse_expr();
-        let fakeblock = P(ast::Block {
-            id: ast::DUMMY_NODE_ID,
-            stmts: vec![],
-            span: body.span,
-            expr: Some(body),
-            rules: DefaultBlock,
-        });
+        let body = match decl.output {
+            DefaultReturn(_) => {
+                // If no explicit return type is given, parse any
+                // expr and wrap it up in a dummy block:
+                let body_expr = self.parse_expr();
+                P(ast::Block {
+                    id: ast::DUMMY_NODE_ID,
+                    stmts: vec![],
+                    span: body_expr.span,
+                    expr: Some(body_expr),
+                    rules: DefaultBlock,
+                })
+            }
+            _ => {
+                // If an explicit return type is given, require a
+                // block to appear (RFC 968).
+                self.parse_block()
+            }
+        };
 
         self.mk_expr(
             lo,
-            fakeblock.span.hi,
-            ExprClosure(capture_clause, decl, fakeblock))
+            body.span.hi,
+            ExprClosure(capture_clause, decl, body))
     }
 
     pub fn parse_else_expr(&mut self) -> P<Expr> {
index 07303ba51ff7075bfaf6c732fd5b82689f53bc63..b58c121c5fd54730df170197ef956b41145b95cc 100644 (file)
@@ -1777,7 +1777,12 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
                 try!(self.print_fn_block_args(&**decl));
                 try!(space(&mut self.s));
 
-                if !body.stmts.is_empty() || !body.expr.is_some() {
+                let default_return = match decl.output {
+                    ast::DefaultReturn(..) => true,
+                    _ => false
+                };
+
+                if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
                     try!(self.print_block_unclosed(&**body));
                 } else {
                     // we extract the block, so as not to create another set of boxes
index d682ef7d70c9db931e5cbb28f74bee4b2c3ed41c..35c551931366d28c732260ef2d523197a14c4759 100644 (file)
@@ -26,7 +26,7 @@ fn main() {
     //~| found `Box<core::ops::Fn(isize, isize)>`
     //~| expected ()
     //~| found box
-    let _: () = (box || -> isize unimplemented!()) as Box<FnMut() -> isize>;
+    let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> isize>;
     //~^ ERROR mismatched types
     //~| expected `()`
     //~| found `Box<core::ops::FnMut() -> isize>`
index 69bceec8c3225ee70366326b172d6b09edbb226a..7c94e33b47b38c4296012d4aa7d63a6bacbf5b2a 100644 (file)
@@ -13,6 +13,6 @@
 fn main() {
     let a: Vec<isize> = Vec::new();
     a.iter().all(|_| -> bool {
-        //~^ ERROR mismatched types
+        //~^ ERROR not all control paths return a value
     });
 }
diff --git a/src/test/parse-fail/closure-return-syntax.rs b/src/test/parse-fail/closure-return-syntax.rs
new file mode 100644 (file)
index 0000000..da62455
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2014 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.
+
+// Test that we cannot parse a closure with an explicit return type
+// unless it uses braces.
+
+fn main() {
+    let x = || -> i32 22; //~ ERROR expected `{`, found `22`
+}
index 54b650d762bf46b2af5c0fa7c9004057c1d92e87..835d356d8aaa1d98106622ff8df38de6c514baae 100644 (file)
@@ -10,5 +10,5 @@
 
 pub fn main() {
     fn as_buf<T, F>(s: String, f: F) -> T where F: FnOnce(String) -> T { f(s) }
-    as_buf("foo".to_string(), |foo: String| -> () println!("{}", foo) );
+    as_buf("foo".to_string(), |foo: String| -> () { println!("{}", foo) });
 }
index 269063bbd05b67712ee44efd55b1072eb2f1b329..4364391cf0c60c9d403250ba778af1ac69bdb926 100644 (file)
@@ -14,6 +14,6 @@
 
 pub fn main() {
     let bar: Box<_> = box 3;
-    let h = || -> int *bar;
+    let h = || -> int { *bar };
     assert_eq!(h(), 3);
 }
index a976eccf89ec4c3166d9e366d66f1c4cc6635499..8e3cb414566c7577279db2ba4f0fc60d45c7af7b 100644 (file)
@@ -14,7 +14,7 @@
 
 fn main() {
     struct Symbol<'a, F: Fn(Vec<&'a str>) -> &'a str> { function: F, marker: PhantomData<&'a ()> }
-    let f = |x: Vec<&str>| -> &str "foobar";
+    let f = |x: Vec<&str>| -> &str { "foobar" };
     let sym = Symbol { function: f, marker: PhantomData };
     (sym.function)(vec![]);
 }