Skip to content

impl has stricter requirements than trait for async fn in traits also incorrectly suggests adding trait bound that already exists #122506

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

Closed
nskobelevs opened this issue Mar 14, 2024 · 1 comment · Fixed by #135055
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nskobelevs
Copy link

I tried this code:

use std::future::Future;

trait Foo {
    fn foo<S>(&self, s: S) -> impl Future<Output = S> + Send;
}

struct Bar;

impl Foo for Bar {
    async fn foo<S>(&self, s: S) -> S
    where
        S: std::marker::Send,
    {
        s
    }
}

which returns the following errors:

error: future cannot be sent between threads safely
  --> src/main.rs:10:5
   |
10 | /     async fn foo<S>(&self, s: S) -> S
11 | |     where
12 | |         S: std::marker::Send,
   | |_____________________________^ future returned by `foo` is not `Send`
   |
note: captured value is not `Send`
  --> src/main.rs:10:28
   |
10 |     async fn foo<S>(&self, s: S) -> S
   |                            ^ has type `S` which is not `Send`
note: required by a bound in `Foo::{opaque#0}`
  --> src/main.rs:4:57
   |
4  |     fn foo<S>(&self, s: S) -> impl Future<Output = S> + Send;
   |                                                         ^^^^ required by this bound in `Foo::{opaque#0}`
help: consider further restricting this bound
   |
12 |         S: std::marker::Send + std::marker::Send,
   |                              +++++++++++++++++++

error[E0276]: impl has stricter requirements than trait
  --> src/main.rs:12:12
   |
4  |     fn foo<S>(&self, s: S) -> impl Future<Output = S> + Send;
   |     --------------------------------------------------------- definition of `foo` from trait
...
12 |         S: std::marker::Send,
   |            ^^^^^^^^^^^^^^^^^ impl has extra requirement `S: Send`

For more information about this error, try `rustc --explain E0276`.

impl has stricter requirements than trait is the correct error and hint here, should the future cannot be sent between threads safely error be surfaced in this case given it's not real cause and it suggests to add a bound that already exists making the output just more confusing.

Meta

This happens on both beta and nightly.
In stable E0276 doesn't even appear at all and it only gives the first error + hint

rustc --version --verbose: (nightly)

rustc 1.78.0-nightly (3b1717c05 2024-03-10)
binary: rustc
commit-hash: 3b1717c052de4a2dbdd3badb0e7a885f40a8ad9e
commit-date: 2024-03-10
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.0

rustc --version --verbose: (beta)

rustc 1.77.0-beta.7 (339fb6965 2024-03-06)
binary: rustc
commit-hash: 339fb6965026c4260d38ab6fd61cfcbecffd8279
commit-date: 2024-03-06
host: aarch64-apple-darwin
release: 1.77.0-beta.7
LLVM version: 17.0.6
@nskobelevs nskobelevs added the C-bug Category: This is a bug. label Mar 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 14, 2024
@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 14, 2024
@compiler-errors compiler-errors self-assigned this Mar 15, 2024
@zekefast
Copy link

zekefast commented Jul 7, 2024

I just got into the same error and also very confused with error message.

$ rustc --version --verbose
rustc 1.81.0-nightly (524d806c6 2024-07-05)
binary: rustc
commit-hash: 524d806c62a82ecc0cf8634b94997ae506f4d6f9
commit-date: 2024-07-05
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

@bors bors closed this as completed in b0b54f2 Jan 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 4, 2025
Rollup merge of rust-lang#135055 - compiler-errors:rpitit-infer-in-stricter-impl, r=estebank

Report impl method has stricter requirements even when RPITIT inference gets in the way

See the comment I added in the code. Fixes rust-lang#122506.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants