Skip to content

Unexpected build pass with a not Send future #108897

Closed
@LYF1999

Description

@LYF1999

I tried this code:

use std::{future::Future, pin::Pin};

trait Handler<R> {
    type Resp;
    type Future: Future<Output = Self::Resp> + Send + 'static;
    fn call(self) -> Self::Future;
}

impl<R, F, Res, Fut> Handler<R> for F
where
    Res: 'static,
    Fut: Future<Output = Res> + Send,
    F: FnOnce() -> Fut + Send + Clone + 'static,
{
    type Resp = Res;

    type Future = Pin<Box<dyn Future<Output = Self::Resp> + Send>>;

    fn call(self) -> Self::Future {
        Box::pin(async move {
            let res = self().await;
            res
        })
    }
}

fn require_handler<H: Handler<()>>(h: H) {}

async fn handler() -> () {
    let a = &1 as *const i32;
    async {}.await;
}

fn main() {
    require_handler(handler)
}

I expected to see this happen:
build failed, because this future doesn't implement Send, so this function shouldn't implement Handler

error: future cannot be sent between threads safely
  --> src/main.rs:35:21
   |
35 |     require_handler(handler)
   |                     ^^^^^^^ future returned by `handler` is not `Send`
   |
   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*const i32`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:31:13
   |
30 |     let a = &1 as *const i32;
   |         - has type `*const i32` which is not `Send`
31 |     async {}.await;
   |             ^^^^^^ await occurs here, with `a` maybe used later
32 | }
   | - `a` is later dropped here
note: required by a bound in `require_handler`
  --> src/main.rs:27:23
   |
27 | fn require_handler<H: Handler<()>>(h: H) {}
   |                       ^^^^^^^^^^^ required by this bound in `require_handler`

Instead, this happened:
build pass

Meta

rustc --version --verbose:

rustc 1.70.0-nightly (e3dfeeaa4 2023-03-07)
binary: rustc
commit-hash: e3dfeeaa45f117281b19773d67f3f253de65cee1
commit-date: 2023-03-07
host: aarch64-apple-darwin
release: 1.70.0-nightly
LLVM version: 15.0.7
Backtrace

<backtrace>

cargo-bisect-rustc show the Regression in 9bb6e60

this bug is introduced by this PR #103695.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions