Skip to content

Inference disrupted by trait bound that is redundant with a HRTB #41617

@dtolnay

Description

@dtolnay

This compiles:

trait Trait<'a> {}

struct S<T>(T);

impl<'a, T> Trait<'a> for S<T> where T: for<'b> Trait<'b> {}

fn main() {}

This following does not. It seems like the impl already has T: for<'b> Trait<'b> so adding T: Trait<'a> should not affect the behavior.

trait Trait<'a> {}

struct S<T>(T);

// Added T: Trait<'a>
impl<'a, T> Trait<'a> for S<T> where T: for<'b> Trait<'b>, T: Trait<'a> {}

fn main() {}
error[E0283]: type annotations required: cannot resolve `T: Trait<'a>`
 --> src/main.rs:6:1
  |
6 | impl<'a, T> Trait<'a> for S<T> where T: for<'b> Trait<'b>, T: Trait<'a> {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: required by `Trait`

This currently affects the following and many people have run into it. The workaround is to remove the bound (rust-lang/rust-clippy#1689).

#[derive(Deserialize)]
struct S<T: serde::de::DeserializeOwned>(T);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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