Skip to content

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

Open
@EFanZh

Description

@EFanZh
Contributor

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

Activity

jyn514

jyn514 commented on Apr 2, 2021

@jyn514
Member
error[E0308]: mismatched types
 --> src/lib.rs:5:31
  |
5 |     consume_reference::<i32>(&async { Box::new(7_i32) }.await);
  |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                               |
  |                               expected `i32`, found struct `Box`
  |                               help: consider dereferencing the type: `*async { Box::new(7_i32) }.await`
  |
  = note: expected type `i32`
           found struct `Box<i32>`

error: aborting due to previous error

The error also happens on nightly.

changed the title [-]Deref coercion does not work for `await` returned values[/-] [+]Deref coercion does not work for some `await` returned values[/+] on Apr 2, 2021
added
A-coercionsArea: implicit and explicit `expr as Type` coercions
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 2, 2021
tmandry

tmandry commented on Apr 2, 2021

@tmandry
Member

@rustbot label +E-medium

Not sure what exactly is going wrong here. Doing some code spelunking with -Ztreat-err-as-bug (and maybe some added debug logs) should make it possible to trace the issue.

added
E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
on Apr 2, 2021
added
AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
on Apr 2, 2021
ldm0

ldm0 commented on Apr 3, 2021

@ldm0
Contributor

This issue has nothing to do with async or await, this is an old coercion issue: #57749.

pnkfelix

pnkfelix commented on Mar 3, 2022

@pnkfelix
Member

@rustbot claim

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @pnkfelix@jonas-schievink@EFanZh@tmandry@jyn514

    Issue actions

      Deref coercion does not work for some `await` returned values · Issue #83783 · rust-lang/rust