From faaab52c81f0e5be4cecd6c934f17f7ff17c1e6b Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 18:44:55 +0100 Subject: [PATCH 1/7] Add a `header` method to `FnKind` --- src/librustc/hir/intravisit.rs | 8 ++++++++ 1 file changed, 8 insertions(+) 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 From b13562afc8513d87c27ade9633a6983da53fb820 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 18:47:52 +0100 Subject: [PATCH 2/7] Refactor some existing methods --- src/librustc/hir/map/blocks.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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> { From db13fe6eacc7181e8ea75c08daaaa33c00dc19aa Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 19:15:43 +0100 Subject: [PATCH 3/7] Feature gate async fn methods --- src/libsyntax/feature_gate.rs | 32 +++++++++++++------------------- src/libsyntax/visit.rs | 10 ++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index c8b020d8c0b03..5b2a2476ec365 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -2034,28 +2034,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-varaidic functions are unstable"); } + visit::walk_fn(self, fn_kind, fn_decl, span); } 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; From b6888dbb6168b8a32a3d02b8f471829324e8dd8b Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 19:15:53 +0100 Subject: [PATCH 4/7] Add test for async fn methods feature gating --- .../feature-gates/feature-gate-async-await.rs | 6 ++++++ .../feature-gate-async-await.stderr | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) 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..f06b0b3639503 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,12 @@ #![feature(futures_api)] +struct S; + +impl S { + async fn foo() {} //~ 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..4132563842235 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,14 @@ error[E0658]: async fn is unstable - --> $DIR/feature-gate-async-await.rs:5:1 + --> $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:11:1 | LL | async fn foo() {} | ^^^^^^^^^^^^^^^^^ @@ -8,7 +17,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:14:13 | LL | let _ = async {}; | ^^^^^^^^ @@ -17,7 +26,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:15:13 | LL | let _ = async || {}; | ^^^^^^^^^^^ @@ -25,6 +34,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 4 previous errors For more information about this error, try `rustc --explain E0658`. From edce367e31209818ed2dbbd3807b8e7161fcd8fa Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 19:16:17 +0100 Subject: [PATCH 5/7] Fix typo in variadic C function warning --- src/libsyntax/feature_gate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 5b2a2476ec365..44c2332925df2 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -2047,7 +2047,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } if fn_decl.c_variadic { - gate_feature_post!(&self, c_variadic, span, "C-varaidic functions are unstable"); + gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable"); } visit::walk_fn(self, fn_kind, fn_decl, span); From 3dbe05f496f088180451285ad0837e73e5bc37a8 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 20:36:32 +0100 Subject: [PATCH 6/7] Fix additional variadic typos --- src/libsyntax/feature_gate.rs | 2 +- src/test/ui/feature-gate/feature-gate-c_variadic.rs | 2 +- src/test/ui/feature-gate/feature-gate-c_variadic.stderr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 44c2332925df2..b05f883767b7f 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -2069,7 +2069,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } 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/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: ...) { } From 7548ca64b4715d2eee0eb035c71974bebc91437a Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 18 Apr 2019 20:57:15 +0100 Subject: [PATCH 7/7] Check async in trait methods --- src/libsyntax/feature_gate.rs | 3 +++ .../feature-gates/feature-gate-async-await.rs | 4 +++ .../feature-gate-async-await.stderr | 26 +++++++++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index b05f883767b7f..c78fe84ca5bc7 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -2067,6 +2067,9 @@ 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-variadic functions are unstable"); 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 f06b0b3639503..5a3ba0b3c6678 100644 --- a/src/test/ui/feature-gates/feature-gate-async-await.rs +++ b/src/test/ui/feature-gates/feature-gate-async-await.rs @@ -8,6 +8,10 @@ impl S { async fn foo() {} //~ ERROR async fn is unstable } +trait T { + async fn foo(); //~ ERROR trait fns cannot be declared `async` +} + 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 4132563842235..ca6466d9ef6aa 100644 --- a/src/test/ui/feature-gates/feature-gate-async-await.stderr +++ b/src/test/ui/feature-gates/feature-gate-async-await.stderr @@ -1,3 +1,9 @@ +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 | @@ -8,7 +14,16 @@ LL | async fn foo() {} = help: add #![feature(async_await)] to the crate attributes to enable error[E0658]: async fn is unstable - --> $DIR/feature-gate-async-await.rs:11:1 + --> $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:15:1 | LL | async fn foo() {} | ^^^^^^^^^^^^^^^^^ @@ -17,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:14:13 + --> $DIR/feature-gate-async-await.rs:18:13 | LL | let _ = async {}; | ^^^^^^^^ @@ -26,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:15:13 + --> $DIR/feature-gate-async-await.rs:19:13 | LL | let _ = async || {}; | ^^^^^^^^^^^ @@ -34,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 4 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`.