Skip to content

existential type complains about '_ in concrete type of self-borrowing generators #60655

Closed
@cramertj

Description

@cramertj
Member

Playground

error: non-defining existential type use in defining scope
  --> src/main.rs:9:22
   |
9  |   fn bar() -> ServeFut {
   |  ______________________^
10 | |     async move {
11 | |         let x = 5;
12 | |         await!(foo(&x))
13 | |     }
14 | | }
   | |_^ lifetime `'_` is part of concrete type but not used in parameter list of existential type

error: aborting due to previous error

cc @oli-obk

Activity

added
A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
on May 9, 2019
oli-obk

oli-obk commented on May 9, 2019

@oli-obk
Contributor

Am I correct in assuming that this should not compile, but report an error about returning a reference to the local variable x? Or is the self-borrowing generator part what makes this work? so the x is part of the generator struct and the &x is the yielded thing?

I'll try pretty printing to the expanded HIR, since I'm not sure what's going on there

oli-obk

oli-obk commented on May 9, 2019

@oli-obk
Contributor

Expanded version:

#![feature(gen_future)]
#![feature(generators)]
#![feature(async_await, await_macro, existential_type)]

use std::future::{self, Future};

async fn foo(__arg0: &u8) {
}

pub existential type ServeFut: Future<Output = ()>;

fn bar() -> ServeFut {
    ::std::future::from_generator(move || {
        let x = 5;

        {
            let mut pinned = foo(&x);
            loop {
                match future::poll_with_tls_context(unsafe {
                    std::pin::Pin::new_unchecked(&mut pinned)
                }) {
                    std::task::Poll::Ready(x) => {
                        break x;
                    }
                    _ => (),
                }
                yield ()
            }
        }
    })
}
fn main() {}

Playground

cramertj

cramertj commented on May 9, 2019

@cramertj
MemberAuthor

Am I correct in assuming that this should not compile, but report an error about returning a reference to the local variable x?

No, this should definitely compile.

Or is the self-borrowing generator part what makes this work? so the x is part of the generator struct and the &x is the yielded thing?

&x isn't yielded, but yes, it's a local variable inside the generator (that references another local variable in the generator).

oli-obk

oli-obk commented on May 10, 2019

@oli-obk
Contributor

Hmm, so because the generator contains references, it has lifetimes, but these lifetimes actually refer to the generator again? Do we have a special kind of lifetime that we use for this? We should just ignore those generator lifetimes for existential types.

cramertj

cramertj commented on May 10, 2019

@cramertj
MemberAuthor

There's no special lifetime afaik (cc @Zoxc @tmandry).

matthewjasper

matthewjasper commented on May 12, 2019

@matthewjasper
Contributor

We should ignore all bound regions. Function pointers have the same issue:

existential type A: Sized;

fn foo() -> A {
    (|x| ()) as for<'a> fn(&'a i32)
}

fn main() {}
added a commit that references this issue on May 14, 2019

Rollup merge of rust-lang#60799 - matthewjasper:allow-bound-regions-i…

b24981a
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 & AwaitA-coroutinesArea: CoroutinesA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @oli-obk@cramertj@matthewjasper

      Issue actions

        `existential type` complains about `'_` in concrete type of self-borrowing generators · Issue #60655 · rust-lang/rust