Skip to content

Commit 8e737ce

Browse files
committed
Add suggestions from withoutboats
1 parent 57a04d2 commit 8e737ce

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

text/0000-allow-self-in-where-clauses.md

+27-15
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# Summary
77
[summary]: #summary
88

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
9+
This RFC proposes allowing the `Self` type to be used in every position in trait
10+
implementations, including where clauses and other parameters to the trait being
1111
implemented.
1212

1313
# Motivation
@@ -46,28 +46,40 @@ on the associated type. It would be nice to reduce some of that duplication.
4646
# Detailed design
4747
[design]: #detailed-design
4848

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.
49+
Instead of blocking `Self` from being used in the "header" of a trait impl,
50+
it will be understood to be a reference to the implementation type. For example,
51+
all of these would be valid:
5352

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.
53+
```rust
54+
impl SomeTrait for SomeType where Self: SomeOtherTrait { }
55+
56+
impl SomeTrait<Self> for SomeType { }
57+
58+
impl SomeTrait for SomeType where SomeOtherType<Self>: SomeTrait { }
59+
60+
impl SomeTrait for SomeType where Self::AssocType: SomeOtherTrait {
61+
AssocType = SomeOtherType;
62+
}
63+
```
64+
65+
If the `Self` type is parameterized by `Self`, an error that the type definition
66+
is recursive is thrown, rather than not recognizing self.
67+
68+
```rust
69+
// The error here is because this would be Vec<Vec<Self>>, Vec<Vec<Vec<Self>>>, ...
70+
impl SomeTrait for Vec<Self> { }
71+
```
6072

6173
# Drawbacks
6274
[drawbacks]: #drawbacks
6375

64-
`Self` is always less explicit than the alternative
76+
`Self` is always less explicit than the alternative.
6577

6678
# Alternatives
6779
[alternatives]: #alternatives
6880

69-
Not implementing this, or only allowing bare `Self` but not associated types in
70-
where clauses
81+
Not implementing this is an alternative, as is accepting Self only in where clauses
82+
and not other positions in the impl header.
7183

7284
# Unresolved questions
7385
[unresolved]: #unresolved-questions

0 commit comments

Comments
 (0)