Nested but non-conflicting projection predicates emerging from supertrait bounds leads to ambiguity
Open
Nobody has claimed this yet.
A-associated-items
A-trait-system
C-bug
T-types
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Zulip Chat thread for discussion
I tried this code:
use std::marker::PhantomData;
pub trait Receiver {
type Target: ?Sized;
}
pub trait Deref: Receiver<Target = <Self as Deref>::Target> {
type Target: ?Sized;
fn deref(&self) -> &<Self as Deref>::Target;
}
impl<T: Deref> Receiver for T {
type Target = <T as Deref>::Target;
}
// ===
pub struct Type<Id, T>(PhantomData<(Id, T)>);
pub struct AliasRef<Id, T: TypePtr<Id = Id>>(PhantomData<(Id, T)>);
pub trait TypePtr: Deref<Target = Type<<Self as TypePtr>::Id, Self>> + Sized {
// ^ the impl head here provides the first candidate
// <T as Deref>::Target := Type<<T as TypePtr>::Id>
type Id;
}
pub struct Alias<Id, T>(PhantomData<(Id, T)>);
impl<Id, T> Deref for Alias<Id, T>
where
T: TypePtr<Id = Id> + Deref<Target = Type<Id, T>>,
// ^ the impl head here provides the second candidate
// <T as Deref>::Target := Type<Id, T>
// and additionally a normalisation is mandatory due to
// the following supertrait relation trait
// Deref: Receiver<Target = <Self as Deref>::Target>
{
type Target = AliasRef<Id, T>;
fn deref(&self) -> &<Self as Deref>::Target {
todo!()
}
}
I expected to see this happen: rustc compiles
Instead, this happened:
error[E0284]: type annotations needed
--> src/lib.rs:28:1
|
28 | / impl<Id, T> Deref for Alias<Id, T>
29 | | where
30 | | T: TypePtr<Id = Id> + Deref<Target = Type<Id, T>>,
| |______________________________________________________^ cannot infer type
|
= note: cannot satisfy `<T as Deref>::Target == _`
Meta
Rust version is at 1.88.0-nightly @ 2025-04-07 e643f59f6da3a84f43e7.
Backtrace
<backtrace>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the reproducer in src/lib.rs with the reported nightly toolchain and compare it with the original test case linked from pull request 139955. Read the Zulip discussion for the intended projection behavior, then add or update a regression test so the example compiles without the type-inference error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100