Skip to content

Commit d6fd895

Browse files
authored
Rollup merge of rust-lang#60088 - varkor:async_await-method-feature-gate, r=cramertj
Feature gate async methods Fixes rust-lang#60069.
2 parents 42996e1 + bf0965c commit d6fd895

File tree

8 files changed

+81
-42
lines changed

8 files changed

+81
-42
lines changed

src/librustc/hir/intravisit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ impl<'a> FnKind<'a> {
5757
FnKind::Closure(attrs) => attrs,
5858
}
5959
}
60+
61+
pub fn header(&self) -> Option<FnHeader> {
62+
match *self {
63+
FnKind::ItemFn(_, _, header, _, _) => Some(header),
64+
FnKind::Method(_, sig, _, _) => Some(sig.header),
65+
FnKind::Closure(_) => None,
66+
}
67+
}
6068
}
6169

6270
/// Specifies what nested things a visitor wants to visit. The most

src/librustc/hir/map/blocks.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,15 @@ impl<'a> FnLikeNode<'a> {
175175
}
176176

177177
pub fn constness(self) -> ast::Constness {
178-
match self.kind() {
179-
FnKind::ItemFn(_, _, header, ..) => header.constness,
180-
FnKind::Method(_, m, ..) => m.header.constness,
181-
_ => ast::Constness::NotConst
182-
}
178+
self.kind().header().map_or(ast::Constness::NotConst, |header| header.constness)
183179
}
184180

185181
pub fn asyncness(self) -> ast::IsAsync {
186-
match self.kind() {
187-
FnKind::ItemFn(_, _, header, ..) => header.asyncness,
188-
FnKind::Method(_, m, ..) => m.header.asyncness,
189-
_ => ast::IsAsync::NotAsync
190-
}
182+
self.kind().header().map_or(ast::IsAsync::NotAsync, |header| header.asyncness)
191183
}
192184

193185
pub fn unsafety(self) -> ast::Unsafety {
194-
match self.kind() {
195-
FnKind::ItemFn(_, _, header, ..) => header.unsafety,
196-
FnKind::Method(_, m, ..) => m.header.unsafety,
197-
_ => ast::Unsafety::Normal
198-
}
186+
self.kind().header().map_or(ast::Unsafety::Normal, |header| header.unsafety)
199187
}
200188

201189
pub fn kind(self) -> FnKind<'a> {

src/libsyntax/feature_gate.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -2035,28 +2035,22 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
20352035
fn_decl: &'a ast::FnDecl,
20362036
span: Span,
20372037
_node_id: NodeId) {
2038-
match fn_kind {
2039-
FnKind::ItemFn(_, header, _, _) => {
2040-
// Check for const fn and async fn declarations.
2041-
if header.asyncness.node.is_async() {
2042-
gate_feature_post!(&self, async_await, span, "async fn is unstable");
2043-
}
2038+
if let Some(header) = fn_kind.header() {
2039+
// Check for const fn and async fn declarations.
2040+
if header.asyncness.node.is_async() {
2041+
gate_feature_post!(&self, async_await, span, "async fn is unstable");
2042+
}
20442043

2045-
if fn_decl.c_variadic {
2046-
gate_feature_post!(&self, c_variadic, span,
2047-
"C-varaidic functions are unstable");
2048-
}
2049-
// Stability of const fn methods are covered in
2050-
// `visit_trait_item` and `visit_impl_item` below; this is
2051-
// because default methods don't pass through this point.
2044+
// Stability of const fn methods are covered in
2045+
// `visit_trait_item` and `visit_impl_item` below; this is
2046+
// because default methods don't pass through this point.
2047+
self.check_abi(header.abi, span);
2048+
}
20522049

2053-
self.check_abi(header.abi, span);
2054-
}
2055-
FnKind::Method(_, sig, _, _) => {
2056-
self.check_abi(sig.header.abi, span);
2057-
}
2058-
_ => {}
2050+
if fn_decl.c_variadic {
2051+
gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
20592052
}
2053+
20602054
visit::walk_fn(self, fn_kind, fn_decl, span);
20612055
}
20622056

@@ -2074,9 +2068,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
20742068
if block.is_none() {
20752069
self.check_abi(sig.header.abi, ti.span);
20762070
}
2071+
if sig.header.asyncness.node.is_async() {
2072+
gate_feature_post!(&self, async_await, ti.span, "async fn is unstable");
2073+
}
20772074
if sig.decl.c_variadic {
20782075
gate_feature_post!(&self, c_variadic, ti.span,
2079-
"C-varaidic functions are unstable");
2076+
"C-variadic functions are unstable");
20802077
}
20812078
if sig.header.constness.node == ast::Constness::Const {
20822079
gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable");

src/libsyntax/visit.rs

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ pub enum FnKind<'a> {
3131
Closure(&'a Expr),
3232
}
3333

34+
impl<'a> FnKind<'a> {
35+
pub fn header(&self) -> Option<&'a FnHeader> {
36+
match *self {
37+
FnKind::ItemFn(_, header, _, _) => Some(header),
38+
FnKind::Method(_, sig, _, _) => Some(&sig.header),
39+
FnKind::Closure(_) => None,
40+
}
41+
}
42+
}
43+
3444
/// Each method of the Visitor trait is a hook to be potentially
3545
/// overridden. Each method's default implementation recursively visits
3646
/// the substructure of the input via the corresponding `walk` method;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#![crate_type="lib"]
22

