In the specialization RFC there are some examples of specializations that should be allowed like the following:
// Allowed: *all* impls impose the 'static requirement; the dispatch is happening
// purely based on `Clone`
trait MustBeStatic {}
impl<T: 'static> MustBeStatic for T {}
impl<T: 'static + Clone> MustBeStatic for T {}
Unfortunately from what I see neither min_specialization
nor the maximally minimal specialization/always applicable impls proposal seem to address this case.
With min_specialization
this is currently rejected because you cannot specialize over Clone
, since it is not marked with #[rustc_specialization_trait]
. However that shouldn't matter because the 'static
bound in the specialized implementation rules out "bad" implementations of Clone
.
With maximally minimal specialization instead, if I read it correctly, the compiler would need to prove something along the lines of (ignoring well-formed predicates, which shouldn't matter here):
// Generalize the one use of T in the specializing impl
forall<A> {
// Assuming the base impl applies to it:
if (exists<T> { T = A, T: 'static }) {
// Check if the specializing impl matches
exists<U> {
// Same types in the header
U = A,
// The bounds are always provable
U: 'static,
U: Clone // <-- issue
}
}
}
Again the Clone
bound is an issue, since there appears to be no way to prove it. However again it should not be an issue because of the 'static
bound.
Am I missing something? If not has this issue been discussed before? It seems a rather big flaw IMO, since this patterns seems to be relatively common (e.g. bevy's could probably use this for reflection, as they're already requiring a 'static
bound for any type implementing Component
or Reflect
). It also seems relatively easy to support, at least in a restricted form, since it should be enough to check whether the impl
being specialized contains any type or lifetime parameters that are not bounded by 'static
.