Compiler wants to add bound which already exists to generic. #123513
Open
Description
opened on Apr 5, 2024
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
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Messages for errors, warnings, and lintsArea: Trait systemCategory: This is a bug.`#![feature(trait_alias)]`Relevant to the compiler team, which will review and decide on the PR/issue.Fixed by the next-generation trait solver, `-Znext-solver`.
Activity