diff --git a/src/librustc/hir/intravisit.rs b/src/librustc/hir/intravisit.rs index c2265eeb30d74..007eaef74a7ad 100644 --- a/src/librustc/hir/intravisit.rs +++ b/src/librustc/hir/intravisit.rs @@ -57,6 +57,14 @@ impl<'a> FnKind<'a> { FnKind::Closure(attrs) => attrs, } } + + pub fn header(&self) -> Option { + match *self { + FnKind::ItemFn(_, _, header, _, _) => Some(header), + FnKind::Method(_, sig, _, _) => Some(sig.header), + FnKind::Closure(_) => None, + } + } } /// Specifies what nested things a visitor wants to visit. The most diff --git a/src/librustc/hir/map/blocks.rs b/src/librustc/hir/map/blocks.rs index 1114ef52bbc0c..f50037a746d97 100644 --- a/src/librustc/hir/map/blocks.rs +++ b/src/librustc/hir/map/blocks.rs @@ -175,27 +175,15 @@ impl<'a> FnLikeNode<'a> { } pub fn constness(self) -> ast::Constness { - match self.kind() { - FnKind::ItemFn(_, _, header, ..) => header.constness, - FnKind::Method(_, m, ..) => m.header.constness, - _ => ast::Constness::NotConst - } + self.kind().header().map_or(ast::Constness::NotConst, |header| header.constness) } pub fn asyncness(self) -> ast::IsAsync { - match self.kind() { - FnKind::ItemFn(_, _, header, ..) => header.asyncness, - FnKind::Method(_, m, ..) => m.header.asyncness, - _ => ast::IsAsync::NotAsync - } + self.kind().header().map_or(ast::IsAsync::NotAsync, |header| header.asyncness) } pub fn unsafety(self) -> ast::Unsafety { - match self.kind() { - FnKind::ItemFn(_, _, header, ..) => header.unsafety, - FnKind::Method(_, m, ..) => m.header.unsafety, - _ => ast::Unsafety::Normal - } + self.kind().header().map_or(ast::Unsafety::Normal, |header| header.unsafety) } pub fn kind(self) -> FnKind<'a> { diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 7bae5ba75719a..bc87a88f9f11a 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -2035,28 +2035,22 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn_decl: &'a ast::FnDecl, span: Span, _node_id: NodeId) { - match fn_kind { - FnKind::ItemFn(_, header, _, _) => { - // Check for const fn and async fn declarations. - if header.asyncness.node.is_async() { - gate_feature_post!(&self, async_await, span, "async fn is unstable"); - } + if let Some(header) = fn_kind.header() { + // Check for const fn and async fn declarations. + if header.asyncness.node.is_async() { + gate_feature_post!(&self, async_await, span, "async fn is unstable"); + } - if fn_decl.c_variadic { - gate_feature_post!(&self, c_variadic, span, - "C-varaidic functions are unstable"); - } - // Stability of const fn methods are covered in - // `visit_trait_item` and `visit_impl_item` below; this is - // because default methods don't pass through this point. + // Stability of const fn methods are covered in + // `visit_trait_item` and `visit_impl_item` below; this is + // because default methods don't pass through this point. + self.check_abi(header.abi, span); + } - self.check_abi(header.abi, span); - } - FnKind::Method(_, sig, _, _) => { - self.check_abi(sig.header.abi, span); - } - _ => {} + if fn_decl.c_variadic { + gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable"); } + visit::walk_fn(self, fn_kind, fn_decl, span); } @@ -2074,9 +2068,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { if block.is_none() { self.check_abi(sig.header.abi, ti.span); } + if sig.header.asyncness.node.is_async() { + gate_feature_post!(&self, async_await, ti.span, "async fn is unstable"); + } if sig.decl.c_variadic { gate_feature_post!(&self, c_variadic, ti.span, - "C-varaidic functions are unstable"); + "C-variadic functions are unstable"); } if sig.header.constness.node == ast::Constness::Const { gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable"); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 8f42d47e69cd3..fe74cbd649612 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -31,6 +31,16 @@ pub enum FnKind<'a> { Closure(&'a Expr), } +impl<'a> FnKind<'a> { + pub fn header(&self) -> Option<&'a FnHeader> { + match *self { + FnKind::ItemFn(_, header, _, _) => Some(header), + FnKind::Method(_, sig, _, _) => Some(&sig.header), + FnKind::Closure(_) => None, + } + } +} + /// Each method of the Visitor trait is a hook to be potentially /// overridden. Each method's default implementation recursively visits /// the substructure of the input via the corresponding `walk` method; diff --git a/src/test/ui/feature-gate/feature-gate-c_variadic.rs b/src/test/ui/feature-gate/feature-gate-c_variadic.rs index 5801a2a89e2c1..8b40c36c7db5e 100644 --- a/src/test/ui/feature-gate/feature-gate-c_variadic.rs +++ b/src/test/ui/feature-gate/feature-gate-c_variadic.rs @@ -1,4 +1,4 @@ #![crate_type="lib"] pub unsafe extern "C" fn test(_: i32, ap: ...) { } -//~^ C-varaidic functions are unstable +//~^ C-variadic functions are unstable diff --git a/src/test/ui/feature-gate/feature-gate-c_variadic.stderr b/src/test/ui/feature-gate/feature-gate-c_variadic.stderr index a136290186243..4367dee55a118 100644 --- a/src/test/ui/feature-gate/feature-gate-c_variadic.stderr +++ b/src/test/ui/feature-gate/feature-gate-c_variadic.stderr @@ -1,4 +1,4 @@ -error[E0658]: C-varaidic functions are unstable +error[E0658]: C-variadic functions are unstable --> $DIR/feature-gate-c_variadic.rs:3:1 | LL | pub unsafe extern "C" fn test(_: i32, ap: ...) { } diff --git a/src/test/ui/feature-gates/feature-gate-async-await.rs b/src/test/ui/feature-gates/feature-gate-async-await.rs index 7ee035644bc95..1fdaec75e9d5a 100644 --- a/src/test/ui/feature-gates/feature-gate-async-await.rs +++ b/src/test/ui/feature-gates/feature-gate-async-await.rs @@ -2,6 +2,17 @@ #![feature(futures_api)] +struct S; + +impl S { + async fn foo() {} //~ ERROR async fn is unstable +} + +trait T { + async fn foo(); //~ ERROR trait fns cannot be declared `async` + //~^ ERROR async fn is unstable +} + async fn foo() {} //~ ERROR async fn is unstable fn main() { diff --git a/src/test/ui/feature-gates/feature-gate-async-await.stderr b/src/test/ui/feature-gates/feature-gate-async-await.stderr index 6bff254607fca..3f6731f47df88 100644 --- a/src/test/ui/feature-gates/feature-gate-async-await.stderr +++ b/src/test/ui/feature-gates/feature-gate-async-await.stderr @@ -1,5 +1,29 @@ +error[E0706]: trait fns cannot be declared `async` + --> $DIR/feature-gate-async-await.rs:12:5 + | +LL | async fn foo(); + | ^^^^^^^^^^^^^^^ + +error[E0658]: async fn is unstable + --> $DIR/feature-gate-async-await.rs:8:5 + | +LL | async fn foo() {} + | ^^^^^^^^^^^^^^^^^ + | + = note: for more information, see https://github.com/rust-lang/rust/issues/50547 + = help: add #![feature(async_await)] to the crate attributes to enable + +error[E0658]: async fn is unstable + --> $DIR/feature-gate-async-await.rs:12:5 + | +LL | async fn foo(); + | ^^^^^^^^^^^^^^^ + | + = note: for more information, see https://github.com/rust-lang/rust/issues/50547 + = help: add #![feature(async_await)] to the crate attributes to enable + error[E0658]: async fn is unstable - --> $DIR/feature-gate-async-await.rs:5:1 + --> $DIR/feature-gate-async-await.rs:16:1 | LL | async fn foo() {} | ^^^^^^^^^^^^^^^^^ @@ -8,7 +32,7 @@ LL | async fn foo() {} = help: add #![feature(async_await)] to the crate attributes to enable error[E0658]: async blocks are unstable - --> $DIR/feature-gate-async-await.rs:8:13 + --> $DIR/feature-gate-async-await.rs:19:13 | LL | let _ = async {}; | ^^^^^^^^ @@ -17,7 +41,7 @@ LL | let _ = async {}; = help: add #![feature(async_await)] to the crate attributes to enable error[E0658]: async closures are unstable - --> $DIR/feature-gate-async-await.rs:9:13 + --> $DIR/feature-gate-async-await.rs:20:13 | LL | let _ = async || {}; | ^^^^^^^^^^^ @@ -25,6 +49,7 @@ LL | let _ = async || {}; = note: for more information, see https://github.com/rust-lang/rust/issues/50547 = help: add #![feature(async_await)] to the crate attributes to enable -error: aborting due to 3 previous errors +error: aborting due to 6 previous errors -For more information about this error, try `rustc --explain E0658`. +Some errors occurred: E0658, E0706. +For more information about an error, try `rustc --explain E0658`.