Skip to content

Commit eeb5a83

Browse files
authored
method-lookup.md improvements (#1296)
1 parent 885d329 commit eeb5a83

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/method-lookup.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,34 @@ behavior should be reconsidered in light of where clauses.
7979
TODO: Is this FIXME still accurate?
8080

8181
**Extension candidates** are derived from imported traits. If I have
82-
the trait `ToString` imported, and I call `to_string()` on a value of
83-
type `T`, then we will go off to find out whether there is an impl of
84-
`ToString` for `T`. These kinds of method calls are called "extension
85-
methods". They can be defined in any crate, not only the one that
86-
defined `T`. Furthermore, you must import the trait to call such a
87-
method.
82+
the trait `ToString` imported, and I call `to_string()` as a method,
83+
then we will list the `to_string()` definition in each impl of
84+
`ToString` as a candidate. These kinds of method calls are called
85+
"extension methods".
8886

8987
So, let's continue our example. Imagine that we were calling a method
9088
`foo` with the receiver `Rc<Box<[T; 3]>>` and there is a trait `Foo`
9189
that defines it with `&self` for the type `Rc<U>` as well as a method
92-
on the type `Box` that defines `Foo` but with `&mut self`. Then we
90+
on the type `Box` that defines `foo` but with `&mut self`. Then we
9391
might have two candidates:
9492

95-
- `&Rc<Box<[T; 3]>>` from the impl of `Foo` for `Rc<U>` where `U=Box<[T; 3]>`
96-
- `&mut Box<[T; 3]>>` from the inherent impl on `Box<U>` where `U=[T; 3]`
93+
- `&Rc<U>` as an extension candidate
94+
- `&mut Box<U>` as an inherent candidate
9795

9896
### Candidate search
9997

10098
Finally, to actually pick the method, we will search down the steps,
10199
trying to match the receiver type against the candidate types. At
102100
each step, we also consider an auto-ref and auto-mut-ref to see whether
103-
that makes any of the candidates match. We pick the first step where
104-
we find a match.
101+
that makes any of the candidates match. For each resulting receiver
102+
type, we consider inherent candidates before extension candidates.
103+
If there are multiple matching candidates in a group, we report an
104+
error, except that multiple impls of the same trait are treated as a
105+
single match. Otherwise we pick the first match we find.
105106

106107
In the case of our example, the first step is `Rc<Box<[T; 3]>>`,
107108
which does not itself match any candidate. But when we autoref it, we
108-
get the type `&Rc<Box<[T; 3]>>` which does match. We would then
109+
get the type `&Rc<Box<[T; 3]>>` which matches `&Rc<U>`. We would then
109110
recursively consider all where-clauses that appear on the impl: if
110111
those match (or we cannot rule out that they do), then this is the
111112
method we would pick. Otherwise, we would continue down the series of

0 commit comments

Comments
 (0)