describe equality constraints#2254
Conversation
Equality constraints on associated types have been supported for a long time, but lacking in documentation. Much of this is based on RFC 195. fixes rust-lang#931
|
@rustbot label +A-generics "New content" |
| ``` | ||
|
|
||
| r[bound.equality.trait-object] | ||
| Equality constraints are *required* for generic [trait objects]. |
There was a problem hiding this comment.
That's too imprecise, they aren't required per se.
They are required for associated types specifically or rephrased, every associated type from the trait itself or from any of its supertraits needs to be constrained in the trait object type ... unless it has a where Self: Sized; clause or was already constrained via supertrait bounds.
In any case, isn't this partially or fully duplicating facts from the Dyn compatibility section and thus redundant?
There was a problem hiding this comment.
Equality constraints appear to currently only be valid for associated types, as stated earlier in the proposed text. Stating that they are required for trait objects implies that they apply only to associated types.
In any case, isn't this partially or fully duplicating facts from the Dyn compatibility section and thus redundant?
Where is the requirement to fully specify associated types on trait objects in the existing Reference? It doesn't logically belong in the section on dyn compatibility, because a generic trait can be dyn compatible.
There is a case for describing this requirement in the section on trait objects, but I think it makes more sense to put it with the other trait bounds.
| Equality constraints cannot currently be expressed using a [WhereClause]. | ||
|
|
||
| ```rust,compile_fail | ||
| # trait Tr { type Output; } | ||
| # struct S {} | ||
| # impl Tr for S { type Output = S; } | ||
| // error: equality constraints are not yet supported in `where` clauses | ||
| fn uses_where<T>() where T::Output = S {} |
There was a problem hiding this comment.
I'm not sure whether it makes sense to document something that isn't supported, especially since the Reference grammar already implies that.
Technically speaking, equality predicates like this are still part of the language in a gray area since they're syntactically valid in rustc. However, that'll change soon since T-lang has accepted my proposal to remove them. See rust-lang/rust#153513, I just need to tie up some loose ends.
That's why I initially clicked on this PR, I thought you were adding them to the Reference grammar.
There was a problem hiding this comment.
The lack of support for equality constraints in where clauses is specifically called out in RFC 195 (even though it is internally inconsistent on about it). I think it's useful to mention it anyway, because where clauses otherwise generally have superset of the capabilities of bounds attached inline to generic parameters.
Equality constraints on associated types have been supported for a long time, but lacking in documentation.
Much of this is based on RFC 195.
fixes #931