Skip to content

Commit

Permalink
Allow async {} expressions in const contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-schievink committed May 16, 2021
1 parent 8cf990c commit bd16825
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 19 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
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", None, 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
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
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
@@ -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
@@ -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

19 changes: 19 additions & 0 deletions src/test/ui/consts/async-block.without_feature.stderr
@@ -0,0 +1,19 @@
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 };
| ^^^^^^^^^^^
|
= 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 {};
| ^^^^^^^^
|
= 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`.
7 changes: 5 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78721.stderr
Expand Up @@ -7,11 +7,13 @@ 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 };
| ^^^^^^^^^^^
|
= 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 +26,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`.
7 changes: 5 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78722.full_tait.stderr
Expand Up @@ -15,11 +15,13 @@ 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 };
| ^^^^^^^^^^^
|
= 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 +34,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`.
7 changes: 5 additions & 2 deletions src/test/ui/impl-trait/issues/issue-78722.min_tait.stderr
Expand Up @@ -7,11 +7,13 @@ 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 };
| ^^^^^^^^^^^
|
= 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 +26,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`.

0 comments on commit bd16825

Please sign in to comment.