Skip to content

Deref coercion does not work for some await returned values #83783

Open
@EFanZh

Description

@EFanZh

I tried this code:

fn consume_reference<T: ?Sized>(_: &T) {}

async fn foo() {
    consume_reference::<i32>(&Box::new(7_i32)); // OK.
    consume_reference::<i32>(&async { Box::new(7_i32) }.await); // Error.
    consume_reference::<[i32]>(&vec![7_i32]); // OK.
    consume_reference::<[i32]>(&async { vec![7_i32] }.await); // OK.
}

I expected to see this happen: The code compiles.

Instead, this happened: This line does not compile while the others lines do:

consume_reference::<i32>(&async { Box::new(7_i32) }.await);

The same thing also happened to awaiting futures::lock::Mutex::lock function:

fn consume_reference(_: &mut i32) {}

async fn foo() {
    let mutex = futures::lock::Mutex::new(7_i32);

    consume_reference(&mut mutex.lock().await);
}

Meta

rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-pc-windows-msvc
release: 1.51.0
LLVM version: 11.0.1

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-coercionsArea: implicit and explicit `expr as Type` coercionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

On deck

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions