Rust: `async` blocks in a const-context error on nightly, but not on stable

Created on 30 Sep 2020  路  10Comments  路  Source: rust-lang/rust

The following code compiles on stable, but not on nightly. I know this is realy bogus code, and I'm not sure it should be rejected:

fn regress() -> [(); { core::mem::ManuallyDrop::new(async { 0 }); 4 }] {
    todo!()
}

On nightly, the error message is:

error: `from_generator` is not yet stable as a const fn
 --> src/lib.rs:2:60
  |
2 | fn regress() -> [(); { core::mem::ManuallyDrop::new(async { 0 }); 4 }] {
  |                                                            ^^^^^^^^^
  |
  = help: add `#![feature(gen_future)]` to the crate attributes to enable

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.
A-async-await A-const-eval A-stability AsyncAwait-Triaged P-medium requires-nightly

Most helpful comment

searched nightlies: from nightly-2020-08-20 to nightly-2020-09-30
regressed nightly: nightly-2020-09-24
searched commits: from https://github.com/rust-lang/rust/commit/0da58007451a154da2480160429e1604a1f5f0ec to https://github.com/rust-lang/rust/commit/8b4085359ae798dedb05c95ad42520557bd25320
regressed commit: https://github.com/rust-lang/rust/commit/f6d59207ad56e24a2bfefa3544de48a0f6491363


bisected with cargo-bisect-rustc v0.5.2

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc 2020-08-20 --end 2020-09-30 --preserve --regress=error --without-cargo 


That's #76850

All 10 comments

cc @ecstatic-morse in case this is related to what you were doing recently

searched nightlies: from nightly-2020-08-20 to nightly-2020-09-30
regressed nightly: nightly-2020-09-24
searched commits: from https://github.com/rust-lang/rust/commit/0da58007451a154da2480160429e1604a1f5f0ec to https://github.com/rust-lang/rust/commit/8b4085359ae798dedb05c95ad42520557bd25320
regressed commit: https://github.com/rust-lang/rust/commit/f6d59207ad56e24a2bfefa3544de48a0f6491363


bisected with cargo-bisect-rustc v0.5.2

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc 2020-08-20 --end 2020-09-30 --preserve --regress=error --without-cargo 


That's #76850

cc @rust-lang/wg-const-eval This code was allowed erroneously; async-await has not been stabilized in a const-context. I believe we have exceptions to the backwards compatibility rules for cases like this? Actually doing anything with the async block (.await, dropping it) will cause an error during const-checking, so this is useless as far as I know.

This also works in a regular const initializer:

const _: i32 = { core::mem::ManuallyDrop::new(async { 0 }); 4 };

Hmm, the span in the error shown above seems wrong:

error: `from_generator` is not yet stable as a const fn
 --> src/lib.rs:2:60
  |
2 | fn regress() -> [(); { core::mem::ManuallyDrop::new(async { 0 }); 4 }] {
  |                                                            ^^^^^^^^^
  |
  = help: add `#![feature(gen_future)]` to the crate attributes to enable

It's pointing at the section from the space before the 0 to the space after the 4? But the error from putting it into a fresh playground looks correct:

error: `from_generator` is not yet stable as a const fn
 --> src/lib.rs:1:53
  |
1 | fn regress() -> [(); { core::mem::ManuallyDrop::new(async { 0 }); 4 }] {
  |                                                     ^^^^^^^^^^^
  |
  = help: add `#![feature(gen_future)]` to the crate attributes to enable

@DutchGhost is that how the error appeared to you when you ran into it?

Temporarily assigning P-medium per the prioritization working group discussion.

Yea this was never intended to get stabilized and since it's only possible in code that doesn't actually do anything, this is unlikely to occur in practice. I don't see a reasonable way to allow this. Note the same effect is possible with std::mem::forget.

So should we remove regression-from-stable-to-nightly? It sounds like it's not a regression; it's a bug fix.

I agree that this falls under "accidental stabilization", and is not a regression.

This also works in a regular const initializer:

Still works, or used to work? If it still works that arguably is a bug,

The second. You can try it in the playground.

I'm assigning myself to give a better error message in these cases, but this will remain an error going forward.

Since the PR improving the error message for cases like this has been merged, this issue can be closed?

Was this page helpful?
0 / 5 - 0 ratings