// `impl Future<Output = T>` here because lower_body
// only cares about the input argument patterns in the function
// declaration (decl), not the return types.
- let body_id = this.lower_async_body(decl, header.asyncness, body);
+ let body_id = this.lower_async_body(decl, header.asyncness.node, body);
let (generics, fn_decl) = this.add_in_band_defs(
generics,
decl,
Some((fn_def_id, idty)),
true,
- header.asyncness.opt_return_id()
+ header.asyncness.node.opt_return_id()
),
);
)
}
ImplItemKind::Method(ref sig, ref body) => {
- let body_id = self.lower_async_body(&sig.decl, sig.header.asyncness, body);
+ let body_id = self.lower_async_body(&sig.decl, sig.header.asyncness.node, body);
let impl_trait_return_allow = !self.is_in_trait_impl;
let (generics, sig) = self.lower_method_sig(
&i.generics,
sig,
impl_item_def_id,
impl_trait_return_allow,
- sig.header.asyncness.opt_return_id(),
+ sig.header.asyncness.node.opt_return_id(),
);
(generics, hir::ImplItemKind::Method(sig, body_id))
}
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
hir::FnHeader {
unsafety: self.lower_unsafety(h.unsafety),
- asyncness: self.lower_asyncness(h.asyncness),
+ asyncness: self.lower_asyncness(h.asyncness.node),
constness: self.lower_constness(h.constness),
abi: h.abi,
}
decl: &'a FnDecl,
body: &'a Block,
) {
- let (closure_id, return_impl_trait_id) = match header.asyncness {
+ let (closure_id, return_impl_trait_id) = match header.asyncness.node {
IsAsync::Async {
closure_id,
return_impl_trait_id,
}
ItemKind::Fn(
ref decl,
- ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
+ ref header,
ref generics,
ref body,
- ) => {
+ ) if header.asyncness.node.is_async() => {
return self.visit_async_fn(
i.id,
i.ident.name,
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
let def_data = match ii.node {
ImplItemKind::Method(MethodSig {
- header: ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
+ ref header,
ref decl,
- }, ref body) => {
+ }, ref body) if header.asyncness.node.is_async() => {
return self.visit_async_fn(
ii.id,
ii.ident.name,
self.invalid_visibility(&impl_item.vis, None);
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
self.check_trait_fn_not_const(sig.header.constness);
- self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness);
+ self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
}
}
}
.note("only trait implementations may be annotated with default").emit();
}
}
- ItemKind::Fn(_, header, ref generics, _) => {
+ ItemKind::Fn(_, ref header, ref generics, _) => {
// We currently do not permit const generics in `const fn`, as
// this is tantamount to allowing compile-time dependent typing.
+ self.visit_fn_header(header);
if header.constness.node == Constness::Const {
// Look for const generics and error if we find any.
for param in &generics.params {
self.no_questions_in_bounds(bounds, "supertraits", true);
for trait_item in trait_items {
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
- self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness);
+ self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness.node);
self.check_trait_fn_not_const(sig.header.constness);
if block.is_none() {
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
.span_bug(mac.span, "macro invocation missed in expansion; did you forget to override \
the relevant `fold_*()` method in `PlaceholderExpander`?");
}
+
+ fn visit_fn_header(&mut self, header: &'a FnHeader) {
+ if header.asyncness.node.is_async() && self.session.rust_2015() {
+ struct_span_err!(self.session, header.asyncness.span, E0670,
+ "`async fn` is not permitted in the 2015 edition").emit();
+ }
+ }
}
pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
break;
}
```
+"##,
+
+E0670: r##"
+Rust 2015 does not permit the use of `async fn`.
+
+Example of erroneous code:
+
+```compile_fail,E0670
+async fn foo() {}
+```
+
+Switch to the Rust 2018 edition to use `async fn`.
"##
}
debug!("(resolving function) entering function");
let (rib_kind, asyncness) = match function_kind {
FnKind::ItemFn(_, ref header, ..) =>
- (ItemRibKind, header.asyncness),
+ (ItemRibKind, header.asyncness.node),
FnKind::Method(_, ref sig, _, _) =>
- (TraitOrImplItemRibKind, sig.header.asyncness),
+ (TraitOrImplItemRibKind, sig.header.asyncness.node),
FnKind::Closure(_) =>
// Async closures aren't resolved through `visit_fn`-- they're
// processed separately
if header.constness.node == ast::Constness::Const {
text.push_str("const ");
}
- if header.asyncness.is_async() {
+ if header.asyncness.node.is_async() {
text.push_str("async ");
}
if header.unsafety == ast::Unsafety::Unsafe {
if m.header.constness.node == ast::Constness::Const {
text.push_str("const ");
}
- if m.header.asyncness.is_async() {
+ if m.header.asyncness.node.is_async() {
text.push_str("async ");
}
if m.header.unsafety == ast::Unsafety::Unsafe {
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
pub struct FnHeader {
pub unsafety: Unsafety,
- pub asyncness: IsAsync,
+ pub asyncness: Spanned<IsAsync>,
pub constness: Spanned<Constness>,
pub abi: Abi,
}
fn default() -> FnHeader {
FnHeader {
unsafety: Unsafety::Normal,
- asyncness: IsAsync::NotAsync,
+ asyncness: dummy_spanned(IsAsync::NotAsync),
constness: dummy_spanned(Constness::NotConst),
abi: Abi::Rust,
}
ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
ast::FnHeader {
unsafety: ast::Unsafety::Normal,
- asyncness: ast::IsAsync::NotAsync,
+ asyncness: dummy_spanned(ast::IsAsync::NotAsync),
constness: dummy_spanned(ast::Constness::NotConst),
abi: Abi::Rust,
},
match fn_kind {
FnKind::ItemFn(_, header, _, _) => {
// Check for const fn and async fn declarations.
- if header.asyncness.is_async() {
+ if header.asyncness.node.is_async() {
gate_feature_post!(&self, async_await, span, "async fn is unstable");
}
// Stability of const fn methods are covered in
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety: _, asyncness, constness: _, abi: _ } = header;
- vis.visit_asyncness(asyncness);
+ vis.visit_asyncness(&mut asyncness.node);
}
pub fn noop_visit_mod<T: MutVisitor>(Mod { inner, items, inline: _ }: &mut Mod, vis: &mut T) {
)
}
+ fn is_async_fn(&mut self) -> bool {
+ self.token.is_keyword(keywords::Async) &&
+ self.look_ahead(1, |t| t.is_keyword(keywords::Fn))
+ }
+
fn is_do_catch_block(&mut self) -> bool {
self.token.is_keyword(keywords::Do) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Catch)) &&
!self.is_union_item() &&
!self.is_crate_vis() &&
!self.is_existential_type_decl() &&
- !self.is_auto_trait_item() {
+ !self.is_auto_trait_item() &&
+ !self.is_async_fn() {
let pth = self.parse_path(PathStyle::Expr)?;
if !self.eat(&token::Not) {
/// Parses an item-position function declaration.
fn parse_item_fn(&mut self,
unsafety: Unsafety,
- asyncness: IsAsync,
+ asyncness: Spanned<IsAsync>,
constness: Spanned<Constness>,
abi: Abi)
-> PResult<'a, ItemInfo> {
-> PResult<'a, (
Spanned<Constness>,
Unsafety,
- IsAsync,
+ Spanned<IsAsync>,
Abi
)>
{
let const_span = self.prev_span;
let unsafety = self.parse_unsafety();
let asyncness = self.parse_asyncness();
+ let asyncness = respan(self.prev_span, asyncness);
let (constness, unsafety, abi) = if is_const_fn {
(respan(const_span, Constness::Const), unsafety, Abi::Rust)
} else {
let abi = opt_abi.unwrap_or(Abi::C);
let (ident, item_, extra_attrs) =
self.parse_item_fn(Unsafety::Normal,
- IsAsync::NotAsync,
+ respan(fn_span, IsAsync::NotAsync),
respan(fn_span, Constness::NotConst),
abi)?;
let prev_span = self.prev_span;
self.bump();
let (ident, item_, extra_attrs) =
self.parse_item_fn(unsafety,
- IsAsync::NotAsync,
+ respan(const_span, IsAsync::NotAsync),
respan(const_span, Constness::Const),
Abi::Rust)?;
let prev_span = self.prev_span;
// ASYNC FUNCTION ITEM
let unsafety = self.parse_unsafety();
self.expect_keyword(keywords::Async)?;
+ let async_span = self.prev_span;
self.expect_keyword(keywords::Fn)?;
let fn_span = self.prev_span;
let (ident, item_, extra_attrs) =
self.parse_item_fn(unsafety,
- IsAsync::Async {
+ respan(async_span, IsAsync::Async {
closure_id: ast::DUMMY_NODE_ID,
return_impl_trait_id: ast::DUMMY_NODE_ID,
- },
+ }),
respan(fn_span, Constness::NotConst),
Abi::Rust)?;
let prev_span = self.prev_span;
item_,
visibility,
maybe_append(attrs, extra_attrs));
+ if self.span.rust_2015() {
+ self.diagnostic().struct_span_err_with_code(
+ async_span,
+ "`async fn` is not permitted in the 2015 edition",
+ DiagnosticId::Error("E0670".into())
+ ).emit();
+ }
return Ok(Some(item));
}
if self.check_keyword(keywords::Unsafe) &&
let fn_span = self.prev_span;
let (ident, item_, extra_attrs) =
self.parse_item_fn(Unsafety::Normal,
- IsAsync::NotAsync,
+ respan(fn_span, IsAsync::NotAsync),
respan(fn_span, Constness::NotConst),
Abi::Rust)?;
let prev_span = self.prev_span;
let fn_span = self.prev_span;
let (ident, item_, extra_attrs) =
self.parse_item_fn(Unsafety::Unsafe,
- IsAsync::NotAsync,
+ respan(fn_span, IsAsync::NotAsync),
respan(fn_span, Constness::NotConst),
abi)?;
let prev_span = self.prev_span;
lo: Span,
visibility: Visibility
) -> PResult<'a, Option<P<Item>>> {
- if macros_allowed && self.token.is_path_start() {
+ if macros_allowed && self.token.is_path_start() &&
+ !(self.is_async_fn() && self.span.rust_2015()) {
// MACRO INVOCATION ITEM
let prev_span = self.prev_span;
fn parse_assoc_macro_invoc(&mut self, item_kind: &str, vis: Option<&Visibility>,
at_end: &mut bool) -> PResult<'a, Option<Mac>>
{
- if self.token.is_path_start() {
+ if self.token.is_path_start() &&
+ !(self.is_async_fn() && self.span.rust_2015()) {
let prev_span = self.prev_span;
let lo = self.span;
let pth = self.parse_path(PathStyle::Mod)?;
ast::Constness::Const => self.word_nbsp("const")?
}
- self.print_asyncness(header.asyncness)?;
+ self.print_asyncness(header.asyncness.node)?;
self.print_unsafety(header.unsafety)?;
if header.abi != Abi::Rust {
ast::FnHeader {
unsafety: ast::Unsafety::Normal,
constness: source_map::dummy_spanned(ast::Constness::NotConst),
- asyncness: ast::IsAsync::NotAsync,
+ asyncness: source_map::dummy_spanned(ast::IsAsync::NotAsync),
abi: Abi::Rust,
},
abba_ident,
#[derive(Copy, Clone)]
pub enum FnKind<'a> {
/// fn foo() or extern "Abi" fn foo()
- ItemFn(Ident, FnHeader, &'a Visibility, &'a Block),
+ ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block),
/// fn foo(&self)
Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a Block),
fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
walk_fn_ret_ty(self, ret_ty)
}
+ fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
+ // Nothing to do
+ }
}
#[macro_export]
visitor.visit_ty(typ);
visitor.visit_expr(expr);
}
- ItemKind::Fn(ref declaration, header, ref generics, ref body) => {
+ ItemKind::Fn(ref declaration, ref header, ref generics, ref body) => {
visitor.visit_generics(generics);
+ visitor.visit_fn_header(header);
visitor.visit_fn(FnKind::ItemFn(item.ident, header,
&item.vis, body),
declaration,
where V: Visitor<'a>,
{
match kind {
- FnKind::ItemFn(_, _, _, body) => {
+ FnKind::ItemFn(_, header, _, body) => {
+ visitor.visit_fn_header(header);
walk_fn_decl(visitor, declaration);
visitor.visit_block(body);
}
- FnKind::Method(_, _, _, body) => {
+ FnKind::Method(_, sig, _, body) => {
+ visitor.visit_fn_header(&sig.header);
walk_fn_decl(visitor, declaration);
visitor.visit_block(body);
}
walk_list!(visitor, visit_expr, default);
}
TraitItemKind::Method(ref sig, None) => {
+ visitor.visit_fn_header(&sig.header);
walk_fn_decl(visitor, &sig.decl);
}
TraitItemKind::Method(ref sig, Some(ref body)) => {
);
return false
}
- if header.asyncness.is_async() {
+ if header.asyncness.node.is_async() {
sd.span_err(
i.span,
"async functions cannot be used for tests"
--- /dev/null
+// edition:2015
+
+#![feature(futures_api, async_await)]
+
+async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+
+fn baz() { async fn foo() {} } //~ ERROR `async fn` is not permitted in the 2015 edition
+
+async fn async_baz() { //~ ERROR `async fn` is not permitted in the 2015 edition
+ async fn bar() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+}
+
+struct Foo {}
+
+impl Foo {
+ async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+}
+
+trait Bar {
+ async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ //~^ ERROR trait fns cannot be declared `async`
+}
+
+fn main() {
+ macro_rules! accept_item { ($x:item) => {} }
+
+ accept_item! {
+ async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ }
+
+ let inside_closure = || {
+ async fn bar() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ };
+}
--- /dev/null
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:5:1
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:7:12
+ |
+LL | fn baz() { async fn foo() {} } //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:10:5
+ |
+LL | async fn bar() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:9:1
+ |
+LL | async fn async_baz() { //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:32:9
+ |
+LL | async fn bar() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:28:9
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:16:5
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error[E0706]: trait fns cannot be declared `async`
+ --> $DIR/edition-deny-async-fns-2015.rs:20:5
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^^^^^^^^^^^^^
+
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/edition-deny-async-fns-2015.rs:20:5
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
+error: aborting due to 9 previous errors
+
+Some errors occurred: E0670, E0706.
+For more information about an error, try `rustc --explain E0670`.
#![feature(futures_api)]
-async fn foo() {} //~ ERROR async fn is unstable
+async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ //~^ ERROR async fn is unstable
fn main() {
let _ = async {}; //~ ERROR cannot find struct, variant or union type `async`
+error[E0670]: `async fn` is not permitted in the 2015 edition
+ --> $DIR/feature-gate-async-await-2015-edition.rs:5:1
+ |
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
+ | ^^^^^
+
error[E0422]: cannot find struct, variant or union type `async` in this scope
- --> $DIR/feature-gate-async-await-2015-edition.rs:8:13
+ --> $DIR/feature-gate-async-await-2015-edition.rs:9:13
|
LL | let _ = async {}; //~ ERROR cannot find struct, variant or union type `async`
| ^^^^^ not found in this scope
error[E0425]: cannot find value `async` in this scope
- --> $DIR/feature-gate-async-await-2015-edition.rs:9:13
+ --> $DIR/feature-gate-async-await-2015-edition.rs:10:13
|
LL | let _ = async || { true }; //~ ERROR cannot find value `async` in this scope
| ^^^^^ not found in this scope
error[E0658]: async fn is unstable (see issue #50547)
--> $DIR/feature-gate-async-await-2015-edition.rs:5:1
|
-LL | async fn foo() {} //~ ERROR async fn is unstable
+LL | async fn foo() {} //~ ERROR `async fn` is not permitted in the 2015 edition
| ^^^^^^^^^^^^^^^^^
|
= help: add #![feature(async_await)] to the crate attributes to enable
-error: aborting due to 3 previous errors
+error: aborting due to 4 previous errors
-Some errors occurred: E0422, E0425, E0658.
+Some errors occurred: E0422, E0425, E0658, E0670.
For more information about an error, try `rustc --explain E0422`.
--- /dev/null
+// edition:2018
+// Test that impl trait does not allow creating recursive types that are
+// otherwise forbidden when using `async` and `await`.
+
+#![feature(await_macro, async_await, futures_api, generators)]
+
+async fn recursive_async_function() -> () { //~ ERROR
+ await!(recursive_async_function());
+}
+
+fn main() {}
--- /dev/null
+error[E0720]: opaque type expands to a recursive type
+ --> $DIR/recursive-async-impl-trait-type.rs:7:40
+ |
+LL | async fn recursive_async_function() -> () { //~ ERROR
+ | ^^ expands to self-referential type
+ |
+ = note: expanded type is `std::future::GenFuture<[static generator@$DIR/recursive-async-impl-trait-type.rs:7:43: 9:2 {impl std::future::Future, ()}]>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0720`.
// Test that impl trait does not allow creating recursive types that are
// otherwise forbidden.
-#![feature(await_macro, async_await, futures_api, generators)]
+#![feature(futures_api, generators)]
fn option(i: i32) -> impl Sized { //~ ERROR
if i < 0 {
}
}
-async fn recursive_async_function() -> () { //~ ERROR
- await!(recursive_async_function());
-}
-
fn use_fn_ptr() -> impl Sized { // OK, error already reported
fn_ptr()
}
= note: expanded type is `[generator@$DIR/recursive-impl-trait-type.rs:58:5: 62:6 {impl Sized, ()}]`
error[E0720]: opaque type expands to a recursive type
- --> $DIR/recursive-impl-trait-type.rs:65:40
- |
-LL | async fn recursive_async_function() -> () { //~ ERROR
- | ^^ expands to self-referential type
- |
- = note: expanded type is `std::future::GenFuture<[static generator@$DIR/recursive-impl-trait-type.rs:65:43: 67:2 {impl std::future::Future, ()}]>`
-
-error[E0720]: opaque type expands to a recursive type
- --> $DIR/recursive-impl-trait-type.rs:73:26
+ --> $DIR/recursive-impl-trait-type.rs:69:26
|
LL | fn mutual_recursion() -> impl Sync { //~ ERROR
| ^^^^^^^^^ expands to self-referential type
= note: type resolves to itself
error[E0720]: opaque type expands to a recursive type
- --> $DIR/recursive-impl-trait-type.rs:77:28
+ --> $DIR/recursive-impl-trait-type.rs:73:28
|
LL | fn mutual_recursion_b() -> impl Sized { //~ ERROR
| ^^^^^^^^^^ expands to self-referential type
|
= note: type resolves to itself
-error: aborting due to 15 previous errors
+error: aborting due to 14 previous errors
For more information about this error, try `rustc --explain E0720`.