Skip to content
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

Unhelpful "constants cannot evaluate destructors" with loops #72907

Closed
RalfJung opened this issue Jun 2, 2020 · 2 comments · Fixed by #73515
Closed

Unhelpful "constants cannot evaluate destructors" with loops #72907

RalfJung opened this issue Jun 2, 2020 · 2 comments · Fixed by #73515
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Jun 2, 2020

This code:

#![feature(const_if_match)]
#![feature(const_loop)]

const _: Option<Vec<i32>> = {
    let mut never_returned = Some(Vec::new());
    let mut always_returned = None;

    let mut i = 0;
    loop {
        always_returned = never_returned;
        never_returned = None;

        i += 1;
        if i == 10 {
            break always_returned;
        }
    
    }
};

yields the following error:

error[E0493]: destructors cannot be evaluated at compile-time
 --> src/lib.rs:7:9
  |
7 |     let mut always_returned = None;
  |         ^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors

error: aborting due to previous error

That is not very helpful as there is nothing with a destructor on that line. The actual line where something could get dropped is line 11, and that is also the line where the drop-requiring thing is assigned into always_returned.

Cc @rust-lang/wg-const-eval

@jonas-schievink jonas-schievink added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. F-const_if_match requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 2, 2020
@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Jun 2, 2020

This isn't specific to loops and conditionals.

Historically, we have emitted live drop errors at the initial definition of a local because the span of the offending Drop terminator is often a single closing brace. It would probably be better to emit a two-part error message that has the place where the local is defined as well as where it is dropped. Perhaps we omit the "where it was dropped" part when the span is a single byte long?

@ecstatic-morse ecstatic-morse removed F-const_if_match requires-nightly This issue requires a nightly compiler in some way. labels Jun 2, 2020
@RalfJung
Copy link
Member Author

RalfJung commented Jun 2, 2020

It would probably be better to emit a two-part error message that has the place where the local is defined as well as where it is dropped.

Yeah that sounds good.

Manishearth added a commit to Manishearth/rust that referenced this issue Jun 22, 2020
…s, r=oli-obk

Add second message for LiveDrop errors

This is an attempt to fix rust-lang#72907 by adding a second message to the `LiveDrop` diagnostics. Changing from this
```
error[E0493]: destructors cannot be evaluated at compile-time
 --> src/lib.rs:7:9
  |
7 |     let mut always_returned = None;
  |         ^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors

error: aborting due to previous error
```
to this
```
error[E0493]: destructors cannot be evaluated at compile-time
  --> foo.rs:6:9
   |
6  |     let mut always_returned = None;
   |         ^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors
...
10 |         always_returned = never_returned;
   |         --------------- value is dropped here

error: aborting due to previous error
```
r? @RalfJung @ecstatic-morse
@bors bors closed this as completed in 0f9a6ed Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants