Skip to content

Poor diagnostic when passing T: Drop (nonconst only) to a function that takes ~const Drop in an inline const  #92713

Closed
@PatchMixolydic

Description

@PatchMixolydic

Given the following code (playground):

#![allow(unused)]
#![feature(const_fn_trait_bound, const_trait_impl, inline_const)]

const fn f<T: ~const Drop>(x: T) {}

struct UnconstDrop;

impl Drop for UnconstDrop {
    fn drop(&mut self) {}
}

fn main() {
    const {
        f(UnconstDrop);
    }
}

The current output is:

error[E0080]: evaluation of `main::{constant#0}::<()>` failed
    |
   ::: src/main.rs:4:35
    |
4   | const fn f<T: ~const Drop>(x: T) {}
    |                                   - inside `f::<UnconstDrop>` at src/main.rs:4:35
...
14  |         f(UnconstDrop);
    |         -------------- inside `main::{constant#0}::<()>` at src/main.rs:14:9

Ideally, the output should be the same as when such a call is made in a const item:

error[E0277]: the trait bound `UnconstDrop: Drop` is not satisfied
  --> src/lib.rs:14:11
   |
14 |         f(UnconstDrop);
   |         - ^^^^^^^^^^^ the trait `Drop` is not implemented for `UnconstDrop`
   |         |
   |         required by a bound introduced by this call
   |
note: required by a bound in `f`
  --> src/lib.rs:4:15
   |
4  | const fn f<T: ~const Drop>(x: T) {}
   |               ^^^^^^^^^^^ required by this bound in `f`

... which can be improved further with the suggestion given in #92712:

error[E0277]: the trait bound `UnconstDrop: const Drop` is not satisfied
  --> src/lib.rs:14:11
   |
14 |         f(UnconstDrop);
   |         - ^^^^^^^^^^^ the trait `Drop` is not implemented for `UnconstDrop` in const contexts
   |         |
   |         required by a bound introduced by this call
   |
note: required by a bound in `f`
  --> src/lib.rs:4:15
   |
4  | const fn f<T: ~const Drop>(x: T) {}
   |               ^^^^^^^^^^^ required by this bound in `f`
   |
help: in const contexts, `~const` trait bounds only accept `const` trait impls
help: for more information, see tracking issue #67792/the Rust Reference

@rustbot modify labels: +A-const-eval +A-traits +D-confusing +D-terse +F-const_trait_impl +F-inline_const

Metadata

Metadata

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-const_trait_impl`#![feature(const_trait_impl)]`F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)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