Skip to content

Commit 0872f23

Browse files
committed
Make some editorial adjustments
1 parent f54263a commit 0872f23

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

text/0000-supertrait-item-shadowing-v2.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
# Summary
77
[summary]: #summary
88

9-
When name resolution encounters an ambiguity between 2 trait methods when both traits are in scope, if one trait is a sub-trait of the other then select that method instead of reporting an ambiguity error.
9+
When name resolution encounters an ambiguity between two trait methods when both traits are in scope, if one trait is a subtrait of the other then select that method instead of reporting an ambiguity error.
1010

1111
# Motivation
1212
[motivation]: #motivation
1313

14-
1514
The libs-api team would like to stabilize `Iterator::intersperse` but has a problem. The `itertools` crate already has:
1615

1716
```rust
@@ -34,18 +33,19 @@ fn foo() -> impl Iterator<Item = &'static str> {
3433
}
3534
```
3635

37-
This code actually works today because `intersperse` is an unstable API, which works because the compiler already has [logic](https://github.com/rust-lang/rust/pull/48552) to prefer stable methods over unstable methods when an ambiguity occurs.
36+
This code actually works today because `intersperse` is an unstable API, and the compiler already has [logic](https://github.com/rust-lang/rust/pull/48552) to prefer stable methods over unstable methods when an ambiguity occurs.
3837

3938
Attempts to stabilize `intersperse` have failed with a large number of regressions [reported by crater](https://github.com/rust-lang/rust/issues/88967) which affect many popular crates. Even if these were to be manually corrected (since ambiguity is considered allowed breakage) we would have to go through this whole process again every time a method from `itertools` is uplifted to the standard library.
4039

4140
# Proposed solution
4241
[proposed-solution]: #proposed-solution
4342

4443
This RFC proposes to change name resolution to resolve the ambiguity in the following specific circumstances:
45-
- All method candidates are trait methods. (Inherent methods are already prioritized over trait methods)
46-
- One trait is transitively a sub-trait of all other traits in the candidate list.
4744

48-
When this happens, the sub-trait method is selected instead of reporting an ambiguity error.
45+
- All method candidates are trait methods (inherent methods are already prioritized over trait methods).
46+
- One trait is transitively a subtrait of all other traits in the candidate list.
47+
48+
When this happens, the subtrait method is selected instead of reporting an ambiguity error.
4949

5050
Note that this only happens when *both* traits are in scope since this is required for the ambiguity to occur in the first place.
5151

@@ -68,7 +68,7 @@ fn main() {
6868
}
6969
```
7070

71-
Today that example will give an ambiguity error because `method` is provided by multiple traits in scope. With this RFC, it will instead always resolve to the sub-trait method and then compilation will fail because `Vec` does not implement the `Copy` trait required by `Bar::method`.
71+
Today that example will give an ambiguity error because `method` is provided by multiple traits in scope. With this RFC, it will instead always resolve to the subtrait method and then compilation will fail because `Vec` does not implement the `Copy` trait required by `Bar::method`.
7272

7373
# Drawbacks
7474
[drawbacks]: #drawbacks
@@ -84,17 +84,17 @@ If we choose not to accept this RFC then there doesn't seem to be a reasonable p
8484

8585
One possible alternative to a general change to the name resolution rules would be to only do so on a case-by-case basis for specific methods in standard library traits. This could be done by using a perma-unstable `#[shadowable]` attribute specifically on methods like `Iterator::intersperse`.
8686

87-
There are both advantages and inconvenients to this approach. While it allows most Rust users to avoid having to think about this issue for most traits, it does make the `Iterator` trait more "magical" in that it doesn't follow the same rules as the rest of the language. Having a consistent rule for how name resolution works is easier to teach people.
87+
There are both advantages and inconveniences to this approach. While it allows most Rust users to avoid having to think about this issue for most traits, it does make the `Iterator` trait more "magical" in that it doesn't follow the same rules as the rest of the language. Having a consistent rule for how name resolution works is easier to teach people.
8888

8989
## Preferring the supertrait method instead
9090

91-
In cases of ambiguity between a subtrait method and a supertrait method, there are 2 ways of resolving the ambiguity. This RFC proposes to resolve in favor of the subtrait since this is most likely to avoid breaking changes in practice.
91+
In cases of ambiguity between a subtrait method and a supertrait method, there are two ways of resolving the ambiguity. This RFC proposes to resolve in favor of the subtrait since this is most likely to avoid breaking changes in practice.
9292

9393
Consider this situation:
9494

95-
- library A has trait `Foo`
96-
- crate B, depending on A, has trait `FooExt` with `Foo` as a supertrait
97-
- A adds a new method to `Foo`, but it has a default implementation so it's not breaking. B has a pre-existing method with the same name.
95+
- Library A has trait `Foo`.
96+
- Crate B, depending on A, has trait `FooExt` with `Foo` as a supertrait.
97+
- A adds a new method to `Foo`, but it has a default implementation so it's not breaking. B has a preexisting method with the same name.
9898

9999
In this general case, the reason this cannot be resolved in favor of the supertrait is that the method signatures are not necessarily compatible.
100100

@@ -130,23 +130,19 @@ fn main() {
130130
}
131131
```
132132

133-
Resolving in favor of `a` is a breaking change; in favor of `b` is not. The only other option is the status quo: not compiling. `a` simply cannot happen lest we violate backwards compatibility and the status quo is not ideal.
133+
Resolving in favor of `a` is a breaking change; resolving in favor of `b` is not. The only other option is the status quo -- not compiling. Resolving to `a` simply cannot happen lest we violate backwards compatibility, and the status quo is not ideal.
134134

135135
# Prior art
136136
[prior-art]: #prior-art
137137

138138
### RFC 2845
139139

140140
RFC 2845 was a previous attempt to address this problem, but it has several drawbacks:
141+
141142
- It doesn't fully address the problem since it only changes name resolution when trait methods are resolved due to generic bounds. In practice, most of the ambiguity from stabilizing `intersperse` comes from non-generic code.
142143
- It adds a lot of complexity because name resolution depends on the specific trait bounds that have been brought into scope.
143144

144145
# Unresolved questions
145146
[unresolved-questions]: #unresolved-questions
146147

147148
- Should we have a warn-by-default lint that fires at the definition-site of a subtrait that shadows a supertrait item?
148-
149-
# Future possibilities
150-
[future-possibilities]: #future-possibilities
151-
152-
None

0 commit comments

Comments
 (0)