Skip to content

Rustc suggesting to add a already existing trait bound, instead of relaxing an entirely different one #98539

Closed
@onestacked

Description

@onestacked

I tried this code:

struct Victim<'a, T: Perpetrator + ?Sized>
where
  Self: Sized // rustc suggests adding this trait bound, even if it already exists, however this is absolutely not needed
{
  value: u8,
  perp: &'a T,
}

trait VictimTrait {
  type Ret;
  fn get(self) -> Self::Ret;
}

// Actual fix is here
impl<'a, T: Perpetrator /*+ ?Sized*/> VictimTrait for Victim<'a, T> {
  type Ret = u8;
  fn get(self) -> Self::Ret {
    self.value
  }
}

trait Perpetrator {
  fn getter<'a>(&'a self) -> Victim<'a, Self> {
    Victim {
      value: 0,
      perp: self,
    }
  }

  fn trigger(&self) {
    self.getter().get();
  }
}

This does not compile with the following error message:

error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): the method `get` exists for struct `Victim<'_, Self>`, but its trait bounds were not satisfied
  --> src/main.rs:31:19
   |
1  | / struct Victim<'a, T: Perpetrator + ?Sized>
2  | | where
3  | |   Self: Sized // rustc sugests adding this trait bound, even if it already exists
4  | | {
5  | |   value: u8,
6  | |   perp: &'a T,
7  | | }
   | | -
   | | |
   | |_method `get` not found for this
   |   doesn't satisfy `Victim<'_, Self>: VictimTrait`
...
31 |       self.getter().get();
   |                     ^^^ method cannot be called on `Victim<'_, Self>` due to unsatisfied trait bounds
   |
note: trait bound `Self: Sized` was not satisfied
  --> src/main.rs:15:10
   |
15 | impl<'a, T: Perpetrator /*+ ?Sized*/> VictimTrait for Victim<'a, T> {
   |          ^                            -----------     -------------
   |          |
   |          unsatisfied trait bound introduced here
help: consider restricting the type parameter to satisfy the trait bound
   |
3  |   Self: Sized, Self: Sized // rustc sugests adding this trait bound, even if it already exists
   |              +++++++++++++

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

This message is misleading, instruction you to add an (possibly) already existing trait bound instead of the actual fix, which is commented out in the code

Meta

This bug currently exists both in stable 1.61.0 and nightly 1.64.0-nightly(2022-06-25 20a6f3a).

playground link
gist link

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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