Skip to content

Weird error in trait with HRTB for the same trait on Self in a method. Also bad diagnostic with nonsensical suggestion. #84435

Open
@steffahn

Description

@steffahn

Given the following code:

trait Trait<'a>: Sized {
    fn method(self)
    where
        for<'b> Self: Trait<'b>;
}

(playground)

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0283]: type annotations needed
 --> src/lib.rs:1:1
  |
1 | trait Trait<'a>: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
  | |
  | cannot infer type for type parameter `Self`
  | required by this bound in `Trait`
  |
  = note: cannot satisfy `Self: Trait<'a>`
help: consider specifying the type arguments in the function call
  |
1 | trait Trait<'a>: Sized::<Self, 'a> {
  |                       ^^^^^^^^^^^^

error: aborting due to previous error

I would’ve expected this code to compile. Even if there is a reason why it shouldn’t compile, the error message is about the most confusing/nonsensical error message I’ve ever seen. The “best” part is how it suggests adding arguments to Sized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.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