about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-04-20 23:44:02 +0000
committerbors <bors@rust-lang.org>2019-04-20 23:44:02 +0000
commitc3a71943545516557f40f93d30c1512f1f16ff64 (patch)
treea71e7d4643cb3219010181d513946aff20ede024
parent33fe1131cadba69d317156847be9a402b89f11bb (diff)
parentee496057a5ba9f006bfad77b2c64603deb6c2450 (diff)
downloadrust-c3a71943545516557f40f93d30c1512f1f16ff64.tar.gz
rust-c3a71943545516557f40f93d30c1512f1f16ff64.zip
Auto merge of #60088 - varkor:async_await-method-feature-gate, r=cramertj
Feature gate async methods

Fixes https://github.com/rust-lang/rust/issues/60069.
-rw-r--r--src/librustc/hir/intravisit.rs8
-rw-r--r--src/librustc/hir/map/blocks.rs18
-rw-r--r--src/libsyntax/feature_gate.rs37
-rw-r--r--src/libsyntax/visit.rs10
-rw-r--r--src/test/ui/feature-gate/feature-gate-c_variadic.rs2
-rw-r--r--src/test/ui/feature-gate/feature-gate-c_variadic.stderr2
-rw-r--r--src/test/ui/feature-gates/feature-gate-async-await.rs11
-rw-r--r--src/test/ui/feature-gates/feature-gate-async-await.stderr32
8 files changed, 79 insertions, 41 deletions
diff --git a/src/librustc/hir/intravisit.rs b/src/librustc/hir/intravisit.rs
index c2265eeb30d..007eaef74a7 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<FnHeader> {
+        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 1114ef52bbc..f50037a746d 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 7bae5ba7571..bc87a88f9f1 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 8f42d47e69c..fe74cbd6496 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 5801a2a89e2..8b40c36c7db 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 a1362901862..4367dee55a1 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 7ee035644bc..1fdaec75e9d 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 6bff254607f..1fa21f52045 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,6 @@ 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`.