|
| 1 | +- Feature Name: `allow_self_in_where_clauses` |
| 2 | +- Start Date: 2016-06-13 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | +[summary]: #summary |
| 8 | + |
| 9 | +This RFC proposes allowing the `Self` type to be used in where clauses for trait |
| 10 | +implementations, as well as referencing associated types for the trait being |
| 11 | +implemented. |
| 12 | + |
| 13 | +# Motivation |
| 14 | +[motivation]: #motivation |
| 15 | + |
| 16 | +`Self` is a useful tool to have to reduce churn when the type changes for |
| 17 | +various reasons. One would expect to be able to write |
| 18 | + |
| 19 | +```rust |
| 20 | +impl SomeTrait for MySuperLongType<T, U, V, W, X> where |
| 21 | + Self: SomeOtherTrait, |
| 22 | +``` |
| 23 | + |
| 24 | +but this will fail to compile today, forcing you to repeat the type, and adding |
| 25 | +one more place that has to change if the type ever changes. |
| 26 | + |
| 27 | +By this same logic, we would also like to be able to reference associated types |
| 28 | +from the traits being implemented. When dealing with generic code, patterns like |
| 29 | +this often emerge: |
| 30 | + |
| 31 | +```rust |
| 32 | +trait MyTrait { |
| 33 | + type MyType: SomeBound; |
| 34 | +} |
| 35 | + |
| 36 | +impl<T, U, V> MyTrait for SomeStruct<T, U, V> where |
| 37 | + SomeOtherStruct<T, U, V>: SomeBound, |
| 38 | +{ |
| 39 | + type MyType = SomeOtherStruct<T, U, V>; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +the only reason the associated type is repeated at all is to restate the bound |
| 44 | +on the associated type. It would be nice to reduce some of that duplication. |
| 45 | + |
| 46 | +# Detailed design |
| 47 | +[design]: #detailed-design |
| 48 | + |
| 49 | +The first half of this RFC is simple. Inside of a where clause for trait |
| 50 | +implementations, `Self` will refer to the type the trait is being implemented |
| 51 | +for. It will have the same value as `Self` being used in the body of the trait |
| 52 | +implementation. |
| 53 | + |
| 54 | +Accessing associated types will have the same result as copying the body of the |
| 55 | +associated type into the place where it's being used. That is to say that it |
| 56 | +will assume that all constraints hold, and evaluate to what the type would have |
| 57 | +been in that case. Ideally one should never have to write `<Self as |
| 58 | +CurrentTrait>::SomeType`, but in practice it will likely be required to remove |
| 59 | +issues with recursive evaluation. |
| 60 | + |
| 61 | +# Drawbacks |
| 62 | +[drawbacks]: #drawbacks |
| 63 | + |
| 64 | +`Self` is always less explicit than the alternative |
| 65 | + |
| 66 | +# Alternatives |
| 67 | +[alternatives]: #alternatives |
| 68 | + |
| 69 | +Not implementing this, or only allowing bare `Self` but not associated types in |
| 70 | +where clauses |
| 71 | + |
| 72 | +# Unresolved questions |
| 73 | +[unresolved]: #unresolved-questions |
| 74 | + |
| 75 | +None |
0 commit comments