@@ -14,36 +14,36 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
14
14
# assert! (1.14 < log_pi && log_pi < 1.15 )
15
15
```
16
16
17
- When resolving method calls on an expression of type ` A ` , Rust looks up methods
18
- both on the type itself and the traits in implements. Additionally, unlike with
19
- non-method function calls, the ` self ` parameter is special and may be
20
- automatically dereferenced in order to resolve it. Rust uses the following
21
- process to resolve method calls.
22
-
23
- First, Rust will attempt to build a list of candidate receiver types. It obtains
17
+ When resolving method calls on an expression of type ` A ` , that expression will
18
+ be the ` self ` parameter. This parameter is special and, unlike with other
19
+ parameters to methods or other functions, may be automatically dereferenced or
20
+ borrowed in order to call a method. This requires a more complex lookup process,
21
+ since there may be a number of possible methods to call. The following procedure
22
+ is used:
23
+
24
+ The first step is to build a list of candidate receiver types. Obtain
24
25
these by repeatedly [ dereferencing] [ dereference ] the type, adding each type
25
26
encountered to the list, then finally attempting an [ unsized coercion] at the
26
27
end, and adding the result type if that is successful. Then, for each candidate
27
- ` T ` , Rust adds ` &T ` and ` &mut T ` to the list immediately afterward .
28
+ ` T ` , add ` &T ` and ` &mut T ` to the list immediately after ` T ` .
28
29
29
- So, for instance, if ` A ` is ` Box<[i32;2]> ` , then the candidate types will be
30
+ For instance, if ` A ` is ` Box<[i32;2]> ` , then the candidate types will be
30
31
` Box<[i32;2]> ` , ` &Box<[i32;2]> ` , ` &mut Box<[i32;2]> ` , ` [i32; 2] ` (by
31
32
dereferencing), ` &[i32; 2] ` , ` &mut [i32; 2] ` , ` [i32] ` (by unsized coercion),
32
33
` &[i32] ` , and finally ` &mut [i32] ` .
33
34
34
- Then, for each candidate type ` T ` , Rust will search for a [ visible] method with
35
+ Then, for each candidate type ` T ` , search for a [ visible] method with
35
36
a receiver of that type in the following places:
36
37
37
38
1 . ` T ` 's inherent methods (methods implemented directly on ` T ` ).
38
- 1 . Any of the methods provided by a trait implemented by ` T ` . If ` T ` is
39
- a type parameter (including the ` Self ` parameter of a trait), then only
40
- methods from the trait constraints on ` T ` are available for lookup. If ` T ` is
41
- not, then methods from any in-scope trait are available.
39
+ 1 . Any of the methods provided by a [ visible] trait implemented by ` T ` . If ` T `
40
+ is a type parameter, methods provided by trait bounds on ` T ` are looked up
41
+ first. Then all remaining methods in scope are looked up.
42
42
43
- Note that the lookup is done for each type in order, which can occasionally lead
44
- to surprising results. The below code will print "In trait impl!", because
45
- ` &self ` methods are looked up first, the trait method is found before the
46
- struct's ` &mut self ` method is found.
43
+ Note: the lookup is done for each type in order, which can occasionally lead
44
+ to surprising results. The below code will print "In trait impl!", because
45
+ ` &self ` methods are looked up first, the trait method is found before the
46
+ struct's ` &mut self ` method is found.
47
47
48
48
``` rust
49
49
struct Foo {}
@@ -74,8 +74,10 @@ If this results in multiple possible candidates, then it is an error, and the
74
74
receiver must be [ converted] [ disambiguate call ] to an appropriate receiver type
75
75
to make the method call.
76
76
77
- The lookup process does not take into account the mutability or lifetime of the
78
- receiver, or whether a method is ` unsafe ` . Once a method is looked up.
77
+ This process does not take into account the mutability or lifetime of the
78
+ receiver, or whether a method is ` unsafe ` . Once a method is looked up, if it
79
+ can't be called for one (or more) of those reasons, the result is a compiler
80
+ error.
79
81
80
82
If a step is reached where there is more than one possible method, such as where
81
83
generic methods or traits are considered the same, then it is a compiler
0 commit comments