Skip to content

Unrelated error when a trait is not implemented #148759

@ShE3py

Description

@ShE3py

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions