Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc suggests invalid syntax to fix lifetime inferring error #62097

Open
Bunogi opened this issue Jun 24, 2019 · 2 comments · May be fixed by #65730

Comments

@Bunogi
Copy link

@Bunogi Bunogi commented Jun 24, 2019

I tried this code:

#![feature(async_await)]

use futures::{stream, StreamExt};

struct Foo {}

impl Foo {
    async fn foo(&self) {}

    async fn bar(&self) {
        stream::iter(0..1)
            .then(|_| runtime::spawn(self.foo()))
            .collect::<()>()
            .await;
    }
}

fn main() {}

The code above does not compile, throwing an error with inferring lifetimes. rustc produces the following output:

error: cannot infer an appropriate lifetime
  --> src/main.rs:10:18
   |
10 |     async fn bar(&self) {
   |                  ^^^^^ ...but this borrow...
11 |         stream::iter(0..1)
12 |             .then(|_| runtime::spawn(self.foo()))
   |                       -------------- this return type evaluates to the `'static` lifetime...
   |
note: ...can't outlive the lifetime '_ as defined on the method body at 10:18
  --> src/main.rs:10:18
   |
10 |     async fn bar(&self) {
   |                  ^
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime '_ as defined on the method body at 10:18
   |
12 |             .then(|_| runtime::spawn + '_(self.foo()))
   |                       ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `report`.

To learn more, run the command again with --verbose.

This suggestion rustc makes however, is invalid syntax. Trying to apply it's suggestion causes a syntax error as seen below.

error: expected `:`, found `(`
  --> src/main.rs:12:42
   |
12 |             .then(|_| runtime::spawn + '_(self.foo()))
   |                                          ^ expected `:`

Meta

rustc --version --verbose:

rustc 1.37.0-nightly (de7c4e423 2019-06-23)
binary: rustc
commit-hash: de7c4e42314c56528640e3b663aa10e0caa6bd9b
commit-date: 2019-06-23
host: x86_64-unknown-linux-gnu
release: 1.37.0-nightly
LLVM version: 8.0
@DarkKirb

This comment has been minimized.

Copy link

@DarkKirb DarkKirb commented Oct 21, 2019

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

@nikomatsakis nikomatsakis commented Oct 22, 2019

@rustbot assign @csmoe

bors added a commit that referenced this issue Nov 1, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
Centril added a commit to Centril/rust that referenced this issue Nov 8, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes rust-lang#62097
Centril added a commit to Centril/rust that referenced this issue Nov 9, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes rust-lang#62097
bors added a commit that referenced this issue Nov 19, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
bors added a commit that referenced this issue Nov 20, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
bors added a commit that referenced this issue Nov 20, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
Centril added a commit to Centril/rust that referenced this issue Nov 20, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes rust-lang#62097
bors added a commit that referenced this issue Nov 20, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
bors added a commit that referenced this issue Nov 20, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes #62097
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.