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

[BUG] Stable (warning): Async traits Self return requires type specification #113538

Closed
Arthurdw opened this issue Jul 10, 2023 · 7 comments · Fixed by #120360
Closed

[BUG] Stable (warning): Async traits Self return requires type specification #113538

Arthurdw opened this issue Jul 10, 2023 · 7 comments · Fixed by #120360
Assignees
Labels
A-async-await Area: Async & Await AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. F-async_fn_in_trait Static async fn in traits requires-nightly This issue requires a nightly compiler in some way.

Comments

@Arthurdw
Copy link

When using the #![feature(async_fn_in_trait)] flag, I stumbled upon a warning that I don't understand and assume to be a bug/missed implementation.

If this is the intended behavior, could anyone please explain why?

This is simplified code that generates the warning:

#![feature(async_fn_in_trait)]

trait Repository {
    async fn new() -> Self;
}

struct MyRepository {}

impl Repository for MyRepository {
    async fn new() -> Self {
        todo!()
    }
}

fn main() {
    todo!()
}
warning: opaque type `impl Future<Output = Self>` does not satisfy its associated type bounds
  --> src\main.rs:4:23
   |
4  |     async fn new() -> Self;
   |                       ^^^^
   |
  ::: C:\Users\arthu\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\future\future.rs:41:5
   |
41 |     type Output;
   |     ------------ this associated type bound is unsatisfied for `Self`
   |
   = note: `#[warn(opaque_hidden_inferred_bound)]` on by default

But when adding a type restriction on self (in this case Sized) the warning does not appear anymore.

#![feature(async_fn_in_trait)]

trait Repository
where
    Self: Sized,
{
    async fn new() -> Self;
}

struct MyRepository {}

impl Repository for MyRepository {
    async fn new() -> Self {
        todo!()
    }
}

fn main() {
    todo!()
}

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (cb80ff132 2023-07-07)
binary: rustc
commit-hash: cb80ff132a0e9aa71529b701427e4e6c243b58df
commit-date: 2023-07-07
host: x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.5
@Arthurdw Arthurdw added the C-bug Category: This is a bug. label Jul 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 10, 2023
@compiler-errors compiler-errors self-assigned this Jul 10, 2023
@compiler-errors compiler-errors added requires-nightly This issue requires a nightly compiler in some way. F-async_fn_in_trait Static async fn in traits A-async-await Area: Async & Await and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 10, 2023
@compiler-errors
Copy link
Member

This is in fact a bug.

@eholk eholk added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label Jul 31, 2023
@Arthurdw
Copy link
Author

FYI: still present on rustc 1.75.0-nightly

@Arthurdw
Copy link
Author

Arthurdw commented Jan 6, 2024

Update: This issue is still present, and has now sneaked into stable with the release of 1.75; as such I'm updating the title to reflect that.

active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.75.0 (82e1608df 2023-12-21)

$ cargo check
warning: opaque type `impl Future<Output = Self>` does not satisfy its associated type bounds
  --> src/main.rs:2:5
   |
2  |     async fn new() -> Self;
   |     ^^^^^^^^^^^^^^^^^^^^^^^
   |
  ::: /var/home/arthur/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/future.rs:41:5
   |
41 |     type Output;
   |     ------------ this associated type bound is unsatisfied for `Self`
   |
   = note: `#[warn(opaque_hidden_inferred_bound)]` on by default

warning: `testing-async` (bin "testing-async") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

@Arthurdw Arthurdw changed the title Nightly (warning): Async traits Self return requires type specification [BUG] Stable (warning): Async traits Self return requires type specification Jan 6, 2024
@BaxHugh
Copy link

BaxHugh commented Jan 12, 2024

@compiler-errors
Copy link
Member

@BaxHugh: This isn't the same bug. This just has to do with a false warning of the opaque_hidden_inferred_bound lint.

@buildwithzephyr
Copy link

👍 on this, also encountering this bug

@xiyu1984
Copy link

Also encountering with rustc 1.74.0-nightly

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 26, 2024
…lcnr

Don't fire `OPAQUE_HIDDEN_INFERRED_BOUND` on sized return of AFIT

Conceptually, we should probably not fire `OPAQUE_HIDDEN_INFERRED_BOUND` for methods like:

```
trait Foo { async fn bar() -> Self; }
```

Even though we technically cannot prove that `Self: Sized`, which is one of the item bounds of the `Output` type in the `-> impl Future<Output = Sized>` from the async desugaring.

This is somewhat justifiable along the same lines as how we allow regular methods to return `-> Self` even though `Self` isn't sized.

Fixes rust-lang#113538

(side-note: some days i wonder if we should just remove the `OPAQUE_HIDDEN_INFERRED_BOUND` lint... it does make me sad that we have non-well-formed types in signatures, though.)
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 26, 2024
Rollup merge of rust-lang#120360 - compiler-errors:afit-sized-lol, r=lcnr

Don't fire `OPAQUE_HIDDEN_INFERRED_BOUND` on sized return of AFIT

Conceptually, we should probably not fire `OPAQUE_HIDDEN_INFERRED_BOUND` for methods like:

```
trait Foo { async fn bar() -> Self; }
```

Even though we technically cannot prove that `Self: Sized`, which is one of the item bounds of the `Output` type in the `-> impl Future<Output = Sized>` from the async desugaring.

This is somewhat justifiable along the same lines as how we allow regular methods to return `-> Self` even though `Self` isn't sized.

Fixes rust-lang#113538

(side-note: some days i wonder if we should just remove the `OPAQUE_HIDDEN_INFERRED_BOUND` lint... it does make me sad that we have non-well-formed types in signatures, though.)
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 AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. F-async_fn_in_trait Static async fn in traits requires-nightly This issue requires a nightly compiler in some way.
Projects
Development

Successfully merging a pull request may close this issue.

7 participants