Skip to content

Trait bound not satisfied #135076

@reenigneEsrever92

Description

@reenigneEsrever92

Code

pub trait Backend {}

pub struct Shape<const D: usize>([usize; D]);

pub struct Tensor<B: Backend, const D: usize, T> {
    backend: B,
    shape: Shape<D>,
    data: Vec<T>,
}

impl<B: Backend, const D: usize, T> Tensor<B, D, T> {
    pub fn empty(backend: B, shape: impl Into<Shape<D>>) -> Tensor<B, D, T> {
        let shape: Shape<D> = shape.into();
        let size = shape.0.iter().product();

        Tensor {
            backend,
            shape,
            data: Vec::with_capacity(size),
        }
    }

    pub fn as_slice(&self) -> &[T] {
        &self.data.as_slice()
    }
}

impl<const D: usize> From<[usize; D]> for Shape<D> {
    fn from(value: [usize; D]) -> Self {
        Shape(value)
    }
}

impl<const D: usize> From<u64> for Shape<D> {
    fn from(value: u64) -> Self {
        Shape([value as usize; D])
    }
}

pub trait Op {}

pub trait ConvImpl<B: Backend, const D: usize, const D2: usize, T> {
    fn conv(
        &self,
        tensor: &Tensor<B, D, T>,
        weights: &Tensor<B, D2, T>,
        stride: Shape<D2>,
    ) -> Tensor<B, D, T>;
}

pub trait Conv<B: Backend, const D: usize, const D2: usize, T> {
    fn conv(&self, weights: &Tensor<B, D2, T>, stride: impl Into<Shape<D>>) -> Tensor<B, D, T>;
}

impl<B: Backend + ConvImpl<B, D, D2, T>, const D: usize, const D2: usize, T> Conv<B, D, D2, T>
    for Tensor<B, D, T>
{
    fn conv(&self, weights: &Tensor<B, D2, T>, stride: impl Into<Shape<D2>>) -> Tensor<B, D, T> {
        self.backend.conv(&self, &weights, stride.into())
    }
}

Current output

error[E0277]: the trait bound `Shape<D2>: From<impl Into<Shape<D2>>>` is not satisfied
  --> crates/afterburner-core/src/lib.rs:58:61
   |
58 |     fn conv(&self, weights: &Tensor<B, D2, T>, stride: impl Into<Shape<D2>>) -> Tensor<B, D, T> {
   |                                                             ^^^^^^^^^^^^^^^ the trait `From<impl Into<Shape<D2>>>` is not implemented for `Shape<D2>`, which is required by `impl Into<Shape<D2>>: Into<Shape<D2>>`
   |
   = note: required for `impl Into<Shape<D2>>` to implement `Into<Shape<D2>>`
note: the requirement `impl Into<Shape<D2>>: Into<Shape<D2>>` appears on the `impl`'s method `conv` but not on the corresponding trait's method
  --> crates/afterburner-core/src/lib.rs:52:8
   |
51 | pub trait Conv<B: Backend, const D: usize, const D2: usize, T> {
   |           ---- in this trait
52 |     fn conv(&self, weights: &Tensor<B, D2, T>, stride: impl Into<Shape<D>>) -> Tensor<B, D, T>;
   |        ^^^^ this trait's method doesn't have the requirement `impl Into<Shape<D2>>: Into<Shape<D2>>`

Desired output

note: the requirement `impl Into<Shape<D2>>: Into<Shape<D2>>` appears on the `impl`'s method `conv` but not on the corresponding trait's method
  --> crates/afterburner-core/src/lib.rs:52:8
   |
51 | pub trait Conv<B: Backend, const D: usize, const D2: usize, T> {
   |           ---- in this trait
52 |     fn conv(&self, weights: &Tensor<B, D2, T>, stride: impl Into<Shape<D>>) -> Tensor<B, D, T>;
   |        ^^^^ this trait's method doesn't have the requirement `impl Into<Shape<D2>>: Into<Shape<D2>>`

Rationale and extra context

I can't find a good reason why rust would tell me that the trait bound ain't satisfied. When in reality the impls and traits method signature differ. Changing them to make them similar resolves the error. The info section already gives the correct clue I would have expected this to be the actual error though instead of just a note.

Other cases

Rust Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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