Associated Type Equality Constraints Drop Trait Bounds #109845
Labels
A-traits
Area: Trait system
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Apologies if this has been covered elsewhere, I wasn't able to find any issues or anything in the RFCs that addresses this specifically.
Issue
Given a trait with an associated type that has some trait bounds:
We can use this trait's associated type
X
(without constrainingX
to a particular type or adding bounds to it) while relying onX: Clone
:However if we are to bound
X
to a particular type (i.e.Foo<_, X = A>
) we are no longer able to rely onX: Clone
:Note that for all of the above,
A
is required to beClone
(whenWitness = ()
) in order for uses of the functions (or of(): Foo<A, X = A>
) to typecheck:(playground link)
(this particular example is minified from something less contrived; as written this is fairly nonsensical since there's not much reason to ask for
Witness: Foo<X = T>
instead of just boundingT
directly)Misc
Prior to Rust 1.49 (I have not bisected to verify but I think the change traces back to #72788 and #73905), functions like
example{1,2,3}
were required to boundA: Clone
themselves so thatFoo<A, X = A>
would be well-formed (godbolt link). In 1.49 and up this check seems to be deferred to usages of the function.This also observable in other ways; for example:
I'm not sure if the intent of the change was merely to shift requirements for associated types to callers of functions and to still require authors of such functions to have to spell out bounds like
A: Clone
explicitly OR if the intent actually was to haveA: Clone
be implied.I was expecting (or really, hoping for) the latter in which case I'd expect to be able to rely on
A: Clone
within the function bodies above.In either case I think
example3
is particularly surprising; if this really is intended behavior I think it'd be nice to have the compiler communicate why things like<Witness as Foo<A>>::X
don't have their trait bounds fromWitness
available whenA
is a ty param on a function (or maybe just for this to be documented?).Edit: IIUC this comment suggests that this is intended behavior. It does still seem unintuitive/suboptimal that as a consequence, the bound (
A: Clone
) is required for these functions to typecheck but that the bound is not available for the function body to rely on.This issue is might just a duplicate of #78893 (the traits/
impl Trait
made me initially believe it wasn't but that's a bit of a red herring I think).The text was updated successfully, but these errors were encountered: