Skip to content

Commit 735991b

Browse files
chorman0773ehuss
authored andcommitted
Add identifier syntax to method-call-expr and operator-expr
1 parent 2e00df4 commit 735991b

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

src/expressions/method-call-expr.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Method-call expressions
22

3+
r[expr.method]
4+
5+
r[expr.method.syntax]
36
> **<sup>Syntax</sup>**\
47
> _MethodCallExpression_ :\
58
> &nbsp;&nbsp; [_Expression_] `.` [_PathExprSegment_] `(`[_CallParams_]<sup>?</sup> `)`
69
10+
r[expr.method.intro]
711
A _method call_ consists of an expression (the *receiver*) followed by a single dot, an expression path segment, and a parenthesized expression-list.
12+
13+
r[expr.method.target]
814
Method calls are resolved to associated [methods] on specific traits, either statically dispatching to a method if the exact `self`-type of the left-hand-side is known, or dynamically dispatching if the left-hand-side expression is an indirect [trait object](../types/trait-object.md).
915

1016
```rust
@@ -13,16 +19,21 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
1319
# assert!(1.14 < log_pi && log_pi < 1.15)
1420
```
1521

22+
r[expr.method.autoref-deref]
1623
When looking up a method call, the receiver may be automatically dereferenced or borrowed in order to call a method.
1724
This requires a more complex lookup process than for other functions, since there may be a number of possible methods to call.
1825
The following procedure is used:
1926

27+
r[expr.method.candidate-recievers]
2028
The first step is to build a list of candidate receiver types.
2129
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.
30+
31+
r[expr.method.candidate-recievers-refs]
2232
Then, for each candidate `T`, add `&T` and `&mut T` to the list immediately after `T`.
2333

2434
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]`.
2535

36+
r[expr.method.candidate-search]
2637
Then, for each candidate type `T`, search for a [visible] method with a receiver of that type in the following places:
2738

2839
1. `T`'s inherent methods (methods implemented directly on `T`).
@@ -58,11 +69,14 @@ Then, for each candidate type `T`, search for a [visible] method with a receiver
5869
> }
5970
> ```
6071
72+
r[expr.method.ambiguious-target]
6173
If this results in multiple possible candidates, then it is an error, and the receiver must be [converted][disambiguate call] to an appropriate receiver type to make the method call.
6274
75+
r[expr.method.constraint]
6376
This process does not take into account the mutability or lifetime of the receiver, or whether a method is `unsafe`.
6477
Once a method is looked up, if it can't be called for one (or more) of those reasons, the result is a compiler error.
6578
79+
r[expr.method.ambiguious-search]
6680
If a step is reached where there is more than one possible method, such as where generic methods or traits are considered the same, then it is a compiler error.
6781
These cases require a [disambiguating function call syntax] for method and function invocation.
6882

0 commit comments

Comments
 (0)