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

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

Open
EFanZh opened this issue Apr 2, 2021 · 4 comments
Open

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

EFanZh opened this issue Apr 2, 2021 · 4 comments
Assignees
Labels
A-async-await Area: Async & Await A-coercions Area: implicit and explicit `expr as Type` coercions AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@EFanZh
Copy link
Contributor

EFanZh commented Apr 2, 2021

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
@EFanZh EFanZh added the C-bug Category: This is a bug. label Apr 2, 2021
@jyn514
Copy link
Member

jyn514 commented Apr 2, 2021

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.

@EFanZh EFanZh changed the title Deref coercion does not work for await returned values Deref coercion does not work for some await returned values Apr 2, 2021
@jonas-schievink jonas-schievink added A-async-await Area: Async & Await A-coercions Area: implicit and explicit `expr as Type` coercions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 2, 2021
@tmandry
Copy link
Member

tmandry commented Apr 2, 2021

@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.

@rustbot rustbot added the E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Apr 2, 2021
@tmandry tmandry added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label Apr 2, 2021
@ldm0
Copy link
Contributor

ldm0 commented Apr 3, 2021

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

@pnkfelix
Copy link
Member

pnkfelix commented Mar 3, 2022

@rustbot claim

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-coercions Area: implicit and explicit `expr as Type` coercions AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: On deck
Development

Successfully merging a pull request may close this issue.

7 participants