Skip to content

Generic param requires bounds on call to function that is already required for calls to the caller #102611

Closed

Description

Seems like this behavior has existed for a while, so apologies in advance if this is already a settled matter. If so, I still think the failure coulld use a better error message.

In short, the following code fails to compile:

pub struct Foo<'a, A>(&'a A);

impl<'a, A> Foo<'a, A> {
    pub fn foo() {}
    
    pub fn call_foo() {
        Self::foo();
    }
}

(playground link)

Error message:

error[E0309]: the parameter type `A` may not live long enough
 --> src/lib.rs:7:9
  |
7 |         Self::foo();
  |         ^^^^^^^^^ ...so that the type `A` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound...
  |
3 | impl<'a, A: 'a> Foo<'a, A> {
  |           ++++

In call_foo, the compiler (correctly) requires that A: 'a, but fails to realize that this bound is already satisfied (because this is also required for callers of call_foo).

To me, this seems like a bug. However, this behavior seems to have been present in stable for long enough that it seems highly unlikely it hasn't already been discussed at length - I couldn't find any issues referencing it though. (closest seems to be #84021).

Happy to help where I can (would need some direction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.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