Skip to content

"Can't drop in const context" suggests adding a trivially false where clause #103267

Closed
@jruderman

Description

@jruderman

Given the following code (playground):

#![feature(const_trait_impl)]
const fn trash<T>(_: T)
where
    T: ~const std::marker::Destruct,
{
}

fn main() {
    const HELLO: String = "Hello".to_string();
    const F_RESULT: () = trash(HELLO);
}

The current output is:

error[E0277]: can't drop `Vec<u8>` in const contexts
  --> src/main.rs:10:32
   |
10 |     const F_RESULT: () = trash(HELLO);
   |                          ----- ^^^^^ within `String`, the trait `~const Destruct` is not implemented for `Vec<u8>`
   |                          |
   |                          required by a bound introduced by this call
   |
note: the trait `Destruct` is implemented for `Vec<u8>`, but that implementation is not `const`
  --> src/main.rs:10:32
   |
10 |     const F_RESULT: () = trash(HELLO);
   |                                ^^^^^
   = note: required because it appears within the type `String`
note: required by a bound in `trash`
  --> src/main.rs:4:8
   |
2  | const fn trash<T>(_: T)
   |          ----- required by a bound in this
3  | where
4  |     T: ~const std::marker::Destruct,
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `trash`
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
   |
8  | fn main() where Vec<u8>: ~const Destruct {
   |           ++++++++++++++++++++++++++++++

The suggestion at the end is nonsense for multiple reasons:

  • main cannot have a where clause.
  • The suggestion introduces a where clause with a trivial bound, i.e. one that does not depend on any type or lifetime parameters). This is an unstable feature.
  • The bound is false and nothing this file does can make it true, because Vec<u8> and Destruct are defined in other files.
  • Destruct is not in scope; it needs to be written as std::marker::Destruct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsF-const_trait_impl`#![feature(const_trait_impl)]`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