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.
This commit is contained in:
parent
f9a7bc58f8
commit
c225824bde
9 changed files with 52 additions and 22 deletions
|
|
@ -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>`
|
||||
|
|
|
|||
|
|
@ -13,6 +13,6 @@ use std::vec::Vec;
|
|||
fn main() {
|
||||
let a: Vec<isize> = Vec::new();
|
||||
a.iter().all(|_| -> bool {
|
||||
//~^ ERROR mismatched types
|
||||
//~^ ERROR not all control paths return a value
|
||||
});
|
||||
}
|
||||
|
|
|
|||
16
src/test/parse-fail/closure-return-syntax.rs
Normal file
16
src/test/parse-fail/closure-return-syntax.rs
Normal file
|
|
@ -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`
|
||||
}
|
||||
|
|
@ -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) });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,6 @@
|
|||
|
||||
pub fn main() {
|
||||
let bar: Box<_> = box 3;
|
||||
let h = || -> int *bar;
|
||||
let h = || -> int { *bar };
|
||||
assert_eq!(h(), 3);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use std::marker::PhantomData;
|
|||
|
||||
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![]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue