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

Closed
Bunogi opened this issue Jun 24, 2019 · 2 comments · Fixed by #65730
Closed

rustc suggests invalid syntax to fix lifetime inferring error #62097

Bunogi opened this issue Jun 24, 2019 · 2 comments · Fixed by #65730
Assignees
Labels
A-async-await Area: Async & Await A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Bunogi
Copy link

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
@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 24, 2019
@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Oct 5, 2019
@DarkKirb
Copy link

Can confirm with rust 1.40.0-nightly

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1d8e97383618ff6ff61ea2b83156c6f7

this only happens with async fns, the same code without async does not trigger the issue

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2fab081c235b071b096b5f6c877f22d8

@jonas-schievink jonas-schievink added the A-async-await Area: Async & Await label Oct 21, 2019
@nikomatsakis nikomatsakis added AsyncAwait-OnDeck AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. AsyncAwait-Polish Async-await issues that are part of the "polish" area and removed AsyncAwait-OnDeck labels Oct 22, 2019
@nikomatsakis
Copy link
Contributor

@rustbot assign @csmoe

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
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
Centril added a commit to Centril/rust that referenced this issue Nov 21, 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 21, 2019
Suggest to add lifetime constraint at explicit ouput of functions

Closes rust-lang#62097
@bors bors closed this as completed in e4a6341 Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants