rust-lang / rust-lang/rust

Const generic equivalence check overrides diagnostic::on_unimplemented

Open
#134,881 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics F-generic_const_exprs P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#![feature(generic_const_exprs)]

trait Trait {
    const VAL: usize;
}

struct A;
impl Trait for A {
    const VAL: usize = 1;
}

struct B;
impl Trait for B {
    const VAL: usize = 2;
}

#[diagnostic::on_unimplemented(
    message = "`{Self}` is not equivalent to `{T}`",
)]
trait Equivalent<T: Trait>: IsEquivalent<T, true> {}
impl<A: Trait + IsEquivalent<B, true>, B: Trait> Equivalent<B> for A {}

trait IsEquivalent<T: Trait, const EQUIVALENT: bool>: Trait {}
impl<A: Trait, B: Trait> IsEquivalent<A, {A::VAL == B::VAL}> for B {}

fn check_equivalent<A: Trait + Equivalent<B>, B: Trait>(_a: &A, _b: &B) {}

fn main() {
    check_equivalent(&A, &A); // works as expected
    check_equivalent(&B, &B); // works as expected
    
    check_equivalent(&A, &B);
 // ^^^^^^^^^^^^^^^^^^^^^^^^ expected `true`, found `false`
}
Current output
error[E0308]: mismatched types
  --> src/main.rs:32:5
   |
32 |     check_equivalent(&A, &B);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ expected `true`, found `false`
   |
   = note: expected constant `true`
              found constant `false`
Desired output
--> src/main.rs:32:5
   |
32 |     check_equivalent(&A, &B);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ `A` is not equivalent to `B`
Rationale and extra context

In the given example, the A does not implement the trait Equivalent<B> and there is a custom #[diagnostic::on_unimplemented] message. However, the diagnostic message is not shown, instead the mismatch between true and false const generics in the implementation of the equivalence check is leaked.

Other cases

Rust Version
1.85-nightly 2024-12-28 8742e0556dee3c64f714 (on Rust playground)
Anything else?

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=eaf4f6a4a8d5336e43446dc1d0e9b575

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided generic_const_exprs example and compare the const-generic mismatch diagnostic with the expected diagnostic::on_unimplemented message. Trace how the Equivalent obligation and the true/false const-generic mismatch are diagnosed. Done means the failing call reports that A is not equivalent to B instead of exposing the const-generic mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.