33
pub unsafe extern "C" fn test(_: i32, ap: ...) { }
4-
//~^ C-varaidic functions are unstable
4+
//~^ C-variadic functions are unstable

src/test/ui/feature-gate/feature-gate-c_variadic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: C-varaidic functions are unstable
1+
error[E0658]: C-variadic functions are unstable
22
--> $DIR/feature-gate-c_variadic.rs:3:1
33
|
44
LL | pub unsafe extern "C" fn test(_: i32, ap: ...) { }

src/test/ui/feature-gates/feature-gate-async-await.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
#![feature(futures_api)]
44

5+
struct S;
6+
7+
impl S {
8+
async fn foo() {} //~ ERROR async fn is unstable
9+
}
10+
11+
trait T {
12+
async fn foo(); //~ ERROR trait fns cannot be declared `async`
13+
//~^ ERROR async fn is unstable
14+
}
15+
516
async fn foo() {} //~ ERROR async fn is unstable
617

718
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
error[E0706]: trait fns cannot be declared `async`
2+
--> $DIR/feature-gate-async-await.rs:12:5
3+
|
4+
LL | async fn foo();
5+
| ^^^^^^^^^^^^^^^
6+
7+
error[E0658]: async fn is unstable
8+
--> $DIR/feature-gate-async-await.rs:8:5
9+
|
10+
LL | async fn foo() {}
11+
| ^^^^^^^^^^^^^^^^^
12+
|
13+
= note: for more information, see https://github.com/rust-lang/rust/issues/50547
14+
= help: add #![feature(async_await)] to the crate attributes to enable
15+
16+
error[E0658]: async fn is unstable
17+
--> $DIR/feature-gate-async-await.rs:12:5
18+
|
19+
LL | async fn foo();
20+
| ^^^^^^^^^^^^^^^
21+
|
22+
= note: for more information, see https://github.com/rust-lang/rust/issues/50547
23+
= help: add #![feature(async_await)] to the crate attributes to enable
24+
125
error[E0658]: async fn is unstable
2-
--> $DIR/feature-gate-async-await.rs:5:1
26+
--> $DIR/feature-gate-async-await.rs:16:1
327
|
428
LL | async fn foo() {}
529
| ^^^^^^^^^^^^^^^^^
@@ -8,7 +32,7 @@ LL | async fn foo() {}
832
= help: add #![feature(async_await)] to the crate attributes to enable
933

1034
error[E0658]: async blocks are unstable
11-
--> $DIR/feature-gate-async-await.rs:8:13
35+
--> $DIR/feature-gate-async-await.rs:19:13
1236
|
1337
LL | let _ = async {};
1438
| ^^^^^^^^
@@ -17,14 +41,15 @@ LL | let _ = async {};
1741
= help: add #![feature(async_await)] to the crate attributes to enable
1842

1943
error[E0658]: async closures are unstable
20-
--> $DIR/feature-gate-async-await.rs:9:13
44+
--> $DIR/feature-gate-async-await.rs:20:13
2145
|
2246
LL | let _ = async || {};
2347
| ^^^^^^^^^^^
2448
|
2549
= note: for more information, see https://github.com/rust-lang/rust/issues/50547
2650
= help: add #![feature(async_await)] to the crate attributes to enable
2751

28-
error: aborting due to 3 previous errors
52+
error: aborting due to 6 previous errors
2953

30-
For more information about this error, try `rustc --explain E0658`.
54+
Some errors occurred: E0658, E0706.
55+
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)