Skip to content

Commit 68a4efb

Browse files
committed
Update functions:
- Review comments. - Separate Function and Method in impl. - Move _Method_ grammar to the section discussing methods. - Be a little more explicit about `self` types. - Fix block ending with expression for functions. - Fix extern blocks not supporting patterns. - Fix trait functions/methods having optional patterns, and the pattern restrictions.
1 parent b2c931b commit 68a4efb

File tree

5 files changed

+111
-53
lines changed

5 files changed

+111
-53
lines changed

src/items/associated-items.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,31 @@ let _: f64 = f64::from_i32(42);
7979

8080
### Methods
8181

82+
> _Method_ :\
83+
> &nbsp;&nbsp; [_FunctionFront_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
84+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _SelfParam_ (`,` [_FunctionParam_])<sup>\*</sup> `,`<sup>?</sup> `)`\
85+
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>\
86+
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
87+
>
88+
> _SelfParam_ :\
89+
> &nbsp;&nbsp; &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`\
90+
> &nbsp;&nbsp; | `mut`<sup>?</sup> `self` (`:` [_Type_])<sup>?</sup>
91+
8292
Associated functions whose first parameter is named `self` are called *methods*
8393
and may be invoked using the [method call operator], for example, `x.foo()`, as
8494
well as the usual function call notation.
8595

86-
The `self` parameter must have one of the following types. As a result, the
87-
following shorthands may be used to declare `self`:
96+
If the type of the `self` parameter is specified, it is limited to the type
97+
being implemented (or `Self`), or a reference or mutable reference to the
98+
type, or a boxed value of the type being implemented (such as `Box<Self>`).
99+
Shorthand syntax can be used without specifying a type, which have the
100+
following equivalents:
88101

89-
* `self` -> `self: Self`
90-
* `&'lifetime self` -> `self: &'lifetime Self`
91-
* `&'lifetime mut self` -> `self: &'lifetime mut Self`
92-
* `self : Box<Self>` (no shorthand)
102+
Shorthand | Equivalent
103+
----------------------|-----------
104+
`self` | `self: Self`
105+
`&'lifetime self` | `self: &'lifetime Self`
106+
`&'lifetime mut self` | `self: &'lifetime mut Self`
93107

94108
> Note: Lifetimes can be and usually are elided with this shorthand.
95109
@@ -272,6 +286,14 @@ fn main() {
272286
}
273287
```
274288

289+
[_BlockExpression_]: expressions/block-expr.html
290+
[_FunctionFront_]: items/functions.html
291+
[_FunctionParam_]: items/functions.html
292+
[_FunctionReturnType_]: items/functions.html
293+
[_Generics_]: items/generics.html
294+
[_Lifetime_]: trait-bounds.html
295+
[_Type_]: types.html
296+
[_WhereClause_]: items/generics.html#where-clauses
275297
[trait]: items/traits.html
276298
[traits]: items/traits.html
277299
[type aliases]: items/type-aliases.html

src/items/external-blocks.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717
>
1818
> _ExternalFunctionItem_ :\
1919
> &nbsp;&nbsp; `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
20-
> &nbsp;&nbsp; `(` [_FunctionParameters_]<sup>?</sup> | _FunctionParametersWithVariadics_ ) `)`\
20+
> &nbsp;&nbsp; `(` _NamedFunctionParameters_<sup>?</sup> | _NamedFunctionParametersWithVariadics_ ) `)`\
2121
> &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup> `;`
2222
>
23-
> _FunctionParametersWithVariadics_ :\
24-
> &nbsp;&nbsp; ( [_FunctionParam_] `,` )<sup>\*</sup> _VariadicFunctionParam_
23+
> _NamedFunctionParameters_ :\
24+
> &nbsp;&nbsp; _NamedFunctionParam_ ( `,` _NamedFunctionParam_ )<sup>\*</sup> `,`<sup>?</sup>
2525
>
26-
> _VariadicFunctionParam_ :\
27-
> &nbsp;&nbsp; [_FunctionParam_] `,` `...`
26+
> _NamedFunctionParam_ :\
27+
> &nbsp;&nbsp; ( [IDENTIFIER] | `_` ) `:` [_Type_]
28+
>
29+
> _NamedFunctionParametersWithVariadics_ :\
30+
> &nbsp;&nbsp; ( _NamedFunctionParam_ `,` )<sup>\*</sup> _NamedFunctionParam_ `,` `...`
2831
2932
External blocks form the basis for Rust's foreign function interface.
3033
Declarations in an external block describe symbols in external, non-Rust
3134
libraries.
3235

3336
Functions within external blocks are declared in the same way as other Rust
3437
functions, with the exception that they may not have a body and are instead
35-
terminated by a semicolon.
38+
terminated by a semicolon. Patterns are not allowed in parameters, only
39+
[IDENTIFIER] or `_` may be used.
3640

3741
Functions within external blocks may be called by Rust code, just like
3842
functions defined in Rust. The Rust compiler automatically translates between

src/items/functions.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
# Functions
22

33
> **<sup>Syntax</sup>**\
4-
> _Function_:\
4+
> _Function_ :\
55
> &nbsp;&nbsp; _FunctionFront_ `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
66
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _FunctionParameters_<sup>?</sup> `)`\
77
> &nbsp;&nbsp; &nbsp;&nbsp; _FunctionReturnType_<sup>?</sup> [_WhereClause_]<sup>?</sup>\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; _BlockWithInnerAttributes_
8+
> &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]
99
>
10-
> _FunctionFront_:\
10+
> _FunctionFront_ :\
1111
> &nbsp;&nbsp; `unsafe`<sup>?</sup> (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
1212
>
13-
> _Abi_:\
13+
> _Abi_ :\
1414
> &nbsp;&nbsp; [STRING_LITERAL] | [RAW_STRING_LITERAL]
1515
>
16-
> _FunctionParameters_:\
16+
> _FunctionParameters_ :\
1717
> &nbsp;&nbsp; _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
1818
>
1919
> _FunctionParam_ :\
2020
> &nbsp;&nbsp; [_Pattern_] `:` [_Type_]
2121
>
22-
> _FunctionReturnType_:\
22+
> _FunctionReturnType_ :\
2323
> &nbsp;&nbsp; `->` [_Type_]
24-
>
25-
> _BlockWithInnerAttributes_ :\
26-
> &nbsp;&nbsp; `{`\
27-
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
28-
> &nbsp;&nbsp; &nbsp;&nbsp; [_Statement_]<sup>\*</sup>\
29-
> &nbsp;&nbsp; `}`
3024
3125
A _function_ consists of a [block], along with a name and a set of parameters.
3226
Other than a name, all these are optional. Functions are declared with the
@@ -169,6 +163,7 @@ attributes].
169163
[IDENTIFIER]: identifiers.html
170164
[RAW_STRING_LITERAL]: tokens.html#raw-string-literals
171165
[STRING_LITERAL]: tokens.html#string-literals
166+
[_BlockExpression_]: expressions/block-expr.html
172167
[_Generics_]: items/generics.html
173168
[_InnerAttribute_]: attributes.html
174169
[_Pattern_]: patterns.html

