You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/expressions/method-call-expr.md
+73-14
Original file line number
Diff line number
Diff line change
@@ -15,20 +15,81 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
15
15
16
16
When looking up a method call, the receiver may be automatically dereferenced or borrowed in order to call a method.
17
17
This requires a more complex lookup process than for other functions, since there may be a number of possible methods to call.
18
-
The following procedure is used:
18
+
The following procedure (described in pseudo-rust) is used:
19
19
20
-
The first step is to build a list of candidate receiver types.
21
-
Obtain these by repeatedly [dereferencing][dereference] the receiver expression's type, adding each type encountered to the list, then finally attempting an [unsized coercion] at the end, and adding the result type if that is successful.
22
-
Then, for each candidate `T`, add `&T` and `&mut T` to the list immediately after `T`.
23
-
24
-
For instance, if the receiver has type `Box<[i32;2]>`, then the candidate types will be `Box<[i32;2]>`, `&Box<[i32;2]>`, `&mut Box<[i32;2]>`, `[i32; 2]` (by dereferencing), `&[i32; 2]`, `&mut [i32; 2]`, `[i32]` (by unsized coercion), `&[i32]`, and finally `&mut [i32]`.
25
-
26
-
Then, for each candidate type `T`, search for a [visible] method with a receiver of that type in the following places:
// If this results in multiple possible candidates, then it is an error,
74
+
// and the receiver must be [converted][disambiguate call] to an
75
+
// appropriate receiver type to make the method call.
76
+
matchprioritized_candidate_methods {
77
+
[] => {}, // Continue
78
+
[method] =>returnmethod,
79
+
_=>panic!("multiple applicable items in scope"),
80
+
}
81
+
matchcandidate_methods {
82
+
[] => {}, // Continue
83
+
[method] =>returnmethod,
84
+
_=>panic!("multiple applicable items in scope"),
85
+
}
86
+
}
87
+
88
+
panic!("no method named `{method_name}` found in the current scope")
89
+
}
90
+
```
27
91
28
-
1.`T`'s inherent methods (methods implemented directly on `T`).
29
-
1. Any of the methods provided by a [visible] trait implemented by `T`.
30
-
If `T` is a type parameter, methods provided by trait bounds on `T` are looked up first.
31
-
Then all remaining methods in scope are looked up.
92
+
As an example, if the receiver has type `Box<[i32;2]>`, then the candidate types will be `Box<[i32;2]>`, `&Box<[i32;2]>`, `&mut Box<[i32;2]>`, `[i32; 2]` (by dereferencing), `&[i32; 2]`, `&mut [i32; 2]`, `[i32]` (by unsized coercion), `&[i32]`, and finally `&mut [i32]`.
32
93
33
94
> Note: the lookup is done for each type in order, which can occasionally lead to surprising results.
34
95
> The below code will print "In trait impl!", because `&self` methods are looked up first, the trait method is found before the struct's `&mut self` method is found.
@@ -58,8 +119,6 @@ Then, for each candidate type `T`, search for a [visible] method with a receiver
0 commit comments