Skip to content

Extra trait bound makes function body fail to typecheck #82219

@jplatte

Description

@jplatte

I tried this code (playground):

pub trait Foo<'a> {}

impl Foo<'_> for () {}
impl<'a, T> Foo<'a> for Option<T> where T: Foo<'a> {}

pub fn bar<T>()
where
    T: for<'a> Foo<'a>,
{
}

fn baz<'a>()
where
    (): Foo<'a>,
{
    bar::<Option<()>>();
}
Or with references instead of Option
impl<'a, 'b, T> Foo<'a> for &'b T where T: Foo<'a> {}
bar::<&()>();

I expected to see this happen: Compile successfully, with or without the unneeded (): Foo<'a> bound.

Instead, this happened: It works when the (): Foo<'a> bound is removed, but as written it produces:

error: implementation of `Foo` is not general enough
  --> src/lib.rs:16:5
   |
1  | pub trait Foo<'a> {}
   | -------------------- trait `Foo` defined here
...
16 |     bar::<Option<()>>();
   |     ^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
   |
   = note: `Option<()>` must implement `Foo<'0>`, for any lifetime `'0`...
   = note: ...but `Option<()>` actually implements `Foo<'1>`, for some specific lifetime `'1`

Meta

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0

Also happens on Nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions