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

Compiler forgets about function-level where clause when a clause for an associated type is present #123173

Open
sfackler opened this issue Mar 28, 2024 · 1 comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-traits Area: Trait system C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@sfackler
Copy link
Member

sfackler commented Mar 28, 2024

I tried this code:

trait Foo {
    type Input;

    fn foo<T>(&self, task: T) -> T::Output
    where
        T: Task<Self::Input> + Send,
        T::Output: Send;
}

trait Task<T> {
    type Output;
    
    fn run(&self, t: &mut T) -> Self::Output;
}

struct FooImpl;

impl Foo for FooImpl {
    type Input = FooInput;

    fn foo<T>(&self, task: T) -> T::Output
    where
        T: Task<Self::Input> + Send,
        T::Output: Send,
    {
        task.run(&mut FooInput)
    }
}

struct FooInput;

I expected to see this happen: It should compile.

Instead, this happened:

error[E0277]: the trait bound `T: Task<FooInput>` is not satisfied
  --> src/lib.rs:21:5
   |
21 | /     fn foo<T>(&self, task: T) -> T::Output
22 | |     where
23 | |         T: Task<Self::Input> + Send,
24 | |         T::Output: Send,
   | |________________________^ the trait `Task<FooInput>` is not implemented for `T`
   |
help: consider further restricting this bound
   |
23 |         T: Task<Self::Input> + Send + Task<FooInput>,
   |                                     ++++++++++++++++

error[E0276]: impl has stricter requirements than trait
  --> src/lib.rs:23:12
   |
4  | /     fn foo<T>(&self, task: T) -> T::Output
5  | |     where
6  | |         T: Task<Self::Input> + Send,
7  | |         T::Output: Send;
   | |________________________- definition of `foo` from trait
...
23 |           T: Task<Self::Input> + Send,
   |              ^^^^^^^^^^^^^^^^^ impl has extra requirement `T: Task<FooInput>`

error[E0277]: the trait bound `T: Task<FooInput>` is not satisfied
  --> src/lib.rs:21:34
   |
21 |     fn foo<T>(&self, task: T) -> T::Output
   |                                  ^^^^^^^^^ the trait `Task<FooInput>` is not implemented for `T`
   |
help: consider further restricting this bound
   |
23 |         T: Task<Self::Input> + Send + Task<FooInput>,
   |                                     ++++++++++++++++

Some errors have detailed explanations: E0276, E0277.
For more information about an error, try `rustc --explain E0276`.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6c4ab41d94cd20eb5102402919325773

Removing the T::Output: Send clauses allows it to compile, as does switching Task's type parameter from Self::Input to a fixed type like ().

Meta

rustc --version --verbose:

rustc 1.77.0 (aedd173a2 2024-03-17)
@sfackler sfackler added the C-bug Category: This is a bug. label Mar 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 28, 2024
@workingjubilee workingjubilee added A-traits Area: Trait system A-associated-items Area: Associated items such as associated types and consts. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 28, 2024
@workingjubilee
Copy link
Contributor

workingjubilee commented Mar 28, 2024

I cannot find a previous version this is a regression from, and this is fixed by -Znext-solver.

@workingjubilee workingjubilee added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-traits Area: Trait system C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants