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

Bad error message using shared borrow of non-sync type across await point #71010

Open
lily-mara opened this issue Apr 10, 2020 · 1 comment
Open
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-async Working group: Async & await

Comments

@lily-mara
Copy link

I tried this code:

struct NonSync(*const ());
unsafe impl Send for NonSync {}

impl NonSync {
    async fn foo(&self) {}
    async fn bar(&mut self) {}
}

fn require_send(_: impl Send) {}

fn main() {
    require_send(async {
        let mut foo = NonSync(std::ptr::null());
        foo.foo().await;
    });
    
    require_send(async {
        let foo = NonSync(std::ptr::null());
        foo.bar().await;
    });
}

I expected to see this happen: one of three things:

  1. Both branches compile
  2. Both branches don't compile
  3. foo branch doesn't compile, but bar branch does, and error message explains that &T is non-sync while &mut T is sync where T: !Sync

Instead, this happened: explanation

foo branch doesn't compile, but bar branch does, error message implies that the error is a lifetime issue rather than related to shared vs exclusive borrowing.

error: future cannot be sent between threads safely
  --> src/main.rs:12:5
   |
9  | fn require_send(_: impl Send) {}
   |    ------------         ---- required by this bound in `require_send`
...
12 |     require_send(async {
   |     ^^^^^^^^^^^^ future returned by `main` is not `Send`
   |
   = help: within `NonSync`, the trait `std::marker::Sync` is not implemented for `*const ()`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:14:9
   |
14 |         foo.foo().await;
   |         ---^^^^^^^^^^^^- `foo` is later dropped here
   |         |
   |         await occurs here, with `foo` maybe used later
   |         has type `&NonSync`
help: consider moving this into a `let` binding to create a shorter lived borrow
  --> src/main.rs:14:9
   |
14 |         foo.foo().await;
   |         ^^^^^^^^^

Meta

I ran this code using the latest stable (1.42.0) and nightly (1.44.0-nightly (2020-04-09 94d3463)) versions of the compiler. Same error message occurs both times.

@lily-mara lily-mara added the C-bug Category: This is a bug. label Apr 10, 2020
@fmease fmease added A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-async-await Area: Async & Await WG-async Working group: Async & await and removed needs-triage-legacy WG-async Working group: Async & await labels Jan 25, 2024
@traviscross traviscross added WG-async Working group: 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. labels Feb 26, 2024
@traviscross
Copy link
Contributor

@rustbot labels +AsyncAwait-Triaged

We reviewed this today in WG-async triage.

The fact that it gives an error is correct here, but the diagnostic could definitely be improved.

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-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-async Working group: Async & await
Projects
None yet
Development

No branches or pull requests

4 participants