Skip to content

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

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
jruderman opened this issue Oct 19, 2022 · 0 comments · Fixed by #103328
Closed

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

jruderman opened this issue Oct 19, 2022 · 0 comments · Fixed by #103328
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints F-const_trait_impl `#![feature(const_trait_impl)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

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.
@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2022
@oli-obk oli-obk added A-const-fn F-const_trait_impl `#![feature(const_trait_impl)]` labels Oct 19, 2022
notriddle added a commit to notriddle/rust that referenced this issue Oct 21, 2022
…t-sugg, r=jackh726

Do not suggest trivially false const predicates

Pass through constness to `predicate_can_apply` and don't suggest other impls if it's satisfied but not const.

Fixes rust-lang#103267
@bors bors closed this as completed in 1a07742 Oct 23, 2022
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
# 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-diagnostics Area: Messages for errors, warnings, and lints F-const_trait_impl `#![feature(const_trait_impl)]` 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