Skip to content

Inherent async fn returning Self treats type's lifetime parameters as 'static #61949

Closed

Description

@Arnavion

rustc 1.37.0-nightly (2887008e0 2019-06-12) and playground's 2019-06-17 b25ee644971a168287ee

Playground

#![feature(async_await)]

pub struct Foo<'a> {
    pub bar: &'a i32,
}

impl<'a> Foo<'a> {
    pub async fn new(bar: &'a i32) -> Self {
        Foo {
            bar
        }
    }
}
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
 --> src/lib.rs:8:22
  |
8 |     pub async fn new(bar: &'a i32) -> Self {
  |                      ^^^
  |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 7:6...
 --> src/lib.rs:7:6
  |
7 | impl<'a> Foo<'a> {
  |      ^^
  = note: ...so that the expression is assignable:
          expected &i32
             found &'a i32
  = note: but, the lifetime must be valid for the static lifetime...
  = note: ...so that the types are compatible:
          expected Foo<'_>
             found Foo<'static>

It works to either change the signature to take bar: &'static i32, or to change the body of the fn to use a static borrow like bar: &5. So the compiler really does want the function to return a Foo<'static>, even though Self is a Foo<'a>


The workaround is to not use Self:

-    pub async fn new(bar: &'a i32) -> Self {
+    pub async fn new(bar: &'a i32) -> Foo<'a> {

Mentoring notes: See notes here on Zulip.

Activity

  1. Arnavion commented on Jun 19, 2019

    @Arnavion
    ContributorAuthor

    jebrosen pointed out on IRC that it also happens with RPIT in general, so this might be the same as #53613 (from async fn expanding to -> impl Future<Output = ...>)

  2. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 19, 2019
  3. nikomatsakis commented on Jul 5, 2019

    @nikomatsakis
    Contributor

    As I pointed out in #53613, there is a more dangerous variant of this involving projections:

    #![feature(async_await)]
    
    pub trait HasItem<'a> {
        type Item;
    }
    
    // Does not compile:
    async fn example1<'a, T: HasItem<'a>>(item: T::Item) -> T::Item {
        item
    }
    
    
    fn main() { }

    Note that this version compiles just fine

    async fn example1<'a, T: HasItem<'a>>(item: T::Item) -> <T as HasItem<'a>>::Item {
        item
    }
  4. nikomatsakis commented on Jul 5, 2019

    @nikomatsakis
    Contributor

    There are some forward compatibility concerns here. In particular, callers can (somewhat incorrectly) assume that the returned futures are 'static. In other words, the callee is committing to returning a future that does not capture 'a in these examples -- right now, that typically means callee doesn't compile, but it could happen that the callee happens to compile, and then callers would be able to use this future in places where 'a is out of scope. If we later fix this bug, those callers will stop compiling.

  5. nikomatsakis commented on Jul 5, 2019

    @nikomatsakis
    Contributor

    The bug involving Self is rather straightforward (but grungy) to fix. It's also possible for us to, temporarily, just create an error if you return a type mentioning Self in an async fn.

    But fixing the T::Item case is rather more complex. We don't know until type-checking that T::Item resolves to <T as HasItem<'a>>::Item. And, if I had my druthers, we might not know until even later than that. I suppose we could do a forward compatibility restriction of some kind.

  6. nikomatsakis commented on Jul 9, 2019

    @nikomatsakis
    Contributor

    An example of something which compiles now but I think should not (and would not, if this were fixed):

    #![feature(async_await)]
    
    pub struct Foo<'a> {
        pub bar: &'a i32,
    }
    
    impl<'a> Foo<'a> {
        pub async fn new(_bar: &'a i32) -> Self {
            Foo {
                bar: &22
            }
        }
    }
    
    async fn foo() {
      let x = {
        let bar = 22;
        Foo::new(&bar).await
      };
      drop(x);
    }
    
    fn main() { }

    the problem here is that the type of x should be Foo<'bar> -- i.e., it should be constrained to the lifetime of bar, but it is actually Foo<'static>.

  7. nikomatsakis commented on Jul 9, 2019

    @nikomatsakis
    Contributor

    I left some notes on how to fix this on Zulip

  8. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    and removed
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Jul 9, 2019
  9. davidtwco commented on Jul 9, 2019

    @davidtwco
    Member

    @rustbot claim

  10. 40 remaining items

  11. removed their assignment
    on Sep 16, 2021
  12. added a commit that references this issue on Feb 12, 2022
  13. Xiretza commented on Apr 24, 2022

    @Xiretza
    Contributor

    #91403 was reverted (by #94088), I think this should be reopened.

  14. reopened this on May 3, 2022
  15. added 2 commits that reference this issue on Sep 28, 2023
  16. Arnavion commented on Jul 31, 2024

    @Arnavion
    ContributorAuthor

    I just revisited the code that had prompted me to file this issue, and both it and the test case in the OP compile fine today. According to godbolt it didn't compile until 1.73 and started compiling since 1.74.0. Is this still an issue?

  17. tmandry commented on Jul 31, 2024

    @tmandry
    Member

    Looks like this is now accepted after #115659 and we can close the issue. Thanks @Arnavion for commenting!

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

    A-async-awaitArea: Async & AwaitAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-mediumMedium priorityT-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