Skip to content

Commit a32bb1b

Browse files
committed
Rollup merge of #23475 - nikomatsakis:closure-ret-syntax, r=acrichto
Require braces when a closure has an explicit return type. This is a [breaking-change]: instead of a closure like `|| -> i32 22`, prefer `|| -> i32 { 22 }`. Fixes #23420.
2 parents c7392be + c225824 commit a32bb1b

File tree

9 files changed

+52
-22
lines changed

9 files changed

+52
-22
lines changed

src/libsyntax/parse/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ pub fn char_lit(lit: &str) -> (char, isize) {
404404
.map(|x| (x, len as isize))
405405
}
406406

407-
let unicode_escape = || -> Option<(char, isize)>
407+
let unicode_escape = || -> Option<(char, isize)> {
408408
if lit.as_bytes()[2] == b'{' {
409409
let idx = lit.find('}').expect(msg2);
410410
let subslice = &lit[3..idx];
@@ -413,7 +413,8 @@ pub fn char_lit(lit: &str) -> (char, isize) {
413413
.map(|x| (x, subslice.chars().count() as isize + 4))
414414
} else {
415415
esc(6, lit)
416-
};
416+
}
417+
};
417418

418419
// Unicode escapes
419420
return match lit.as_bytes()[1] as char {

src/libsyntax/parse/parser.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,10 +2099,7 @@ impl<'a> Parser<'a> {
20992099
}
21002100
},
21012101
token::OpenDelim(token::Brace) => {
2102-
self.bump();
2103-
let blk = self.parse_block_tail(lo, DefaultBlock);
2104-
return self.mk_expr(blk.span.lo, blk.span.hi,
2105-
ExprBlock(blk));
2102+
return self.parse_block_expr(lo, DefaultBlock);
21062103
},
21072104
token::BinOp(token::Or) | token::OrOr => {
21082105
return self.parse_lambda_expr(CaptureByRef);
@@ -2998,19 +2995,30 @@ impl<'a> Parser<'a> {
29982995
{
29992996
let lo = self.span.lo;
30002997
let decl = self.parse_fn_block_decl();
3001-
let body = self.parse_expr();
3002-
let fakeblock = P(ast::Block {
3003-
id: ast::DUMMY_NODE_ID,
3004-
stmts: vec![],
3005-
span: body.span,
3006-
expr: Some(body),
3007-
rules: DefaultBlock,
3008-
});
2998+
let body = match decl.output {
2999+
DefaultReturn(_) => {
3000+
// If no explicit return type is given, parse any
3001+
// expr and wrap it up in a dummy block:
3002+
let body_expr = self.parse_expr();
3003+
P(ast::Block {
3004+
id: ast::DUMMY_NODE_ID,
3005+
stmts: vec![],
3006+
span: body_expr.span,
3007+
expr: Some(body_expr),
3008+
rules: DefaultBlock,
3009+
})
3010+
}
3011+
_ => {
3012+
// If an explicit return type is given, require a
3013+
// block to appear (RFC 968).
3014+
self.parse_block()
3015+
}
3016+
};
30093017

30103018
self.mk_expr(
30113019
lo,
3012-
fakeblock.span.hi,
3013-
ExprClosure(capture_clause, decl, fakeblock))
3020+
body.span.hi,
3021+
ExprClosure(capture_clause, decl, body))
30143022
}
30153023

30163024
pub fn parse_else_expr(&mut self) -> P<Expr> {

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,12 @@ impl<'a> State<'a> {
17771777
try!(self.print_fn_block_args(&**decl));
17781778
try!(space(&mut self.s));
17791779

1780-
if !body.stmts.is_empty() || !body.expr.is_some() {
1780+
let default_return = match decl.output {
1781+
ast::DefaultReturn(..) => true,
1782+
_ => false
1783+
};
1784+
1785+
if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
17811786
try!(self.print_block_unclosed(&**body));
17821787
} else {
17831788
// we extract the block, so as not to create another set of boxes

src/test/compile-fail/fn-trait-formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
//~| found `Box<core::ops::Fn(isize, isize)>`
2727
//~| expected ()
2828
//~| found box
29-
let _: () = (box || -> isize unimplemented!()) as Box<FnMut() -> isize>;
29+
let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> isize>;
3030
//~^ ERROR mismatched types
3131
//~| expected `()`
3232
//~| found `Box<core::ops::FnMut() -> isize>`

src/test/compile-fail/liveness-issue-2163.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ use std::vec::Vec;
1313
fn main() {
1414
let a: Vec<isize> = Vec::new();
1515
a.iter().all(|_| -> bool {
16-
//~^ ERROR mismatched types
16+
//~^ ERROR not all control paths return a value
1717
});
1818
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 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+
// Test that we cannot parse a closure with an explicit return type
12+
// unless it uses braces.
13+
14+
fn main() {
15+
let x = || -> i32 22; //~ ERROR expected `{`, found `22`
16+
}

src/test/run-pass/block-explicit-types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
pub fn main() {
1212
fn as_buf<T, F>(s: String, f: F) -> T where F: FnOnce(String) -> T { f(s) }
13-
as_buf("foo".to_string(), |foo: String| -> () println!("{}", foo) );
13+
as_buf("foo".to_string(), |foo: String| -> () { println!("{}", foo) });
1414
}

src/test/run-pass/borrowck-move-by-capture-ok.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
pub fn main() {
1616
let bar: Box<_> = box 3;
17-
let h = || -> int *bar;
17+
let h = || -> int { *bar };
1818
assert_eq!(h(), 3);
1919
}

src/test/run-pass/issue-17816.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::marker::PhantomData;
1414

1515
fn main() {
1616
struct Symbol<'a, F: Fn(Vec<&'a str>) -> &'a str> { function: F, marker: PhantomData<&'a ()> }
17-
let f = |x: Vec<&str>| -> &str "foobar";
17+
let f = |x: Vec<&str>| -> &str { "foobar" };
1818
let sym = Symbol { function: f, marker: PhantomData };
1919
(sym.function)(vec![]);
2020
}

0 commit comments

Comments
 (0)