Skip to content

Commit f7ffa06

Browse files
authored
Rollup merge of #60088 - varkor:async_await-method-feature-gate, r=cramertj
Feature gate async methods Fixes #60069.
2 parents 9e7c140 + edce367 commit f7ffa06

File tree

6 files changed

+53
-38
lines changed

6 files changed

+53
-38
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

+13-19
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

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;

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

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

33
#![feature(futures_api)]
44

5+
struct S;
6+
7+
impl S {
8+
async fn foo() {} //~ ERROR async fn is unstable
9+
}
10+
511
async fn foo() {} //~ ERROR async fn is unstable
612

713
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
error[E0658]: async fn is unstable
2-
--> $DIR/feature-gate-async-await.rs:5:1
2+
--> $DIR/feature-gate-async-await.rs:8:5
3+
|
4+
LL | async fn foo() {}
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/50547
8+
= help: add #![feature(async_await)] to the crate attributes to enable
9+
10+
error[E0658]: async fn is unstable
11+
--> $DIR/feature-gate-async-await.rs:11:1
312
|
413
LL | async fn foo() {}
514
| ^^^^^^^^^^^^^^^^^
@@ -8,7 +17,7 @@ LL | async fn foo() {}
817
= help: add #![feature(async_await)] to the crate attributes to enable
918

1019
error[E0658]: async blocks are unstable
11-
--> $DIR/feature-gate-async-await.rs:8:13
20+
--> $DIR/feature-gate-async-await.rs:14:13
1221
|
1322
LL | let _ = async {};
1423
| ^^^^^^^^
@@ -17,14 +26,14 @@ LL | let _ = async {};
1726
= help: add #![feature(async_await)] to the crate attributes to enable
1827

1928
error[E0658]: async closures are unstable
20-
--> $DIR/feature-gate-async-await.rs:9:13
29+
--> $DIR/feature-gate-async-await.rs:15:13
2130
|
2231
LL | let _ = async || {};
2332
| ^^^^^^^^^^^
2433
|
2534
= note: for more information, see https://github.com/rust-lang/rust/issues/50547
2635
= help: add #![feature(async_await)] to the crate attributes to enable
2736

28-
error: aborting due to 3 previous errors
37+
error: aborting due to 4 previous errors
2938

3039
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)