Skip to content

rustc refuses to compile const function due to potential drops even when nothing is being dropped #115403

New issue

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

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

Already on GitHub? # to your account

Closed
TheNeikos opened this issue Aug 31, 2023 · 2 comments · Fixed by #115527
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-destructors Area: Destructors (`Drop`, …) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@TheNeikos
Copy link
Contributor

I tried this code:

use core::marker::PhantomData;
use std::sync::Arc;
struct Foo<T> {
    drop_value: Arc<u32>,
    pd: PhantomData<fn(T)>,
}

impl<T> Foo<T> {
    const fn convert<R>(self) -> Foo<R> {
        let Foo { drop_value, pd: _ } = self;

        Foo {
            drop_value,
            pd: PhantomData,
        }
    }
    
    const fn convert2<R>(self) -> Foo<R> {
        Foo {
            drop_value: self.drop_value,
            pd: PhantomData,
        }
    }
}

I expected to see this happen: It compiles

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error[[E0493]](https://doc.rust-lang.org/nightly/error_codes/E0493.html): destructor of `Foo<T>` cannot be evaluated at compile-time
  --> src/lib.rs:9:25
   |
9  |     const fn convert<R>(self) -> Foo<R> {
   |                         ^^^^ the destructor for this type cannot be evaluated in constant functions
...
16 |     }
   |     - value is dropped here

For more information about this error, try `rustc --explain E0493`.
error: could not compile `playground` (lib) due to previous error

Meta

This errors on stable 1.72 as well as on nightly-2023-08-30.

This is maybe the same as #109427, but since that is about patterns, and this is not, I thought the independent report is justified.

@TheNeikos TheNeikos added the C-bug Category: This is a bug. label Aug 31, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 31, 2023
@TheNeikos
Copy link
Contributor Author

TheNeikos commented Aug 31, 2023

This will be fixed by: #73255

Adding #![feature(const_precise_live_drops)] makes it compile as expected.

@itsxaos
Copy link

itsxaos commented Aug 31, 2023

I have a similar issue. My crate construe compiles on stable 1.72 but fails with this error on beta & nightly.

I've removed everything not needed to reproduce the error and put it in a playground here:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6d368dce7e0476b4243d8295ae24e5b2

@fmease fmease added A-destructors Area: Destructors (`Drop`, …) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 6, 2023
@bors bors closed this as completed in 1e746d7 Sep 7, 2023
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Sep 12, 2023
Don't require `Drop` for `[PhantomData<T>; N]` where `N` and `T` are generic, if `T` requires `Drop`

fixes rust-lang/rust#115403
fixes rust-lang/rust#115410

This was accidentally regressed in rust-lang/rust#114134, because it was accidentally stabilized in #102204 (cc `@rust-lang/lang,` seems like an innocent stabilization, considering this PR is more of a bugfix than a feature).

While we have a whole month to beta backport this change before the regression hits stable, I'd still prefer not to go through an FCP on this PR (which fixes a regression), if T-lang wants an FCP, I can can open an issue about the change itself.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-destructors Area: Destructors (`Drop`, …) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants