Skip to content

rustc suggests invalid syntax to fix lifetime inferring error #62097

Closed
@Bunogi

Description

@Bunogi

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

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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