rust-lang / rust-lang/rust

Unrelated error when a trait is not implemented

Open
#148,759 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-trait-system fixed-by-next-solver T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use core::marker::PhantomData;
use core::ops::Add;

struct Matrix<T> {
    _phantom: PhantomData<T>,
}

fn entrywise_op<L, R, LR>(f: fn(L, R) -> LR) -> Matrix<LR> {
    todo!()
}

impl<'u, U, T> Add<&'u Matrix<U>> for Matrix<T> where T: Add<&'u U> {
    type Output = Matrix<<T as Add<&'u U>>::Output>;

    fn add(self, rhs: &'u Matrix<U>) -> Self::Output {
        entrywise_op(<&'u T as Add<U>>::add)
        //^ should be <T as Add<&'u U>::add
    }
}

impl<'t, 'u, U, T> Add<&'u Matrix<U>> for &'t Matrix<T> where &'t T: Add<&'u U> {
    type Output = Matrix<<&'t T as Add<&'u U>>::Output>;

    fn add(self, rhs: &'u Matrix<U>) -> Self::Output {
        todo!()
    }
}

I expected to have:

error[E0277]: cannot add `U` to `&'u T`
  --> src/lib.rs:16:22
   |
16 |         entrywise_op(<&'u T as Add<U>>::add)
   |                      ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `&'u T + U`
   |
   = help: the trait `Add<U>` is not implemented for `&'u T`
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
   |
12 | impl<'u, U, T> Add<&'u Matrix<U>> for Matrix<T> where T: Add<&'u U>, &'u T: Add<U> {
   |                                                                    +++++++++++++++

Instead, I got an error on a unrelated (?) impl:

error[E0275]: overflow evaluating the requirement `&Simd<_, _>: Add<&Simd<_, _>>`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`playground`)
note: required for `&Matrix<Simd<_, _>>` to implement `Add<&Matrix<Simd<_, _>>>`
  --> src/lib.rs:21:20
   |
21 | impl<'t, 'u, U, T> Add<&'u Matrix<U>> for &'t Matrix<T> where &'t T: Add<&'u U> {
   |                    ^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^              ---------- unsatisfied trait bound introduced here
   = note: 126 redundant requirements hidden
   = note: required for `&Matrix<Matrix<Matrix<Matrix<Matrix<Matrix<Matrix<Matrix<...>>>>>>>>` to implement `Add<&Matrix<Matrix<Matrix<Matrix<Matrix<Matrix<Matrix<...>>>>>>>>`
   = note: the full name for the type has been written to '/playground/target/debug/deps/playground-3eac006988d30169.long-type-16970046461422569834.txt'
   = note: consider using `--verbose` to print the full type name to the console

For more information about this error, try `rustc --explain E0275`.
error: could not compile `playground` (lib) due to 1 previous error

Removing the 2nd impl gives the expected error.


@rustbot label +A-diagnostics +A-trait-system +T-compiler

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 compiling the provided reproducer in src/lib.rs, especially the expressions at lines 16 and 21, and compare the diagnostics with the second impl removed. Investigate the compiler's trait-system diagnostic path and make the reported error identify the invalid requirement in the first impl rather than surfacing an unrelated overflow from the second impl.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.