Description
Currently, we don't have any kind of programmatic way of ensuring that we don't accidentally change the variances of any libcore
/libstd
types. While some types do have explicit tests for variance (e.g. here and in PR #72902), this is not done in any kind of systematic way.
I think it would be useful to have an internal #[variance(Co/Contra/In)]
(names bikesheddable) which causes an error if the actual and expected variances do not match.
We could require that every generic parameter on every public type have a #[variance]
attribute, similar to how we require #[stable]
/#[unstable]
. However, this could end up being unnecessarily verbose, especially if most types are covariant. Instead, we might want to treat the absence of a #[variance]
annotation as an implicit #[variance(Covariant)]
(or #[variance(Invariant)]
for const generics). If covariance is normally what we want, then this would only require us to annotate cases where something 'unusual' is happening. Of course, we would run the risk of cases like #71814 'slipping under the radar', where the generic parameter really should have been invariant to begin with.