Skip to content

Improve await error messaging for trait obligations #71137

New issue

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

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

Already on GitHub? # to your account

Closed
rokob opened this issue Apr 14, 2020 · 0 comments · Fixed by #71203
Closed

Improve await error messaging for trait obligations #71137

rokob opened this issue Apr 14, 2020 · 0 comments · Fixed by #71203
Assignees
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rokob
Copy link
Contributor

rokob commented Apr 14, 2020

I tried this code:

use std::future::Future;
use std::sync::Mutex;

fn fake_spawn<F: Future + Send + 'static>(f: F) { }

async fn wrong_mutex() {
  let m = Mutex::new(1);
  {
    let mut guard = m.lock().unwrap();
    (async { "right"; }).await;
    *guard += 1;
  }

  (async { "wrong"; }).await;
}

fn main() {
  fake_spawn(wrong_mutex());
}

I expected to see this happen:

error: future cannot be sent between threads safely
  --> src/main.rs:18:3
   |
4  | fn fake_spawn<F: Future + Send + 'static>(f: F) { }
   |    ----------             ---- required by this bound in `fake_spawn`
...
18 |   fake_spawn(wrong_mutex());
   |   ^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
   |
   = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:14:3
   |
9  |     let mut guard = m.lock().unwrap();
   |         --------- has type `std::sync::MutexGuard<'_, i32>`
...
12 |   }
   |   - `mut guard` is later dropped here
9  |     let mut guard = m.lock().unwrap();
10 |   (async { "right"; }).await;
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here, with `mut guard` maybe used later

error: aborting due to previous error

Instead, this happened:

error: future cannot be sent between threads safely
  --> src/main.rs:18:3
   |
4  | fn fake_spawn<F: Future + Send + 'static>(f: F) { }
   |    ----------             ---- required by this bound in `fake_spawn`
...
18 |   fake_spawn(wrong_mutex());
   |   ^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
   |
   = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:14:3
   |
9  |     let mut guard = m.lock().unwrap();
   |         --------- has type `std::sync::MutexGuard<'_, i32>`
...
12 |   }
   |   - `mut guard` is later dropped here
13 |
14 |   (async { "wrong"; }).await;
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here, with `mut guard` maybe used later

error: aborting due to previous error

Note that the await pointed to be the error message is wrong.

The code that gets the span for the await is here

let await_span = tables.generator_interior_types.iter().map(|t| t.span).last().unwrap();
. The last span in generator_interior_types is not guaranteed to be the right await point.

Meta

rustc --version --verbose:

rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-apple-darwin
release: 1.42.0
LLVM version: 9.0

I also see the same behaviour with:

rustc 1.44.0-nightly (6dee5f112 2020-04-06)
binary: rustc
commit-hash: 6dee5f1126dfd5c9314ee5ae9d9eb010e35ef257
commit-date: 2020-04-06
host: x86_64-apple-darwin
release: 1.44.0-nightly
LLVM version: 9.0
@rokob rokob added the C-bug Category: This is a bug. label Apr 14, 2020
@jonas-schievink jonas-schievink added A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 14, 2020
@csmoe csmoe self-assigned this Apr 16, 2020
@tmandry tmandry added P-medium Medium priority AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. labels Apr 21, 2020
@bors bors closed this as completed in 7b1ce6e Apr 22, 2020
@tmandry tmandry moved this to Done in wg-async work Dec 8, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants