Skip to content

Misleading diagnostic when implementing const Trait method #103040

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

Open
jhpratt opened this issue Oct 14, 2022 · 1 comment
Open

Misleading diagnostic when implementing const Trait method #103040

jhpratt opened this issue Oct 14, 2022 · 1 comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-const_trait_impl `#![feature(const_trait_impl)]` 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.

Comments

@jhpratt
Copy link
Member

jhpratt commented Oct 14, 2022

(playground)

#![feature(const_trait_impl, const_cmp)]

pub struct MyInstant(pub std::time::Instant);

impl const PartialEq<MyInstant> for std::time::Instant {
    fn eq(&self, rhs: &MyInstant) -> bool {
        self.eq(&rhs.0)
    }
}
error[E0308]: mismatched types
 --> src/lib.rs:7:17
  |
7 |         self.eq(&rhs.0)
  |              -- ^^^^^^ expected struct `MyInstant`, found struct `Instant`
  |              |
  |              arguments to this function are incorrect
  |
  = note: expected reference `&MyInstant`
             found reference `&Instant`
note: associated function defined here

This error is technically correct, as the only const eq method on std::time::Instant is the one being written. However, it is quite misleading. &Instant is the type that we want to accept as a parameter, with one caveat. Instant has to implement const PartialEq. It doesn't so the code shouldn't compile, but with a significantly different error message — ideally one pointing to that fact.

I have deliberately chosen Instant for this example because it is unlikely to ever implement const PartialEq. As such it can be used in tests without much concern.

For reference, the diagnostic is pretty much as expected (although still a bit suboptimal in my opinion) when not writing a trait method with the same name. (playground)

#![feature(const_trait_impl, const_cmp)]

pub struct MyInstant(pub std::time::Instant);

pub const fn check_equality(mine: MyInstant, theirs: std::time::Instant) -> bool {
    theirs.eq(&mine.0)
}
error[E0277]: can't compare `Instant` with `_` in const contexts
 --> src/lib.rs:6:15
  |
6 |     theirs.eq(&mine.0)
  |            -- ^^^^^^^ no implementation for `Instant == _`
  |            |
  |            required by a bound introduced by this call
  |
  = help: the trait `~const PartialEq<_>` is not implemented for `Instant`
note: the trait `PartialEq<_>` is implemented for `Instant`, but that implementation is not `const`
 --> src/lib.rs:6:15
  |
6 |     theirs.eq(&mine.0)
  |               ^^^^^^^

@rustbot label +A-const-fn +A-diagnostics +D-confusing +F-const-trait-impl +T-compiler +requires-nightly

@Rageking8
Copy link
Contributor

Rageking8 commented Oct 14, 2022

@rustbot label +A-diagnostics +D-confusing +F-const_trait_impl +T-compiler +requires-nightly +A-const-fn

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. F-const_trait_impl `#![feature(const_trait_impl)]` 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. A-const-fn labels Oct 14, 2022
@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed A-const-fn labels Dec 1, 2024
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. F-const_trait_impl `#![feature(const_trait_impl)]` 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.
Projects
None yet
Development

No branches or pull requests

4 participants