Skip to content

Commit 57892c5

Browse files
Amanieutraviscross
authored andcommitted
Add lint and add type inference example
1 parent 95594f7 commit 57892c5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,31 @@ When this happens, the sub-trait method is selected instead of reporting an ambi
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

52+
When an ambiguity is resolved in this way, a lint warning is also emitted to warn the user about the potential ambiguity. The aim of this lint is to discourage reliance on this mechanism in normal code usage: it should only be used for backwards-compatibilty and the lint can be silenced by having users change their code. We can always later change this lint to be allowed by default if we consider that there are valid use cases for this feature other than backwards-compatiblity.
53+
54+
### Type inference
55+
56+
This change happens during name resolution and specifically doesn't interact with type inference. Consider this example:
57+
58+
```rust
59+
trait Foo { fn method(&self) {} }
60+
trait Bar: Foo { fn method(&self) {} }
61+
impl<T> Foo for Vec<T> { }
62+
impl<T: Copy> Bar for Vec<T> { }
63+
64+
fn main() {
65+
let x = vec![];
66+
x.method(); // which to call?
67+
x.push(Box::new(22)); // oh, looks like `Foo`
68+
}
69+
```
70+
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`.
72+
5273
# Drawbacks
5374
[drawbacks]: #drawbacks
5475

55-
This behavior can be surprising: adding a method to a sub-trait can change which function is called in unrelated code. A lint could be emitted to warn users about the potential ambiguity.
76+
This behavior can be surprising: adding a method to a sub-trait can change which function is called in unrelated code. This is mitigated by the which warns users about the potential ambiguity.
5677

5778
# Rationale and alternatives
5879
[rationale-and-alternatives]: #rationale-and-alternatives

0 commit comments

Comments
 (0)