From b220db03bd334b2e74c4dfd62ed1318941b82a3c Mon Sep 17 00:00:00 2001 From: P1start Date: Fri, 29 Aug 2014 17:16:23 +1200 Subject: [PATCH 1/2] Allow `!` as the return type of proc/closure literals Fixes #13490. --- src/libsyntax/parse/parser.rs | 20 ++++++++++---------- src/test/run-pass/closure-syntax.rs | 4 ++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 37bda15ac2c..c70a171ff7b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4092,20 +4092,20 @@ impl<'a> Parser<'a> { (optional_unboxed_closure_kind, args) } }; - let output = if self.eat(&token::RARROW) { - self.parse_ty(true) + let (style, output) = if self.token == token::RARROW { + self.parse_ret_ty() } else { - P(Ty { + (Return, P(Ty { id: ast::DUMMY_NODE_ID, node: TyInfer, span: self.span, - }) + })) }; (P(FnDecl { inputs: inputs_captures, output: output, - cf: Return, + cf: style, variadic: false }), optional_unboxed_closure_kind) } @@ -4118,20 +4118,20 @@ impl<'a> Parser<'a> { seq_sep_trailing_allowed(token::COMMA), |p| p.parse_fn_block_arg()); - let output = if self.eat(&token::RARROW) { - self.parse_ty(true) + let (style, output) = if self.token == token::RARROW { + self.parse_ret_ty() } else { - P(Ty { + (Return, P(Ty { id: ast::DUMMY_NODE_ID, node: TyInfer, span: self.span, - }) + })) }; P(FnDecl { inputs: inputs, output: output, - cf: Return, + cf: style, variadic: false }) } diff --git a/src/test/run-pass/closure-syntax.rs b/src/test/run-pass/closure-syntax.rs index c2fbc2a4bf2..b5a94a02b34 100644 --- a/src/test/run-pass/closure-syntax.rs +++ b/src/test/run-pass/closure-syntax.rs @@ -78,6 +78,10 @@ fn bar<'b>() { let a = A; a.foo::<<'a>||>(); + + // issue #13490 + let _ = || -> ! loop {}; + let _ = proc() -> ! loop {}; } struct B; From e5bbbbe274dff5d420f256b58ff107e619ddf86d Mon Sep 17 00:00:00 2001 From: P1start Date: Sat, 30 Aug 2014 20:37:11 +1200 Subject: [PATCH 2/2] Add some tests for closures that return `!` --- src/test/compile-fail/closure-that-fails.rs | 3 +++ .../compile-fail/dead-code-closure-bang.rs | 17 ++++++++++++++++ src/test/run-pass/closure-return-bang.rs | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/test/compile-fail/dead-code-closure-bang.rs create mode 100644 src/test/run-pass/closure-return-bang.rs diff --git a/src/test/compile-fail/closure-that-fails.rs b/src/test/compile-fail/closure-that-fails.rs index 60c80f872ec..7ce8e95a761 100644 --- a/src/test/compile-fail/closure-that-fails.rs +++ b/src/test/compile-fail/closure-that-fails.rs @@ -13,5 +13,8 @@ fn foo(f: || -> !) {} fn main() { // Type inference didn't use to be able to handle this: foo(|| fail!()); + foo(|| -> ! fail!()); foo(|| 22); //~ ERROR mismatched types + foo(|| -> ! 22); //~ ERROR mismatched types + let x = || -> ! 1; //~ ERROR mismatched types } diff --git a/src/test/compile-fail/dead-code-closure-bang.rs b/src/test/compile-fail/dead-code-closure-bang.rs new file mode 100644 index 00000000000..96e8378a35d --- /dev/null +++ b/src/test/compile-fail/dead-code-closure-bang.rs @@ -0,0 +1,17 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(unreachable_code)] + +fn main() { + let x: || -> ! = || fail!(); + x(); + println!("Foo bar"); //~ ERROR: unreachable statement +} diff --git a/src/test/run-pass/closure-return-bang.rs b/src/test/run-pass/closure-return-bang.rs new file mode 100644 index 00000000000..e164aeca013 --- /dev/null +++ b/src/test/run-pass/closure-return-bang.rs @@ -0,0 +1,20 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] + +fn f(x: || -> !) -> ! { + x(); +} + +fn main() { + let x: || -> ! = || fail!(); + let _y: || -> ! = || x(); +}