Skip to content

inaccurate const drop error doesn't mention #![feature(const_precise_live_drops)] #79166

Open
@meithecatte

Description

@meithecatte
struct Test<T>(T);

impl<T> Test<T> {
    const fn unpack(self) -> T {
        self.0
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0493]: destructors cannot be evaluated at compile-time
 --> src/lib.rs:4:21
  |
4 |     const fn unpack(self) -> T {
  |                     ^^^^ constant functions cannot evaluate destructors
5 |         self.0
6 |     }
  |     - value is dropped here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0493`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

However, no destructors are actually ran, as can be seen in the MIR output:

fn <impl at src/lib.rs:3:1: 7:2>::unpack(_1: Test<T>) -> T {
    debug self => _1;                    // in scope 0 at src/lib.rs:4:15: 4:19
    let mut _0: T;                       // return place in scope 0 at src/lib.rs:4:24: 4:25

    bb0: {
        _0 = move (_1.0: T);             // scope 0 at src/lib.rs:5:9: 5:15
        return;                          // scope 0 at src/lib.rs:6:6: 6:6
    }
}

The same effect can be observed if the field access is replaced by pattern matching, either in let or match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-destructorsArea: Destructors (`Drop`, …)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions