Skip to content

The derive macro did not support generics well #147

@tguichaoua

Description

@tguichaoua

The following code did not compile because the compiler says the type T did not implement the valuable trait. It seems the macro did not add the trait bound when implementing the trait.

The workaround is to put the trait bound on the type definition, but it's not really idiomatic, as it should be put on the impl block.

#[derive(Valuable)]
struct Foo<T> {
    x: T,
}

#[derive(Valuable)]
enum Goo<T> {
    Goo(T),
}
The compiler error

error[E0277]: the trait bound `T: Valuable` is not satisfied
 --> src\main.rs:9:8
  |
7 | #[derive(Valuable)]
  |          -------- required by a bound introduced by this call
8 | struct Foo<T> {
9 |     x: T,
  |        ^ the trait `Valuable` is not implemented for `T`
  |
help: consider restricting type parameter `T` with trait `Valuable`
  |
8 | struct Foo<T: valuable::Valuable> {
  |             ++++++++++++++++++++

error[E0277]: the trait bound `T: Valuable` is not satisfied
  --> src\main.rs:14:9
   |
12 | #[derive(Valuable)]
   |          -------- required by a bound introduced by this call
13 | enum Goo<T> {
14 |     Goo(T),
   |         ^ the trait `Valuable` is not implemented for `T`
   |
   = note: required for `&T` to implement `Valuable`
help: consider restricting type parameter `T` with trait `Valuable`
   |
13 | enum Goo<T: valuable::Valuable> {
   |           ++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions