Skip to content

Commit 44ec846

Browse files
committed
Auto merge of #85353 - jonas-schievink:async-blocks-in-ctfe, r=oli-obk
Allow `async {}` expressions in const contexts Gated behind a new `const_async_blocks` feature.
2 parents 3396a38 + 014e8d4 commit 44ec846

File tree

10 files changed

+75
-19
lines changed

10 files changed

+75
-19
lines changed

compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ declare_features! (
650650
/// Allows unsizing coercions in `const fn`.
651651
(active, const_fn_unsize, "1.53.0", Some(64992), None),
652652

653+
/// Allows `async {}` expressions in const contexts.
654+
(active, const_async_blocks, "1.53.0", Some(85368), None),
655+
653656
/// Allows using imported `main` function
654657
(active, imported_main, "1.53.0", Some(28937), None),
655658

compiler/rustc_mir/src/transform/check_consts/ops.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,20 @@ impl NonConstOp for FnPtrCast {
141141
pub struct Generator(pub hir::GeneratorKind);
142142
impl NonConstOp for Generator {
143143
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
144-
Status::Forbidden
144+
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
145+
Status::Unstable(sym::const_async_blocks)
146+
} else {
147+
Status::Forbidden
148+
}
145149
}
146150

147151
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
148152
let msg = format!("{}s are not allowed in {}s", self.0, ccx.const_kind());
149-
ccx.tcx.sess.struct_span_err(span, &msg)
153+
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
154+
feature_err(&ccx.tcx.sess.parse_sess, sym::const_async_blocks, span, &msg)
155+
} else {
156+
ccx.tcx.sess.struct_span_err(span, &msg)
157+
}
150158
}
151159
}
152160

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ symbols! {
374374
conservative_impl_trait,
375375
console,
376376
const_allocate,
377+
const_async_blocks,
377378
const_compare_raw_pointers,
378379
const_constructor,
379380
const_eval_limit,

src/test/ui/consts/async-block.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
// From <https://github.com/rust-lang/rust/issues/77361>
1+
// gate-test-const_async_blocks
22

33
// edition:2018
4+
// revisions: with_feature without_feature
5+
6+
#![feature(rustc_attrs)]
7+
#![cfg_attr(with_feature, feature(const_async_blocks))]
8+
9+
use std::future::Future;
410

11+
// From <https://github.com/rust-lang/rust/issues/77361>
512
const _: i32 = { core::mem::ManuallyDrop::new(async { 0 }); 4 };
6-
//~^ `async` block
13+
//[without_feature]~^ `async` block
14+
15+
static _FUT: &(dyn Future<Output = ()> + Sync) = &async {};
16+
//[without_feature]~^ `async` block
717

8-
fn main() {}
18+
#[rustc_error]
19+
fn main() {} //[with_feature]~ fatal error triggered by #[rustc_error]

src/test/ui/consts/async-block.stderr

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: fatal error triggered by #[rustc_error]
2+
--> $DIR/async-block.rs:19:1
3+
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: `async` blocks are not allowed in constants
2+
--> $DIR/async-block.rs:12:47
3+
|
4+
LL | const _: i32 = { core::mem::ManuallyDrop::new(async { 0 }); 4 };
5+
| ^^^^^^^^^^^
6+
|
7+
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
8+
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable
9+
10+
error[E0658]: `async` blocks are not allowed in statics
11+
--> $DIR/async-block.rs:15:51
12+
|
13+
LL | static _FUT: &(dyn Future<Output = ()> + Sync) = &async {};
14+
| ^^^^^^^^
15+
|
16+
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
17+
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/impl-trait/issues/issue-78721.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ LL | #![feature(impl_trait_in_bindings)]
77
= note: `#[warn(incomplete_features)]` on by default
88
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information
99

10-
error: `async` blocks are not allowed in constants
10+
error[E0658]: `async` blocks are not allowed in constants
1111
--> $DIR/issue-78721.rs:8:57
1212
|
1313
LL | let f: impl core::future::Future<Output = u8> = async { 1 };
1414
| ^^^^^^^^^^^
15+
|
16+
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
17+
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable
1518

1619
error[E0493]: destructors cannot be evaluated at compile-time
1720
--> $DIR/issue-78721.rs:8:13
@@ -24,4 +27,5 @@ LL | }],
2427

2528
error: aborting due to 2 previous errors; 1 warning emitted
2629

27-
For more information about this error, try `rustc --explain E0493`.
30+
Some errors have detailed explanations: E0493, E0658.
31+
For more information about an error, try `rustc --explain E0493`.

src/test/ui/impl-trait/issues/issue-78722.full_tait.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ LL | #![feature(impl_trait_in_bindings)]
1515
|
1616
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information
1717

18-
error: `async` blocks are not allowed in constants
18+
error[E0658]: `async` blocks are not allowed in constants
1919
--> $DIR/issue-78722.rs:17:20
2020
|
2121
LL | let f: F = async { 1 };
2222
| ^^^^^^^^^^^
23+
|
24+
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
25+
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable
2326

2427
error[E0493]: destructors cannot be evaluated at compile-time
2528
--> $DIR/issue-78722.rs:17:13
@@ -32,4 +35,5 @@ LL | }],
3235

3336
error: aborting due to 2 previous errors; 2 warnings emitted
3437

35-
For more information about this error, try `rustc --explain E0493`.
38+
Some errors have detailed explanations: E0493, E0658.
39+
For more information about an error, try `rustc --explain E0493`.

src/test/ui/impl-trait/issues/issue-78722.min_tait.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ LL | #![feature(impl_trait_in_bindings)]
77
= note: `#[warn(incomplete_features)]` on by default
88
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information
99

10-
error: `async` blocks are not allowed in constants
10+
error[E0658]: `async` blocks are not allowed in constants
1111
--> $DIR/issue-78722.rs:17:20
1212
|
1313
LL | let f: F = async { 1 };
1414
| ^^^^^^^^^^^
15+
|
16+
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
17+
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable
1518

1619
error[E0493]: destructors cannot be evaluated at compile-time
1720
--> $DIR/issue-78722.rs:17:13
@@ -24,4 +27,5 @@ LL | }],
2427

2528
error: aborting due to 2 previous errors; 1 warning emitted
2629

27-
For more information about this error, try `rustc --explain E0493`.
30+
Some errors have detailed explanations: E0493, E0658.
31+
For more information about an error, try `rustc --explain E0493`.

0 commit comments

Comments
 (0)