Skip to content

Compiler wants to add bound which already exists to generic. #123513

Open
@meltah

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-trait_alias`#![feature(trait_alias)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions