Skip to content

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

Closed

Description

@sharnoff

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).

Activity

  1. lcnr commented on Oct 3, 2022

    @lcnr
    Contributor
    pub struct Foo<'a, A>(&'a A);
    
    impl<'a, A> Foo<'a, A> {
        pub fn foo() {
            Self::foo();
        }
    }

    yeah, implied bounds from the impl header are ignored while typechecking a function body but apparently still required somewhere 😁 i am currently working on this and should hopefully be able to fix this in the somewhat near future

    @rustbot claim

  2. aliemjay commented on Oct 3, 2022

    @aliemjay
    Contributor

    but apparently still required somewhere grin

    They're required for the WF-check of Self in borrowck. This is a duplicate of #98852.

  3. Rageking8 commented on Oct 4, 2022

    @Rageking8
    Contributor

    @rustbot label +T-compiler +C-bug

  4. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 4, 2022
  5. removed their assignment
    on Jun 8, 2023
  6. added 2 commits that reference this issue on Nov 8, 2023
    6c5ba88
    a7ce432
  7. added a commit that references this issue on Nov 20, 2023
    66ac3f1
  8. added a commit that references this issue on Jan 17, 2024
    6bf600b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

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