rust-lang / rust-lang/rust

Compiler wants to add bound which already exists to generic.

Open
#123,513 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It looks like the compiler can't unify associated types with their actual types when given a concrete type inside an impl. Additionally, the diagnostics are very misleading. Here's a minimum reproducible example:

#![feature(trait_alias)]

trait Something {}

trait Test<A> { type T; }

trait Alias<A, T: Test<A>> = Test<A> where T::T: Something;

trait ArgHaver { type Arg; }
impl<T> ArgHaver for T { type Arg = T; }

trait Q<A: ArgHaver> {
    fn f<X, T: Test<A::Arg>>() where X: Alias<A::Arg, T>;
}

impl<F> Q<()> for F {
    // both versions don't work
    // fn f<X, T: Test<<() as ArgHaver>::Arg>>() where X: Alias<<() as ArgHaver>::Arg, T> {}
    fn f<X, T: Test<()>>() where X: Alias<(), T> {}
}

I expected to see this happen: The program compiles successfully.

Instead, this happened:

error[E0277]: the trait bound `T: Test<()>` is not satisfied
  --> <source>:19:5
   |
19 |     fn f<X, T: Test<()>>() where X: Alias<(), T> {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Test<()>` is not implemented for `T`
   |
help: consider further restricting this bound
   |
19 |     fn f<X, T: Test<()> + Test<()>>() where X: Alias<(), T> {}
   |                         ++++++++++

error[E0276]: impl has stricter requirements than trait
  --> <source>:19:16
   |
13 |     fn f<X, T: Test<A::Arg>>() where X: Alias<A::Arg, T>;
   |     ----------------------------------------------------- definition of `f` from trait
...
19 |     fn f<X, T: Test<()>>() where X: Alias<(), T> {}
   |                ^^^^^^^^ impl has extra requirement `T: Test<()>`

error[E0277]: the trait bound `X: Alias<(), T>` is not satisfied
  --> <source>:19:37
   |
19 |     fn f<X, T: Test<()>>() where X: Alias<(), T> {}
   |                                     ^^^^^^^^^^^^ the trait `Test<()>` is not implemented for `X`, which is required by `X: Alias<(), T>`
   |
   = note: required for `X` to implement `Alias<(), T>`
note: the requirement `X: Alias<(), T>` appears on the `impl`'s method `f` but not on the corresponding trait's method
  --> <source>:13:8
   |
12 | trait Q<A: ArgHaver> {
   |       - in this trait
13 |     fn f<X, T: Test<A::Arg>>() where X: Alias<A::Arg, T>;
   |        ^ this trait's method doesn't have the requirement `X: Alias<(), T>`
help: consider further restricting this bound
   |
19 |     fn f<X, T: Test<()>>() where X: Alias<(), T> + Test<()> {}
   |                                                  ++++++++++
Meta

rustc --version --verbose:

rustc 1.79.0-nightly (88c2f4f5f 2024-04-02)
binary: rustc
commit-hash: 88c2f4f5f50ace5ddc7655ea311435104d3659bd
commit-date: 2024-04-02
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

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 compiling the inline minimum reproducible example with the recorded rustc nightly and confirm the associated-type and diagnostic failures. Then trace the compiler's handling of the generic bounds and associated types; done means the example compiles successfully and no longer reports the misleading extra-bound diagnostics.

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.