rust-lang / rust-lang/rust

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

Open
#123,173 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-trait-system C-bug fixed-by-next-solver T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure from the example in src/lib.rs, using the linked Rust Playground or rustc 1.77.0, and compare it with the cases where the associated-type bound or the function-level where clause is removed. Trace the compiler path handling trait bounds and associated types; done means the provided example compiles without adding the suggested redundant bound and regression coverage preserves that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.