src/items/implementations.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
> _InherentImplItem_ :\
1414
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
1515
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup>\
16-
> &nbsp;&nbsp; ( [_ConstantItem_] | _Method_ )
16+
> &nbsp;&nbsp; ( [_ConstantItem_] | [_Function_] | [_Method_] )
1717
>
1818
> _TraitImpl_ :\
1919
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `impl` [_Generics_] `!`<sup>?</sup>
@@ -27,22 +27,7 @@
2727
> _TraitImplItem_ :\
2828
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
2929
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup>\
30-
> &nbsp;&nbsp; ( [_TypeAlias_] | [_ConstantItem_] | _Method_ )
31-
>
32-
> _Method_:\
33-
> &nbsp;&nbsp; _MethodType_ [_BlockWithInnerAttributes_]
34-
>
35-
> _MethodType_:\
36-
> &nbsp;&nbsp; [_FunctionFront_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
37-
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _MethodParameters_<sup>?</sup> `)`\
38-
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
39-
>
40-
> _MethodParameters_:\
41-
> &nbsp;&nbsp; (_SelfParam_ | [_FunctionParam_] ) (`,` [_FunctionParam_])<sup>\*</sup> `,`<sup>?</sup>
42-
>
43-
> _SelfParam_:\
44-
> &nbsp;&nbsp; &nbsp;&nbsp; (`&` | `&` [_Lifetime_])<sup>?</sup> `mut`<sup>?</sup> `self`\
45-
> &nbsp;&nbsp; | `mut`<sup>?</sup> `self` (`:` [_Type_])<sup>?</sup>
30+
> &nbsp;&nbsp; ( [_TypeAlias_] | [_ConstantItem_] | [_Function_] | [_Method_] )
4631
4732
An _implementation_ is an item that associates items with an _implementing type_.
4833
Implementations are defined with the keyword `impl` and contain functions
@@ -191,18 +176,15 @@ meaning here are [`cfg`], [`deprecated`], [`doc`], and [the lint check
191176
attributes].
192177

193178
[IDENTIFIER]: identifiers.html
194-
[_BlockWithInnerAttributes_]: items/functions.html
195179
[_ConstantItem_]: items/constant-items.html
196-
[_FunctionFront_]: items/functions.html
197-
[_FunctionParam_]: items/functions.html
198-
[_FunctionReturnType_]: items/functions.html
180+
[_Function_]: items/functions.html
199181
[_Generics_]: items/generics.html
200182
[_InnerAttribute_]: attributes.html
201-
[_Lifetime_]: trait-bounds.html
183+
[_Method_]: items/associated-items.html#methods
202184
[_OuterAttribute_]: attributes.html
203-
[_Type_]: types.html
204185
[_TypeAlias_]: items/type-aliases.html
205186
[_TypePath_]: paths.html#paths-in-types
187+
[_Type_]: types.html
206188
[_Visibility_]: visibility-and-privacy.html
207189
[_WhereClause_]: items/generics.html#where-clauses
208190
[trait]: items/traits.html

src/items/traits.md

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@
99
> &nbsp;&nbsp; `}`
1010
>
1111
> _TraitItem_ :\
12-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (_TraitMethod_ | _TraitConst_ | _TraitType_)
12+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (_TraitFunc_ | _TraitMethod_ | _TraitConst_ | _TraitType_)
13+
>
14+
> _TraitFunc_ :\
15+
> &nbsp;&nbsp; &nbsp;&nbsp; _TraitFunctionDecl_ ( `;` | [_BlockExpression_] )
1316
>
1417
> _TraitMethod_ :\
15-
> &nbsp;&nbsp; [_MethodType_] `;` | [_Method_]
18+
> &nbsp;&nbsp; &nbsp;&nbsp; _TraitMethodDecl_ ( `;` | [_BlockExpression_] )
19+
>
20+
> _TraitFunctionDecl_ :\
21+
> &nbsp;&nbsp; [_FunctionFront_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
22+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _TraitFunctionParameters_<sup>?</sup> `)`\
23+
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
24+
>
25+
> _TraitMethodDecl_ :\
26+
> &nbsp;&nbsp; [_FunctionFront_] `fn` [IDENTIFIER]&nbsp;[_Generics_]<sup>?</sup>\
27+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` [_SelfParam_] (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup> `)`\
28+
> &nbsp;&nbsp; &nbsp;&nbsp; [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
29+
>
30+
> _TraitFunctionParameters_ :\
31+
> &nbsp;&nbsp; _TraitFunctionParam_ (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
32+
>
33+
> _TraitFunctionParam_<sup>[](#parameter-patterns)</sup> :\
34+
> &nbsp;&nbsp; ( [_Pattern_] `:` )<sup>?</sup> [_Type_]
1635
>
1736
> _TraitConst_ :\
1837
> &nbsp;&nbsp; `const` [IDENTIFIER] ( ( `:` [_Type_] ) ( `=` [_Expression_] )<sup>?</sup> )<sup>?</sup> `;`
@@ -137,18 +156,54 @@ let nonsense = circle.radius() * circle.area();
137156

138157
## Unsafe traits
139158

140-
Traits that begin with the `unsafe` keyword indicate that *implementing* the
159+
Traits items that begin with the `unsafe` keyword indicate that *implementing* the
141160
trait may be [unsafe]. It is safe to use a correctly implemented unsafe trait.
142-
The [trait implementation] must also include the `unsafe` keyword.
161+
The [trait implementation] must also begin with the `unsafe` keyword.
143162

144163
[`Sync`] and [`Send`] are examples of unsafe traits.
145164

165+
## Parameter patterns
166+
167+
The pattern for a trait function or method parameter is optional:
168+
169+
```rust
170+
trait T {
171+
fn f(i32); // Parameter identifiers are not required.
172+
}
173+
```
174+
175+
The kinds of patterns for parameters is limited to one of the following:
176+
177+
* [IDENTIFIER]
178+
* `mut` [IDENTIFIER]
179+
* [`_`][WildcardPattern]
180+
* `&` [IDENTIFIER]
181+
* `&&` [IDENTIFIER]
182+
183+
Function or method declarations without a body only allow [IDENTIFIER] or
184+
[wild card][WildcardPattern] patterns. `mut` [IDENTIFIER] is currently
185+
allowed, but it is deprecated and will become a hard error in the future.
186+
<!-- https://github.com/rust-lang/rust/issues/35203 -->
187+
188+
<!-- 2018 changes:
189+
190+
Function or method parameter patterns are no longer optional, and are required.
191+
192+
All irrefutable pattern kinds are allowed (as long as there is a body).
193+
-->
194+
195+
146196
[IDENTIFIER]: identifiers.html
197+
[WildcardPattern]: patterns.html#wildcard-pattern
198+
[_BlockExpression_]: expressions/block-expr.html
147199
[_Expression_]: expressions.html
200+
[_FunctionFront_]: items/functions.html
201+
[_FunctionParam_]: items/functions.html
202+
[_FunctionReturnType_]: items/functions.html
148203
[_Generics_]: items/generics.html
149-
[_Method_]: items/implementations.html
150-
[_MethodType_]: items/implementations.html
151204
[_OuterAttribute_]: attributes.html
205+
[_Pattern_]: patterns.html
206+
[_SelfParam_]: items/associated-items.html#methods
152207
[_TypeParamBounds_]: trait-bounds.html
153208
[_Type_]: types.html
154209
[_WhereClause_]: items/generics.html#where-clauses

0 commit comments

Comments
 (0)