Skip to content

Allow async {} expressions in const contexts #85353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ declare_features! (
/// Allows unsizing coercions in `const fn`.
(active, const_fn_unsize, "1.53.0", Some(64992), None),

/// Allows `async {}` expressions in const contexts.
(active, const_async_blocks, "1.53.0", Some(85368), None),

/// Allows using imported `main` function
(active, imported_main, "1.53.0", Some(28937), None),

Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_mir/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,20 @@ impl NonConstOp for FnPtrCast {
pub struct Generator(pub hir::GeneratorKind);
impl NonConstOp for Generator {
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
Status::Forbidden
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
Status::Unstable(sym::const_async_blocks)
} else {
Status::Forbidden
}
}

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

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ symbols! {
conservative_impl_trait,
console,
const_allocate,
const_async_blocks,
const_compare_raw_pointers,
const_constructor,
const_eval_limit,
Expand Down
17 changes: 14 additions & 3 deletions src/test/ui/consts/async-block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// From <https://github.com/rust-lang/rust/issues/77361>
// gate-test-const_async_blocks

// edition:2018
// revisions: with_feature without_feature

#![feature(rustc_attrs)]
#![cfg_attr(with_feature, feature(const_async_blocks))]

use std::future::Future;

// From <https://github.com/rust-lang/rust/issues/77361>
const _: i32 = { core::mem::ManuallyDrop::new(async { 0 }); 4 };
//~^ `async` block
//[without_feature]~^ `async` block

static _FUT: &(dyn Future<Output = ()> + Sync) = &async {};
//[without_feature]~^ `async` block

fn main() {}
#[rustc_error]
fn main() {} //[with_feature]~ fatal error triggered by #[rustc_error]
8 changes: 0 additions & 8 deletions src/test/ui/consts/async-block.stderr

This file was deleted.

8 changes: 8 additions & 0 deletions src/test/ui/consts/async-block.with_feature.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: fatal error triggered by #[rustc_error]
--> $DIR/async-block.rs:19:1
|
LL | fn main() {}
| ^^^^^^^^^

error: aborting due to previous error

21 changes: 21 additions & 0 deletions src/test/ui/consts/async-block.without_feature.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0658]: `async` blocks are not allowed in constants
--> $DIR/async-block.rs:12:47
|
LL | const _: i32 = { core::mem::ManuallyDrop::new(async { 0 }); 4 };
| ^^^^^^^^^^^
|
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable

error[E0658]: `async` blocks are not allowed in statics
--> $DIR/async-block.rs:15:51
|
LL | static _FUT: &(dyn Future<Output = ()> + Sync) = &async {};
| ^^^^^^^^
|
= note: see issue #85368 <https://github.com/rust-lang/rust/issues/85368> for more information
= help: add `#![feature(const_async_blocks)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
8 changes: 6 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78721.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ LL | #![feature(impl_trait_in_bindings)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information

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

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

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

For more information about this error, try `rustc --explain E0493`.
Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.
8 changes: 6 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78722.full_tait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ LL | #![feature(impl_trait_in_bindings)]
|
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information

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

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

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

For more information about this error, try `rustc --explain E0493`.
Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.
8 changes: 6 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78722.min_tait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ LL | #![feature(impl_trait_in_bindings)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information

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

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

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

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