a-mir-formality icon indicating copy to clipboard operation
a-mir-formality copied to clipboard

require impl generic parameters be constrained by the trait def

Open nikomatsakis opened this issue 3 years ago • 2 comments

Rust prohibits type parameters in a trait impl that don't appear in the trait definition:

impl<T> Iterator for () { type Item = (); } // ERROR

and unconstrained lifetime parameters that appear in a trait value

impl<'a> Iterator for () { type Item = &'a (); } // ERROR

We need to extend the rules to cover these. Careful, though, some uses of a generic parameter don't count as constrained:

impl<T> Iterator for <T as Iterator>::Item { type Item = (); } // also an ERROR

nikomatsakis avatar Jun 16 '22 09:06 nikomatsakis

Probably we want some kind of "well-formed decls" check analogous to well-formed-mir? It could also check that e.g. all TraitId are defined somewhere (which current results in assertion failures).

nikomatsakis avatar Jun 16 '22 09:06 nikomatsakis

In other words, I would just define these rules using redex type judgments, not try to make logical predicates.

nikomatsakis avatar Jun 16 '22 09:06 nikomatsakis